Skip to content

Commit

Permalink
Fixed for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
yacinehmito committed Apr 25, 2023
1 parent 5f6b67d commit 2447e2b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text=auto
/tests/__snapshots__/** text eol=lf
/testdata/** text eol=lf

Large diffs are not rendered by default.

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
parse,
relative,
resolve,
SEP,
toFileUrl,
} from "https://deno.land/std@0.182.0/path/mod.ts";
import {
ensureFileSync,
Expand Down Expand Up @@ -30,10 +32,6 @@ const updateQueue = createUpdateQueue();
type TranspileResult = Awaited<ReturnType<typeof emit>>;
type BundleResult = Awaited<ReturnType<typeof bundle>>;

const osType = typeof Deno?.build?.os === "string" ? Deno.build.os : "linux";

const EOL = osType === "windows" ? "\r\n" : "\n";

/**
* Calls `emit` with the provided parameters and checks that the output is
* consistent with the snapshots.
Expand Down Expand Up @@ -85,7 +83,7 @@ export function testTranspile(

await assertSnapshot(
resolve(testDir, "mapping.json"),
JSON.stringify(mapping, null, 2) + EOL,
JSON.stringify(mapping, null, 2) + "\n",
snapshotMode,
);
await assertSnapshots(
Expand Down Expand Up @@ -145,11 +143,11 @@ export function testBundle(
/**
* Provides the full path of a fixture file stored in "testdata".
*
* @param path Path relative to the folder with the fixtures.
* @param parts Path relative to the folder with the fixtures.
* @returns the full path of the fixture.
*/
export function resolveFixture(path: string): string {
return resolve(Deno.cwd(), "testdata", path);
export function resolveFixture(...parts: string[]): string {
return resolve(Deno.cwd(), "testdata", ...parts);
}

async function assertSnapshot(
Expand Down Expand Up @@ -184,7 +182,7 @@ async function assertSnapshot(
);
const diffMsg = buildMessage(diffResult);
throw new AssertionError(
`Snapshot at ${relativePath} does not match:${EOL}${diffMsg}`,
`Snapshot at ${relativePath} does not match:\n${diffMsg}`,
);
}
}
Expand Down Expand Up @@ -314,7 +312,11 @@ function applyUpdate(path: string, actual: string | undefined): void {
function normalizeIfFileUrl(urlString: string): string {
const url = new URL(urlString);
if (url.protocol === "file:") {
url.pathname = relative(Deno.cwd(), url.pathname);
const path = fromFileUrl(url);
// We prepend with the separator instead of using `resolve()` because, on
// Windows, this adds the device prefix (e.g. `C:`), which we don't want.
const normalizedPath = SEP + relative(Deno.cwd(), path);
return toFileUrl(normalizedPath).toString();
}
return url.toString();
}
Expand Down Expand Up @@ -346,7 +348,7 @@ function fixSourceMap(sourceMapJsonString: string): string {
}

function fixInlineSourceMap(code: string): string {
const lines = code.split(EOL);
const lines = code.split("\n");

const indexOfLastLine = lines.findLastIndex((line) => line !== "");
const match = lines[indexOfLastLine]?.match(inlineSourceMapRegex);
Expand All @@ -362,7 +364,7 @@ function fixInlineSourceMap(code: string): string {
lines[indexOfLastLine] =
`//# sourceMappingURL=data:application/json;base64,${newSourceMapBase64}`;

return lines.join(EOL);
return lines.join("\n");
}

async function hashShortSha1(input: string): Promise<string> {
Expand Down

0 comments on commit 2447e2b

Please sign in to comment.