Skip to content

Commit

Permalink
Add tests for determinePackageManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilzu committed Jul 22, 2024
1 parent 066e3f5 commit f43b06d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand Down
30 changes: 28 additions & 2 deletions src/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
});
});

0 comments on commit f43b06d

Please sign in to comment.