-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpm.test.js
More file actions
193 lines (167 loc) · 6.16 KB
/
pm.test.js
File metadata and controls
193 lines (167 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const { execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const os = require("os");
const CLI_PATH = path.join(__dirname, "pm.js");
// Helper to run the CLI
function runCLI(args = "", options = {}) {
const cmd = `node "${CLI_PATH}" ${args}`;
try {
const output = execSync(cmd, {
encoding: "utf-8",
cwd: options.cwd || __dirname,
env: { ...process.env, ...options.env },
});
return { stdout: output, exitCode: 0 };
} catch (error) {
return {
stdout: error.stdout || "",
stderr: error.stderr || "",
exitCode: error.status,
};
}
}
// Helper to create a temp directory with specific files
function createTempProject(files = {}) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pm-test-"));
for (const [filename, content] of Object.entries(files)) {
fs.writeFileSync(path.join(tmpDir, filename), content);
}
return tmpDir;
}
// Helper to clean up temp directory
function cleanupTempDir(dir) {
if (dir && dir.startsWith(os.tmpdir())) {
fs.rmSync(dir, { recursive: true, force: true });
}
}
describe("pm CLI", () => {
describe("--version flag", () => {
test("shows version with --version", () => {
const result = runCLI("--version");
expect(result.exitCode).toBe(0);
expect(result.stdout).toMatch(/pm version \d+\.\d+\.\d+/);
});
test("shows version with -v", () => {
const result = runCLI("-v");
expect(result.exitCode).toBe(0);
expect(result.stdout).toMatch(/pm version \d+\.\d+\.\d+/);
});
});
describe("--help flag", () => {
test("shows help with --help", () => {
const result = runCLI("--help");
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("pm - Universal package manager CLI");
expect(result.stdout).toContain("Usage:");
expect(result.stdout).toContain("Options:");
expect(result.stdout).toContain("Examples:");
});
test("shows help with -h", () => {
const result = runCLI("-h");
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("pm - Universal package manager CLI");
});
});
describe("--verbose flag", () => {
test("shows detected package manager with --verbose", () => {
const result = runCLI("--verbose --version");
// When --version is present, it exits early before verbose
// so let's test verbose with a different scenario
expect(result.exitCode).toBe(0);
});
});
describe("package manager detection", () => {
let tmpDir;
afterEach(() => {
if (tmpDir) {
cleanupTempDir(tmpDir);
tmpDir = null;
}
});
test("detects npm from package-lock.json", () => {
tmpDir = createTempProject({
"package.json": JSON.stringify({ name: "test", version: "1.0.0" }),
"package-lock.json": JSON.stringify({ lockfileVersion: 3 }),
});
const result = runCLI("--verbose --help", { cwd: tmpDir });
expect(result.stdout).toContain("Detected package manager: npm");
});
test("detects yarn from yarn.lock", () => {
tmpDir = createTempProject({
"package.json": JSON.stringify({ name: "test", version: "1.0.0" }),
"yarn.lock": "# yarn lockfile v1",
});
const result = runCLI("--verbose --help", { cwd: tmpDir });
expect(result.stdout).toContain("Detected package manager: yarn");
});
test("detects pnpm from pnpm-lock.yaml", () => {
tmpDir = createTempProject({
"package.json": JSON.stringify({ name: "test", version: "1.0.0" }),
"pnpm-lock.yaml": "lockfileVersion: 5.4",
});
const result = runCLI("--verbose --help", { cwd: tmpDir });
expect(result.stdout).toContain("Detected package manager: pnpm");
});
test("detects bun from bun.lockb", () => {
tmpDir = createTempProject({
"package.json": JSON.stringify({ name: "test", version: "1.0.0" }),
"bun.lockb": "", // Empty file is enough for detection
});
const result = runCLI("--verbose --help", { cwd: tmpDir });
expect(result.stdout).toContain("Detected package manager: bun");
});
});
describe("argument passing", () => {
test("passes arguments correctly to package manager", () => {
// Test that arguments are preserved (using --help which should work with any PM)
const result = runCLI("--help install lodash");
expect(result.exitCode).toBe(0);
// The help text should be shown, confirming args were processed
expect(result.stdout).toContain("pm - Universal package manager CLI");
});
test("filters out --verbose from passed arguments", () => {
const result = runCLI("--verbose --help");
expect(result.exitCode).toBe(0);
// Should show verbose output AND help
expect(result.stdout).toContain("Detected package manager:");
expect(result.stdout).toContain("pm - Universal package manager CLI");
});
});
describe("error handling", () => {
test("handles invalid commands gracefully", () => {
// Create a temp project to ensure detection works
const tmpDir = createTempProject({
"package.json": JSON.stringify({ name: "test", version: "1.0.0" }),
"package-lock.json": JSON.stringify({ lockfileVersion: 3 }),
});
try {
// Use a command that npm will fail on
const result = runCLI("this-is-not-a-valid-command-12345", {
cwd: tmpDir,
});
// Should have non-zero exit code
expect(result.exitCode).not.toBe(0);
} finally {
cleanupTempDir(tmpDir);
}
});
});
describe("version consistency", () => {
test("CLI version matches package.json version", () => {
const pkg = require("./package.json");
const result = runCLI("--version");
expect(result.stdout).toContain(`pm version ${pkg.version}`);
});
});
});
describe("pm.js module structure", () => {
test("file exists and is executable", () => {
const stats = fs.statSync(CLI_PATH);
expect(stats.isFile()).toBe(true);
});
test("has proper shebang", () => {
const content = fs.readFileSync(CLI_PATH, "utf-8");
expect(content.startsWith("#!/usr/bin/env node")).toBe(true);
});
});