-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathprepare.test.js
246 lines (217 loc) · 8.11 KB
/
prepare.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import path from "path";
import test from "ava";
import fs from "fs-extra";
import { temporaryDirectory, temporaryFile } from "tempy";
import { execa } from "execa";
import { stub } from "sinon";
import { WritableStreamBuffer } from "stream-buffers";
import prepare from "../lib/prepare.js";
test.beforeEach((t) => {
t.context.log = stub();
t.context.logger = { log: t.context.log };
t.context.stdout = new WritableStreamBuffer();
t.context.stderr = new WritableStreamBuffer();
});
test("Update package.json", async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const packagePath = path.resolve(cwd, "package.json");
await fs.outputJson(packagePath, { version: "0.0.0-dev" });
await prepare(
npmrc,
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json has been updated
t.is((await fs.readJson(packagePath)).version, "1.0.0");
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", cwd]);
});
test("Updade package.json and npm-shrinkwrap.json", async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const packagePath = path.resolve(cwd, "package.json");
const shrinkwrapPath = path.resolve(cwd, "npm-shrinkwrap.json");
await fs.outputJson(packagePath, { version: "0.0.0-dev" });
// Create a npm-shrinkwrap.json file
await execa("npm", ["shrinkwrap"], { cwd });
await prepare(
npmrc,
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json and npm-shrinkwrap.json have been updated
t.is((await fs.readJson(packagePath)).version, "1.0.0");
t.is((await fs.readJson(shrinkwrapPath)).version, "1.0.0");
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", cwd]);
});
test("Updade package.json and package-lock.json", async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const packagePath = path.resolve(cwd, "package.json");
const packageLockPath = path.resolve(cwd, "package-lock.json");
await fs.outputJson(packagePath, { version: "0.0.0-dev" });
await fs.appendFile(path.resolve(cwd, ".npmrc"), "package-lock = true");
// Create a package-lock.json file
await execa("npm", ["install"], { cwd });
await prepare(
npmrc,
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json and package-lock.json have been updated
t.is((await fs.readJson(packagePath)).version, "1.0.0");
t.is((await fs.readJson(packageLockPath)).version, "1.0.0");
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", cwd]);
});
test("Updade package.json and npm-shrinkwrap.json in a sub-directory", async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const pkgRoot = "dist";
const packagePath = path.resolve(cwd, pkgRoot, "package.json");
const shrinkwrapPath = path.resolve(cwd, pkgRoot, "npm-shrinkwrap.json");
await fs.outputJson(packagePath, { version: "0.0.0-dev" });
// Create a npm-shrinkwrap.json file
await execa("npm", ["shrinkwrap"], { cwd: path.resolve(cwd, pkgRoot) });
await prepare(
npmrc,
{ pkgRoot },
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json and npm-shrinkwrap.json have been updated
t.is((await fs.readJson(packagePath)).version, "1.0.0");
t.is((await fs.readJson(shrinkwrapPath)).version, "1.0.0");
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", path.resolve(cwd, pkgRoot)]);
});
test("Updade package.json and package-lock.json in a sub-directory", async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const pkgRoot = "dist";
const packagePath = path.resolve(cwd, pkgRoot, "package.json");
const packageLockPath = path.resolve(cwd, pkgRoot, "package-lock.json");
await fs.outputJson(packagePath, { version: "0.0.0-dev" });
await fs.appendFile(path.resolve(cwd, pkgRoot, ".npmrc"), "package-lock = true");
// Create a package-lock.json file
await execa("npm", ["install"], { cwd: path.resolve(cwd, pkgRoot) });
await prepare(
npmrc,
{ pkgRoot },
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json and package-lock.json have been updated
t.is((await fs.readJson(packagePath)).version, "1.0.0");
t.is((await fs.readJson(packageLockPath)).version, "1.0.0");
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", path.resolve(cwd, pkgRoot)]);
});
test("Preserve indentation and newline", async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const packagePath = path.resolve(cwd, "package.json");
await fs.outputFile(packagePath, `{\r\n "name": "package-name",\r\n "version": "0.0.0-dev"\r\n}\r\n`);
await prepare(
npmrc,
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json has been updated
t.is(
await fs.readFile(packagePath, "utf-8"),
`{\r\n "name": "package-name",\r\n "version": "1.0.0"\r\n}\r\n`
);
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", cwd]);
});
test('Create the package in the "tarballDir" directory', async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const packagePath = path.resolve(cwd, "package.json");
const pkg = { name: "my-pkg", version: "0.0.0-dev" };
await fs.outputJson(packagePath, pkg);
await prepare(
npmrc,
{ tarballDir: "tarball" },
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json has been updated
t.is((await fs.readJson(packagePath)).version, "1.0.0");
t.true(await fs.pathExists(path.resolve(cwd, `tarball/${pkg.name}-1.0.0.tgz`)));
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", cwd]);
});
test('Only move the created tarball if the "tarballDir" directory is not the CWD', async (t) => {
const cwd = temporaryDirectory();
const npmrc = temporaryFile({ name: ".npmrc" });
const packagePath = path.resolve(cwd, "package.json");
const pkg = { name: "my-pkg", version: "0.0.0-dev" };
await fs.outputJson(packagePath, pkg);
await prepare(
npmrc,
{ tarballDir: "." },
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: { version: "1.0.0" },
logger: t.context.logger,
}
);
// Verify package.json has been updated
t.is((await fs.readJson(packagePath)).version, "1.0.0");
t.true(await fs.pathExists(path.resolve(cwd, `${pkg.name}-1.0.0.tgz`)));
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ["Write version %s to package.json in %s", "1.0.0", cwd]);
});