Skip to content

Commit 9d01492

Browse files
authored
fix: bindings now error when closed during empty writes (#1872)
Also fix tests not running!
1 parent 524a272 commit 9d01492

File tree

5 files changed

+60
-68
lines changed

5 files changed

+60
-68
lines changed

packages/binding-abstract/binding-abstract.js

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ The in progress writes must error when the port is closed with an error object t
119119

120120
debug('write', buffer.length, 'bytes')
121121
if (!this.isOpen) {
122+
debug('write', 'error port is not open')
123+
122124
throw new Error('Port is not open')
123125
}
124126
}

packages/bindings/lib/bindings-test.js renamed to packages/bindings/lib/bindings.test.js

+40-56
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ function disconnect(err) {
3838
throw err || new Error('Unknown disconnection')
3939
}
4040

41+
function shouldReject(promise, errType = Error, message = 'Should have rejected') {
42+
return promise.then(
43+
() => {
44+
throw new Error(message)
45+
},
46+
err => {
47+
assert.instanceOf(err, errType)
48+
}
49+
)
50+
}
51+
4152
// All bindings are required to work with an "echo" firmware
4253
// The echo firmware should respond with this data when it's
4354
// ready to echo. This allows for remote device bootup.
@@ -156,22 +167,12 @@ function testBinding(bindingName, Binding, testPort) {
156167
})
157168
})
158169

159-
it('throws when not given a path', done => {
160-
try {
161-
binding.open('')
162-
} catch (e) {
163-
assert.instanceOf(e, TypeError)
164-
done()
165-
}
170+
it('throws when not given a path', async () => {
171+
await shouldReject(binding.open(''), TypeError)
166172
})
167173

168-
it('throws when not given options', done => {
169-
try {
170-
binding.open('COMBAD')
171-
} catch (e) {
172-
assert.instanceOf(e, TypeError)
173-
done()
174-
}
174+
it('throws when not given options', async () => {
175+
await shouldReject(binding.open('COMBAD'), TypeError)
175176
})
176177

177178
if (!testPort) {
@@ -278,15 +279,9 @@ function testBinding(bindingName, Binding, testPort) {
278279
})
279280

280281
describe('#update', () => {
281-
it('throws when not given an object', done => {
282+
it('throws when not given an object', async () => {
282283
const binding = new Binding({ disconnect })
283-
284-
try {
285-
binding.update()
286-
} catch (e) {
287-
assert.instanceOf(e, TypeError)
288-
done()
289-
}
284+
await shouldReject(binding.update(), TypeError)
290285
})
291286

292287
it('errors asynchronously when not open', done => {
@@ -315,53 +310,47 @@ function testBinding(bindingName, Binding, testPort) {
315310

316311
afterEach(() => binding.close())
317312

318-
it('throws errors when updating nothing', done => {
319-
try {
320-
binding.update({})
321-
} catch (err) {
322-
assert.instanceOf(err, Error)
323-
done()
324-
}
313+
it('throws errors when updating nothing', async () => {
314+
await shouldReject(binding.update({}), Error)
325315
})
326316

327-
it('errors when not called with options', done => {
328-
try {
329-
binding.set(() => {})
330-
} catch (e) {
331-
assert.instanceOf(e, Error)
332-
done()
333-
}
317+
it('errors when not called with options', async () => {
318+
await shouldReject(binding.set(() => {}), Error)
334319
})
335320

336321
it('updates baudRate', () => {
337322
return binding.update({ baudRate: 57600 })
338323
})
339324
})
340325

341-
describe('#write', () => {
326+
describe.only('#write', () => {
342327
it('errors asynchronously when not open', done => {
343328
const binding = new Binding({
344329
disconnect,
345330
})
346331
let noZalgo = false
347-
binding.write(Buffer.from([])).catch(err => {
348-
assert.instanceOf(err, Error)
349-
assert(noZalgo)
350-
done()
351-
})
332+
binding
333+
.write(Buffer.from([]))
334+
.then(
335+
data => {
336+
console.log({ data })
337+
throw new Error('Should have errored')
338+
},
339+
err => {
340+
assert.instanceOf(err, Error)
341+
assert(noZalgo)
342+
done()
343+
}
344+
)
345+
.catch(done)
352346
noZalgo = true
353347
})
354348

355-
it('throws when not given a buffer', done => {
349+
it('throws when not given a buffer', async () => {
356350
const binding = new Binding({
357351
disconnect,
358352
})
359-
try {
360-
binding.write(null)
361-
} catch (e) {
362-
assert.instanceOf(e, TypeError)
363-
done()
364-
}
353+
await shouldReject(binding.write(null), TypeError)
365354
})
366355

367356
if (!testPort) {
@@ -494,16 +483,11 @@ function testBinding(bindingName, Binding, testPort) {
494483
noZalgo = true
495484
})
496485

497-
it('throws when not called with options', done => {
486+
it('throws when not called with options', async () => {
498487
const binding = new Binding({
499488
disconnect,
500489
})
501-
try {
502-
binding.set(() => {})
503-
} catch (e) {
504-
assert.instanceOf(e, TypeError)
505-
done()
506-
}
490+
await shouldReject(binding.set(() => {}), TypeError)
507491
})
508492

509493
if (!testPort) {

packages/bindings/lib/darwin.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ class DarwinBinding extends AbstractBinding {
6565
}
6666

6767
async write(buffer) {
68-
if (buffer.length === 0) {
69-
return
70-
}
7168
this.writeOperation = super
7269
.write(buffer)
73-
.then(() => unixWrite.call(this, buffer))
70+
.then(() => {
71+
if (buffer.length === 0) {
72+
return
73+
}
74+
return unixWrite.call(this, buffer)
75+
})
7476
.then(() => {
7577
this.writeOperation = null
7678
})

packages/bindings/lib/linux.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ class LinuxBinding extends AbstractBinding {
6565
}
6666

6767
async write(buffer) {
68-
if (buffer.length === 0) {
69-
return
70-
}
7168
this.writeOperation = super
7269
.write(buffer)
73-
.then(() => unixWrite.call(this, buffer))
70+
.then(() => {
71+
if (buffer.length === 0) {
72+
return
73+
}
74+
return unixWrite.call(this, buffer)
75+
})
7476
.then(() => {
7577
this.writeOperation = null
7678
})

packages/bindings/lib/win32.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ class WindowsBinding extends AbstractBinding {
7474
}
7575

7676
async write(buffer) {
77-
if (buffer.length === 0) {
78-
return
79-
}
8077
this.writeOperation = super
8178
.write(buffer)
82-
.then(() => asyncWrite(this.fd, buffer))
79+
.then(() => {
80+
if (buffer.length === 0) {
81+
return
82+
}
83+
return asyncWrite(this.fd, buffer)
84+
})
8385
.then(() => {
8486
this.writeOperation = null
8587
})

0 commit comments

Comments
 (0)