Skip to content

Commit 384aad6

Browse files
committed
Add test case for file change happening as part of file create and delete
1 parent 044fb53 commit 384aad6

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/harness/unittests/tscWatchMode.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,38 @@ namespace ts.tscWatch {
17191719
return [files[0]];
17201720
}
17211721
});
1722+
1723+
it("file is deleted and created as part of change", () => {
1724+
const projectLocation = "/home/username/project";
1725+
const file: FileOrFolder = {
1726+
path: `${projectLocation}/app/file.ts`,
1727+
content: "var a = 10;"
1728+
};
1729+
const fileJs = `${projectLocation}/app/file.js`;
1730+
const configFile: FileOrFolder = {
1731+
path: `${projectLocation}/tsconfig.json`,
1732+
content: JSON.stringify({
1733+
"include": [
1734+
"app/**/*.ts"
1735+
]
1736+
})
1737+
};
1738+
const files = [file, configFile, libFile];
1739+
const host = createWatchedSystem(files, { currentDirectory: projectLocation, useCaseSensitiveFileNames: true });
1740+
createWatchOfConfigFile("tsconfig.json", host);
1741+
verifyProgram();
1742+
1743+
file.content += "var b = 10;";
1744+
1745+
host.reloadFS(files, { invokeFileDeleteCreateAsPartInsteadOfChange: true });
1746+
host.runQueuedTimeoutCallbacks();
1747+
verifyProgram();
1748+
1749+
function verifyProgram() {
1750+
assert.isTrue(host.fileExists(fileJs));
1751+
assert.equal(host.readFile(fileJs), file.content + "\n");
1752+
}
1753+
});
17221754
});
17231755

17241756
describe("tsc-watch module resolution caching", () => {

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,12 @@ interface Array<T> {}`
246246
}
247247

248248
export interface ReloadWatchInvokeOptions {
249+
/** Invokes the directory watcher for the parent instead of the file changed */
249250
invokeDirectoryWatcherInsteadOfFileChanged: boolean;
251+
/** When new file is created, do not invoke watches for it */
250252
ignoreWatchInvokedWithTriggerAsFileCreate: boolean;
253+
/** Invoke the file delete, followed by create instead of file changed */
254+
invokeFileDeleteCreateAsPartInsteadOfChange: boolean;
251255
}
252256

253257
export class TestServerHost implements server.ServerHost, FormatDiagnosticsHost, ModuleResolutionHost {
@@ -315,12 +319,18 @@ interface Array<T> {}`
315319
if (isString(fileOrDirectory.content)) {
316320
// Update file
317321
if (currentEntry.content !== fileOrDirectory.content) {
318-
currentEntry.content = fileOrDirectory.content;
319-
if (options && options.invokeDirectoryWatcherInsteadOfFileChanged) {
320-
this.invokeDirectoryWatcher(getDirectoryPath(currentEntry.fullPath), currentEntry.fullPath);
322+
if (options && options.invokeFileDeleteCreateAsPartInsteadOfChange) {
323+
this.removeFileOrFolder(currentEntry, returnFalse);
324+
this.ensureFileOrFolder(fileOrDirectory);
321325
}
322326
else {
323-
this.invokeFileWatcher(currentEntry.fullPath, FileWatcherEventKind.Changed);
327+
currentEntry.content = fileOrDirectory.content;
328+
if (options && options.invokeDirectoryWatcherInsteadOfFileChanged) {
329+
this.invokeDirectoryWatcher(getDirectoryPath(currentEntry.fullPath), currentEntry.fullPath);
330+
}
331+
else {
332+
this.invokeFileWatcher(currentEntry.fullPath, FileWatcherEventKind.Changed);
333+
}
324334
}
325335
}
326336
}

0 commit comments

Comments
 (0)