Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case renames not marking previous casing as missing in time entries #143

Merged
merged 5 commits into from Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/DirectoryWatcher.js
Expand Up @@ -64,6 +64,8 @@ class DirectoryWatcher extends EventEmitter {
// timestamp is a value that should be compared with another timestamp (mtime)
/** @type {Map<string, { safeTime: number, timestamp: number }} */
this.files = new Map();
/** @type {Map<string, number>} */
this.filesWithoutCase = new Map();
this.directories = new Map();
this.lastWatchEvent = 0;
this.initialScan = true;
Expand Down Expand Up @@ -154,8 +156,15 @@ class DirectoryWatcher extends EventEmitter {
const oldFile = this.files.get(itemPath);
if (oldFile) {
this.files.delete(itemPath);
const key = withoutCase(itemPath);
const count = this.filesWithoutCase.get(key) - 1;
if (count <= 0) {
this.filesWithoutCase.delete(key);
this.forEachWatcher(itemPath, w => w.emit("remove", type));
} else {
this.filesWithoutCase.set(key, count);
}

this.forEachWatcher(itemPath, w => w.emit("remove", type));
if (!initial) {
this.forEachWatcher(this.path, w =>
w.emit("change", itemPath, null, type)
Expand Down Expand Up @@ -198,6 +207,18 @@ class DirectoryWatcher extends EventEmitter {
this._cachedTimeInfoEntries = undefined;

if (!old) {
const key = withoutCase(filePath);
const count = this.filesWithoutCase.get(key);
this.filesWithoutCase.set(key, (count || 0) + 1);
if (count !== undefined) {
// There is already a file with case-insenstive-equal name
// On a case-insenstive filesystem we may miss the renaming
// when only casing is changed.
// To be sure that our information is correct
// we trigger a rescan here
this.doScan(false);
}

this.forEachWatcher(filePath, w => {
if (!initial || w.checkStartTime(safeTime, initial)) {
w.emit("change", mtime, type);
Expand Down
70 changes: 70 additions & 0 deletions test/Casing.js
Expand Up @@ -44,5 +44,75 @@ if (fsIsCaseInsensitive) {
testHelper.file("A");
});
});

it("should mark as missing on changing filename casing (dir watch)", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
});
var dir = "case-rename";
var testFile = path.join(dir, "hello.txt");
var testFileRename = path.join(dir, "hEllO.txt");
testHelper.dir(dir);
testHelper.file(testFile);

w.on("aggregated", function(changes, removals) {
const files = w.getTimeInfoEntries();
w.close();

changes.has(path.join(fixtures, dir)).should.be.eql(true);

for (const file of files.keys()) {
if (file.endsWith("hello.txt")) {
return done(new Error(`Renamed file was still in timeInfoEntries`));
}
}
return done();
});

testHelper.tick(function() {
w.watch([], [path.join(fixtures, "case-rename")]);

testHelper.tick(function() {
testHelper.rename(testFile, testFileRename);
});
});
});

it("should mark as missing on changing filename casing (file watch)", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
});
var dir = "case-rename";
var testFile = path.join(dir, "hello.txt");
var testFileRename = path.join(dir, "hEllO.txt");
testHelper.dir(dir);
testHelper.file(testFile);

w.on("aggregated", function(changes, removals) {
const files = w.getTimeInfoEntries();
w.close();

changes.has(path.join(fixtures, testFileRename)).should.be.eql(true);
removals.has(path.join(fixtures, testFileRename)).should.be.eql(false);

for (const file of files.keys()) {
if (file.endsWith("hello.txt") && files.get(file)) {
return done(new Error(`Renamed file was still in timeInfoEntries`));
}
}
return done();
});

testHelper.tick(function() {
w.watch({
files: [path.join(fixtures, testFile)],
missing: [path.join(fixtures, testFileRename)]
});

testHelper.tick(function() {
testHelper.rename(testFile, testFileRename);
});
});
});
});
}
4 changes: 4 additions & 0 deletions test/helpers/TestHelper.js
Expand Up @@ -68,6 +68,10 @@ TestHelper.prototype.dir = function dir(name) {
fs.mkdirSync(path.join(this.testdir, name));
};

TestHelper.prototype.rename = function rename(orig, dest) {
fs.renameSync(path.join(this.testdir, orig), path.join(this.testdir, dest));
};

TestHelper.prototype.file = function file(name) {
fs.writeFileSync(path.join(this.testdir, name), Math.random() + "", "utf-8");
};
Expand Down