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