Skip to content

Commit

Permalink
Continue when isValid path fails. Koenkk/zigbee2mqtt#2122
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 24, 2019
1 parent b636821 commit f18d11a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@ abstract class Adapter extends events.EventEmitter {
throw new Error("No path provided and failed to auto detect path");
}
} else {
// Path can be a symlink, resolve it.
serialPortOptions.path = RealpathSync(serialPortOptions.path);

// Determine adapter to use
for (const candidate of adapters) {
if (await candidate.isValidPath(serialPortOptions.path)) {
debug(`Path '${serialPortOptions.path}' is valid for '${candidate.name}'`);
adapter = candidate;
break;
try {
// Path can be a symlink, resolve it.
serialPortOptions.path = RealpathSync(serialPortOptions.path);

// Determine adapter to use
for (const candidate of adapters) {
if (await candidate.isValidPath(serialPortOptions.path)) {
debug(`Path '${serialPortOptions.path}' is valid for '${candidate.name}'`);
adapter = candidate;
break;
}
}
} catch (error) {
debug(`Failed to validate path: '${error}'`);
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,13 @@ describe('Controller', () => {
expect(ZStackAdapter).toHaveBeenCalledWith(null, {"baudRate": 100, "path": "/dev/bla", "rtscts": false}, null);
});

it('Adapter create continue when is valid path fails', async () => {
mockZStackAdapterIsValidPath.mockImplementationOnce(() => {throw new Error('failed')});
await Adapter.create(null, {path: '/dev/bla', baudRate: 100, rtscts: false}, null);
expect(mockZStackAdapterIsValidPath).toHaveBeenCalledWith('/dev/bla');
expect(ZStackAdapter).toHaveBeenCalledWith(null, {"baudRate": 100, "path": "/dev/bla", "rtscts": false}, null);
});

it('Adapter create auto detect', async () => {
mockZStackAdapterIsValidPath.mockReturnValueOnce(true);
mockZStackAdapterAutoDetectPath.mockReturnValueOnce('/dev/test');
Expand Down

0 comments on commit f18d11a

Please sign in to comment.