updates/test.js

449 lines
12 KiB
JavaScript
Raw Normal View History

"use strict";
2019-12-18 17:19:05 +00:00
const del = require("del");
2020-03-08 09:23:44 +00:00
const execa = require("execa");
2020-04-18 12:54:05 +00:00
const restana = require("restana");
2020-03-08 09:23:44 +00:00
const tempy = require("tempy");
2020-03-08 19:08:13 +00:00
const {join} = require("path");
2020-03-08 09:23:44 +00:00
const {writeFile, readFile} = require("fs").promises;
2020-10-12 19:21:34 +00:00
const enableDestroy = require("server-destroy");
2020-07-14 22:01:04 +00:00
const testFile = "./fixtures/test.json";
const testPkg = require(testFile);
2019-12-18 17:19:05 +00:00
const testDir = tempy.directory();
2020-10-05 21:26:38 +00:00
const script = join(__dirname, "updates");
2019-12-18 17:19:05 +00:00
2020-03-08 09:56:31 +00:00
const dependencyTypes = [
"dependencies",
"devDependencies",
"peerDependencies",
"optionalDependencies",
"resolutions",
2020-03-08 09:23:44 +00:00
];
2019-12-18 17:19:05 +00:00
2020-03-15 00:14:12 +00:00
const testPackages = new Set();
2020-03-08 09:56:31 +00:00
for (const dependencyType of dependencyTypes) {
2020-07-14 22:01:04 +00:00
for (const name of Object.keys(testPkg[dependencyType] || [])) {
2020-03-15 00:14:12 +00:00
testPackages.add(name);
2020-03-08 09:56:31 +00:00
}
}
2020-04-18 12:54:05 +00:00
function makeUrl(server) {
2020-10-05 21:26:38 +00:00
const {port} = server.address();
return Object.assign(new URL("http://localhost"), {port}).toString();
2020-04-18 12:54:05 +00:00
}
2020-07-21 18:38:38 +00:00
function defaultRoute(req, res) {
console.error(`default handler hit for ${req.url}`);
res.send(404);
}
function resolutionsBasePackage(name) {
const packages = name.match(/(@[^/]+\/)?([^/]+)/g) || [];
return packages[packages.length - 1];
}
2020-03-15 00:14:12 +00:00
let npmServer, githubServer, githubUrl, npmUrl;
2020-03-08 09:23:44 +00:00
beforeAll(async () => {
2020-03-09 22:57:04 +00:00
let commits, tags;
[npmServer, githubServer, commits, tags] = await Promise.all([
2020-07-21 18:38:38 +00:00
restana({defaultRoute}),
restana({defaultRoute}),
2020-03-09 22:57:04 +00:00
readFile(join(__dirname, "fixtures/github/updates-commits.json")),
readFile(join(__dirname, "fixtures/github/updates-tags.json"))
]);
2020-03-08 19:04:37 +00:00
for (const pkgName of testPackages) {
const name = testPkg.resolutions[pkgName] ? resolutionsBasePackage(pkgName) : pkgName;
const urlName = name.replace(/\//g, "%2f");
const path = join(__dirname, `fixtures/npm/${urlName}.json`);
npmServer.get(`/${urlName}`, async (_, res) => res.send(await readFile(path)));
2019-12-18 17:19:05 +00:00
}
2020-07-21 18:54:05 +00:00
githubServer.get("/repos/silverwind/updates/commits", (_, res) => res.send(commits));
githubServer.get("/repos/silverwind/updates/git/refs/tags", (_, res) => res.send(tags));
2020-03-15 00:14:12 +00:00
2020-04-18 12:54:05 +00:00
[githubServer, npmServer] = await Promise.all([
githubServer.start(0),
npmServer.start(0),
2020-03-15 00:14:12 +00:00
]);
2020-03-08 18:12:16 +00:00
2020-10-12 19:21:34 +00:00
enableDestroy(npmServer);
enableDestroy(githubServer);
2020-04-18 12:54:05 +00:00
githubUrl = makeUrl(githubServer);
npmUrl = makeUrl(npmServer);
2020-03-15 00:14:12 +00:00
await writeFile(join(testDir, ".npmrc"), `registry=${npmUrl}`); // Fake registry
2020-07-14 22:01:04 +00:00
await writeFile(join(testDir, "package.json"), JSON.stringify(testPkg, null, 2)); // Copy fixture
2020-03-08 09:23:44 +00:00
});
2019-12-18 17:19:05 +00:00
2020-03-08 09:23:44 +00:00
afterAll(async () => {
2020-03-08 18:12:16 +00:00
await Promise.all([
del(testDir, {force: true}),
2020-10-12 19:21:34 +00:00
npmServer && npmServer.destroy(),
githubServer && githubServer.destroy(),
2020-03-08 18:12:16 +00:00
]);
2020-03-08 09:23:44 +00:00
});
2020-03-08 09:23:44 +00:00
function makeTest(args, expected) {
return async () => {
2020-07-14 22:01:04 +00:00
const argsArr = [...args.split(/\s+/), "-c", "-G", githubUrl];
const {stdout} = await execa(script, argsArr, {cwd: testDir});
2020-03-08 16:06:58 +00:00
const {results} = JSON.parse(stdout);
2020-03-08 16:44:04 +00:00
// Parse results, with custom validation for the dynamic "age" property
2020-03-08 16:06:58 +00:00
for (const dependencyType of dependencyTypes) {
for (const [name, actual] of Object.entries(results[dependencyType] || {})) {
2020-05-25 16:14:37 +00:00
for (const [key, actualValue] of Object.entries(actual || {})) {
const expectedValue = expected[dependencyType][name][key];
2020-03-08 16:06:58 +00:00
if (key === "age") {
2020-05-25 16:14:37 +00:00
expect(typeof actualValue).toEqual("string");
expect(actualValue.length > 0).toBeTruthy();
2020-03-08 16:06:58 +00:00
} else {
2020-05-25 16:14:37 +00:00
expect(expectedValue).toEqual(actualValue);
2020-03-08 16:06:58 +00:00
}
}
}
}
2020-03-08 09:23:44 +00:00
};
}
2020-07-14 22:01:04 +00:00
test("simple", async () => {
2020-07-21 18:54:05 +00:00
const {stdout, stderr, exitCode} = await execa(script, ["-C", "-G", githubUrl, "-f", testFile]);
2020-07-14 22:01:04 +00:00
expect(stderr).toEqual("");
expect(stdout).toInclude("prismjs");
expect(stdout).toInclude("https://github.com/silverwind/updates");
expect(exitCode).toEqual(0);
});
2020-07-21 20:41:43 +00:00
if (process.env.CI) {
test("global", async () => {
await execa("npm", ["i", "-g", "."]);
const {stdout, stderr, exitCode} = await execa("updates", ["-C", "-G", githubUrl, "-f", testFile]);
expect(stderr).toEqual("");
expect(stdout).toInclude("prismjs");
expect(stdout).toInclude("https://github.com/silverwind/updates");
expect(exitCode).toEqual(0);
});
}
2020-03-08 09:56:31 +00:00
test("latest", makeTest("-j", {
2020-03-08 09:23:44 +00:00
dependencies: {
"gulp-sourcemaps": {
old: "2.0.0",
new: "2.6.5",
info: "https://github.com/gulp-sourcemaps/gulp-sourcemaps",
},
"prismjs": {
old: "1.0.0",
new: "1.17.1",
info: "https://github.com/LeaVerou/prism",
},
"svgstore": {
old: "^3.0.0",
new: "^3.0.0-2",
info: "https://github.com/svgstore/svgstore",
},
"html-webpack-plugin": {
old: "4.0.0-alpha.2",
new: "4.0.0-beta.11",
info: "https://github.com/jantimon/html-webpack-plugin",
},
"noty": {
old: "3.1.0",
new: "3.2.0-beta",
info: "https://github.com/needim/noty",
},
"jpeg-buffer-orientation": {
old: "0.0.0",
new: "2.0.3",
info: "https://github.com/fisker/jpeg-buffer-orientation",
},
"styled-components": {
old: "2.5.0-1",
new: "5.0.0-rc.2",
info: "https://github.com/styled-components/styled-components",
},
"@babel/preset-env": {
old: "7.0.0",
new: "7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
2020-03-08 18:12:16 +00:00
},
"updates": {
old: "6941e05",
new: "537ccb7",
info: "https://github.com/silverwind/updates",
},
2020-03-08 09:23:44 +00:00
},
peerDependencies: {
"@babel/preset-env": {
2020-03-09 21:00:55 +00:00
old: "~6.0.0",
new: "~7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
},
},
resolutions: {
"versions/updates": {
old: "^1.0.0",
new: "^10.0.0",
info: "https://github.com/silverwind/updates",
},
2020-03-08 09:23:44 +00:00
},
}));
2019-01-20 21:34:40 +00:00
2020-03-08 09:56:31 +00:00
test("greatest", makeTest("-j -g", {
2020-03-08 09:23:44 +00:00
dependencies: {
"gulp-sourcemaps": {
old: "2.0.0",
new: "2.6.5",
info: "https://github.com/gulp-sourcemaps/gulp-sourcemaps",
},
"prismjs": {
old: "1.0.0",
new: "1.17.1",
info: "https://github.com/LeaVerou/prism",
},
"html-webpack-plugin": {
old: "4.0.0-alpha.2",
new: "4.0.0-beta.11",
info: "https://github.com/jantimon/html-webpack-plugin",
},
"noty": {
old: "3.1.0",
new: "3.1.4",
info: "https://github.com/needim/noty",
},
"jpeg-buffer-orientation": {
old: "0.0.0",
new: "2.0.3",
info: "https://github.com/fisker/jpeg-buffer-orientation",
},
"styled-components": {
old: "2.5.0-1",
new: "5.0.0-rc.2",
info: "https://github.com/styled-components/styled-components",
},
"@babel/preset-env": {
old: "7.0.0",
new: "7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
2020-03-08 18:12:16 +00:00
},
"updates": {
old: "6941e05",
new: "537ccb7",
info: "https://github.com/silverwind/updates",
},
2020-03-08 09:23:44 +00:00
},
peerDependencies: {
"@babel/preset-env": {
2020-03-09 21:00:55 +00:00
old: "~6.0.0",
new: "~7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
},
},
resolutions: {
"versions/updates": {
old: "^1.0.0",
new: "^10.0.0",
info: "https://github.com/silverwind/updates",
},
},
2020-03-08 09:23:44 +00:00
}));
2020-03-08 09:56:31 +00:00
test("prerelease", makeTest("-j -g -p", {
2020-03-08 09:23:44 +00:00
dependencies: {
"gulp-sourcemaps": {
old: "2.0.0",
new: "2.6.5",
info: "https://github.com/gulp-sourcemaps/gulp-sourcemaps",
},
"prismjs": {
old: "1.0.0",
new: "1.17.1",
info: "https://github.com/LeaVerou/prism",
},
"svgstore": {
old: "^3.0.0",
new: "^3.0.0-2",
info: "https://github.com/svgstore/svgstore",
},
"html-webpack-plugin": {
old: "4.0.0-alpha.2",
new: "4.0.0-beta.11",
info: "https://github.com/jantimon/html-webpack-plugin",
},
"noty": {
old: "3.1.0",
new: "3.2.0-beta",
info: "https://github.com/needim/noty",
},
"jpeg-buffer-orientation": {
old: "0.0.0",
new: "2.0.3",
info: "https://github.com/fisker/jpeg-buffer-orientation",
},
"styled-components": {
old: "2.5.0-1",
new: "5.0.0-rc.2",
info: "https://github.com/styled-components/styled-components",
},
"@babel/preset-env": {
old: "7.0.0",
new: "7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
2020-03-08 18:12:16 +00:00
},
"updates": {
old: "6941e05",
new: "537ccb7",
info: "https://github.com/silverwind/updates",
},
2020-03-08 09:23:44 +00:00
},
peerDependencies: {
"@babel/preset-env": {
2020-03-09 21:00:55 +00:00
old: "~6.0.0",
new: "~7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
},
},
resolutions: {
"versions/updates": {
old: "^1.0.0",
new: "^10.0.0",
info: "https://github.com/silverwind/updates",
},
2020-03-08 09:23:44 +00:00
},
}));
2020-03-08 09:56:31 +00:00
test("release", makeTest("-j -R", {
2020-03-08 09:23:44 +00:00
dependencies: {
"gulp-sourcemaps": {
old: "2.0.0",
new: "2.6.5",
info: "https://github.com/gulp-sourcemaps/gulp-sourcemaps",
},
"prismjs": {
old: "1.0.0",
new: "1.17.1",
info: "https://github.com/LeaVerou/prism",
},
"svgstore": {
old: "^3.0.0",
new: "^2.0.3",
info: "https://github.com/svgstore/svgstore",
},
"html-webpack-plugin": {
old: "4.0.0-alpha.2",
new: "3.2.0",
info: "https://github.com/jantimon/html-webpack-plugin",
},
"noty": {
old: "3.1.0",
new: "3.1.4",
info: "https://github.com/needim/noty",
},
"jpeg-buffer-orientation": {
old: "0.0.0",
new: "2.0.3",
info: "https://github.com/fisker/jpeg-buffer-orientation",
},
"styled-components": {
old: "2.5.0-1",
new: "4.4.1",
info: "https://github.com/styled-components/styled-components",
},
"@babel/preset-env": {
old: "7.0.0",
new: "7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
2020-03-08 18:12:16 +00:00
},
"updates": {
old: "6941e05",
new: "537ccb7",
info: "https://github.com/silverwind/updates",
},
2020-03-08 09:23:44 +00:00
},
peerDependencies: {
"@babel/preset-env": {
2020-03-09 21:00:55 +00:00
old: "~6.0.0",
new: "~7.11.5",
info: "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env",
},
},
resolutions: {
"versions/updates": {
old: "^1.0.0",
new: "^10.0.0",
info: "https://github.com/silverwind/updates",
2020-03-08 18:12:16 +00:00
},
2020-03-08 09:23:44 +00:00
},
}));
2020-03-08 09:56:31 +00:00
test("patch", makeTest("-j -P", {
2020-03-08 09:23:44 +00:00
dependencies: {
"gulp-sourcemaps": {
old: "2.0.0",
new: "2.0.1",
info: "https://github.com/floridoo/gulp-sourcemaps",
},
"svgstore": {
old: "^3.0.0",
new: "^3.0.0-2",
info: "https://github.com/svgstore/svgstore",
},
"html-webpack-plugin": {
old: "4.0.0-alpha.2",
new: "4.0.0-beta.11",
info: "https://github.com/jantimon/html-webpack-plugin",
},
"noty": {
old: "3.1.0",
new: "3.1.4",
info: "https://github.com/needim/noty",
},
2020-03-08 18:12:16 +00:00
"updates": {
old: "6941e05",
new: "537ccb7",
info: "https://github.com/silverwind/updates",
},
2020-03-08 09:23:44 +00:00
},
resolutions: {
"versions/updates": {
old: "^1.0.0",
new: "^1.0.6",
info: "https://github.com/silverwind/updates",
},
},
2020-03-08 09:23:44 +00:00
}));
test("include version deps", makeTest("-j -i noty", {
dependencies: {
"noty": {
old: "3.1.0",
new: "3.2.0-beta",
info: "https://github.com/needim/noty",
},
},
}));
test("include version deps #2", makeTest("-j -i noty -i noty,noty", {
dependencies: {
"noty": {
old: "3.1.0",
new: "3.2.0-beta",
info: "https://github.com/needim/noty",
},
},
}));
test("exclude version deps", makeTest("-j -e gulp-sourcemaps,prismjs,svgstore,html-webpack-plugin,noty,jpeg-buffer-orientation,styled-components,@babel/preset-env,versions/updates", {
dependencies: {
"updates": {
old: "6941e05",
new: "537ccb7",
info: "https://github.com/silverwind/updates",
},
},
}));