add test for file:// urls

url generation code duplicates localPathToUri (which mostly used to create file urls in the rest code), except URI.normalize to avoid adding uri-js package to test environment (and because in general getContent should work with any valid file url)
This commit is contained in:
muravvv 2025-05-11 23:06:44 +03:00
parent 9e5e3f293b
commit 828604cbc6

View file

@ -3,6 +3,7 @@ import * as http from "node:http";
import { AddressInfo } from "node:net";
import * as os from "node:os";
import * as path from "node:path";
import { pathToFileURL } from "url";
import { PackageIdentifier } from "./interfaces/slugs.js";
import { RegistryClient } from "./registryClient.js";
@ -134,6 +135,7 @@ describe("RegistryClient", () => {
describe("getContentFromFilePath", () => {
let absoluteFilePath: string;
let fileUrl: string;
let relativeFilePath: string;
beforeEach(() => {
@ -141,6 +143,11 @@ describe("RegistryClient", () => {
absoluteFilePath = path.join(tempDir, "absolute-path.yaml");
fs.writeFileSync(absoluteFilePath, "absolute file content", "utf8");
const urlFilePath = path.join(tempDir, "file-url-path.yaml");
fs.writeFileSync(urlFilePath, "file:// file content", "utf8");
const url = pathToFileURL(urlFilePath);
fileUrl = url.toString();
// Create a subdirectory and file in the temp directory
const subDir = path.join(tempDir, "sub");
fs.mkdirSync(subDir, { recursive: true });
@ -160,6 +167,14 @@ describe("RegistryClient", () => {
expect(result).toBe("absolute file content");
});
it("should read from local file url directly", () => {
const client = new RegistryClient();
const result = (client as any).getContentFromFilePath(fileUrl);
expect(result).toBe("file:// file content");
});
it("should use rootPath for relative paths when provided", () => {
const client = new RegistryClient({ rootPath: tempDir });