Skip to content

Commit

Permalink
Fix path trimming in tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Gerrit0 committed Nov 13, 2019
1 parent 6cdc62a commit 7d0ce87
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 192 deletions.
55 changes: 0 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -53,7 +53,6 @@
"mocha": "^6.2.2",
"mockery": "^2.1.0",
"nyc": "15.0.0-beta.0",
"ts-node": "^8.4.1",
"tslint": "^5.20.1"
},
"files": [
Expand All @@ -66,7 +65,8 @@
"scripts": {
"pretest": "node scripts/copy_test_files.js",
"test": "nyc --reporter=html --reporter=text-summary mocha --timeout=10000 dist/test/*.test.js",
"test:ts": "mocha --require ts-node/register --watch-extensions ts --timeout=10000 src/test/*.test.ts",
"prerebuild_specs": "npm run pretest",
"rebuild_specs": "node scripts/rebuild_specs.js",
"build": "tsc --project .",
"postbuild": "node scripts/replace_application_version.js",
"build_and_test": "npm run build && npm run test",
Expand Down
164 changes: 100 additions & 64 deletions scripts/rebuild_specs.js
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs-extra');
const path = require('path');
const TypeDoc = require(path.join(__dirname, '..'));
const TypeDoc = require('..');

const app = new TypeDoc.Application({
mode: 'Modules',
Expand All @@ -18,75 +18,111 @@ const app = new TypeDoc.Application({
],
});

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

fs.remove(path.join(__dirname, '../src/test/renderer/specs'))
.then(() => fs.readdir(base))
.then(dirs => {
// Get converter directories
return Promise.all(dirs.map(dir => {
const dirPath = path.join(base, dir);
return Promise.all([ dirPath, fs.stat(dirPath) ]);
}));
}).then(dirs => {
// Rebuild converter specs
return dirs.map(([ fullPath, isDir ]) => {
if (!isDir) return;
/** @type {[string, () => void, () => void][]} */
const conversions = [
['specs', () => { }, () => { }],
['specs-without-exported',
() => app.options.setValue('excludeNotExported', true),
() => app.options.setValue('excludeNotExported', false)
],
['specs-with-lump-categories',
() => app.options.setValue('categorizeByGroup', false),
() => app.options.setValue('categorizeByGroup', true)
],
];

console.log(fullPath);
TypeDoc.resetReflectionID();
const src = app.expandInputFiles([ fullPath ]);
const out = path.join(fullPath, 'specs.json');
const result = app.convert(src);
const data = JSON.stringify(result.toObject(), null, ' ')
.split(TypeDoc.normalizePath(base))
.join('%BASE%');
/**
* Rebuilds the converter specs for the provided dirs.
* @param {string[]} dirs
*/
function rebuildConverterTests(dirs) {
return Promise.all(dirs.map(fullPath => {
console.log(fullPath);
const src = app.expandInputFiles([fullPath]);
return Promise.all(conversions.map(([file, before, after]) => {
const out = path.join(fullPath, `${file}.json`);
if (fs.existsSync(out)) {
TypeDoc.resetReflectionID();
before();
const result = app.convert(src);
const data = JSON.stringify(result.toObject(), null, ' ')
.split(TypeDoc.normalizePath(base))
.join('%BASE%');
after();
return fs.writeFile(out.replace('dist', 'src'), data);
}
}));
}));
}

return fs.writeFile(out, data);
})
}).then(() => {
// Rebuild renderer example
const src = path.join(__dirname, '../examples/basic/src');
const out = path.join(__dirname, '../src/test/renderer/specs');
async function rebuildRendererTest() {
await fs.remove(path.join(__dirname, '../src/test/renderer/specs'));
const src = path.join(__dirname, '../examples/basic/src');
const out = path.join(__dirname, '../src/test/renderer/specs');

return fs.remove(out)
.then(() => app.generateDocs(app.expandInputFiles([src]), out))
.then(() => fs.remove(path.join(out, 'assets')))
.then(() => out);
}).then(out => {
// Rewrite GitHub urls
await fs.remove(out)
app.generateDocs(app.expandInputFiles([src]), out)
await fs.remove(path.join(out, 'assets'))

/**
* Avoiding sync methods here is... difficult.
* @param {string} base
* @param {string} dir
* @param {string[]} results
* @returns {string[]}
*/
function getFiles(base, dir = '', results = []) {
const files = fs.readdirSync(path.join(base, dir));
for (const file of files) {
const relativeToBase = path.join(dir, file);
if (fs.statSync(path.join(base, relativeToBase)).isDirectory()) {
getFiles(base, relativeToBase, results);
} else {
results.push(relativeToBase);
}
/**
* Avoiding sync methods here is... difficult.
* @param {string} base
* @param {string} dir
* @param {string[]} results
* @returns {string[]}
*/
function getFiles(base, dir = '', results = []) {
const files = fs.readdirSync(path.join(base, dir));
for (const file of files) {
const relativeToBase = path.join(dir, file);
if (fs.statSync(path.join(base, relativeToBase)).isDirectory()) {
getFiles(base, relativeToBase, results);
} else {
results.push(relativeToBase);
}
return results;
}
return results;
}

const gitHubRegExp = /https:\/\/github.com\/[A-Za-z0-9\-]+\/typedoc\/blob\/[^\/]*\/examples/g;
return getFiles(out).map(file => {
const full = path.join(out, file);
return fs.readFile(full, { encoding: 'utf-8' })
.then(text => fs.writeFile(
full,
text.replace(gitHubRegExp, 'https://github.com/sebastian-lenz/typedoc/blob/master/examples')
));
});
})
.catch(reason => {
console.error(reason);
process.exit(1);
const gitHubRegExp = /https:\/\/github.com\/[A-Za-z0-9\-]+\/typedoc\/blob\/[^\/]*\/examples/g;
return getFiles(out).map(file => {
const full = path.join(out, file);
return fs.readFile(full, { encoding: 'utf-8' })
.then(text => fs.writeFile(
full,
text.replace(gitHubRegExp, 'https://github.com/sebastian-lenz/typedoc/blob/master/examples')
));
});
}

async function main(command = 'all', filter = '') {
if (!['all', 'converter', 'renderer'].includes(command)) {
console.error('Invalid command. Usage: node scripts/rebuild_specs.js <all|converter|renderer> [filter]');
throw new Error();
}

if (['all', 'converter'].includes(command)) {
const dirs = await Promise.all((await fs.readdir(base)).map(dir => {
const dirPath = path.join(base, dir);
return Promise.all([dirPath, fs.stat(dirPath)]);
}));

await rebuildConverterTests(dirs.filter(([fullPath, stat]) => {
if (!stat.isDirectory()) return false;
return fullPath.endsWith(filter);
}).map(([path]) => path));
}

if (['all', 'renderer'].includes(command)) {
await rebuildRendererTest();
}
}

main(process.argv[2], process.argv[3]).catch(reason => {
console.error(reason);
process.exit(1);
});
1 change: 1 addition & 0 deletions src/lib/converter/plugins/SourcePlugin.ts
Expand Up @@ -92,6 +92,7 @@ export class SourcePlugin extends ConverterComponent {
}
const sourceFile = node.getSourceFile();
const fileName = sourceFile.fileName;
this.basePath.add(fileName);
const file: SourceFile = this.getSourceFile(fileName, context.project);

let position: ts.LineAndCharacter;
Expand Down

0 comments on commit 7d0ce87

Please sign in to comment.