Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit c6d9f7a

Browse files
Merge pull request #14 from g-script/fix/undefined-target
Fix undefined target
2 parents a0cbc3b + bd88b39 commit c6d9f7a

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ function isFD (fd) {
1313
try {
1414
fs.fstatSync(fd)
1515

16-
return true
16+
return fd
1717
} catch (err) {
1818
if (err.code === 'EBADF') {
1919
return false
2020
}
2121

2222
throw err
2323
}
24+
} else if (fd && fd.fd) {
25+
return isFD(fd.fd)
2426
}
2527

2628
return false
@@ -114,16 +116,11 @@ class Tail extends Readable {
114116
this.debug('Guessing target type')
115117

116118
try {
117-
if (!target) {
118-
this.type = 'stream'
119-
this.target = process.stdin
120-
this.options.follow = true
121-
} else if (isFD(target)) {
122-
this.type = 'fd'
123-
this.target = target
124-
} else if (isFD(target.fd)) {
119+
const fd = isFD(target)
120+
121+
if (fd) {
125122
this.type = 'fd'
126-
this.target = target.fd
123+
this.target = fd
127124
} else if (target instanceof fs.ReadStream) {
128125
this.type = 'stream'
129126
this.target = target

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "better-tail",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Node.js implementation of UNIX tail command using streams. No dependencies.",
55
"main": "index.js",
66
"files": [

test/index.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('better-tail', function () {
136136
}
137137
})
138138

139-
describe('should tail file last 10 lines (default)', function () {
139+
describe('no options', function () {
140140
function test (target, done) {
141141
const data = []
142142

@@ -150,21 +150,21 @@ describe('better-tail', function () {
150150
})
151151
}
152152

153-
it('with target file path', function (done) {
153+
it('should work with file path', function (done) {
154154
test(corpus.path, done)
155155
})
156156

157-
it('with file descriptor', function (done) {
157+
it('should work with file descriptor', function (done) {
158158
test(corpus.fd, done)
159159
})
160160

161-
it('with stream', function (done) {
161+
it('should work with file read stream', function (done) {
162162
test(corpus.rs, done)
163163
})
164164
})
165165

166-
describe('should tail file last N bytes', function () {
167-
function test (target, done) {
166+
describe('bytes option (last bytes)', function () {
167+
function test (target, done, options = {}) {
168168
// Get content length in bytes
169169
const contentByteLength = Buffer.byteLength(corpus.expectations.content, 'utf8')
170170

@@ -177,9 +177,9 @@ describe('better-tail', function () {
177177

178178
const data = []
179179

180-
new Tail(target, {
180+
new Tail(target, Object.assign(options, {
181181
bytes
182-
}).on('data', (line) => {
182+
})).on('data', (line) => {
183183
data.push(line)
184184
}).on('end', () => {
185185
expect(data[0]).to.be.instanceof(Buffer, 'Expected data to be an instance of Buffer')
@@ -188,20 +188,24 @@ describe('better-tail', function () {
188188
})
189189
}
190190

191-
it('with target file path', function (done) {
191+
it('should work with file path', function (done) {
192192
test(corpus.path, done)
193193
})
194194

195-
it('with file descriptor', function (done) {
195+
it('should work with file descriptor', function (done) {
196196
test(corpus.fd, done)
197197
})
198198

199-
it('with stream', function (done) {
199+
it('should work with file read stream', function (done) {
200200
test(corpus.rs, done)
201201
})
202+
203+
it('should superseed lines option', function (done) {
204+
test(corpus.path, done, { lines: 42 })
205+
})
202206
})
203207

204-
describe('should tail file from Nth byte', function () {
208+
describe('bytes option (from byte)', function () {
205209
function test (target, done) {
206210
// Get content length in bytes
207211
const contentByteLength = Buffer.byteLength(corpus.expectations.content, 'utf8')
@@ -226,20 +230,20 @@ describe('better-tail', function () {
226230
})
227231
}
228232

229-
it('with target file path', function (done) {
233+
it('should work with file path', function (done) {
230234
test(corpus.path, done)
231235
})
232236

233-
it('with file descriptor', function (done) {
237+
it('should work with file descriptor', function (done) {
234238
test(corpus.fd, done)
235239
})
236240

237-
it('with stream', function (done) {
241+
it('should work with file read stream', function (done) {
238242
test(corpus.rs, done)
239243
})
240244
})
241245

242-
describe('should tail file last N lines', function () {
246+
describe('lines option (last lines)', function () {
243247
function test (target, done) {
244248
// Get content length in bytes
245249
const contentByteLength = Buffer.byteLength(corpus.expectations.content, 'utf8')
@@ -271,20 +275,20 @@ describe('better-tail', function () {
271275
})
272276
}
273277

274-
it('with target file path', function (done) {
278+
it('should work with file path', function (done) {
275279
test(corpus.path, done)
276280
})
277281

278-
it('with file descriptor', function (done) {
282+
it('should work with file descriptor', function (done) {
279283
test(corpus.fd, done)
280284
})
281285

282-
it('with stream', function (done) {
286+
it('should work with file read stream', function (done) {
283287
test(corpus.rs, done)
284288
})
285289
})
286290

287-
describe('should tail file from Nth line', function () {
291+
describe('lines option (from line)', function () {
288292
function test (target, done) {
289293
// Get content length in bytes
290294
const contentByteLength = Buffer.byteLength(corpus.expectations.content, 'utf8')
@@ -315,20 +319,20 @@ describe('better-tail', function () {
315319
})
316320
}
317321

318-
it('with target file path', function (done) {
322+
it('should work with file path', function (done) {
319323
test(corpus.path, done)
320324
})
321325

322-
it('with file descriptor', function (done) {
326+
it('should work with file descriptor', function (done) {
323327
test(corpus.fd, done)
324328
})
325329

326-
it('with stream', function (done) {
330+
it('should work with file read stream', function (done) {
327331
test(corpus.rs, done)
328332
})
329333
})
330334

331-
describe('follow mode', function () {
335+
describe('follow option', function () {
332336
it('should follow data quickly appended to file (backpressure)', function (done) {
333337
const rndStrSize = randomInt()
334338

@@ -372,16 +376,26 @@ describe('better-tail', function () {
372376
})
373377

374378
describe('errors', function () {
379+
it('should fail to tail undefined target', function (done) {
380+
new Tail().on('error', (err) => {
381+
expect(err).to.be.instanceof(Error)
382+
.and.have.property('message', 'Invalid target provided')
383+
done()
384+
})
385+
})
386+
375387
it('should fail to tail invalid file', function (done) {
376388
new Tail(path.resolve(__dirname, `${randomString(10)}.txt`)).on('error', (err) => {
377389
expect(err).to.be.instanceof(Error)
390+
.and.have.property('code', 'ENOENT')
378391
done()
379392
})
380393
})
381394

382395
it('should fail to tail invalid file descriptor', function (done) {
383396
new Tail(654).on('error', (err) => {
384397
expect(err).to.be.instanceof(Error)
398+
.and.have.property('message', 'Invalid target provided')
385399
done()
386400
})
387401
})

0 commit comments

Comments
 (0)