Skip to content

Commit 7d0ce87

Browse files
committed
Fix path trimming in tests
Incidentally, this also: Fixes #800 Fixes #642 Removes ts-node since this fix results in tests breaking when run through ts-node. And because I needed to rebuild tests, pulls in the refactoring for rebuild_specs.js from #801
1 parent 6cdc62a commit 7d0ce87

File tree

5 files changed

+174
-192
lines changed

5 files changed

+174
-192
lines changed

package-lock.json

Lines changed: 0 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
"mocha": "^6.2.2",
5454
"mockery": "^2.1.0",
5555
"nyc": "15.0.0-beta.0",
56-
"ts-node": "^8.4.1",
5756
"tslint": "^5.20.1"
5857
},
5958
"files": [
@@ -66,7 +65,8 @@
6665
"scripts": {
6766
"pretest": "node scripts/copy_test_files.js",
6867
"test": "nyc --reporter=html --reporter=text-summary mocha --timeout=10000 dist/test/*.test.js",
69-
"test:ts": "mocha --require ts-node/register --watch-extensions ts --timeout=10000 src/test/*.test.ts",
68+
"prerebuild_specs": "npm run pretest",
69+
"rebuild_specs": "node scripts/rebuild_specs.js",
7070
"build": "tsc --project .",
7171
"postbuild": "node scripts/replace_application_version.js",
7272
"build_and_test": "npm run build && npm run test",

scripts/rebuild_specs.js

Lines changed: 100 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs-extra');
44
const path = require('path');
5-
const TypeDoc = require(path.join(__dirname, '..'));
5+
const TypeDoc = require('..');
66

77
const app = new TypeDoc.Application({
88
mode: 'Modules',
@@ -18,75 +18,111 @@ const app = new TypeDoc.Application({
1818
],
1919
});
2020

21-
const base = path.join(__dirname, '../src/test/converter');
21+
// Note that this uses the test files in dist, not in src, this is important since
22+
// when running the tests we copy the tests to dist and then convert them.
23+
const base = path.join(__dirname, '../dist/test/converter');
2224

23-
fs.remove(path.join(__dirname, '../src/test/renderer/specs'))
24-
.then(() => fs.readdir(base))
25-
.then(dirs => {
26-
// Get converter directories
27-
return Promise.all(dirs.map(dir => {
28-
const dirPath = path.join(base, dir);
29-
return Promise.all([ dirPath, fs.stat(dirPath) ]);
30-
}));
31-
}).then(dirs => {
32-
// Rebuild converter specs
33-
return dirs.map(([ fullPath, isDir ]) => {
34-
if (!isDir) return;
25+
/** @type {[string, () => void, () => void][]} */
26+
const conversions = [
27+
['specs', () => { }, () => { }],
28+
['specs-without-exported',
29+
() => app.options.setValue('excludeNotExported', true),
30+
() => app.options.setValue('excludeNotExported', false)
31+
],
32+
['specs-with-lump-categories',
33+
() => app.options.setValue('categorizeByGroup', false),
34+
() => app.options.setValue('categorizeByGroup', true)
35+
],
36+
];
3537

36-
console.log(fullPath);
37-
TypeDoc.resetReflectionID();
38-
const src = app.expandInputFiles([ fullPath ]);
39-
const out = path.join(fullPath, 'specs.json');
40-
const result = app.convert(src);
41-
const data = JSON.stringify(result.toObject(), null, ' ')
42-
.split(TypeDoc.normalizePath(base))
43-
.join('%BASE%');
38+
/**
39+
* Rebuilds the converter specs for the provided dirs.
40+
* @param {string[]} dirs
41+
*/
42+
function rebuildConverterTests(dirs) {
43+
return Promise.all(dirs.map(fullPath => {
44+
console.log(fullPath);
45+
const src = app.expandInputFiles([fullPath]);
46+
return Promise.all(conversions.map(([file, before, after]) => {
47+
const out = path.join(fullPath, `${file}.json`);
48+
if (fs.existsSync(out)) {
49+
TypeDoc.resetReflectionID();
50+
before();
51+
const result = app.convert(src);
52+
const data = JSON.stringify(result.toObject(), null, ' ')
53+
.split(TypeDoc.normalizePath(base))
54+
.join('%BASE%');
55+
after();
56+
return fs.writeFile(out.replace('dist', 'src'), data);
57+
}
58+
}));
59+
}));
60+
}
4461

45-
return fs.writeFile(out, data);
46-
})
47-
}).then(() => {
48-
// Rebuild renderer example
49-
const src = path.join(__dirname, '../examples/basic/src');
50-
const out = path.join(__dirname, '../src/test/renderer/specs');
62+
async function rebuildRendererTest() {
63+
await fs.remove(path.join(__dirname, '../src/test/renderer/specs'));
64+
const src = path.join(__dirname, '../examples/basic/src');
65+
const out = path.join(__dirname, '../src/test/renderer/specs');
5166

52-
return fs.remove(out)
53-
.then(() => app.generateDocs(app.expandInputFiles([src]), out))
54-
.then(() => fs.remove(path.join(out, 'assets')))
55-
.then(() => out);
56-
}).then(out => {
57-
// Rewrite GitHub urls
67+
await fs.remove(out)
68+
app.generateDocs(app.expandInputFiles([src]), out)
69+
await fs.remove(path.join(out, 'assets'))
5870

59-
/**
60-
* Avoiding sync methods here is... difficult.
61-
* @param {string} base
62-
* @param {string} dir
63-
* @param {string[]} results
64-
* @returns {string[]}
65-
*/
66-
function getFiles(base, dir = '', results = []) {
67-
const files = fs.readdirSync(path.join(base, dir));
68-
for (const file of files) {
69-
const relativeToBase = path.join(dir, file);
70-
if (fs.statSync(path.join(base, relativeToBase)).isDirectory()) {
71-
getFiles(base, relativeToBase, results);
72-
} else {
73-
results.push(relativeToBase);
74-
}
71+
/**
72+
* Avoiding sync methods here is... difficult.
73+
* @param {string} base
74+
* @param {string} dir
75+
* @param {string[]} results
76+
* @returns {string[]}
77+
*/
78+
function getFiles(base, dir = '', results = []) {
79+
const files = fs.readdirSync(path.join(base, dir));
80+
for (const file of files) {
81+
const relativeToBase = path.join(dir, file);
82+
if (fs.statSync(path.join(base, relativeToBase)).isDirectory()) {
83+
getFiles(base, relativeToBase, results);
84+
} else {
85+
results.push(relativeToBase);
7586
}
76-
return results;
7787
}
88+
return results;
89+
}
7890

79-
const gitHubRegExp = /https:\/\/github.com\/[A-Za-z0-9\-]+\/typedoc\/blob\/[^\/]*\/examples/g;
80-
return getFiles(out).map(file => {
81-
const full = path.join(out, file);
82-
return fs.readFile(full, { encoding: 'utf-8' })
83-
.then(text => fs.writeFile(
84-
full,
85-
text.replace(gitHubRegExp, 'https://github.com/sebastian-lenz/typedoc/blob/master/examples')
86-
));
87-
});
88-
})
89-
.catch(reason => {
90-
console.error(reason);
91-
process.exit(1);
91+
const gitHubRegExp = /https:\/\/github.com\/[A-Za-z0-9\-]+\/typedoc\/blob\/[^\/]*\/examples/g;
92+
return getFiles(out).map(file => {
93+
const full = path.join(out, file);
94+
return fs.readFile(full, { encoding: 'utf-8' })
95+
.then(text => fs.writeFile(
96+
full,
97+
text.replace(gitHubRegExp, 'https://github.com/sebastian-lenz/typedoc/blob/master/examples')
98+
));
9299
});
100+
}
101+
102+
async function main(command = 'all', filter = '') {
103+
if (!['all', 'converter', 'renderer'].includes(command)) {
104+
console.error('Invalid command. Usage: node scripts/rebuild_specs.js <all|converter|renderer> [filter]');
105+
throw new Error();
106+
}
107+
108+
if (['all', 'converter'].includes(command)) {
109+
const dirs = await Promise.all((await fs.readdir(base)).map(dir => {
110+
const dirPath = path.join(base, dir);
111+
return Promise.all([dirPath, fs.stat(dirPath)]);
112+
}));
113+
114+
await rebuildConverterTests(dirs.filter(([fullPath, stat]) => {
115+
if (!stat.isDirectory()) return false;
116+
return fullPath.endsWith(filter);
117+
}).map(([path]) => path));
118+
}
119+
120+
if (['all', 'renderer'].includes(command)) {
121+
await rebuildRendererTest();
122+
}
123+
}
124+
125+
main(process.argv[2], process.argv[3]).catch(reason => {
126+
console.error(reason);
127+
process.exit(1);
128+
});

src/lib/converter/plugins/SourcePlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class SourcePlugin extends ConverterComponent {
9292
}
9393
const sourceFile = node.getSourceFile();
9494
const fileName = sourceFile.fileName;
95+
this.basePath.add(fileName);
9596
const file: SourceFile = this.getSourceFile(fileName, context.project);
9697

9798
let position: ts.LineAndCharacter;

0 commit comments

Comments
 (0)