Skip to content

Commit

Permalink
Update tests for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesrosiers committed Aug 7, 2024
1 parent d0c7612 commit 1432d7f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
7 changes: 4 additions & 3 deletions lib/uri-schemes/file-scheme-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createReadStream } from "node:fs";
import { readlink, lstat } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { fileURLToPath, pathToFileURL } from "node:url";
import { parseIri, toAbsoluteIri } from "@hyperjump/uri";
import { getFileMediaType } from "../media-types/media-types.js";

Expand All @@ -19,12 +19,13 @@ const retrieve = async (uri, baseUri) => {
const filePath = fileURLToPath(uri);
const stats = await lstat(filePath);
if (stats.isSymbolicLink()) {
responseUri = "file://" + await readlink(filePath);
responseUri = pathToFileURL(await readlink(filePath)).toString();
}

const contentType = await getFileMediaType(filePath);
const stream = createReadStream(filePath);
const response = new Response(stream, {
headers: { "Content-Type": await getFileMediaType(filePath) }
headers: { "Content-Type": contentType }
});
Object.defineProperty(response, "url", { value: responseUri });

Expand Down
35 changes: 20 additions & 15 deletions lib/uri-schemes/file-scheme-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { mkdir, rm, writeFile, symlink } from "node:fs/promises";
import { cwd } from "node:process";
import { join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { describe, it, beforeEach, afterEach, expect } from "vitest";
import { MockAgent, setGlobalDispatcher } from "undici";
import { get, RetrievalError } from "../index.js";
Expand All @@ -12,14 +14,15 @@ describe("JSON Browser", () => {
describe("get", () => {
describe("`file:` scheme", () => {
const fixtureDirectory = "__test-fixtures__";
const testUri = `file://${cwd()}`;
const testUri = pathToFileURL(cwd()).toString();
const testPath = fileURLToPath(`${testUri}/${fixtureDirectory}`);

beforeEach(async () => {
await mkdir(`${cwd()}/${fixtureDirectory}`);
await mkdir(testPath, { recursive: true });
});

afterEach(async () => {
await rm(`${cwd()}/${fixtureDirectory}`, { recursive: true });
await rm(testPath, { recursive: true, force: true });
});

it("not found", async () => {
Expand All @@ -38,7 +41,7 @@ describe("JSON Browser", () => {
foo: 42
`;

await writeFile(`${cwd()}/${path}`, jref, { flag: "w+" });
await writeFile(fileURLToPath(`${testUri}/${path}`), jref);

try {
await get(path);
Expand Down Expand Up @@ -83,8 +86,8 @@ foo: 42
const fooPath = `${fixtureDirectory}/foo.jref`;
const jref = "{}";

await writeFile(`${cwd()}/${rootPath}`, jref, { flag: "w+" });
await writeFile(`${cwd()}/${fooPath}`, jref, { flag: "w+" });
await writeFile(fileURLToPath(`${testUri}/${rootPath}`), jref);
await writeFile(fileURLToPath(`${testUri}/${fooPath}`), jref);

const root = await get(rootPath);
const browser = await get(`./foo.jref`, root);
Expand All @@ -106,7 +109,7 @@ foo: 42
"bar": { "$ref": "${href}" }
}`;

await writeFile(`${cwd()}/${path}`, jref, { flag: "w+" });
await writeFile(fileURLToPath(`${testUri}/${path}`), jref);

const browser = await get(`${path}#${fragment}`);

Expand All @@ -117,15 +120,16 @@ foo: 42
});

it("full path", async () => {
const path = `${cwd()}/${fixtureDirectory}/foo.jref`;
const path = fileURLToPath(`${testUri}/${fixtureDirectory}/foo.jref`, { windows: false });
const fragment = "/foo";
const href = "#/foo";
const jref = `{
"foo": 42,
"bar": { "$ref": "${href}" }
}`;

await writeFile(path, jref, { flag: "w+" });
const filesystemPath = fileURLToPath(pathToFileURL(path, { windows: false }));
await writeFile(filesystemPath, jref);

const browser = await get(`${path}#${fragment}`);

Expand All @@ -136,15 +140,16 @@ foo: 42
});

it("full URI with authority", async () => {
const path = `${cwd()}/${fixtureDirectory}/foo.jref`;
const path = fileURLToPath(`${testUri}/${fixtureDirectory}/foo.jref`, { windows: false });
const fragment = "/foo";
const href = "#/foo";
const jref = `{
"foo": 42,
"bar": { "$ref": "${href}" }
}`;

await writeFile(path, jref, { flag: "w+" });
const filesystemPath = fileURLToPath(pathToFileURL(path, { windows: false }));
await writeFile(filesystemPath, jref);

const browser = await get(`file://${path}#${fragment}`);

Expand All @@ -155,15 +160,15 @@ foo: 42
});

it("full URI without authority", async () => {
const path = `${cwd()}/${fixtureDirectory}/foo.jref`;
const path = fileURLToPath(`${testUri}/${fixtureDirectory}/foo.jref`);
const fragment = "/foo";
const href = "#/foo";
const jref = `{
"foo": 42,
"bar": { "$ref": "${href}" }
}`;

await writeFile(path, jref, { flag: "w+" });
await writeFile(path, jref);

const browser = await get(`file:${path}#${fragment}`);

Expand All @@ -184,8 +189,8 @@ foo: 42
"bar": { "$ref": "${href}" }
}`;

await writeFile(`${cwd()}/${actualPath}`, jref, { flag: "w+" });
await symlink(`${cwd()}/${actualPath}`, `${cwd()}/${path}`);
await writeFile(fileURLToPath(`${testUri}/${actualPath}`), jref);
await symlink(fileURLToPath(`${testUri}/${actualPath}`), join(cwd(), path));

const browser = await get(`${path}#${fragment}`);

Expand Down

0 comments on commit 1432d7f

Please sign in to comment.