-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.test.ts
More file actions
160 lines (126 loc) · 4.36 KB
/
utils.test.ts
File metadata and controls
160 lines (126 loc) · 4.36 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
import { describe, it, expect, beforeEach, afterEach, vi } from "@voidzero-dev/vite-plus/test";
import { existsSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { detectLockFile } from "./utils.js";
import { LockFileType } from "./types.js";
// Mock fs module
vi.mock("node:fs", async () => {
const actual = await vi.importActual<typeof import("node:fs")>("node:fs");
return {
...actual,
existsSync: vi.fn(),
readdirSync: vi.fn(),
};
});
describe("detectLockFile", () => {
const mockWorkspace = "/test/workspace";
beforeEach(() => {
vi.stubEnv("GITHUB_WORKSPACE", mockWorkspace);
});
afterEach(() => {
vi.unstubAllEnvs();
vi.resetAllMocks();
});
describe("with explicit path", () => {
it("should return lock file info for pnpm-lock.yaml", () => {
vi.mocked(existsSync).mockReturnValue(true);
const result = detectLockFile("pnpm-lock.yaml");
expect(result).toEqual({
type: LockFileType.Pnpm,
path: join(mockWorkspace, "pnpm-lock.yaml"),
filename: "pnpm-lock.yaml",
});
});
it("should return lock file info for package-lock.json", () => {
vi.mocked(existsSync).mockReturnValue(true);
const result = detectLockFile("package-lock.json");
expect(result).toEqual({
type: LockFileType.Npm,
path: join(mockWorkspace, "package-lock.json"),
filename: "package-lock.json",
});
});
it("should return lock file info for yarn.lock", () => {
vi.mocked(existsSync).mockReturnValue(true);
const result = detectLockFile("yarn.lock");
expect(result).toEqual({
type: LockFileType.Yarn,
path: join(mockWorkspace, "yarn.lock"),
filename: "yarn.lock",
});
});
it("should return undefined if explicit file does not exist", () => {
vi.mocked(existsSync).mockReturnValue(false);
const result = detectLockFile("pnpm-lock.yaml");
expect(result).toBeUndefined();
});
it("should handle absolute paths", () => {
vi.mocked(existsSync).mockReturnValue(true);
const absolutePath = "/custom/path/pnpm-lock.yaml";
const result = detectLockFile(absolutePath);
expect(result).toEqual({
type: LockFileType.Pnpm,
path: absolutePath,
filename: "pnpm-lock.yaml",
});
});
});
describe("auto-detection", () => {
it("should detect pnpm-lock.yaml first (highest priority)", () => {
vi.mocked(readdirSync).mockReturnValue([
"package-lock.json",
"pnpm-lock.yaml",
"yarn.lock",
] as unknown as ReturnType<typeof readdirSync>);
const result = detectLockFile();
expect(result).toEqual({
type: LockFileType.Pnpm,
path: join(mockWorkspace, "pnpm-lock.yaml"),
filename: "pnpm-lock.yaml",
});
});
it("should detect package-lock.json when pnpm-lock.yaml is absent", () => {
vi.mocked(readdirSync).mockReturnValue([
"package-lock.json",
"yarn.lock",
] as unknown as ReturnType<typeof readdirSync>);
const result = detectLockFile();
expect(result).toEqual({
type: LockFileType.Npm,
path: join(mockWorkspace, "package-lock.json"),
filename: "package-lock.json",
});
});
it("should detect npm-shrinkwrap.json", () => {
vi.mocked(readdirSync).mockReturnValue(["npm-shrinkwrap.json"] as unknown as ReturnType<
typeof readdirSync
>);
const result = detectLockFile();
expect(result).toEqual({
type: LockFileType.Npm,
path: join(mockWorkspace, "npm-shrinkwrap.json"),
filename: "npm-shrinkwrap.json",
});
});
it("should detect yarn.lock when higher priority files are absent", () => {
vi.mocked(readdirSync).mockReturnValue(["yarn.lock"] as unknown as ReturnType<
typeof readdirSync
>);
const result = detectLockFile();
expect(result).toEqual({
type: LockFileType.Yarn,
path: join(mockWorkspace, "yarn.lock"),
filename: "yarn.lock",
});
});
it("should return undefined when no lock files found", () => {
vi.mocked(readdirSync).mockReturnValue([
"package.json",
"src",
"README.md",
] as unknown as ReturnType<typeof readdirSync>);
const result = detectLockFile();
expect(result).toBeUndefined();
});
});
});