2023-09-02 08:23:26 +00:00
|
|
|
import path from 'path'
|
2023-09-03 19:43:59 +00:00
|
|
|
import fs from 'fs'
|
2023-09-02 08:23:26 +00:00
|
|
|
import assert from '../lib/assert.mjs'
|
|
|
|
import t from '../lib/eltro.mjs'
|
|
|
|
import { Builder, Counter } from './watch/builder.mjs'
|
2023-09-03 19:43:59 +00:00
|
|
|
import Watcher from '../lib/watch/index.mjs'
|
2023-09-02 08:23:26 +00:00
|
|
|
|
|
|
|
const builder = new Builder()
|
|
|
|
let watcher
|
|
|
|
|
|
|
|
function htTimeToMs(end) {
|
|
|
|
return end[0] * 1000 + Math.round(end[1] / 1000000)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.before(function() {
|
|
|
|
return builder.init()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.afterEach(function(done) {
|
|
|
|
if (watcher && !watcher.isClosed()) {
|
|
|
|
watcher.on('close', done)
|
|
|
|
watcher.close()
|
|
|
|
} else {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.after(function() {
|
|
|
|
if (builder) {
|
|
|
|
builder.cleanup()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
t.describe('watcher', function() {
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('process events', function() {
|
|
|
|
t.test('should emit `close` event', function(done) {
|
|
|
|
var file = 'home/a/file1'
|
|
|
|
var fpath = builder.getPath(file)
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, function() {})
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('close', done)
|
|
|
|
watcher.close()
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should emit `ready` event when watching a file', function(done) {
|
|
|
|
var file = 'home/a/file1'
|
|
|
|
var fpath = builder.getPath(file)
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath)
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should emit `ready` event when watching a directory recursively', function(done) {
|
|
|
|
var dir = builder.getPath('home')
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, { recursive: true })
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', done)
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
2023-09-03 00:12:49 +00:00
|
|
|
|
|
|
|
t.test('should emit `ready` properly in a composed watcher', function(done) {
|
|
|
|
var dir1 = builder.getPath('home/a')
|
|
|
|
var dir2 = builder.getPath('home/b')
|
|
|
|
var file = builder.getPath('home/b/file1')
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher([dir1, dir2, file], { recursive: true })
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', done)
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('watch for files', function() {
|
|
|
|
t.test('should watch a single file and keep watching', function(done) {
|
|
|
|
var counter = new Counter(done, 3)
|
|
|
|
var file = 'home/a/file1'
|
|
|
|
var fpath = builder.getPath(file)
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, { delay: 0 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.strictEqual(fpath, name)
|
|
|
|
counter.count()
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
2023-09-03 00:12:49 +00:00
|
|
|
counter.waitForCount(builder.modify(file))
|
|
|
|
.then(() => counter.waitForCount(builder.modify(file)))
|
|
|
|
.then(() => counter.waitForCount(builder.modify(file)))
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should watch files inside a directory', function(done) {
|
|
|
|
var fpath = builder.getPath('home/a')
|
|
|
|
var set = new Set()
|
|
|
|
set.add(builder.getPath('home/a/file1'))
|
|
|
|
set.add(builder.getPath('home/a/file2'))
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
Promise.all([
|
|
|
|
builder.newFile('home/a/file1'),
|
|
|
|
builder.newFile('home/a/file2'),
|
|
|
|
]).then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, { delay: 0 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
set.delete(name)
|
|
|
|
if (!set.size) {
|
|
|
|
done()
|
|
|
|
}
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
2023-09-03 00:12:49 +00:00
|
|
|
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
Promise.all([
|
|
|
|
builder.modify('home/a/file1'),
|
|
|
|
builder.modify('home/a/file2'),
|
|
|
|
]).then()
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
2023-09-03 00:12:49 +00:00
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should debounce multiple triggers', function(done) {
|
|
|
|
var counter = new Counter()
|
|
|
|
var file = 'home/a/file2'
|
|
|
|
var fpath = builder.getPath(file)
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var start = process.hrtime()
|
|
|
|
var middle = start
|
|
|
|
var end = start
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, { delay: 100 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (fpath === name) counter.count()
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
watcher.on('ready', function() {
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.modify(file)
|
|
|
|
.then(() => builder.modify(file))
|
|
|
|
.then(() => builder.modify(file))
|
|
|
|
.then(() => {
|
|
|
|
middle = htTimeToMs(process.hrtime(start))
|
|
|
|
return counter.waitForCount()
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
assert.strictEqual(counter.counter, 1)
|
|
|
|
end = htTimeToMs(process.hrtime(start))
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.ok(end - middle > 50)
|
|
|
|
assert.ok(end >= 100)
|
|
|
|
done()
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
.catch(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should listen to new created files', function(done) {
|
|
|
|
var home = builder.getPath('home')
|
|
|
|
var counter = new Counter()
|
|
|
|
var newfile1 = 'home/a/newfile' + builder.randomName()
|
|
|
|
var newfile2 = 'home/a/newfile' + builder.randomName()
|
|
|
|
var set = new Set([
|
|
|
|
builder.getPath(newfile1),
|
|
|
|
builder.getPath(newfile2),
|
|
|
|
])
|
|
|
|
var changes = []
|
|
|
|
var extra = []
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, { delay: 0, recursive: true }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (set.has(name)) {
|
|
|
|
changes.push(name)
|
2023-09-02 08:23:26 +00:00
|
|
|
counter.count()
|
2023-09-03 00:12:49 +00:00
|
|
|
} else {
|
|
|
|
extra.push(name)
|
2023-09-02 08:23:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
2023-09-03 00:12:49 +00:00
|
|
|
counter.waitForCount(builder.newFile(newfile1))
|
|
|
|
.then(() => counter.waitForCount(builder.newFile(newfile2)))
|
2023-09-02 08:23:26 +00:00
|
|
|
.then(() => {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.deepStrictEqual(changes, [...set.values()])
|
|
|
|
// assert.deepStrictEqual(extra, [])
|
2023-09-02 08:23:26 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should error when parent gets deleted before calling fs.watch', function(done) {
|
|
|
|
var fpath = builder.getPath('home/a/file1')
|
|
|
|
builder.newFile('home/a/file1')
|
|
|
|
.then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, null, null, { fs: { watch: function(path, options) {
|
|
|
|
builder.removeSync('home/a')
|
|
|
|
return fs.watch(path, options)
|
|
|
|
} } })
|
|
|
|
|
|
|
|
watcher.on('error', done.finish(function(err) {
|
|
|
|
assert.deepStrictEqual(watcher.listeners, [])
|
2023-09-03 00:12:49 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('watch for directories', function() {
|
|
|
|
t.test('should watch directories inside a directory', function(done) {
|
|
|
|
var home = builder.getPath('home')
|
|
|
|
var dir = builder.getPath('home/c')
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.createDirectory('home/c').then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, { delay: 0, recursive: true }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (name === dir && evt === 'remove') {
|
|
|
|
done()
|
2023-09-02 08:23:26 +00:00
|
|
|
}
|
2023-09-03 00:12:49 +00:00
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.remove('home/c').catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should watch new created directories', function(done) {
|
|
|
|
var home = builder.getPath('home')
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.remove('home/new').then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, { delay: 0, recursive: true }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (name === builder.getPath('home/new/file1')) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.newFile('home/new/file1')
|
|
|
|
.then(() => builder.modify('home/new/file1'))
|
|
|
|
.catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should not watch new created directories which are being skipped in the filter', function(done) {
|
|
|
|
var counter = new Counter(done, 1)
|
|
|
|
var home = builder.getPath('home')
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var options = {
|
|
|
|
delay: 0,
|
|
|
|
recursive: true,
|
2023-09-03 19:43:59 +00:00
|
|
|
filter: function(filePath) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (/ignored/.test(filePath)) {
|
|
|
|
counter.count()
|
2023-09-03 19:43:59 +00:00
|
|
|
return false
|
2023-09-03 00:12:49 +00:00
|
|
|
}
|
2023-09-03 19:43:59 +00:00
|
|
|
return true
|
2023-09-02 08:23:26 +00:00
|
|
|
}
|
2023-09-03 00:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.remove('home/ignored/file').then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, options, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.fail("should not watch new created directories which are being skipped in the filter event detect: " + name)
|
|
|
|
})
|
2023-09-03 19:43:59 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.newFile('home/ignored/file')
|
|
|
|
.catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should keep watching after removal of sub directory', function(done) {
|
|
|
|
var counter = new Counter(done, 3)
|
|
|
|
var home = builder.getPath('home')
|
|
|
|
var file1 = builder.getPath('home/e/file1')
|
|
|
|
var file2 = builder.getPath('home/e/file2')
|
|
|
|
var dir = builder.getPath('home/e/sub')
|
|
|
|
var set = new Set()
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
Promise.all([
|
|
|
|
builder.newFile('home/e/sub/testfile'),
|
|
|
|
builder.newFile('home/e/file1'),
|
|
|
|
builder.newFile('home/e/file2'),
|
|
|
|
]).then(() => {
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, { delay: 0, recursive: true }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (name === dir || name === file1 || name === file2) {
|
|
|
|
if (!set.has(name)) {
|
|
|
|
set.add(name)
|
|
|
|
counter.count()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.remove('home/e/sub')
|
|
|
|
builder.modify('home/e/file1')
|
|
|
|
builder.modify('home/e/file2')
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should watch new directories without delay', function(done) {
|
|
|
|
var counter = new Counter(done, 1)
|
|
|
|
var home = builder.getPath('home')
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.remove('home/new').then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, { delay: 200, recursive: true }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (name === builder.getPath('home/new/file1')) {
|
|
|
|
counter.count()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.newFile('home/new/file1')
|
|
|
|
.then(() => builder.modify('home/new/file1'))
|
|
|
|
.then(() => builder.modify('home/new/file1'))
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
2023-09-03 00:12:49 +00:00
|
|
|
|
|
|
|
t.test('should error when directory gets deleted before calling fs.watch', function(done) {
|
|
|
|
var dir = 'home/c'
|
|
|
|
var fpath = builder.getPath(dir)
|
|
|
|
|
|
|
|
builder.createDirectory(dir).then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, null, null, { fs: { watch: function(path, options) {
|
|
|
|
builder.removeSync(dir)
|
|
|
|
return fs.watch(path, options)
|
|
|
|
} } })
|
|
|
|
|
|
|
|
watcher.on('error', done.finish(function(err) {
|
|
|
|
assert.deepStrictEqual(watcher.listeners, [])
|
2023-09-03 00:12:49 +00:00
|
|
|
}))
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('file events', function() {
|
|
|
|
var file = 'home/a/file1'
|
|
|
|
var fpath = builder.getPath(file)
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.beforeEach(function() {
|
|
|
|
return builder.newFile(file)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should identify `remove` event', function(done) {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, { delay: 0 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (evt === 'remove' && name === fpath) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
watcher.on('ready', function() {
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.remove(file).catch(done)
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should identify `remove` event on directory', function(done) {
|
|
|
|
var dir = 'home/a'
|
|
|
|
var home = builder.getPath('home')
|
|
|
|
var fpath = builder.getPath(dir)
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, { delay: 0 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (evt === 'remove' && name === fpath) done()
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.remove(dir).catch(done)
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should be able to handle many events on deleting', function(done) {
|
|
|
|
var dir = 'home/a'
|
|
|
|
var fpath = builder.getPath(dir)
|
|
|
|
|
|
|
|
builder.newRandomFiles(dir, 100).then(names => {
|
|
|
|
var counter = new Counter(done, names.length)
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, { delay: 10 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (evt === 'remove') counter.count()
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', function() {
|
|
|
|
Promise.all(names.map(x => builder.remove(path.join(dir, x)))).catch(done)
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
2023-09-03 00:12:49 +00:00
|
|
|
|
|
|
|
t.test('should identify `update` event', function(done) {
|
|
|
|
var file = 'home/b/file1'
|
|
|
|
var fpath = builder.getPath(file)
|
|
|
|
builder.newFile(file).then(() => {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpath, { delay: 0 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (evt === 'update' && name === fpath) done()
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.modify(file).catch(done)
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should report `update` on new files', function(done) {
|
|
|
|
var dir = builder.getPath('home/a')
|
|
|
|
var file = 'home/a/newfile_' + builder.randomName()
|
|
|
|
var fpath = builder.getPath(file)
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, { delay: 0 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (evt === 'update' && name === fpath) done()
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.newFile(file).catch(done)
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('options', function() {
|
|
|
|
t.describe('recursive', function() {
|
|
|
|
t.test('should watch recursively with `recursive: true` option', function(done) {
|
|
|
|
var dir = builder.getPath('home')
|
|
|
|
var file = builder.getPath('home/bb/file1')
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, { delay: 0, recursive: true }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (file === name) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.modify('home/bb/file1').catch(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('encoding', function() {
|
|
|
|
let options = {
|
|
|
|
delay: 0,
|
|
|
|
encoding: 'unknown'
|
|
|
|
};
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var fdir = builder.getPath('home/a')
|
|
|
|
var file = 'home/a/file1'
|
|
|
|
var fpath = builder.getPath(file)
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.before(() => {
|
|
|
|
return builder.newFile(file)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should throw on invalid encoding', function(done) {
|
|
|
|
options.encoding = 'unknown'
|
|
|
|
try {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fdir, options)
|
2023-09-03 00:12:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
done()
|
|
|
|
}
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should accept an encoding string', function(done) {
|
|
|
|
options.encoding = 'utf8'
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fdir, options, done.finish(function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.strictEqual(name.toString(), fpath)
|
|
|
|
}))
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.modify(file).catch(done)
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should support buffer encoding', function(done) {
|
|
|
|
options.encoding = 'buffer'
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fdir, options, done.finish(function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.ok(Buffer.isBuffer(name), 'not a Buffer')
|
|
|
|
assert.strictEqual(name.toString(), fpath)
|
|
|
|
}))
|
|
|
|
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.modify(file).catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should support base64 encoding', function(done) {
|
|
|
|
options.encoding = 'base64'
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fdir, options, done.finish(function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
name,
|
|
|
|
Buffer.from(fpath).toString('base64'),
|
|
|
|
'wrong base64 encoding'
|
|
|
|
)
|
|
|
|
}))
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.modify(file).catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should support hex encoding', function(done) {
|
|
|
|
options.encoding = 'hex'
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fdir, options, done.finish(function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
name,
|
|
|
|
Buffer.from(fpath).toString('hex'),
|
|
|
|
'wrong hex encoding'
|
|
|
|
)
|
|
|
|
}))
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.modify(file).catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('filter', function() {
|
|
|
|
t.test('should only watch filtered directories', function(done) {
|
|
|
|
var matchRegularDir = false
|
|
|
|
var matchIgnoredDir = false
|
|
|
|
var counter = new Counter(done.finish(function() {
|
|
|
|
assert(matchRegularDir, 'watch failed to detect regular file')
|
|
|
|
assert(!matchIgnoredDir, 'fail to ignore path `deep_node_modules`')
|
|
|
|
}), 1, true)
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var options = {
|
|
|
|
delay: 0,
|
|
|
|
recursive: true,
|
|
|
|
filter: function(name) {
|
|
|
|
return !/deep_node_modules/.test(name)
|
|
|
|
}
|
2023-09-02 08:23:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(builder.getPath('home'), options, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (/deep_node_modules/.test(name)) {
|
|
|
|
matchIgnoredDir = true
|
|
|
|
} else {
|
|
|
|
matchRegularDir = true
|
|
|
|
}
|
|
|
|
counter.count()
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
counter.startCounting()
|
|
|
|
builder.modify('home/deep_node_modules/ma/file1')
|
|
|
|
.then(() => builder.modify('home/b/file1'))
|
|
|
|
.catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should only report filtered files', function(done) {
|
|
|
|
var dir = builder.getPath('home')
|
|
|
|
var file1 = 'home/bb/file1'
|
|
|
|
var file2 = 'home/bb/file2'
|
2023-09-02 08:23:26 +00:00
|
|
|
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var counter = new Counter(done.finish(function() {
|
|
|
|
assert.strictEqual(matchIgnoredFile, false, 'home/bb/file1 should be ignored')
|
|
|
|
}), 1, true)
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var options = {
|
|
|
|
delay: 0,
|
|
|
|
recursive: true,
|
|
|
|
filter: function(name) {
|
|
|
|
return /file2/.test(name)
|
|
|
|
}
|
2023-09-02 08:23:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var times = 0
|
|
|
|
var matchIgnoredFile = false
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, options, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (name === builder.getPath(file1)) {
|
|
|
|
matchIgnoredFile = true
|
|
|
|
}
|
|
|
|
counter.count()
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
counter.startCounting()
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.modify(file2)
|
|
|
|
.then(() => builder.modify(file1))
|
|
|
|
.catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should be able to filter directly with regexp', function(done) {
|
|
|
|
var dir = builder.getPath('home')
|
|
|
|
var file1 = 'home/bb/file1'
|
|
|
|
var file2 = 'home/bb/file2'
|
2023-09-02 08:23:26 +00:00
|
|
|
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var counter = new Counter(done.finish(function() {
|
|
|
|
assert.strictEqual(matchIgnoredFile, false, 'home/bb/file1 should be ignored')
|
|
|
|
}), 1, true)
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var options = {
|
|
|
|
delay: 0,
|
|
|
|
recursive: true,
|
|
|
|
filter: /file2/
|
2023-09-02 08:23:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var times = 0
|
|
|
|
var matchIgnoredFile = false
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, options, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (name === builder.getPath(file1)) {
|
|
|
|
matchIgnoredFile = true
|
|
|
|
}
|
|
|
|
counter.count()
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
counter.startCounting()
|
|
|
|
|
|
|
|
builder.modify(file2)
|
|
|
|
.then(() => builder.modify(file1))
|
|
|
|
.catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should be able to skip subdirectories with `skip` flag', function(done) {
|
|
|
|
var home = builder.getPath('home')
|
|
|
|
var options = {
|
|
|
|
delay: 0,
|
|
|
|
recursive: true,
|
2023-09-03 19:43:59 +00:00
|
|
|
manualRecursive: true,
|
2023-09-03 00:12:49 +00:00
|
|
|
filter: function(name, skip) {
|
|
|
|
if (/\/deep_node_modules/.test(name)) return skip
|
|
|
|
}
|
2023-09-02 08:23:26 +00:00
|
|
|
}
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(home, options)
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher.on('ready', done.finish(function() {
|
|
|
|
let homeFiltered = builder.getAllDirectories().filter(function(name) {
|
|
|
|
return !/\/deep_node_modules/.test(name)
|
|
|
|
}).sort()
|
|
|
|
let watchersPaths = watcher.listeners.map(x => x.path).sort()
|
|
|
|
|
|
|
|
assert.deepStrictEqual(watchersPaths, homeFiltered)
|
|
|
|
}))
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('parameters', function() {
|
|
|
|
t.test('should throw error on non-existed file', function(done) {
|
|
|
|
var somedir = builder.getPath('home/somedir')
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(somedir)
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('error', done.finish(function(err) {
|
|
|
|
assert.match(err.message, /not exist/)
|
|
|
|
}))
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should accept filename as Buffer', function(done) {
|
|
|
|
var fpath = builder.getPath('home/a/file1')
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(Buffer.from(fpath), { delay: 0 }, done.finish(function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
assert.strictEqual(name, fpath)
|
|
|
|
}))
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
builder.modify('home/a/file1').catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should compose array of files or directories', function(done) {
|
|
|
|
var counter = new Counter(done, 2)
|
|
|
|
var file1 = 'home/a/file1'
|
|
|
|
var file2 = 'home/a/file2'
|
|
|
|
var fpaths = [
|
|
|
|
builder.getPath(file1),
|
|
|
|
builder.getPath(file2)
|
|
|
|
]
|
|
|
|
var set = new Set(fpaths)
|
|
|
|
|
2023-09-02 08:23:26 +00:00
|
|
|
Promise.all([
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.newFile(file1),
|
|
|
|
builder.newFile(file2),
|
|
|
|
]).then(function() {
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpaths, { delay: 0 }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
if (set.has(name)) {
|
|
|
|
set.delete(name)
|
|
|
|
counter.count()
|
|
|
|
}
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', function() {
|
|
|
|
Promise.all([
|
|
|
|
builder.modify(file1),
|
|
|
|
builder.modify(file2),
|
|
|
|
]).catch(done)
|
|
|
|
})
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should filter duplicate events for composed watcher', function(done) {
|
|
|
|
var counter = new Counter(done, 2)
|
|
|
|
var home = 'home'
|
|
|
|
var dir = 'home/a'
|
|
|
|
var file1 = 'home/a/file1'
|
|
|
|
var file2 = 'home/a/file2'
|
|
|
|
var fpaths = [
|
|
|
|
builder.getPath(home),
|
|
|
|
builder.getPath(dir),
|
|
|
|
builder.getPath(file1),
|
|
|
|
builder.getPath(file2)
|
|
|
|
]
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
var changes = []
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(fpaths, { delay: 100, recursive: true }, function(evt, name) {
|
2023-09-03 00:12:49 +00:00
|
|
|
changes.push(name)
|
|
|
|
counter.count()
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', function() {
|
|
|
|
new Promise(res => {
|
|
|
|
counter.updateRes(res)
|
|
|
|
|
|
|
|
builder.modify(file1)
|
|
|
|
.then(() => builder.modify(file2))
|
|
|
|
})
|
|
|
|
.then(() => builder.delay(50))
|
|
|
|
.then(() => {
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
changes,
|
|
|
|
[fpaths[2], fpaths[3]]
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
}).catch(done)
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('watcher object', function() {
|
|
|
|
t.test('should using watcher object to watch', function(done) {
|
2023-09-02 08:23:26 +00:00
|
|
|
var dir = builder.getPath('home/a')
|
|
|
|
var file = 'home/a/file1'
|
2023-09-03 00:12:49 +00:00
|
|
|
var fpath = builder.getPath(file)
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, { delay: 0 })
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('change', done.finish(function(evt, name) {
|
|
|
|
assert.strictEqual(evt, 'update')
|
|
|
|
assert.strictEqual(name, fpath)
|
|
|
|
}))
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('ready', done.wrap(function() {
|
2023-09-03 19:43:59 +00:00
|
|
|
builder.modify(file).catch(done)
|
2023-09-03 00:12:49 +00:00
|
|
|
}))
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.describe('close()', function() {
|
|
|
|
t.test('should close a watcher using .close()', function(done) {
|
|
|
|
var dir = builder.getPath('home/a')
|
|
|
|
var file = 'home/a/file1'
|
|
|
|
var times = 0
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, { delay: 0 })
|
2023-09-03 00:12:49 +00:00
|
|
|
watcher.on('change', function(evt, name) {
|
|
|
|
times++
|
|
|
|
})
|
|
|
|
watcher.on('ready', function() {
|
|
|
|
watcher.close()
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
builder.modify(file)
|
|
|
|
.then(() => builder.delay(50))
|
|
|
|
.then(() => builder.modify(file))
|
|
|
|
.then(() => builder.delay(50))
|
|
|
|
.then(() => {
|
|
|
|
assert(watcher.isClosed(), 'watcher should be closed')
|
|
|
|
assert.strictEqual(times, 0, 'failed to close the watcher')
|
|
|
|
done()
|
|
|
|
}).catch(done)
|
|
|
|
})
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 00:12:49 +00:00
|
|
|
t.test('should not watch after .close() is called', function(done) {
|
|
|
|
var dir = builder.getPath('home')
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher = new Watcher(dir, { delay: 0, recursive: true })
|
2023-09-02 08:23:26 +00:00
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher.on('ready', function() {
|
|
|
|
watcher.close()
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
|
2023-09-03 19:43:59 +00:00
|
|
|
watcher.on('close', done.finish(function(dirs) {
|
|
|
|
assert.strictEqual(watcher.listeners.length, 0)
|
2023-09-03 00:12:49 +00:00
|
|
|
}))
|
2023-09-02 08:23:26 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-09-03 00:12:49 +00:00
|
|
|
})
|