diff options
author | Refael Ackermann <refack@gmail.com> | 2017-06-05 18:11:52 -0400 |
---|---|---|
committer | Refael Ackermann <refack@gmail.com> | 2017-06-07 08:49:04 -0400 |
commit | c05561eb0529d973d873e48adfd86690c3be90e5 (patch) | |
tree | 454393a7b6ed95f7efc7b2459617295516c6efd1 /test | |
parent | 07ca28892977aaa2b42da1f4ec8fb9b3aa152c17 (diff) | |
download | android-node-v8-c05561eb0529d973d873e48adfd86690c3be90e5.tar.gz android-node-v8-c05561eb0529d973d873e48adfd86690c3be90e5.tar.bz2 android-node-v8-c05561eb0529d973d873e48adfd86690c3be90e5.zip |
test,fs: test fs.watch for `filename`
PR-URL: https://github.com/nodejs/node/pull/13411
Refs: https://github.com/nodejs/node/pull/13385
Refs: https://github.com/nodejs/node/issues/13248
Refs: https://github.com/nodejs/node/issues/13377
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-fs-watch.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/test/parallel/test-fs-watch.js b/test/parallel/test-fs-watch.js new file mode 100644 index 0000000000..6edb141129 --- /dev/null +++ b/test/parallel/test-fs-watch.js @@ -0,0 +1,77 @@ +'use strict'; +const common = require('../common'); + +// tests if `filename` is provided to watcher on supported platforms + +const fs = require('fs'); +const assert = require('assert'); +const { join } = require('path'); + +class WatchTestCase { + constructor(shouldInclude, dirName, fileName, field) { + this.dirName = dirName; + this.fileName = fileName; + this.field = field; + this.shouldSkip = !shouldInclude; + } + get dirPath() { return join(common.tmpDir, this.dirName); } + get filePath() { return join(this.dirPath, this.fileName); } +} + +const cases = [ + // Watch on a directory should callback with a filename on supported systems + new WatchTestCase( + common.isLinux || common.isOSX || common.isWindows || common.isAix, + 'watch1', + 'foo', + 'filePath' + ), + // Watch on a file should callback with a filename on supported systems + new WatchTestCase( + common.isLinux || common.isOSX || common.isWindows, + 'watch2', + 'bar', + 'dirPath' + ) +]; + +common.refreshTmpDir(); + +for (const testCase of cases) { + if (testCase.shouldSkip) continue; + fs.mkdirSync(testCase.dirPath); + // long content so it's actually flushed. + const content1 = Date.now() + testCase.fileName.toLowerCase().repeat(1e4); + fs.writeFileSync(testCase.filePath, content1); + + let interval; + const watcher = fs.watch(testCase[testCase.field]); + watcher.on('error', (err) => { + if (interval) { + clearInterval(interval); + interval = null; + } + assert.fail(err); + }); + watcher.on('change', common.mustCall(function(eventType, argFilename) { + if (interval) { + clearInterval(interval); + interval = null; + } + if (common.isOSX) + assert.strictEqual(['rename', 'change'].includes(eventType), true); + else + assert.strictEqual(eventType, 'change'); + assert.strictEqual(argFilename, testCase.fileName); + + // end of test case + watcher.close(); + })); + + // long content so it's actually flushed. toUpperCase so there's real change. + const content2 = Date.now() + testCase.fileName.toUpperCase().repeat(1e4); + interval = setInterval(() => { + fs.writeFileSync(testCase.filePath, ''); + fs.writeFileSync(testCase.filePath, content2); + }, 100); +} |