IDE to run CLI with auto assigned port (#673)

* get daemon port from CLI stdout

* config-service to use CLI daemon port

* updating LS

* fixed tests

* fix upload blocked when selectedBoard.port is undefined

* bump arduino-cli to 0.20.2

Co-authored-by: Alberto Iannaccone <a.iannaccone@arduino.cc>
This commit is contained in:
Francesco Stasi
2021-12-09 15:08:26 +01:00
committed by GitHub
parent 767b09d2f1
commit 49d12d99ff
10 changed files with 88 additions and 75 deletions

View File

@@ -2,21 +2,17 @@ import * as fs from 'fs';
// import * as net from 'net';
import * as path from 'path';
import * as temp from 'temp';
import { fail } from 'assert';
import { expect } from 'chai';
import { ChildProcess } from 'child_process';
import { safeLoad, safeDump } from 'js-yaml';
import { DaemonError, ArduinoDaemonImpl } from '../../node/arduino-daemon-impl';
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 port: string | number,
private logFormat: 'text' | 'json'
) {
constructor(private logFormat: 'text' | 'json') {
super();
}
@@ -24,7 +20,7 @@ class SilentArduinoDaemonImpl extends ArduinoDaemonImpl {
// NOOP
}
async spawnDaemonProcess(): Promise<ChildProcess> {
async spawnDaemonProcess(): Promise<{ daemon: ChildProcess; port: string }> {
return super.spawnDaemonProcess();
}
@@ -32,6 +28,10 @@ class SilentArduinoDaemonImpl extends ArduinoDaemonImpl {
const cliConfigPath = await this.initCliConfig();
return [
'daemon',
'--format',
'jsonmini',
'--port',
'0',
'--config-file',
cliConfigPath,
'-v',
@@ -53,7 +53,7 @@ class SilentArduinoDaemonImpl extends ArduinoDaemonImpl {
encoding: 'utf8',
});
const cliConfig = safeLoad(content) as any;
cliConfig.daemon.port = String(this.port);
// cliConfig.daemon.port = String(this.port);
const modifiedContent = safeDump(cliConfig);
fs.writeFileSync(path.join(destDir, CLI_CONFIG), modifiedContent, {
encoding: 'utf8',
@@ -113,43 +113,23 @@ describe('arduino-daemon-impl', () => {
// }
// });
it('should parse an error - unknown address [json]', async () => {
try {
await new SilentArduinoDaemonImpl('foo', 'json').spawnDaemonProcess();
fail('Expected a failure.');
} catch (e) {
expect(e).to.be.instanceOf(DaemonError);
expect(e.code).to.be.equal(DaemonError.UNKNOWN_ADDRESS);
}
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 an error - unknown address [text]', async () => {
try {
await new SilentArduinoDaemonImpl('foo', 'text').spawnDaemonProcess();
fail('Expected a failure.');
} catch (e) {
expect(e).to.be.instanceOf(DaemonError);
expect(e.code).to.be.equal(DaemonError.UNKNOWN_ADDRESS);
}
});
it('should parse the port address when the log format is text', async () => {
const { daemon, port } = await new SilentArduinoDaemonImpl(
'text'
).spawnDaemonProcess();
it('should parse an error - invalid port [json]', async () => {
try {
await new SilentArduinoDaemonImpl(-1, 'json').spawnDaemonProcess();
fail('Expected a failure.');
} catch (e) {
expect(e).to.be.instanceOf(DaemonError);
expect(e.code).to.be.equal(DaemonError.INVALID_PORT);
}
});
it('should parse an error - invalid port [text]', async () => {
try {
await new SilentArduinoDaemonImpl(-1, 'text').spawnDaemonProcess();
fail('Expected a failure.');
} catch (e) {
expect(e).to.be.instanceOf(DaemonError);
expect(e.code).to.be.equal(DaemonError.INVALID_PORT);
}
expect(port).not.to.be.undefined;
expect(port).not.to.be.equal('0');
daemon.kill();
});
});