-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmatlab.unit.test.ts
More file actions
161 lines (126 loc) · 5.65 KB
/
Copy pathmatlab.unit.test.ts
File metadata and controls
161 lines (126 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2020-2023 The MathWorks, Inc.
import { promises as fs } from "fs";
import * as path from "path";
import * as matlab from "./matlab";
afterEach(() => {
jest.resetAllMocks();
});
describe("script generation", () => {
const mockPath = "/tmp/run-matlab-command-ABC123";
const workspaceDir = "/home/sweet/home";
const command = "disp('hello, world!');";
const rejection = "yeah, no";
it("properly generates a script", async () => {
const mkdtemp = jest.spyOn(fs, "mkdtemp");
const writeFile = jest.spyOn(fs, "writeFile");
mkdtemp.mockResolvedValue(mockPath);
writeFile.mockResolvedValue(undefined);
const actual = matlab.generateScript(workspaceDir, command);
await expect(actual).resolves.toBeDefined();
expect(mkdtemp).toHaveBeenCalledTimes(1);
expect(writeFile).toHaveBeenCalledTimes(1);
});
it("fails when the temporary directory cannot be made", async () => {
const mkdtemp = jest.spyOn(fs, "mkdtemp");
const writeFile = jest.spyOn(fs, "writeFile");
mkdtemp.mockRejectedValue(rejection);
writeFile.mockRejectedValue("this should not have been called");
const actual = matlab.generateScript(workspaceDir, command);
await expect(actual).rejects.toBeDefined();
expect(mkdtemp).toHaveBeenCalledTimes(1);
expect(writeFile).not.toHaveBeenCalled();
});
it("fails when there's an error writing to the file", async () => {
const mkdtemp = jest.spyOn(fs, "mkdtemp");
const writeFile = jest.spyOn(fs, "writeFile");
mkdtemp.mockResolvedValue(mockPath);
writeFile.mockRejectedValue(rejection);
const actual = matlab.generateScript(workspaceDir, command);
await expect(actual).rejects.toBeDefined();
expect(mkdtemp).toHaveBeenCalledTimes(1);
expect(writeFile).toHaveBeenCalledTimes(1);
});
});
describe("run command", () => {
const helperScript = { dir: "/home/sweet/home", command: "disp('hello, world');" };
const platform = "win32";
const architecture = "x64"
it("ideally works", async () => {
const chmod = jest.spyOn(fs, "chmod");
const execFn = jest.fn();
chmod.mockResolvedValue(undefined);
execFn.mockResolvedValue(0);
const actual = matlab.runCommand(helperScript, platform, architecture, execFn);
await expect(actual).resolves.toBeUndefined();
});
it("ideally works with arguments", async () => {
const chmod = jest.spyOn(fs, "chmod");
const execFn = jest.fn();
chmod.mockResolvedValue(undefined);
execFn.mockResolvedValue(0);
const actual = matlab.runCommand(helperScript, platform, architecture, execFn, ["-nojvm", "-logfile", "file"]);
await expect(actual).resolves.toBeUndefined();
expect(execFn.mock.calls[0][1][1]).toBe("-nojvm");
expect(execFn.mock.calls[0][1][2]).toBe("-logfile");
expect(execFn.mock.calls[0][1][3]).toBe("file");
});
it("fails when chmod fails", async () => {
const chmod = jest.spyOn(fs, "chmod");
const execFn = jest.fn();
chmod.mockRejectedValue(null);
const actual = matlab.runCommand(helperScript, platform, architecture, execFn);
await expect(actual).rejects.toBeDefined();
expect(chmod).toHaveBeenCalledTimes(1);
expect(execFn).not.toHaveBeenCalled();
});
it("fails when the execFn fails", async () => {
const chmod = jest.spyOn(fs, "chmod");
const execFn = jest.fn();
chmod.mockResolvedValue(undefined);
execFn.mockRejectedValue(null);
const actual = matlab.runCommand(helperScript, platform, architecture, execFn);
await expect(actual).rejects.toBeDefined();
expect(chmod).toHaveBeenCalledTimes(1);
expect(execFn).toHaveBeenCalledTimes(1);
});
it("fails when the execFn has a non-zero exit code", async () => {
const chmod = jest.spyOn(fs, "chmod");
const execFn = jest.fn();
chmod.mockResolvedValue(undefined);
execFn.mockResolvedValue(1);
const actual = matlab.runCommand(helperScript, platform, architecture, execFn);
await expect(actual).rejects.toBeDefined();
expect(chmod).toHaveBeenCalledTimes(1);
expect(execFn).toHaveBeenCalledTimes(1);
});
});
describe("ci helper path", () => {
const platform = "linux"
const architecture = "x64"
const testExtension = (platform: string, ext: string) => {
it(`considers the appropriate script on ${platform}`, () => {
const actualPath = matlab.getRunMATLABCommandScriptPath(platform, architecture);
const actualExt = path.extname(actualPath);
expect(actualExt).toMatch(ext);
});
};
const testDirectory = (platform: string, architecture: string, subdirectory: string) => {
it(`considers the appropriate script on ${platform}`, () => {
const actualPath = matlab.getRunMATLABCommandScriptPath(platform, architecture);
expect(actualPath).toContain(subdirectory);
});
};
testExtension("win32", "exe");
testExtension("darwin", "");
testExtension("linux", "");
testDirectory("win32", "x64", "win64");
testDirectory("darwin", "x64", "maci64");
testDirectory("darwin", "arm64", "maca64");
testDirectory("linux", "x64", "glnxa64");
it("errors on unsupported platform", () => {
expect(() => matlab.getRunMATLABCommandScriptPath('sunos',architecture)).toThrow();
})
it("errors on unsupported architecture", () => {
expect(() => matlab.getRunMATLABCommandScriptPath(platform, 'x86')).toThrow();
})
});