From f43b06d2f7e3407e2a658b0cb5e1f7a42d3260f9 Mon Sep 17 00:00:00 2001 From: Santeri Hiltunen Date: Mon, 22 Jul 2024 16:22:58 +0300 Subject: [PATCH] Add tests for determinePackageManager --- package.json | 2 +- src/main.mjs | 4 ++-- src/test.mjs | 30 ++++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 9377536..6acd2f5 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "test": "concurrently --group npm:test:*", "test:format": "prettier --check .", "test:lint": "eslint .", - "test:node": "node --test", + "test:node": "node --test --test-reporter spec", "update-template-deps": "(cd template && npx npm-check-updates@latest --upgrade)" }, "dependencies": { diff --git a/src/main.mjs b/src/main.mjs index 7aba6a6..e21931e 100644 --- a/src/main.mjs +++ b/src/main.mjs @@ -19,7 +19,7 @@ const templatePath = pathJoin(__dirname, "..", "template"); debug("__dirname", __dirname); debug("templatePath", templatePath); -export const derivePackageManager = () => { +export const determinePackageManager = () => { const npmUserAgent = env.npm_config_user_agent; debug("npmUserAgent", npmUserAgent); const npmExecPath = env.npm_execpath; @@ -79,7 +79,7 @@ export const create = async () => { const { projectName, projectPath } = deriveProjectNameAndPath(argv[2]); debug("projectName", projectName); debug("projectPath", projectPath); - const packageManagerType = derivePackageManager(); + const packageManagerType = determinePackageManager(); debug("packageManagerType", packageManagerType); const packageManagerRun = packageManagerType === "npm" ? "npm run" diff --git a/src/test.mjs b/src/test.mjs index 981881e..7a784be 100644 --- a/src/test.mjs +++ b/src/test.mjs @@ -2,8 +2,8 @@ import test from "node:test"; import assert from "node:assert/strict"; import { tmpdir } from "node:os"; import { join as pathJoin } from "node:path"; - -import { deriveProjectNameAndPath } from "./main.mjs"; +import { env } from "node:process"; +import { deriveProjectNameAndPath, determinePackageManager } from "./main.mjs"; test("deriveProjectNameAndPath", async (t) => { const dir = tmpdir(); @@ -42,3 +42,29 @@ test("deriveProjectNameAndPath", async (t) => { assert.equal(projectPath, pathJoin(dir, "test")); }); }); + +test("determinePackageManager", async (t) => { + let npmUserAgent; + t.beforeEach(() => { + npmUserAgent = env.npm_config_user_agent; + }); + t.afterEach(() => { + env.npm_config_user_agent = npmUserAgent; + }); + + await t.test("with npm", (_t) => { + env.npm_config_user_agent = + "npm/10.7.0 node/v18.20.4 linux x64 workspaces/false ci/github-actions"; + assert.equal(determinePackageManager(), "npm"); + }); + + await t.test("with yarn", (_t) => { + env.npm_config_user_agent = "yarn/1.22.22 npm/? node/v20.15.1 darwin arm64"; + assert.equal(determinePackageManager(), "yarn"); + }); + + await t.test("with pnpm", (_t) => { + env.npm_config_user_agent = "pnpm/9.6.0 npm/? node/v22.5.1 win32 x64"; + assert.equal(determinePackageManager(), "pnpm"); + }); +});