| 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-path"); |
| 18 |
|
| 19 |
module.exports = function (t, a, d) { |
| 20 |
var ondirchange = [] |
| 21 |
, onfilechange = [] |
| 22 |
, DELAY = 100 |
| 23 |
, dirPath = resolve(pgPath, "tmpdir") |
| 24 |
, filePath = resolve(dirPath, "tmpfile") |
| 25 |
, watch1 |
| 26 |
, watch2; |
| 27 |
|
| 28 |
watch1 = t(dirPath); |
| 29 |
watch1.on("change", function (e) { ondirchange.push(e.type); }); |
| 30 |
watch2 = t(filePath); |
| 31 |
watch2.on("change", function (e) { onfilechange.push(e.type); }); |
| 32 |
|
| 33 |
delay(function () { return mkdir(dirPath); }, DELAY)()( |
| 34 |
delay(function () { |
| 35 |
a(String(ondirchange), "create", "Dir: Dir created"); |
| 36 |
a(String(onfilechange), "", "File: Dir created"); |
| 37 |
ondirchange = []; |
| 38 |
onfilechange = []; |
| 39 |
return writeFile(filePath, "raz"); |
| 40 |
}, DELAY) |
| 41 |
)( |
| 42 |
delay(function () { |
| 43 |
a(String(ondirchange), "modify", "Dir: File created"); |
| 44 |
a(String(onfilechange), "create", "File: File created"); |
| 45 |
ondirchange = []; |
| 46 |
onfilechange = []; |
| 47 |
return open(filePath, "a")(function (fd) { |
| 48 |
return write( |
| 49 |
fd, bufferFrom("dwatrzy"), 0, 3, null |
| 50 |
)(function () { return close(fd); }); |
| 51 |
}); |
| 52 |
}, DELAY) |
| 53 |
)( |
| 54 |
delay(function () { |
| 55 |
a(String(ondirchange), "", "Dir: File changed"); |
| 56 |
a(String(onfilechange), "modify", "File: File changed"); |
| 57 |
ondirchange = []; |
| 58 |
onfilechange = []; |
| 59 |
return rename(filePath, filePath + "r"); |
| 60 |
}, DELAY) |
| 61 |
)( |
| 62 |
delay(function () { |
| 63 |
a(String(ondirchange), "modify", "Dir: File renamed"); |
| 64 |
a(String(onfilechange), "remove", "File: File renamed"); |
| 65 |
ondirchange = []; |
| 66 |
onfilechange = []; |
| 67 |
return rename(filePath + "r", filePath); |
| 68 |
}, DELAY) |
| 69 |
)( |
| 70 |
delay(function () { |
| 71 |
a(String(ondirchange), "modify", "Dir: File renamed back"); |
| 72 |
a(String(onfilechange), "create", "File: File renamed back"); |
| 73 |
ondirchange = []; |
| 74 |
onfilechange = []; |
| 75 |
return unlink(filePath); |
| 76 |
}, DELAY) |
| 77 |
)( |
| 78 |
delay(function () { |
| 79 |
a(String(ondirchange), "modify", "Dir: File removed"); |
| 80 |
a(String(onfilechange), "remove", "File: File removed"); |
| 81 |
ondirchange = []; |
| 82 |
onfilechange = []; |
| 83 |
return rmdir(dirPath); |
| 84 |
}, DELAY) |
| 85 |
)( |
| 86 |
delay(function () { |
| 87 |
a(String(ondirchange), "remove", "Dir: Dir removed"); |
| 88 |
a(String(onfilechange), "", "File: Dir removed"); |
| 89 |
ondirchange = []; |
| 90 |
onfilechange = []; |
| 91 |
return mkdir(dirPath); |
| 92 |
}, DELAY) |
| 93 |
)( |
| 94 |
delay(function () { |
| 95 |
a(String(ondirchange), "create", "Dir: Dir created #2"); |
| 96 |
a(String(onfilechange), "", "File: Dir created #2"); |
| 97 |
ondirchange = []; |
| 98 |
onfilechange = []; |
| 99 |
return writeFile(filePath, "raz"); |
| 100 |
}, DELAY) |
| 101 |
)( |
| 102 |
delay(function () { |
| 103 |
a(String(ondirchange), "modify", "Dir: File created #2"); |
| 104 |
a(String(onfilechange), "create", "File: File created #2"); |
| 105 |
ondirchange = []; |
| 106 |
onfilechange = []; |
| 107 |
return unlink(filePath); |
| 108 |
}, DELAY) |
| 109 |
)( |
| 110 |
delay(function () { |
| 111 |
a(String(ondirchange), "modify", "Dir: File removed #2"); |
| 112 |
a(String(onfilechange), "remove", "File: File removed #2"); |
| 113 |
ondirchange = []; |
| 114 |
onfilechange = []; |
| 115 |
return rmdir(dirPath); |
| 116 |
}, DELAY) |
| 117 |
)( |
| 118 |
delay(function () { |
| 119 |
a(String(ondirchange), "remove", "Dir: Dir removed #2"); |
| 120 |
a(String(onfilechange), "", "File: Dir removed #2"); |
| 121 |
watch1.close(); |
| 122 |
watch2.close(); |
| 123 |
}, DELAY) |
| 124 |
).done(d, d); |
| 125 |
}; |