Skip to content

Commit

Permalink
Add test for rename on file with case change that fails
Browse files Browse the repository at this point in the history
Test for #25460
  • Loading branch information
sheetalkamat committed Oct 17, 2019
1 parent 580e3f5 commit 7f77d24
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/harness/virtualFileSystemWithWatch.ts
Expand Up @@ -518,6 +518,26 @@ interface Array<T> { length: number; [n: number]: T; }`
}
}

renameFile(fileName: string, newFileName: string) {
const fullPath = getNormalizedAbsolutePath(fileName, this.currentDirectory);
const path = this.toPath(fullPath);
const file = this.fs.get(path) as FsFile;
Debug.assert(!!file);

// Only remove the file
this.removeFileOrFolder(file, returnFalse, /*isRenaming*/ true);

// Add updated folder with new folder name
const newFullPath = getNormalizedAbsolutePath(newFileName, this.currentDirectory);
const newFile = this.toFsFile({ path: newFullPath, content: file.content });
const newPath = newFile.path;
const basePath = getDirectoryPath(path);
Debug.assert(basePath !== path);
Debug.assert(basePath === getDirectoryPath(newPath));
const baseFolder = this.fs.get(basePath) as FsFolder;
this.addFileOrFolderInFolder(baseFolder, newFile);
}

renameFolder(folderName: string, newFolderName: string) {
const fullPath = getNormalizedAbsolutePath(folderName, this.currentDirectory);
const path = this.toPath(fullPath);
Expand Down
26 changes: 26 additions & 0 deletions src/testRunner/unittests/tscWatch/programUpdates.ts
Expand Up @@ -1365,5 +1365,31 @@ ${aFile.content}`;
getDiagnosticOfFileFromProgram(watch(), aFile.path, aContent.indexOf(`"../b"`), `"../b"`.length, Diagnostics.File_0_is_not_under_rootDir_1_rootDir_is_expected_to_contain_all_source_files, bFile.path, projectRoot)
]);
});

it("forceConsistentCasingInFileNames works when renaming file with different casing", () => {
const loggerFile: File = {
path: `${projectRoot}/logger.ts`,
content: `export class logger { }`
};
const anotherFile: File = {
path: `${projectRoot}/another.ts`,
content: `import { logger } from "./logger"; new logger();`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { forceConsistentCasingInFileNames: true }
})
};

const host = createWatchedSystem([loggerFile, anotherFile, tsconfig, libFile, tsconfig]);
createWatchOfConfigFile(tsconfig.path, host);
checkOutputErrorsInitial(host, emptyArray);
host.writeFile(anotherFile.path, anotherFile.content.replace("./logger", "./Logger"));
host.runQueuedTimeoutCallbacks();
checkOutputErrorsIncremental(host, [
createCompilerDiagnostic(Diagnostics.File_name_0_differs_from_already_included_file_name_1_only_in_casing, loggerFile.path, `${projectRoot}/Logger.ts`),
]);
});
});
}
Expand Up @@ -41,5 +41,104 @@ namespace ts.projectSystem {
const diagnostics = configuredProjectAt(projectService, 0).getLanguageService().getCompilerOptionsDiagnostics();
assert.deepEqual(diagnostics, []);
});

it("works when renaming file with different casing", () => {
const loggerFile: File = {
path: `${projectRoot}/Logger.ts`,
content: `export class logger { }`
};
const anotherFile: File = {
path: `${projectRoot}/another.ts`,
content: `import { logger } from "./Logger"; new logger();`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { forceConsistentCasingInFileNames: true }
})
};

const host = createServerHost([loggerFile, anotherFile, tsconfig, libFile, tsconfig]);
const session = createSession(host, { canUseEvents: true, logger: projectSystem.createLoggerWritingToConsole() });
session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
openFiles: [{
file: loggerFile.path,
fileContent: loggerFile.content,
projectRootPath: projectRoot
}]
}
});
const service = session.getProjectService();
checkNumberOfProjects(service, { configuredProjects: 1 });
const project = service.configuredProjects.get(tsconfig.path)!;
checkProjectActualFiles(project, [loggerFile.path, anotherFile.path, libFile.path, tsconfig.path]);
verifyGetErrRequest({
host,
session,
expected: [
{ file: loggerFile.path, syntax: [], semantic: [], suggestion: [] }
]
});

const newLoggerPath = loggerFile.path.toLowerCase();
host.renameFile(loggerFile.path, newLoggerPath);
session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
closedFiles: [
loggerFile.path
]
}
});
session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
openFiles: [{
file: newLoggerPath,
fileContent: loggerFile.content,
projectRootPath: projectRoot
}]
}
});

// Apply edits for rename
session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
openFiles: [{
file: anotherFile.path,
fileContent: anotherFile.content,
projectRootPath: projectRoot
}]
}
});
session.executeCommandSeq<protocol.UpdateOpenRequest>({
command: protocol.CommandTypes.UpdateOpen,
arguments: {
changedFiles: [{
fileName: anotherFile.path,
textChanges: [{
newText: "./logger",
...projectSystem.protocolTextSpanFromSubstring(
anotherFile.content,
"./Logger"
)
}]
}]
}
});

// Check errors in both files
verifyGetErrRequest({
host,
session,
expected: [
{ file: newLoggerPath, syntax: [], semantic: [], suggestion: [] },
{ file: anotherFile.path, syntax: [], semantic: [], suggestion: [] }
]
});
});
});
}

0 comments on commit 7f77d24

Please sign in to comment.