-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy patharchive.test.js
158 lines (142 loc) · 4.83 KB
/
archive.test.js
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
// @ts-check
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const { expect } = require('chai');
const temp = require('temp');
const { unpack, adjustArchiveStructure } = require('../scripts/archive');
describe('archive', () => {
describe('adjustArchiveStructure', () => {
let tracked;
let originalLog;
before(() => {
tracked = temp.track();
originalLog = console.log;
console.log = () => {
/*NOOP*/
};
});
after(() => {
if (originalLog) {
console.log = originalLog;
}
});
afterEach(() => {
tracked.cleanupSync();
});
it('should reject when not a zip file', async () => {
try {
const invalid = path.join(__dirname, 'test-resources', 'not-a-zip.dmg');
await adjustArchiveStructure(invalid, tracked.mkdirSync());
throw new Error('Expected a rejection');
} catch (e) {
expect(e).to.be.an.instanceOf(Error);
expect(e.message).to.be.equal('Expected a ZIP file.');
}
});
it('should reject when target directory does not exist', async () => {
try {
const zip = path.join(
__dirname,
'test-resources',
'zip-with-base-folder.zip'
);
await adjustArchiveStructure(
zip,
path.join(__dirname, 'some', 'missing', 'path')
);
throw new Error('Expected a rejection');
} catch (e) {
expect(e).to.be.an.instanceOf(Error);
expect(e.message.endsWith('does not exist.')).to.be.true;
}
});
it('should reject when target is a file', async () => {
try {
const zip = path.join(
__dirname,
'test-resources',
'zip-with-base-folder.zip'
);
await adjustArchiveStructure(zip, path.join(__filename));
throw new Error('Expected a rejection');
} catch (e) {
expect(e).to.be.an.instanceOf(Error);
expect(e.message.endsWith('is not a directory.')).to.be.true;
}
});
it('should be a NOOP when the zip already has the desired base folder', async () => {
const zip = path.join(
__dirname,
'test-resources',
'zip-with-base-folder.zip'
);
const actual = await adjustArchiveStructure(zip, tracked.mkdirSync());
expect(actual).to.be.equal(zip);
});
it('should handle whitespace in file path gracefully', async () => {
const zip = path.join(
__dirname,
'test-resources',
'zip with whitespace.zip'
);
const out = tracked.mkdirSync();
const actual = await adjustArchiveStructure(zip, out, true);
expect(actual).to.be.equal(path.join(out, 'zip with whitespace.zip'));
console.log(actual);
expect(fs.existsSync(actual)).to.be.true;
const verifyOut = tracked.mkdirSync();
await unpack(actual, verifyOut);
const root = path.join(verifyOut, 'zip with whitespace');
expect(fs.existsSync(root)).to.be.true;
expect(fs.lstatSync(root).isDirectory()).to.be.true;
const subs = fs.readdirSync(root);
expect(subs).to.have.lengthOf(3);
expect(subs.sort()).to.be.deep.equal(['a.txt', 'b.txt', 'foo']);
});
it('should keep the symlinks after ZIP adjustments', async function () {
if (process.platform === 'win32') {
this.skip();
}
const zip = path.join(
__dirname,
'test-resources',
'zip-with-symlink.zip'
);
const out = tracked.mkdirSync();
const actual = await adjustArchiveStructure(zip, out, true);
expect(actual).to.be.equal(path.join(out, 'zip-with-symlink.zip'));
console.log(actual);
expect(fs.existsSync(actual)).to.be.true;
const verifyOut = tracked.mkdirSync();
await unpack(actual, verifyOut);
expect(
fs
.lstatSync(
path.join(verifyOut, 'zip-with-symlink', 'folder', 'symlinked-sub')
)
.isSymbolicLink()
).to.be.true;
});
it('should adjust the archive structure if base folder is not present', async () => {
const zip = path.join(
__dirname,
'test-resources',
'zip-without-symlink.zip'
);
const out = tracked.mkdirSync();
const actual = await adjustArchiveStructure(zip, out, true);
expect(actual).to.be.equal(path.join(out, 'zip-without-symlink.zip'));
console.log(actual);
expect(fs.existsSync(actual)).to.be.true;
const verifyOut = tracked.mkdirSync();
await unpack(actual, verifyOut);
const root = path.join(verifyOut, 'zip-without-symlink');
expect(fs.existsSync(root)).to.be.true;
expect(fs.lstatSync(root).isDirectory()).to.be.true;
const subs = fs.readdirSync(root);
expect(subs).to.have.lengthOf(3);
expect(subs.sort()).to.be.deep.equal(['a.txt', 'b.txt', 'foo']);
});
});
});