From 93c2b10f6861b52a96444375a5025d9c81eb8209 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 18 Oct 2017 15:29:11 -0700 Subject: [PATCH] Fix tsc-instrumented 1. Make recursiveCreateDirectory correctly handle relative paths. 2. Remove dependency on Harness 3. Correctly increment iocapture0, iocapture1, ... iocaptureN. 4. Stop double-nesting baseline files. --- src/compiler/sys.ts | 3 +-- src/harness/loggedIO.ts | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index fa3ec22f58..c68292078c 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -570,10 +570,9 @@ namespace ts { realpath }; } - function recursiveCreateDirectory(directoryPath: string, sys: System) { const basePath = getDirectoryPath(directoryPath); - const shouldCreateParent = directoryPath !== basePath && !sys.directoryExists(basePath); + const shouldCreateParent = basePath !== "" && directoryPath !== basePath && !sys.directoryExists(basePath); if (shouldCreateParent) { recursiveCreateDirectory(basePath, sys); } diff --git a/src/harness/loggedIO.ts b/src/harness/loggedIO.ts index 3123024f41..ecdcc1a740 100644 --- a/src/harness/loggedIO.ts +++ b/src/harness/loggedIO.ts @@ -157,11 +157,20 @@ namespace Playback { return log; } + const canonicalizeForHarness = ts.createGetCanonicalFileName(/*caseSensitive*/ false); // This is done so tests work on windows _and_ linux + function sanitizeTestFilePath(name: string) { + const path = ts.toPath(ts.normalizeSlashes(name.replace(/[\^<>:"|?*%]/g, "_")).replace(/\.\.\//g, "__dotdot/"), "", canonicalizeForHarness); + if (ts.startsWith(path, "/")) { + return path.substring(1); + } + return path; + } + export function oldStyleLogIntoNewStyleLog(log: IOLog, writeFile: typeof Harness.IO.writeFile, baseTestName: string) { if (log.filesAppended) { for (const file of log.filesAppended) { if (file.contents !== undefined) { - file.contentsPath = ts.combinePaths("appended", Harness.Compiler.sanitizeTestFilePath(file.path)); + file.contentsPath = ts.combinePaths("appended", sanitizeTestFilePath(file.path)); writeFile(ts.combinePaths(baseTestName, file.contentsPath), file.contents); delete file.contents; } @@ -170,7 +179,7 @@ namespace Playback { if (log.filesWritten) { for (const file of log.filesWritten) { if (file.contents !== undefined) { - file.contentsPath = ts.combinePaths("written", Harness.Compiler.sanitizeTestFilePath(file.path)); + file.contentsPath = ts.combinePaths("written", sanitizeTestFilePath(file.path)); writeFile(ts.combinePaths(baseTestName, file.contentsPath), file.contents); delete file.contents; } @@ -180,7 +189,7 @@ namespace Playback { for (const file of log.filesRead) { const { contents } = file.result; if (contents !== undefined) { - file.result.contentsPath = ts.combinePaths("read", Harness.Compiler.sanitizeTestFilePath(file.path)); + file.result.contentsPath = ts.combinePaths("read", sanitizeTestFilePath(file.path)); writeFile(ts.combinePaths(baseTestName, file.result.contentsPath), contents); const len = contents.length; if (len >= 2 && contents.charCodeAt(0) === 0xfeff) { @@ -235,8 +244,8 @@ namespace Playback { if (recordLog !== undefined) { let i = 0; const fn = () => recordLogFileNameBase + i; - while (underlying.fileExists(fn() + ".json")) i++; - underlying.writeFile(ts.combinePaths(fn(), "test.json"), JSON.stringify(oldStyleLogIntoNewStyleLog(recordLog, (path, string) => underlying.writeFile(ts.combinePaths(fn(), path), string), fn()), null, 4)); // tslint:disable-line:no-null-keyword + while (underlying.fileExists(ts.combinePaths(fn(), "test.json"))) i++; + underlying.writeFile(ts.combinePaths(fn(), "test.json"), JSON.stringify(oldStyleLogIntoNewStyleLog(recordLog, (path, string) => underlying.writeFile(path, string), fn()), null, 4)); // tslint:disable-line:no-null-keyword recordLog = undefined; } }; @@ -415,4 +424,4 @@ namespace Playback { initWrapper(wrapper, underlying); return wrapper; } -} \ No newline at end of file +}