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