From 828604cbc68eb3fa6486b05627fbce6e1decb8a2 Mon Sep 17 00:00:00 2001 From: muravvv <50175509+muravvv@users.noreply.github.com> Date: Sun, 11 May 2025 23:06:44 +0300 Subject: [PATCH] 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) --- packages/config-yaml/src/registryClient.test.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/config-yaml/src/registryClient.test.ts b/packages/config-yaml/src/registryClient.test.ts index d820579ab..bff0b9e84 100644 --- a/packages/config-yaml/src/registryClient.test.ts +++ b/packages/config-yaml/src/registryClient.test.ts @@ -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 });