/
pack-directory.test.js
230 lines (206 loc) · 6.23 KB
/
pack-directory.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict";
// actually _run_ the lifecycles, gorrammit
jest.unmock("@lerna/run-lifecycle");
const path = require("path");
const normalizePath = require("normalize-path");
const { printObjectProperties } = require("pretty-format/build/collections");
const npmConf = require("@lerna/npm-conf");
const { getPackages } = require("@lerna/project");
const initFixture = require("@lerna-test/init-fixture")(__dirname);
const packDirectory = require("..");
// temp-write creates temp directories that are 36 characters long (uuid.v4())
const TAR_DIR_REGEXP = /([^\s"]*[\\/][-0-9a-f]{36})([^\s"]*)/g;
const hasOwn = Object.prototype.hasOwnProperty;
function isObject(val) {
return val && typeof val === "object" && Array.isArray(val) === false;
}
function isString(val) {
return val && typeof val === "string";
}
function serializeTempDir(match, cwd, subPath) {
return normalizePath(path.join("__TMP_DIR__", subPath));
}
// process.umask() differs between macOS and Ubuntu,
// so we need to overwrite derived hashes for consistency
expect.addSnapshotSerializer({
test(val) {
if (isObject(val)) {
// 420 in macOS, 436 in Ubuntu
return hasOwn.call(val, "mode") || hasOwn.call(val, "size");
}
if (isString(val)) {
// integrity or shasum
return /sha512-[\S]{88}/.test(val) || /[0-9a-f]{40}/.test(val) || TAR_DIR_REGEXP.test(val);
}
},
serialize(val, config, indentation, depth, refs, printer) {
if (isString(val)) {
if (/tgz$/.test(val)) {
const str = val.replace(TAR_DIR_REGEXP, serializeTempDir);
// top-level strings don't need quotes, but nested ones do (object properties, etc)
// ...and this is always an object property
return `"${str}"`;
}
if (val.indexOf("sha512") > -1) {
return '"INTEGRITY"';
}
return '"SHASUM"';
}
/* eslint-disable no-param-reassign */
if (depth) {
// nested file objects
val.mode = "MODE";
} else {
// bloody tar algorithms
val.size = "TAR_SIZE";
}
/* eslint-enable no-param-reassign */
let result = "Object {";
result += printObjectProperties(val, config, indentation, depth, refs, printer);
result += "}";
return result;
},
});
describe("pack-directory", () => {
it("resolves tarball metadata objects on success", async () => {
const cwd = await initFixture("pack-directory");
const conf = npmConf({ prefix: cwd }).snapshot;
const pkgs = await getPackages(cwd);
// choose first and last package since the middle two are repetitive
const [head, tail] = await Promise.all(
[pkgs.shift(), pkgs.pop()].map(pkg => packDirectory(pkg, pkg.location, conf))
);
const INTEGRITY_PATTERN = /sha512-[\S]{88}/;
const SHASUM_PATTERN = /[0-9a-f]{40}/;
expect(head).toMatchInlineSnapshot(`
Object {
"bundled": Array [],
"entryCount": 2,
"filename": "scope-head-1.0.0.tgz",
"files": Array [
Object {
"mode": "MODE",
"path": "dist/index.js",
"size": 37,
},
Object {
"mode": "MODE",
"path": "package.json",
"size": 159,
},
],
"id": "@scope/head@1.0.0",
"integrity": "INTEGRITY",
"name": "@scope/head",
"shasum": "SHASUM",
"size": "TAR_SIZE",
"tarFilePath": "__TMP_DIR__/scope-head-1.0.0.tgz",
"unpackedSize": 196,
"version": "1.0.0",
}
`);
// integrity is an instance of Integrity
// https://github.com/zkat/ssri/blob/a4337cd672f341deee2b52699b6720d82e4d0ddf/index.js#L83
expect(head.integrity.toString()).toMatch(INTEGRITY_PATTERN);
expect(head.shasum).toMatch(SHASUM_PATTERN);
expect(tail).toMatchInlineSnapshot(`
Object {
"bundled": Array [],
"entryCount": 2,
"filename": "tail-4.0.0.tgz",
"files": Array [
Object {
"mode": "MODE",
"path": "index.js",
"size": 37,
},
Object {
"mode": "MODE",
"path": "package.json",
"size": 131,
},
],
"id": "tail@4.0.0",
"integrity": "INTEGRITY",
"name": "tail",
"shasum": "SHASUM",
"size": "TAR_SIZE",
"tarFilePath": "__TMP_DIR__/tail-4.0.0.tgz",
"unpackedSize": 168,
"version": "4.0.0",
}
`);
expect(tail.integrity.toString()).toMatch(INTEGRITY_PATTERN);
expect(tail.shasum).toMatch(SHASUM_PATTERN);
const next = pkgs.pop();
const pubs = await packDirectory(
next,
next.location,
Object.assign({}, conf, {
"ignore-prepublish": true,
"lerna-command": "publish",
})
);
expect(pubs).toMatchInlineSnapshot(`
Object {
"bundled": Array [],
"entryCount": 2,
"filename": "pubs-3.0.0.tgz",
"files": Array [
Object {
"mode": "MODE",
"path": "index.js",
"size": 21,
},
Object {
"mode": "MODE",
"path": "package.json",
"size": 171,
},
],
"id": "pubs@3.0.0",
"integrity": "INTEGRITY",
"name": "pubs",
"shasum": "SHASUM",
"size": "TAR_SIZE",
"tarFilePath": "__TMP_DIR__/pubs-3.0.0.tgz",
"unpackedSize": 192,
"version": "3.0.0",
}
`);
const last = pkgs.pop();
const subs = await packDirectory(
last,
// a real package doesn't _actually_ need this argument
undefined,
conf
);
expect(subs).toMatchInlineSnapshot(`
Object {
"bundled": Array [],
"entryCount": 2,
"filename": "subs-2.0.0.tgz",
"files": Array [
Object {
"mode": "MODE",
"path": "prepacked.js",
"size": 21,
},
Object {
"mode": "MODE",
"path": "package.json",
"size": 115,
},
],
"id": "subs@2.0.0",
"integrity": "INTEGRITY",
"name": "subs",
"shasum": "SHASUM",
"size": "TAR_SIZE",
"tarFilePath": "__TMP_DIR__/subs-2.0.0.tgz",
"unpackedSize": 136,
"version": "2.0.0",
}
`);
});
});