Skip to content

Commit

Permalink
Merge 6aea335 into 97f713d
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 15, 2020
2 parents 97f713d + 6aea335 commit 6a13b05
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/DirectoryWatcher.js
Expand Up @@ -171,6 +171,14 @@ class DirectoryWatcher extends EventEmitter {
} else {
safeTime = now;
accuracy = 0;

if (old && old.timestamp === mtime && mtime + FS_ACCURACY < now - 1000) {
// We are sure that mtime is untouched
// This can be caused by some file attribute change
// e. g. when access time has been changed
// but the file content is untouched
return;
}
}

if (ignoreWhenEqual && old && old.timestamp === mtime) return;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -36,7 +36,8 @@
"mocha": "^5.0.1",
"prettier": "^1.11.0",
"rimraf": "^2.6.2",
"should": "^8.3.1"
"should": "^8.3.1",
"write-file-atomic": "^3.0.1"
},
"dependencies": {
"glob-to-regexp": "^0.4.1",
Expand Down
33 changes: 33 additions & 0 deletions test/Watchpack.js
Expand Up @@ -832,6 +832,39 @@ describe("Watchpack", function() {
});
});

it("should not detect file reading as change, but atomic file writes", done => {
var w = new Watchpack({
aggregateTimeout: 1000
});
w.on("aggregated", function(changes, removals) {
Array.from(changes).should.be.eql([
path.join(fixtures, "dir", "b"),
path.join(fixtures, "dir", "c")
]);
Array.from(removals).should.be.eql([]);
w.close();
done();
});
testHelper.dir("dir");
testHelper.file(path.join("dir", "a"));
testHelper.file(path.join("dir", "b"));
testHelper.file(path.join("dir", "c"));
testHelper.tick(1000, () => {
w.watch({
files: [
path.join(fixtures, "dir", "a"),
path.join(fixtures, "dir", "b"),
path.join(fixtures, "dir", "c")
]
});
testHelper.tick(1000, () => {
testHelper.accessFile(path.join("dir", "a"));
testHelper.fileAtomic(path.join("dir", "b"));
testHelper.file(path.join("dir", "c"));
});
});
});

let symlinksSupported = false;
try {
const fs = require("fs");
Expand Down
19 changes: 19 additions & 0 deletions test/helpers/TestHelper.js
Expand Up @@ -3,6 +3,7 @@
var fs = require("fs");
var path = require("path");
var rimraf = require("rimraf");
var writeFileAtomic = require("write-file-atomic");

require("../../lib/getWatcherManager");
var watcherManagerModule =
Expand Down Expand Up @@ -71,6 +72,24 @@ TestHelper.prototype.file = function file(name) {
fs.writeFileSync(path.join(this.testdir, name), Math.random() + "", "utf-8");
};

TestHelper.prototype.fileAtomic = function fileAtomic(name) {
writeFileAtomic.sync(
path.join(this.testdir, name),
Math.random() + "",
"utf-8"
);
};

TestHelper.prototype.accessFile = function accessFile(name) {
const stat = fs.statSync(path.join(this.testdir, name));
fs.utimesSync(
path.join(this.testdir, name),
new Date(Date.now() - 1000 * 60 * 60 * 24),
stat.mtime
);
fs.readFileSync(path.join(this.testdir, name));
};

TestHelper.prototype.symlinkFile = function symlinkFile(name, target) {
fs.symlinkSync(target, path.join(this.testdir, name), "file");
};
Expand Down
19 changes: 18 additions & 1 deletion yarn.lock
Expand Up @@ -714,7 +714,7 @@ is-promise@^2.1.0:
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.1.0.tgz#79a2a9ece7f096e80f36d2b2f3bc16c1ff4bf3fa"
integrity sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=

is-typedarray@~1.0.0:
is-typedarray@^1.0.0, is-typedarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=
Expand Down Expand Up @@ -1322,6 +1322,13 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typedarray-to-buffer@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"
integrity sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==
dependencies:
is-typedarray "^1.0.0"

uglify-js@^3.1.4:
version "3.7.3"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.7.3.tgz#f918fce9182f466d5140f24bb0ff35c2d32dcc6a"
Expand Down Expand Up @@ -1373,6 +1380,16 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=

write-file-atomic@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-3.0.1.tgz#558328352e673b5bb192cf86500d60b230667d4b"
integrity sha512-JPStrIyyVJ6oCSz/691fAjFtefZ6q+fP6tm+OS4Qw6o+TGQxNp1ziY2PgS+X/m0V8OWhZiO/m4xSj+Pr4RrZvw==
dependencies:
imurmurhash "^0.1.4"
is-typedarray "^1.0.0"
signal-exit "^3.0.2"
typedarray-to-buffer "^3.1.5"

write@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/write/-/write-0.2.1.tgz#5fc03828e264cea3fe91455476f7a3c566cb0757"
Expand Down

0 comments on commit 6a13b05

Please sign in to comment.