Skip to content

Commit

Permalink
Throw error when path doesn't exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 16, 2019
1 parent 48bc84d commit bcb13ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/adapter/serialPortUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ async function is(path: string, matchers: PortInfoMatch[]): Promise<boolean> {
const devices = await SerialPort.list();
// @ts-ignore; not sure why this is needed as path exists (definition is wrong?)
const device = devices.find((device) => device.path === path);
if (!device) {
throw new Error(`Path '${path}' does not exist`);
}

return matchers.find((matcher) => EqualsPartial(device, matcher)) != null;
}

Expand Down
18 changes: 18 additions & 0 deletions test/adapter/z-stack/znp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ describe('ZNP', () => {
expect(await Znp.isValidPath('/dev/autodetected2')).toBeFalsy();
});

it('Check if path is valid; throw error when path does not exist', async () => {
mockSerialPortList.mockReturnValue([
{manufacturer: 'Not texas instruments', vendorId: '0451', productId: '16a8', path: '/dev/autodetected2'},
{path: '/dev/tty.usbmodemL43001T22', manufacturer: 'Texas Instruments', vendorId: '0451', productId: 'bef3'},
{path: '/dev/tty.usbmodemL43001T24', manufacturer: 'Texas Instruments', vendorId: '0451', productId: 'bef3'},
{path: '/dev/tty.usbmodemL43001T21', manufacturer: 'Texas Instruments', vendorId: '0451', productId: 'bef3'},
])

let error;
try {
await Znp.isValidPath('/dev/notexisting')
} catch (e) {
error = e;
}

expect(error).toStrictEqual(new Error(`Path '/dev/notexisting' does not exist`));
});

it('Open with error', async () => {
mockSerialPortOpen.mockImplementationOnce((cb) => cb('failed!'));
mockSerialPortIsOpen = true;
Expand Down

0 comments on commit bcb13ad

Please sign in to comment.