Skip to content

Commit

Permalink
Merge pull request #54 from shulandmimi/main
Browse files Browse the repository at this point in the history
revert: tests deps collect and clean
  • Loading branch information
i5ting authored Oct 25, 2022
2 parents 36b85cb + fc82c9d commit 127d94f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 83 deletions.
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
"dependencies": {
"@ts-junit/core": "workspace:*",
"@ts-junit/utils": "workspace:*",
"bluebird": "^3.7.2",
"yargs": "^17.5.1"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.32.0",
"@types/yargs": "^17.0.13",
"@types/node": "^18.7.18",
"@types/yargs": "^17.0.13",
"np": "^7.6.2",
"rimraf": "^3.0.2",
"typescript": "^4.8.3"
Expand Down
47 changes: 46 additions & 1 deletion packages/cli/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// @ts-nocheck
import ts from "typescript";
import { getFiles, getCompileFiles, Context, Strategy } from "@ts-junit/core";
import path from "path";
import Promise2 from "bluebird";
import { sleep } from "@ts-junit/utils";
import { watch, runTestEmitter } from "./watch";
import { getCompileFilesNotExistInDistDirectory } from "./utils";
import {
getCompileFilesNotExistInDistDirectory,
registerRequireExtension,
unregisterRequireExtension,
collectionUnitDep,
} from "./utils";
import { debug } from "./debug";

export class CliContext extends Context {
Expand All @@ -28,6 +37,42 @@ export class CliContext extends Context {
});
}

public async runCliTests(): any {
const that = this;
debug("runCliTests");
debug(this.base);
debug(this.buildOutput);
debug(this.rest);
let files = getFiles(this);
files = files.map(function (file) {
return (
that.buildOutput +
path.resolve(
that.buildOutput,
file.replace(that.buildRoot, "").replace(".ts", "") + ".js",
)
);
});

debug(files);

const iterator = async (element) => {
return this._runTsTestFile(element).then(sleep(100));
};

const originExtension = { ...require.extensions };

registerRequireExtension(originExtension, (_, module) => {
collectionUnitDep(module.id);
});

const result = await Promise2.each(files, iterator);

unregisterRequireExtension(originExtension);

return result;
}

runTests() {
const that = this;

Expand Down
37 changes: 37 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,40 @@ export function processRequire(

return _code.join("\n");
}

export function registerRequireExtension(
target: NodeJS.RequireExtensions,
callback: (ext: string, module: NodeJS.Module, context: string) => void,
) {
const extensions = Object.keys(target) as (keyof typeof target)[];

Object.assign(
require.extensions,
extensions.reduce((result, ext) => {
return {
...result,
[ext]: (module: NodeJS.Module, context: string) => {
callback(ext as string, module, context);
return target[ext]?.(module, context);
},
};
}, {} as NodeJS.RequireExtensions),
);
}

export function unregisterRequireExtension(target: NodeJS.RequireExtensions) {
Object.assign(require.extensions, target);
}

const deps: string[] = [];

export function collectionUnitDep(filename: string) {
deps.push(filename);
}

export function cleanUnitTestsRequireCache() {
if (deps.length) {
deps.forEach((filename) => Reflect.deleteProperty(require.cache, filename));
deps.length = 0;
}
}
13 changes: 9 additions & 4 deletions packages/cli/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EventEmitter } from "node:events";
import ts from "typescript";
import { ensureDirectoryExistence } from "@ts-junit/core";
import { debug } from "./debug";
import { processRequire } from "./utils";
import { cleanUnitTestsRequireCache, processRequire } from "./utils";

const files: ts.MapLike<{ version: number }> = {};
export const runTestEmitter = new EventEmitter();
Expand Down Expand Up @@ -85,7 +85,9 @@ export function watch(
logErrors(fileName);
}

output.outputFiles.forEach((o) => {
cleanUnitTestsRequireCache();

const tasks = output.outputFiles.map((o) => {
const fileName = path.join(
path.resolve(__dirname, "../"),
"output/" + o.name.replace(path.resolve(__dirname, "../"), ""),
Expand All @@ -97,10 +99,13 @@ export function watch(

const code = processRequire(fileName, o.text, needReplaceFiles);

fs.writeFileSync(fileName, code);
return fs.promises.writeFile(fileName, code);
});

runTestEmitter.emit("runTestEvent");
// wait the file write
Promise.all(tasks).then(() => {
runTestEmitter.emit("runTestEvent");
});
}

function logErrors(fileName: string) {
Expand Down
76 changes: 0 additions & 76 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
// @ts-nocheck
import * as path from "node:path";
import Promise2 from "bluebird";
import { sleep } from "@ts-junit/utils";
import { Strategy } from "@ts-junit/strategy";
import { loadFromCache } from "@ts-junit/decorator";
import { debug } from "./debug";
import { getFiles } from "./utils";

// function registerRequireExtension(
// target: NodeJS.RequireExtensions,
// callback: (ext: string, module: NodeJS.Module, context: string) => void,
// ) {
// const extensions = Object.keys(target) as (keyof typeof target)[];

// Object.assign(
// require.extensions,
// extensions.reduce((result, ext) => {
// return {
// ...result,
// [ext]: (module: NodeJS.Module, context: string) => {
// callback(ext as string, module, context);
// return target[ext]?.(module, context);
// },
// };
// }, {} as NodeJS.RequireExtensions),
// );
// }

// function unregisterRequireExtension(target: NodeJS.RequireExtensions) {
// Object.assign(require.extensions, target);
// }

interface ContextOptions {
rest: string[];
base: string;
Expand Down Expand Up @@ -77,56 +51,6 @@ export class Context {
this.strategy = strategy;
}

// public async runTsTestFiles(files: string[]): Promise<any> {
// files = files.map(function (file) {
// return file.replace(".ts", "");
// });

// const iterator = async (element) => this._runTsTestFile(element);

// const deps: string[] = [];
// const originExtension = { ...require.extensions };

// // 收集测试用例引用的依赖
// registerRequireExtension(originExtension, (ext, module) => {
// deps.push(module.filename);
// });

// const result = await Promise2.each(files, iterator);

// unregisterRequireExtension(originExtension);

// // 删除依赖
// // todo: exclude some path
// deps.forEach((filename) => Reflect.deleteProperty(require.cache, filename));

// return result;
// }

public runCliTests(): any {
const that = this;
debug("runCliTests");
debug(this.base);
debug(this.buildOutput);
debug(this.rest);
let files = getFiles(this);
files = files.map(function (file) {
return (
that.buildOutput +
path.resolve(
that.buildOutput,
file.replace(that.buildRoot, "").replace(".ts", "") + ".js",
)
);
});

debug(files);
const iterator = async (element) => {
return this._runTsTestFile(element).then(sleep(100));
};
return Promise2.each(files, iterator);
}

public runTests(): any {
// const rest = this.rest;
// get all file from rest(file or folder)
Expand Down
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit 127d94f

Please sign in to comment.