dir-normalization.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Set the umask, so that it works the same everywhere.
  2. process.umask(parseInt('22', 8))
  3. var fs = require('fs')
  4. var path = require('path')
  5. var fstream = require('fstream')
  6. var test = require('tap').test
  7. var tar = require('../tar.js')
  8. var file = path.resolve(__dirname, 'dir-normalization.tar')
  9. var target = path.resolve(__dirname, 'tmp/dir-normalization-test')
  10. var ee = 0
  11. var expectEntries = [
  12. { path: 'fixtures/',
  13. mode: '755',
  14. type: '5',
  15. linkpath: ''
  16. },
  17. { path: 'fixtures/a/',
  18. mode: '755',
  19. type: '5',
  20. linkpath: ''
  21. },
  22. { path: 'fixtures/the-chumbler',
  23. mode: '755',
  24. type: '2',
  25. linkpath: path.resolve(target, 'a/b/c/d/the-chumbler'),
  26. },
  27. { path: 'fixtures/a/b/',
  28. mode: '755',
  29. type: '5',
  30. linkpath: ''
  31. },
  32. { path: 'fixtures/a/x',
  33. mode: '644',
  34. type: '0',
  35. linkpath: ''
  36. },
  37. { path: 'fixtures/a/b/c/',
  38. mode: '755',
  39. type: '5',
  40. linkpath: ''
  41. },
  42. { path: 'fixtures/a/b/c/y',
  43. mode: '755',
  44. type: '2',
  45. linkpath: '../../x',
  46. }
  47. ]
  48. var ef = 0
  49. var expectFiles = [
  50. { path: '',
  51. mode: '40755',
  52. type: 'Directory',
  53. depth: 0,
  54. linkpath: undefined
  55. },
  56. { path: '/fixtures',
  57. mode: '40755',
  58. type: 'Directory',
  59. depth: 1,
  60. linkpath: undefined
  61. },
  62. { path: '/fixtures/a',
  63. mode: '40755',
  64. type: 'Directory',
  65. depth: 2,
  66. linkpath: undefined
  67. },
  68. { path: '/fixtures/a/b',
  69. mode: '40755',
  70. type: 'Directory',
  71. depth: 3,
  72. linkpath: undefined
  73. },
  74. { path: '/fixtures/a/b/c',
  75. mode: '40755',
  76. type: 'Directory',
  77. depth: 4,
  78. linkpath: undefined
  79. },
  80. { path: '/fixtures/a/b/c/y',
  81. mode: '120755',
  82. type: 'SymbolicLink',
  83. depth: 5,
  84. linkpath: '../../x'
  85. },
  86. { path: '/fixtures/a/x',
  87. mode: '100644',
  88. type: 'File',
  89. depth: 3,
  90. linkpath: undefined
  91. },
  92. { path: '/fixtures/the-chumbler',
  93. mode: '120755',
  94. type: 'SymbolicLink',
  95. depth: 2,
  96. linkpath: path.resolve(target, 'a/b/c/d/the-chumbler')
  97. }
  98. ]
  99. test('preclean', function (t) {
  100. require('rimraf').sync(path.join(__dirname, '/tmp/dir-normalization-test'))
  101. t.pass('cleaned!')
  102. t.end()
  103. })
  104. test('extract test', function (t) {
  105. var extract = tar.Extract(target)
  106. var inp = fs.createReadStream(file)
  107. inp.pipe(extract)
  108. extract.on('end', function () {
  109. t.equal(ee, expectEntries.length, 'should see ' + expectEntries.length + ' entries')
  110. // should get no more entries after end
  111. extract.removeAllListeners('entry')
  112. extract.on('entry', function (e) {
  113. t.fail('Should not get entries after end!')
  114. })
  115. next()
  116. })
  117. extract.on('entry', function (entry) {
  118. var mode = entry.props.mode & (~parseInt('22', 8))
  119. var found = {
  120. path: entry.path,
  121. mode: mode.toString(8),
  122. type: entry.props.type,
  123. linkpath: entry.props.linkpath,
  124. }
  125. var wanted = expectEntries[ee++]
  126. t.equivalent(found, wanted, 'tar entry ' + ee + ' ' + (wanted && wanted.path))
  127. })
  128. function next () {
  129. var r = fstream.Reader({
  130. path: target,
  131. type: 'Directory',
  132. sort: 'alpha'
  133. })
  134. r.on('ready', function () {
  135. foundEntry(r)
  136. })
  137. r.on('end', finish)
  138. function foundEntry (entry) {
  139. var p = entry.path.substr(target.length)
  140. var mode = entry.props.mode & (~parseInt('22', 8))
  141. var found = {
  142. path: p,
  143. mode: mode.toString(8),
  144. type: entry.props.type,
  145. depth: entry.props.depth,
  146. linkpath: entry.props.linkpath
  147. }
  148. var wanted = expectFiles[ef++]
  149. t.equivalent(found, wanted, 'unpacked file ' + ef + ' ' + (wanted && wanted.path))
  150. entry.on('entry', foundEntry)
  151. }
  152. function finish () {
  153. t.equal(ef, expectFiles.length, 'should have ' + ef + ' items')
  154. t.end()
  155. }
  156. }
  157. })