| 1 |
"use strict"; |
| 2 |
|
| 3 |
var fs = require("fs") |
| 4 |
, resolve = require("path").resolve |
| 5 |
, deferred = require("deferred") |
| 6 |
, bufferFrom = require("../lib/buffer-from") |
| 7 |
, delay = deferred.delay |
| 8 |
, promisify = deferred.promisify |
| 9 |
, mkdir = promisify(fs.mkdir) |
| 10 |
, open = promisify(fs.open) |
| 11 |
, rename = promisify(fs.rename) |
| 12 |
, write = promisify(fs.write) |
| 13 |
, close = promisify(fs.close) |
| 14 |
, writeFile = promisify(fs.writeFile) |
| 15 |
, unlink = promisify(fs.unlink) |
| 16 |
, rmdir = promisify(fs.rmdir) |
| 17 |
, pgPath = resolve(__dirname, "./__playground/watch"); |
| 18 |
|
| 19 |
module.exports = function (t, a, d) { |
| 20 |
var ondirchange = 0 |
| 21 |
, onfilechange = 0 |
| 22 |
, ondirend = 0 |
| 23 |
, onfileend = 0 |
| 24 |
, w1 |
| 25 |
, w2 |
| 26 |
, w3 |
| 27 |
, w4 |
| 28 |
, DELAY = 500 |
| 29 |
, dirPath = resolve(pgPath, "tmpdir") |
| 30 |
, filePath = resolve(dirPath, "tmpfile"); |
| 31 |
|
| 32 |
a.throws(function () { t(filePath); }, "Not existing"); |
| 33 |
|
| 34 |
delay(function () { return mkdir(dirPath); }, DELAY)()( |
| 35 |
delay(function () { |
| 36 |
w1 = t(dirPath); |
| 37 |
w1.on("change", function () { ++ondirchange; }); |
| 38 |
w2 = t(dirPath); |
| 39 |
w2.on("end", function () { ++ondirend; }); |
| 40 |
}, DELAY) |
| 41 |
)(delay(function () { return writeFile(filePath, "raz"); }, DELAY))( |
| 42 |
delay(function () { |
| 43 |
a(ondirchange, 1, "Dir change: File created"); |
| 44 |
a(ondirend, 0, "Dir end: File created"); |
| 45 |
ondirchange = ondirend = 0; |
| 46 |
w3 = t(filePath); |
| 47 |
w3.on("change", function () { ++onfilechange; }); |
| 48 |
w4 = t(filePath); |
| 49 |
w4.on("end", function () { ++onfileend; }); |
| 50 |
}, DELAY) |
| 51 |
)( |
| 52 |
delay(function () { |
| 53 |
return open(filePath, "a")(function (fd) { |
| 54 |
return write( |
| 55 |
fd, bufferFrom("dwatrzy"), 0, 3, null |
| 56 |
)(function () { return close(fd); }); |
| 57 |
}); |
| 58 |
}, DELAY) |
| 59 |
)( |
| 60 |
delay(function () { |
| 61 |
a(ondirchange, 0, "Dir change: File changed"); |
| 62 |
a(ondirend, 0, "Dir end: File changed"); |
| 63 |
a(onfilechange, 1, "File change: File created"); |
| 64 |
a(onfileend, 0, "File end: File created"); |
| 65 |
ondirchange = ondirend = onfilechange = onfileend = 0; |
| 66 |
return rename(filePath, filePath + "r"); |
| 67 |
}, DELAY) |
| 68 |
)( |
| 69 |
delay(function () { |
| 70 |
a(ondirchange, 1, "Dir change: File renamed"); |
| 71 |
a(ondirend, 0, "Dir end: File renamed"); |
| 72 |
a(onfilechange, 0, "File change: File renamed"); |
| 73 |
a(onfileend, 1, "File end: File renamed"); |
| 74 |
ondirchange = ondirend = onfilechange = onfileend = 0; |
| 75 |
return rename(filePath + "r", filePath); |
| 76 |
}, DELAY) |
| 77 |
)( |
| 78 |
delay(function () { |
| 79 |
a(ondirchange, 1, "Dir change: File renamed back"); |
| 80 |
a(ondirend, 0, "Dir end: File renamed back"); |
| 81 |
a(onfilechange, 0, "File change: File renamed back"); |
| 82 |
a(onfileend, 0, "File end: File renamed back"); |
| 83 |
ondirchange = ondirend = onfilechange = onfileend = 0; |
| 84 |
return unlink(filePath); |
| 85 |
}, DELAY) |
| 86 |
)( |
| 87 |
delay(function () { |
| 88 |
a(ondirchange, 1, "Dir change: File removed"); |
| 89 |
a(ondirend, 0, "Dir end: File removed"); |
| 90 |
a(onfilechange, 0, "File change: File removed"); |
| 91 |
a(onfileend, 0, "File end: File removed"); |
| 92 |
ondirchange = ondirend = onfilechange = onfileend = 0; |
| 93 |
return rmdir(dirPath); |
| 94 |
}, DELAY) |
| 95 |
)( |
| 96 |
delay(function () { |
| 97 |
a(ondirchange, 0, "Dir change: Dir removed"); |
| 98 |
a(ondirend, 1, "Dir end: Dir removed"); |
| 99 |
a(onfilechange, 0, "File change: Dir removed"); |
| 100 |
a(onfileend, 0, "File end: Dir removed"); |
| 101 |
w1.close(); |
| 102 |
w2.close(); |
| 103 |
w3.close(); |
| 104 |
w4.close(); |
| 105 |
}, DELAY) |
| 106 |
).done(d, d); |
| 107 |
}; |