mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-11-25 18:07:15 +00:00
Use eslint&prettier for code linting&formatting
This commit is contained in:
committed by
Francesco Stasi
parent
2a3873a923
commit
0592199858
@@ -3,7 +3,7 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as temp from 'temp';
|
||||
import { fail } from 'assert';
|
||||
import { expect } from 'chai'
|
||||
import { expect } from 'chai';
|
||||
import { ChildProcess } from 'child_process';
|
||||
import { safeLoad, safeDump } from 'js-yaml';
|
||||
import { DaemonError, ArduinoDaemonImpl } from '../../node/arduino-daemon-impl';
|
||||
@@ -13,8 +13,10 @@ 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 port: string | number,
|
||||
private logFormat: 'text' | 'json'
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -28,28 +30,42 @@ class SilentArduinoDaemonImpl extends ArduinoDaemonImpl {
|
||||
|
||||
protected async getSpawnArgs(): Promise<string[]> {
|
||||
const cliConfigPath = await this.initCliConfig();
|
||||
return ['daemon', '--config-file', cliConfigPath, '-v', '--log-format', this.logFormat];
|
||||
return [
|
||||
'daemon',
|
||||
'--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' });
|
||||
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' });
|
||||
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') {
|
||||
@@ -99,8 +115,11 @@ describe('arduino-daemon-impl', () => {
|
||||
|
||||
it('should parse an error - unknown address [json]', async () => {
|
||||
try {
|
||||
await new SilentArduinoDaemonImpl('foo', 'json').spawnDaemonProcess();
|
||||
fail('Expected a failure.')
|
||||
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);
|
||||
@@ -109,8 +128,11 @@ describe('arduino-daemon-impl', () => {
|
||||
|
||||
it('should parse an error - unknown address [text]', async () => {
|
||||
try {
|
||||
await new SilentArduinoDaemonImpl('foo', 'text').spawnDaemonProcess();
|
||||
fail('Expected a failure.')
|
||||
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);
|
||||
@@ -120,7 +142,7 @@ describe('arduino-daemon-impl', () => {
|
||||
it('should parse an error - invalid port [json]', async () => {
|
||||
try {
|
||||
await new SilentArduinoDaemonImpl(-1, 'json').spawnDaemonProcess();
|
||||
fail('Expected a failure.')
|
||||
fail('Expected a failure.');
|
||||
} catch (e) {
|
||||
expect(e).to.be.instanceOf(DaemonError);
|
||||
expect(e.code).to.be.equal(DaemonError.INVALID_PORT);
|
||||
@@ -130,11 +152,10 @@ describe('arduino-daemon-impl', () => {
|
||||
it('should parse an error - invalid port [text]', async () => {
|
||||
try {
|
||||
await new SilentArduinoDaemonImpl(-1, 'text').spawnDaemonProcess();
|
||||
fail('Expected a failure.')
|
||||
fail('Expected a failure.');
|
||||
} catch (e) {
|
||||
expect(e).to.be.instanceOf(DaemonError);
|
||||
expect(e.code).to.be.equal(DaemonError.INVALID_PORT);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -2,43 +2,56 @@ import { expect } from 'chai';
|
||||
import { DefaultCliConfig } from '../../node/cli-config';
|
||||
|
||||
describe('cli-config', () => {
|
||||
type ConfigProvider = DefaultCliConfig | { (): DefaultCliConfig };
|
||||
|
||||
type ConfigProvider = DefaultCliConfig | { (): DefaultCliConfig }
|
||||
|
||||
([
|
||||
[defaultConfig, defaultConfig, true],
|
||||
[() => {
|
||||
const conf = defaultConfig();
|
||||
delete conf.board_manager
|
||||
return conf
|
||||
}, defaultConfig, true],
|
||||
[() => {
|
||||
const conf = defaultConfig();
|
||||
(conf.daemon as any).port = String(conf.daemon.port);
|
||||
return conf
|
||||
}, defaultConfig, true],
|
||||
] as [ConfigProvider, ConfigProvider, boolean][]).forEach(([leftInput, rightInput, expectation]) => {
|
||||
(
|
||||
[
|
||||
[defaultConfig, defaultConfig, true],
|
||||
[
|
||||
() => {
|
||||
const conf = defaultConfig();
|
||||
delete conf.board_manager;
|
||||
return conf;
|
||||
},
|
||||
defaultConfig,
|
||||
true,
|
||||
],
|
||||
[
|
||||
() => {
|
||||
const conf = defaultConfig();
|
||||
(conf.daemon as any).port = String(conf.daemon.port);
|
||||
return conf;
|
||||
},
|
||||
defaultConfig,
|
||||
true,
|
||||
],
|
||||
] as [ConfigProvider, ConfigProvider, boolean][]
|
||||
).forEach(([leftInput, rightInput, expectation]) => {
|
||||
const left = typeof leftInput === 'function' ? leftInput() : leftInput;
|
||||
const right = typeof rightInput === 'function' ? rightInput() : rightInput;
|
||||
it(`${JSON.stringify(left)} should ${expectation ? '' : 'not '}be the same as ${JSON.stringify(right)}`, () => {
|
||||
expect(DefaultCliConfig.sameAs(left, right)).to.be.equal(expectation);
|
||||
const right =
|
||||
typeof rightInput === 'function' ? rightInput() : rightInput;
|
||||
it(`${JSON.stringify(left)} should ${
|
||||
expectation ? '' : 'not '
|
||||
}be the same as ${JSON.stringify(right)}`, () => {
|
||||
expect(DefaultCliConfig.sameAs(left, right)).to.be.equal(
|
||||
expectation
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function defaultConfig(): DefaultCliConfig {
|
||||
return {
|
||||
board_manager: {
|
||||
additional_urls: []
|
||||
additional_urls: [],
|
||||
},
|
||||
daemon: {
|
||||
port: 5000
|
||||
port: 5000,
|
||||
},
|
||||
directories: {
|
||||
data: 'data',
|
||||
downloads: 'downloads',
|
||||
user: 'user'
|
||||
}
|
||||
}
|
||||
user: 'user',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
import * as os from 'os';
|
||||
import { expect, use } from 'chai';
|
||||
import { getExecPath } from '../../node/exec-util'
|
||||
import { getExecPath } from '../../node/exec-util';
|
||||
|
||||
use(require('chai-string'));
|
||||
|
||||
describe('getExecPath', () => {
|
||||
|
||||
it('should resolve arduino-cli', async () => {
|
||||
const actual = await getExecPath('arduino-cli', onError, 'version');
|
||||
const expected = os.platform() === 'win32' ? '\\arduino-cli.exe' : '/arduino-cli';
|
||||
const expected =
|
||||
os.platform() === 'win32' ? '\\arduino-cli.exe' : '/arduino-cli';
|
||||
expect(actual).to.endsWith(expected);
|
||||
});
|
||||
|
||||
it('should resolve arduino-language-server', async () => {
|
||||
const actual = await getExecPath('arduino-language-server');
|
||||
const expected = os.platform() === 'win32' ? '\\arduino-language-server.exe' : '/arduino-language-server';
|
||||
const expected =
|
||||
os.platform() === 'win32'
|
||||
? '\\arduino-language-server.exe'
|
||||
: '/arduino-language-server';
|
||||
expect(actual).to.endsWith(expected);
|
||||
});
|
||||
|
||||
@@ -27,5 +30,4 @@ describe('getExecPath', () => {
|
||||
function onError(error: Error): void {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user