|
16 | 16 | import { suite, test } from "@testdeck/mocha"; |
17 | 17 | import { should, expect } from "chai"; |
18 | 18 | import { Executor, WoTContext } from "../src/executor"; |
19 | | -import { writeFileSync, unlinkSync } from "fs"; |
20 | | -import { join } from "path"; |
| 19 | +import { writeFileSync } from "fs"; |
21 | 20 | import { Helpers } from "@node-wot/core"; |
| 21 | +import tmp from "tmp"; |
22 | 22 |
|
23 | 23 | should(); |
24 | 24 |
|
25 | 25 | @suite("Executor") |
26 | 26 | class ExecutorTest { |
27 | 27 | private executor!: Executor; |
28 | | - private testFilePath!: string; |
| 28 | + private basetTestFilePath!: string; |
29 | 29 | private mockWoTContext!: WoTContext; |
30 | 30 |
|
31 | 31 | before() { |
| 32 | + tmp.setGracefulCleanup(); |
32 | 33 | this.executor = new Executor(); |
33 | | - this.testFilePath = join(__dirname, "./resources", "test-script-" + Date.now()); |
34 | 34 | this.mockWoTContext = { |
35 | 35 | // We are not using WoT inside this testing scripts |
36 | 36 | runtime: {} as typeof WoT, |
37 | 37 | helpers: {} as Helpers, |
38 | 38 | }; |
39 | 39 | } |
40 | 40 |
|
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() {} |
63 | 42 |
|
64 | 43 | @test async "should execute JavaScript file"() { |
65 | 44 | const scriptContent = "module.exports = 'test result';"; |
66 | | - writeFileSync(this.testFilePath + ".js", scriptContent); |
| 45 | + const testFile = tmp.fileSync({ postfix: ".js" }).name; |
| 46 | + writeFileSync(testFile, scriptContent); |
67 | 47 |
|
68 | | - const result = await this.executor.exec(this.testFilePath + ".js", this.mockWoTContext); |
| 48 | + const result = await this.executor.exec(testFile, this.mockWoTContext); |
69 | 49 |
|
70 | 50 | expect(result).to.equal("test result"); |
71 | 51 | } |
72 | 52 |
|
73 | 53 | @test async "should have WoT defined"() { |
74 | 54 | 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); |
76 | 57 |
|
77 | | - const result = await this.executor.exec(this.testFilePath + ".js", this.mockWoTContext); |
| 58 | + const result = await this.executor.exec(testFile, this.mockWoTContext); |
78 | 59 |
|
79 | 60 | expect(result).to.be.true; |
80 | 61 | } |
81 | 62 |
|
82 | 63 | @test async "should handle module exports"() { |
83 | 64 | const scriptContent = "module.exports = { message: 'hello' };"; |
84 | | - writeFileSync(this.testFilePath + ".js", scriptContent); |
| 65 | + const testFile = tmp.fileSync({ postfix: ".js" }).name; |
| 66 | + writeFileSync(testFile, scriptContent); |
85 | 67 |
|
86 | | - const result = await this.executor.exec(this.testFilePath + ".js", this.mockWoTContext); |
| 68 | + const result = await this.executor.exec(testFile, this.mockWoTContext); |
87 | 69 |
|
88 | 70 | expect(result).to.have.property("message", "hello"); |
89 | 71 | } |
90 | 72 |
|
91 | 73 | @test async "should detect TypeScript files by .ts extension"() { |
92 | 74 | 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); |
94 | 77 |
|
95 | | - const { value } = (await this.executor.exec(this.testFilePath + ".ts", this.mockWoTContext)) as { |
| 78 | + const { value } = (await this.executor.exec(testFile, this.mockWoTContext)) as { |
96 | 79 | value: number; |
97 | 80 | }; |
98 | 81 |
|
99 | 82 | expect(value).to.be.eq(42); |
100 | 83 | } |
101 | 84 |
|
102 | 85 | @test async "should handle .mjs files as ES modules"() { |
103 | | - const filePath = this.testFilePath + ".mjs"; |
| 86 | + const filePath = tmp.fileSync({ postfix: ".mjs" }).name; |
104 | 87 | const scriptContent = "export const value = 'es module';"; |
105 | 88 | writeFileSync(filePath, scriptContent); |
106 | 89 |
|
|
0 commit comments