Skip to content

Commit 998e610

Browse files
committed
Add some tests for the --packages option
1 parent 50bc055 commit 998e610

File tree

9 files changed

+256
-6
lines changed

9 files changed

+256
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
],
6868
"scripts": {
6969
"pretest": "node scripts/copy_test_files.js",
70-
"test": "nyc --reporter=html --reporter=text-summary mocha --timeout=10000 'dist/test/**/*.test.js'",
70+
"test": "nyc --reporter=html --reporter=text-summary mocha --timeout=10000 'dist/test/**/*.test.js' --exclude 'dist/test/packages/**'",
7171
"prerebuild_specs": "npm run pretest",
7272
"rebuild_specs": "node scripts/rebuild_specs.js",
7373
"build": "tsc --project .",

scripts/copy_test_files.js

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,128 @@
22

33
const fs = require("fs-extra");
44
const { join } = require("path");
5+
const { spawn } = require("child_process");
6+
7+
function promiseFromChildProcess(childProcess) {
8+
return new Promise(function (resolve, reject) {
9+
childProcess.on("error", function (error) {
10+
reject(
11+
new Error(
12+
childProcess.spawnargs.join(" ") + " : " + error.message
13+
)
14+
);
15+
});
16+
childProcess.on("exit", function (code) {
17+
if (code !== 0) {
18+
reject(
19+
new Error(
20+
childProcess.spawnargs.join(" ") +
21+
" : exited with code " +
22+
code
23+
)
24+
);
25+
} else {
26+
resolve();
27+
}
28+
});
29+
});
30+
}
31+
32+
const isWindows = process.platform === "win32";
33+
const npmCommand = isWindows ? "npm.cmd" : "npm";
34+
35+
function ensureNpmVersion() {
36+
return Promise.resolve().then(() => {
37+
const npmProc = spawn(npmCommand, ["--version"], {
38+
stdio: ["ignore", "pipe", "inherit"],
39+
});
40+
let npmVersion = "";
41+
npmProc.stdout.on("data", (data) => {
42+
npmVersion += data;
43+
});
44+
return promiseFromChildProcess(npmProc).then(() => {
45+
npmVersion = npmVersion.trim();
46+
let firstDot = npmVersion.indexOf(".");
47+
const npmMajorVer = parseInt(
48+
npmVersion.slice(0, npmVersion.indexOf("."))
49+
);
50+
if (npmMajorVer < 7) {
51+
throw new Error(
52+
"npm version must be at least 7, version installed is " +
53+
npmVersion
54+
);
55+
}
56+
});
57+
});
58+
}
59+
60+
function prepareMonorepoFolder() {
61+
return Promise.resolve()
62+
.then(() => {
63+
return promiseFromChildProcess(
64+
spawn(
65+
"git",
66+
["clone", "https://github.com/efokschaner/ts-monorepo.git"],
67+
{
68+
cwd: join(__dirname, "../dist/test/packages"),
69+
stdio: "inherit",
70+
}
71+
)
72+
);
73+
})
74+
.then(() => {
75+
return promiseFromChildProcess(
76+
spawn(
77+
"git",
78+
["checkout", "73bdd4c6458ad4cc3de35498e65d55a1a44a8499"],
79+
{
80+
cwd: join(
81+
__dirname,
82+
"../dist/test/packages/ts-monorepo"
83+
),
84+
stdio: "inherit",
85+
}
86+
)
87+
);
88+
})
89+
.then(() => {
90+
return promiseFromChildProcess(
91+
spawn(npmCommand, ["install"], {
92+
cwd: join(__dirname, "../dist/test/packages/ts-monorepo"),
93+
stdio: "inherit",
94+
})
95+
);
96+
})
97+
.then(() => {
98+
return promiseFromChildProcess(
99+
spawn(npmCommand, ["run", "build"], {
100+
cwd: join(__dirname, "../dist/test/packages/ts-monorepo"),
101+
stdio: "inherit",
102+
})
103+
);
104+
});
105+
}
106+
107+
function prepareSinglePackageExample() {
108+
return Promise.resolve().then(() => {
109+
return promiseFromChildProcess(
110+
spawn(npmCommand, ["run", "build"], {
111+
cwd: join(
112+
__dirname,
113+
"../dist/test/packages/typedoc-single-package-example"
114+
),
115+
stdio: "inherit",
116+
})
117+
);
118+
});
119+
}
5120

6121
const copy = [
7122
"test/converter",
8123
"test/converter2",
9124
"test/renderer",
10125
"test/module",
126+
"test/packages",
11127
"test/utils/options/readers/data",
12128
];
13129

@@ -20,7 +136,12 @@ const copies = copy.map((dir) => {
20136
.then(() => fs.copy(source, target));
21137
});
22138

23-
Promise.all(copies).catch((reason) => {
24-
console.error(reason);
25-
process.exit(1);
26-
});
139+
Promise.all(copies)
140+
.then(ensureNpmVersion)
141+
.then(() =>
142+
Promise.all([prepareMonorepoFolder(), prepareSinglePackageExample()])
143+
)
144+
.catch((reason) => {
145+
console.error(reason);
146+
process.exit(1);
147+
});

src/test/packages.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { ok, strictEqual } from "assert";
2+
import * as Path from "path";
3+
4+
import * as td from "..";
5+
import { Logger } from "../lib/utils";
6+
import { expandPackages } from "../lib/utils/package-manifest";
7+
8+
describe("Packages support", () => {
9+
it("handles monorepos", () => {
10+
const base = Path.join(__dirname, "packages", "ts-monorepo");
11+
const app = new td.Application();
12+
app.options.addReader(new td.TypeDocReader());
13+
app.bootstrap({
14+
options: Path.join(base, "typedoc.json"),
15+
});
16+
const project = app.convert();
17+
ok(project, "Failed to convert");
18+
const result = app.serializer.projectToObject(project);
19+
ok(result.children !== undefined);
20+
strictEqual(
21+
result.children.length,
22+
4,
23+
"incorrect number of packages processed"
24+
);
25+
});
26+
27+
it("handles single packages", () => {
28+
const base = Path.join(
29+
__dirname,
30+
"packages",
31+
"typedoc-single-package-example"
32+
);
33+
const app = new td.Application();
34+
app.options.addReader(new td.TypeDocReader());
35+
app.bootstrap({
36+
options: Path.join(base, "typedoc.json"),
37+
});
38+
const project = app.convert();
39+
ok(project, "Failed to convert");
40+
const result = app.serializer.projectToObject(project);
41+
ok(result.children !== undefined);
42+
strictEqual(
43+
result.children.length,
44+
1,
45+
"incorrect number of packages processed"
46+
);
47+
});
48+
49+
describe("expandPackages", () => {
50+
it("handles a glob", () => {
51+
const base = Path.join(__dirname, "packages", "ts-monorepo");
52+
const expandedPackages = expandPackages(new Logger(), base, [
53+
"packages/*",
54+
]);
55+
strictEqual(
56+
expandedPackages.length,
57+
3,
58+
"Found an unexpected number of packages"
59+
);
60+
});
61+
});
62+
});

src/test/packages/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Example repos for "--packages" mode
2+
3+
This folder contains examples for the testing of the "--packages" mode.
4+
5+
The codebase https://github.com/efokschaner/ts-monorepo/tree/typedoc is also pulled dynamically in to the `dist` copy of this folder during tests.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "typedoc-single-package-example",
3+
"version": "1.0.0",
4+
"description": "An example of using typedoc with a single package",
5+
"private": true,
6+
"main": "dist/index.js",
7+
"scripts": {
8+
"build": "tsc",
9+
"test": "node ."
10+
},
11+
"devDependencies": {
12+
"typescript": "^4.2.4"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git://github.com/TypeStrong/TypeDoc.git"
17+
},
18+
"keywords": [
19+
"typedoc",
20+
"example"
21+
],
22+
"author": "",
23+
"license": "Apache-2.0",
24+
"bugs": {
25+
"url": "https://github.com/TypeStrong/TypeDoc/issues"
26+
},
27+
"homepage": "https://github.com/TypeStrong/TypeDoc#readme"
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function helloWorld() {
2+
return "Hello World!";
3+
}
4+
5+
if (require.main === module) {
6+
console.log(helloWorld());
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": false,
4+
"declaration": true,
5+
"declarationMap": true,
6+
"incremental": true,
7+
"module": "commonjs",
8+
"moduleResolution": "node",
9+
"noUnusedLocals": true,
10+
"noUnusedParameters": true,
11+
"outDir": "dist",
12+
"preserveConstEnums": true,
13+
"removeComments": false,
14+
"sourceMap": true,
15+
"strict": true,
16+
"target": "es2015"
17+
},
18+
"include": ["src/**/*"]
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"gitRevision": "master",
3+
"name": "typedoc-single-package-example",
4+
"out": "docs",
5+
"packages": ["."],
6+
"plugin": []
7+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"src/test/converter2/**/*.ts",
3434
"src/test/renderer/specs",
3535
"src/test/.dot/**/*.ts",
36-
"src/test/module/**/*.ts"
36+
"src/test/module/**/*.ts",
37+
"src/test/packages/**/*.ts"
3738
]
3839
}

0 commit comments

Comments
 (0)