Skip to content

Commit dea1381

Browse files
committed
add test, fix lint
1 parent 83f0a39 commit dea1381

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

lib/linux-list.test.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { assert, shouldReject } from '../test/assert'
22
import { mockLinuxList as linuxList, mockLinuxListError } from './mocks/linux-list'
3+
import fs from 'fs'
4+
import sinon from 'sinon'
35

46
const mockUdevOutput = String.raw`
57
P: /devices/platform/serial8250/tty/ttyS0
@@ -27,8 +29,8 @@ E: DEVNAME=/dev/ttyACM0
2729
E: DEVPATH=/devices/pci0000:00/0000:00:06.0/usb1/1-2/1-2:1.0/tty/ttyACM0
2830
E: ID_BUS=usb
2931
E: ID_MM_CANDIDATE=1
30-
E: ID_MODEL=0043
31-
E: ID_MODEL_ENC=0043
32+
E: ID_MODEL=Arduino_Uno
33+
E: ID_MODEL_ENC=Arduino\x20Uno
3234
E: ID_MODEL_FROM_DATABASE=Uno R3 (CDC ACM)
3335
E: ID_MODEL_ID=0043
3436
E: ID_PATH=pci-0000:00:06.0-usb-0:2:1.0
@@ -75,14 +77,9 @@ N: ttyNOTASERIALPORT
7577
`
7678

7779
const portOutput = [
78-
{
79-
path: '/dev/ttyS0',
80-
},
81-
{
82-
path: '/dev/ttyS1',
83-
},
8480
{
8581
path: '/dev/ttyACM0',
82+
friendlyName: 'Arduino Uno',
8683
manufacturer: 'Arduino (www.arduino.cc)',
8784
serialNumber: '752303138333518011C1',
8885
productId: '0043',
@@ -93,17 +90,25 @@ const portOutput = [
9390
path: '/dev/ttyAMA_im_a_programmer',
9491
pnpId: 'pci-NATA_Siolynx2_C8T6VI1F-if00-port0',
9592
},
96-
{
97-
path: '/dev/ttyMFD0',
98-
vendorId: '2343',
99-
productId: '0043',
100-
},
101-
{
102-
path: '/dev/rfcomm4',
103-
},
10493
]
10594

10695
describe('listLinux', () => {
96+
let readdirSyncStub: sinon.SinonStub
97+
let realpathSyncStub: sinon.SinonStub
98+
99+
beforeEach(() => {
100+
readdirSyncStub = sinon.stub(fs, 'readdirSync')
101+
realpathSyncStub = sinon.stub(fs, 'realpathSync')
102+
103+
readdirSyncStub.withArgs('/dev/serial/by-path').returns(['pci-0000:00:06.0-usb-0:2:1.0', 'pci-NATA_Siolynx2_C8T6VI1F-if00-port0'])
104+
realpathSyncStub.withArgs('/dev/serial/by-path/pci-0000:00:06.0-usb-0:2:1.0').returns('/dev/ttyACM0')
105+
realpathSyncStub.withArgs('/dev/serial/by-path/pci-NATA_Siolynx2_C8T6VI1F-if00-port0').returns('/dev/ttyAMA_im_a_programmer')
106+
})
107+
108+
afterEach(() => {
109+
sinon.restore()
110+
})
111+
107112
it('lists available serialports', async () => {
108113
const ports = await linuxList(mockUdevOutput)
109114
assert.containSubset(ports, portOutput)

lib/linux-list.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ function propVal(name: string, val: string) {
5151

5252
function getActiveDevices(): Set<string> {
5353
try {
54+
/* eslint-disable comma-dangle */
5455
const validPaths = fs.readdirSync('/dev/serial/by-path').map(file =>
5556
fs.realpathSync(`/dev/serial/by-path/${file}`)
5657
)
58+
/* eslint-enable comma-dangle */
5759
return new Set(validPaths)
5860
} catch {
5961
return new Set()

0 commit comments

Comments
 (0)