Skip to content

Commit e5ddb9b

Browse files
committed
test(cli): use tmp library for script files
1 parent 5718765 commit e5ddb9b

3 files changed

Lines changed: 41 additions & 37 deletions

File tree

package-lock.json

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
"keywords": [],
5050
"devDependencies": {
5151
"@types/lodash": "^4.14.199",
52-
"json-schema-to-ts": "^3.1.1"
52+
"@types/tmp": "^0.2.6",
53+
"json-schema-to-ts": "^3.1.1",
54+
"tmp": "^0.2.5"
5355
}
5456
}

packages/cli/test/executor.ts

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,91 +16,74 @@
1616
import { suite, test } from "@testdeck/mocha";
1717
import { should, expect } from "chai";
1818
import { Executor, WoTContext } from "../src/executor";
19-
import { writeFileSync, unlinkSync } from "fs";
20-
import { join } from "path";
19+
import { writeFileSync } from "fs";
2120
import { Helpers } from "@node-wot/core";
21+
import tmp from "tmp";
2222

2323
should();
2424

2525
@suite("Executor")
2626
class ExecutorTest {
2727
private executor!: Executor;
28-
private testFilePath!: string;
28+
private basetTestFilePath!: string;
2929
private mockWoTContext!: WoTContext;
3030

3131
before() {
32+
tmp.setGracefulCleanup();
3233
this.executor = new Executor();
33-
this.testFilePath = join(__dirname, "./resources", "test-script-" + Date.now());
3434
this.mockWoTContext = {
3535
// We are not using WoT inside this testing scripts
3636
runtime: {} as typeof WoT,
3737
helpers: {} as Helpers,
3838
};
3939
}
4040

41-
after() {
42-
try {
43-
unlinkSync(this.testFilePath + ".js");
44-
} catch {
45-
// File may not exist
46-
}
47-
try {
48-
unlinkSync(this.testFilePath + ".mjs");
49-
} catch {
50-
// File may not exist
51-
}
52-
try {
53-
unlinkSync(this.testFilePath + ".ts");
54-
} catch {
55-
// File may not exist
56-
}
57-
try {
58-
unlinkSync(this.testFilePath + ".tsx");
59-
} catch {
60-
// File may not exist
61-
}
62-
}
41+
after() {}
6342

6443
@test async "should execute JavaScript file"() {
6544
const scriptContent = "module.exports = 'test result';";
66-
writeFileSync(this.testFilePath + ".js", scriptContent);
45+
const testFile = tmp.fileSync({ postfix: ".js" }).name;
46+
writeFileSync(testFile, scriptContent);
6747

68-
const result = await this.executor.exec(this.testFilePath + ".js", this.mockWoTContext);
48+
const result = await this.executor.exec(testFile, this.mockWoTContext);
6949

7050
expect(result).to.equal("test result");
7151
}
7252

7353
@test async "should have WoT defined"() {
7454
const scriptContent = "module.exports = typeof global.WoT !== 'undefined';";
75-
writeFileSync(this.testFilePath + ".js", scriptContent);
55+
const testFile = tmp.fileSync({ postfix: ".js" }).name;
56+
writeFileSync(testFile, scriptContent);
7657

77-
const result = await this.executor.exec(this.testFilePath + ".js", this.mockWoTContext);
58+
const result = await this.executor.exec(testFile, this.mockWoTContext);
7859

7960
expect(result).to.be.true;
8061
}
8162

8263
@test async "should handle module exports"() {
8364
const scriptContent = "module.exports = { message: 'hello' };";
84-
writeFileSync(this.testFilePath + ".js", scriptContent);
65+
const testFile = tmp.fileSync({ postfix: ".js" }).name;
66+
writeFileSync(testFile, scriptContent);
8567

86-
const result = await this.executor.exec(this.testFilePath + ".js", this.mockWoTContext);
68+
const result = await this.executor.exec(testFile, this.mockWoTContext);
8769

8870
expect(result).to.have.property("message", "hello");
8971
}
9072

9173
@test async "should detect TypeScript files by .ts extension"() {
9274
const scriptContent = "export const value: number = 42;";
93-
writeFileSync(this.testFilePath + ".ts", scriptContent);
75+
const testFile = tmp.fileSync({ postfix: ".ts" }).name;
76+
writeFileSync(testFile, scriptContent);
9477

95-
const { value } = (await this.executor.exec(this.testFilePath + ".ts", this.mockWoTContext)) as {
78+
const { value } = (await this.executor.exec(testFile, this.mockWoTContext)) as {
9679
value: number;
9780
};
9881

9982
expect(value).to.be.eq(42);
10083
}
10184

10285
@test async "should handle .mjs files as ES modules"() {
103-
const filePath = this.testFilePath + ".mjs";
86+
const filePath = tmp.fileSync({ postfix: ".mjs" }).name;
10487
const scriptContent = "export const value = 'es module';";
10588
writeFileSync(filePath, scriptContent);
10689

0 commit comments

Comments
 (0)