-
-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy patharduino-daemon-impl.test.ts
135 lines (122 loc) · 4.1 KB
/
arduino-daemon-impl.test.ts
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
import * as fs from 'fs';
// import * as net from 'net';
import * as path from 'path';
import * as temp from 'temp';
import { expect } from 'chai';
import { ChildProcess } from 'child_process';
import { safeLoad, safeDump } from 'js-yaml';
import { ArduinoDaemonImpl } from '../../node/arduino-daemon-impl';
import { spawnCommand } from '../../node/exec-util';
import { CLI_CONFIG } from '../../node/cli-config';
const track = temp.track();
class SilentArduinoDaemonImpl extends ArduinoDaemonImpl {
constructor(private logFormat: 'text' | 'json') {
super();
}
onData(data: string): void {
// NOOP
}
async spawnDaemonProcess(): Promise<{ daemon: ChildProcess; port: string }> {
return super.spawnDaemonProcess();
}
protected async getSpawnArgs(): Promise<string[]> {
const cliConfigPath = await this.initCliConfig();
return [
'daemon',
'--format',
'jsonmini',
'--port',
'0',
'--config-file',
cliConfigPath,
'-v',
'--log-format',
this.logFormat,
];
}
private async initCliConfig(): Promise<string> {
const cliPath = await this.getExecPath();
const destDir = track.mkdirSync();
await spawnCommand(`"${cliPath}"`, [
'config',
'init',
'--dest-dir',
destDir,
]);
const content = fs.readFileSync(path.join(destDir, CLI_CONFIG), {
encoding: 'utf8',
});
const cliConfig = safeLoad(content) as any;
// cliConfig.daemon.port = String(this.port);
const modifiedContent = safeDump(cliConfig);
fs.writeFileSync(path.join(destDir, CLI_CONFIG), modifiedContent, {
encoding: 'utf8',
});
return path.join(destDir, CLI_CONFIG);
}
}
describe('arduino-daemon-impl', () => {
after(() => {
track.cleanupSync();
});
// it('should parse an error - address already in use error [json]', async function (): Promise<void> {
// if (process.platform === 'win32') {
// this.skip();
// }
// let server: net.Server | undefined = undefined;
// try {
// server = await new Promise<net.Server>(resolve => {
// const server = net.createServer();
// server.listen(() => resolve(server));
// });
// const address = server.address() as net.AddressInfo;
// await new SilentArduinoDaemonImpl(address.port, 'json').spawnDaemonProcess();
// fail('Expected a failure.')
// } catch (e) {
// expect(e).to.be.instanceOf(DaemonError);
// expect(e.code).to.be.equal(DaemonError.ADDRESS_IN_USE);
// } finally {
// if (server) {
// server.close();
// }
// }
// });
// it('should parse an error - address already in use error [text]', async function (): Promise<void> {
// if (process.platform === 'win32') {
// this.skip();
// }
// let server: net.Server | undefined = undefined;
// try {
// server = await new Promise<net.Server>(resolve => {
// const server = net.createServer();
// server.listen(() => resolve(server));
// });
// const address = server.address() as net.AddressInfo;
// await new SilentArduinoDaemonImpl(address.port, 'text').spawnDaemonProcess();
// fail('Expected a failure.')
// } catch (e) {
// expect(e).to.be.instanceOf(DaemonError);
// expect(e.code).to.be.equal(DaemonError.ADDRESS_IN_USE);
// } finally {
// if (server) {
// server.close();
// }
// }
// });
it('should parse the port address when the log format is json', async () => {
const { daemon, port } = await new SilentArduinoDaemonImpl(
'json'
).spawnDaemonProcess();
expect(port).not.to.be.undefined;
expect(port).not.to.be.equal('0');
daemon.kill();
});
it('should parse the port address when the log format is text', async () => {
const { daemon, port } = await new SilentArduinoDaemonImpl(
'text'
).spawnDaemonProcess();
expect(port).not.to.be.undefined;
expect(port).not.to.be.equal('0');
daemon.kill();
});
});