Skip to content

Commit

Permalink
feat: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyreilly committed Nov 10, 2024
1 parent 54c939e commit 427619c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as prompts from "@clack/prompts";
import chalk from "chalk";
import ci from "ci-info";
import path from "node:path";
import { parseArgs } from "node:util";
import { fromZodError } from "zod-validation-error";

Expand Down Expand Up @@ -97,7 +98,9 @@ export async function bin(args: string[]) {
try {
const parsedProjectNpmrc = await withSpinner(`Parsing project .npmrc`, () =>
parseProjectNpmrc({
config,
npmrcPath: config
? path.resolve(config)
: path.resolve(process.cwd(), ".npmrc"),
logger,
}),
);
Expand Down
38 changes: 38 additions & 0 deletions src/parseProjectNpmrc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it, vi } from "vitest";

import { parseProjectNpmrc } from "./parseProjectNpmrc.js";

const mockReadFile = vi.fn();

vi.mock("./shared/readFileSafe.js", () => ({
get readFileSafe() {
return mockReadFile;
},
}));

describe("parseProjectNpmrc", () => {
it("outputs the expected structure on successful parse", async () => {
mockReadFile.mockResolvedValue(`registry=https://pkgs.dev.azure.com/johnnyreilly/_packaging/npmrc-script-organization/npm/registry/
always-auth=true`);
const result = await parseProjectNpmrc({
npmrcPath: "/home/john/code/github/ado-npm-auth-lite/.npmrc",
});
expect(result).toEqual({
organisation: "johnnyreilly",
urlWithoutRegistryAtEnd:
"//pkgs.dev.azure.com/johnnyreilly/_packaging/npmrc-script-organization/npm/",
urlWithoutRegistryAtStart:
"//pkgs.dev.azure.com/johnnyreilly/_packaging/npmrc-script-organization/npm/registry/",
});
});

it("errors on invalid content", async () => {
mockReadFile.mockResolvedValue(`stuff`);
await expect(() =>
parseProjectNpmrc({
npmrcPath: "/home/john/code/github/ado-npm-auth-lite/.npmrc",
}),
).rejects.toThrowError("Unable to extract information from project .npmrc");
});
});
10 changes: 2 additions & 8 deletions src/parseProjectNpmrc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from "node:path";

import { fallbackLogger, type Logger } from "./logger.js";
import { readFileSafe } from "./shared/readFileSafe.js";

Expand All @@ -13,16 +11,12 @@ export interface ParsedProjectNpmrc {
* Read the project .npmrc file to acquire necessary info
*/
export async function parseProjectNpmrc({
config,
npmrcPath,
logger = fallbackLogger,
}: {
config?: string | undefined;
npmrcPath: string;
logger?: Logger;
}): Promise<ParsedProjectNpmrc> {
const npmrcPath = config
? path.resolve(config)
: path.resolve(process.cwd(), ".npmrc");

logger.info(`Loading .npmrc at: ${npmrcPath}`);

const npmrcContents = await readFileSafe(npmrcPath, "");
Expand Down

0 comments on commit 427619c

Please sign in to comment.