-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.test.ts
More file actions
414 lines (368 loc) · 11.6 KB
/
tools.test.ts
File metadata and controls
414 lines (368 loc) · 11.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { Command } from "commander";
import { registerToolsCommand } from "./tools";
import * as fs from "fs";
import { AnalysisService } from "../api/client/services/AnalysisService";
import { ToolsService } from "../api/client/services/ToolsService";
import { CodingStandardsService } from "../api/client/services/CodingStandardsService";
import * as importConfig from "../utils/import-config";
import * as prompt from "../utils/prompt";
vi.mock("../api/client/services/AnalysisService");
vi.mock("../api/client/services/CodingStandardsService");
vi.mock("../api/client/services/ToolsService");
vi.mock("../utils/credentials", () => ({ loadCredentials: vi.fn(() => null) }));
vi.spyOn(console, "log").mockImplementation(() => {});
vi.spyOn(console, "error").mockImplementation(() => {});
function createProgram(): Command {
const program = new Command();
program.option("-o, --output <format>", "output format", "table");
registerToolsCommand(program);
return program;
}
const mockTools = [
{
uuid: "uuid-eslint",
name: "ESLint",
isClientSide: false,
settings: {
isEnabled: true,
followsStandard: false,
isCustom: false,
hasConfigurationFile: true,
usesConfigurationFile: true,
enabledBy: [{ name: "OWASP Top 10" }],
},
},
{
uuid: "uuid-semgrep",
name: "Semgrep",
isClientSide: false,
settings: {
isEnabled: true,
followsStandard: false,
isCustom: false,
hasConfigurationFile: false,
usesConfigurationFile: false,
enabledBy: [{ name: "OWASP Top 10" }],
},
},
{
uuid: "uuid-trivy",
name: "Trivy",
isClientSide: true,
settings: {
isEnabled: false,
followsStandard: false,
isCustom: false,
hasConfigurationFile: false,
usesConfigurationFile: false,
enabledBy: [],
},
},
];
function getAllOutput(): string {
return (console.log as ReturnType<typeof vi.fn>).mock.calls
.map((c) => c[0])
.join("\n");
}
describe("tools command", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.CODACY_API_TOKEN = "test-token";
vi.mocked(AnalysisService.listRepositoryTools).mockResolvedValue({
data: mockTools,
pagination: undefined,
} as any);
});
it("should list enabled and disabled tool groups", async () => {
const program = createProgram();
await program.parseAsync([
"node",
"test",
"tools",
"gh",
"test-org",
"test-repo",
]);
expect(AnalysisService.listRepositoryTools).toHaveBeenCalledWith(
"gh",
"test-org",
"test-repo",
);
const output = getAllOutput();
expect(output).toContain("Enabled tools");
expect(output).toContain("Disabled tools");
expect(output).toContain("ESLint");
expect(output).toContain("Semgrep");
expect(output).toContain("Trivy");
});
it("should show config file status correctly", async () => {
const program = createProgram();
await program.parseAsync([
"node",
"test",
"tools",
"gh",
"test-org",
"test-repo",
]);
const output = getAllOutput();
// ESLint uses a config file → Applied
expect(output).toContain("Applied");
// ESLint uses a config file → Applied; Semgrep has no config file → "—" (dim dash)
expect(output).not.toContain("Not Available");
});
it("should show coding standards in Via Standard column", async () => {
const program = createProgram();
await program.parseAsync([
"node",
"test",
"tools",
"gh",
"test-org",
"test-repo",
]);
const output = getAllOutput();
expect(output).toContain("OWASP Top 10");
});
it("should show client-side note for client-side tools", async () => {
const program = createProgram();
await program.parseAsync([
"node",
"test",
"tools",
"gh",
"test-org",
"test-repo",
]);
const output = getAllOutput();
expect(output).toContain("Client-side tool");
});
it("should output JSON when --output json is specified", async () => {
const program = createProgram();
await program.parseAsync([
"node",
"test",
"--output",
"json",
"tools",
"gh",
"test-org",
"test-repo",
]);
const output = getAllOutput();
expect(output).toContain('"ESLint"');
expect(output).toContain('"uuid-eslint"');
});
it("should show 'Overwritten by file' in Via Standard when config file is applied", async () => {
const program = createProgram();
await program.parseAsync([
"node",
"test",
"tools",
"gh",
"test-org",
"test-repo",
]);
const output = getAllOutput();
// ESLint uses a config file → Via Standard shows "Overwritten by file"
expect(output).toContain("Overwritten by file");
});
it("should show 'Available' for tool with config file not applied", async () => {
vi.mocked(AnalysisService.listRepositoryTools).mockResolvedValue({
data: [
{
uuid: "uuid-pylint",
name: "Pylint",
isClientSide: false,
settings: {
isEnabled: true,
followsStandard: false,
isCustom: false,
hasConfigurationFile: true,
usesConfigurationFile: false,
enabledBy: [],
},
},
],
pagination: undefined,
} as any);
const program = createProgram();
await program.parseAsync([
"node",
"test",
"tools",
"gh",
"test-org",
"test-repo",
]);
const output = getAllOutput();
expect(output).toContain("Available");
});
it("should fail when CODACY_API_TOKEN is not set", async () => {
delete process.env.CODACY_API_TOKEN;
const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit called");
});
const program = createProgram();
await expect(
program.parseAsync([
"node",
"test",
"tools",
"gh",
"test-org",
"test-repo",
]),
).rejects.toThrow("process.exit called");
mockExit.mockRestore();
});
// ─── Import mode ──────────────────────────────────────────────────────
describe("--import", () => {
const configContent = JSON.stringify({
version: 1,
metadata: {
repositoryId: null,
repositoryName: null,
createdAt: "2025-01-01",
updatedAt: "2025-01-01",
languages: ["TypeScript"],
},
tools: [
{
toolId: "ESLint",
patterns: [{ patternId: "no-unused-vars" }],
},
],
});
const tmpConfigPath = "/tmp/test-import-config.json";
beforeEach(() => {
fs.writeFileSync(tmpConfigPath, configContent);
vi.mocked(AnalysisService.updateRepositoryToolPatterns).mockResolvedValue(undefined as any);
vi.mocked(AnalysisService.configureTool).mockResolvedValue(undefined as any);
vi.spyOn(importConfig, "fetchAllTools").mockResolvedValue([
{
uuid: "uuid-eslint",
name: "ESLint",
shortName: "eslint",
prefix: "ESLint_",
version: "1.0",
needsCompilation: false,
configurationFilenames: [],
dockerImage: "docker/eslint",
languages: ["TypeScript"],
clientSide: false,
standalone: false,
enabledByDefault: false,
configurable: true,
},
] as any);
vi.spyOn(importConfig, "getLocalSupportedToolIds").mockResolvedValue(["ESLint"]);
vi.mocked(AnalysisService.getRepositoryWithAnalysis).mockResolvedValue({
data: {
repository: {
provider: "gh",
owner: "test-org",
name: "test-repo",
standards: [],
languages: [],
problems: [],
},
},
} as any);
});
afterEach(() => {
if (fs.existsSync(tmpConfigPath)) fs.unlinkSync(tmpConfigPath);
});
it("should import config with --skip-approval", async () => {
const program = createProgram();
await program.parseAsync([
"node", "test", "tools", "gh", "test-org", "test-repo",
"--import", tmpConfigPath, "-y",
]);
const output = getAllOutput();
expect(output).toContain("imported successfully");
});
it("should cancel import when user declines confirmation", async () => {
vi.spyOn(prompt, "confirmAction").mockResolvedValue(false);
const program = createProgram();
await program.parseAsync([
"node", "test", "tools", "gh", "test-org", "test-repo",
"--import", tmpConfigPath,
]);
const output = getAllOutput();
expect(output).toContain("cancelled");
expect(AnalysisService.configureTool).not.toHaveBeenCalled();
});
it("should warn about coding standards", async () => {
vi.mocked(AnalysisService.getRepositoryWithAnalysis).mockResolvedValue({
data: {
repository: {
provider: "gh",
owner: "test-org",
name: "test-repo",
standards: [{ id: 1, name: "Security" }],
languages: [],
problems: [],
},
},
} as any);
vi.spyOn(prompt, "confirmAction").mockResolvedValue(false);
const program = createProgram();
await program.parseAsync([
"node", "test", "tools", "gh", "test-org", "test-repo",
"--import", tmpConfigPath,
]);
const output = getAllOutput();
expect(output).toContain("Security");
expect(output).toContain("coding standard");
});
it("should unlink coding standards with --force", async () => {
vi.mocked(CodingStandardsService.applyCodingStandardToRepositories).mockResolvedValue({} as any);
vi.mocked(AnalysisService.getRepositoryWithAnalysis).mockResolvedValue({
data: {
repository: {
provider: "gh",
owner: "test-org",
name: "test-repo",
standards: [
{ id: 100, name: "Security" },
{ id: 200, name: "OWASP10" },
],
languages: [],
problems: [],
},
},
} as any);
const program = createProgram();
await program.parseAsync([
"node", "test", "tools", "gh", "test-org", "test-repo",
"--import", tmpConfigPath, "--force", "-y",
]);
// Should unlink both standards
expect(CodingStandardsService.applyCodingStandardToRepositories).toHaveBeenCalledWith(
"gh", "test-org", 100, { link: [], unlink: ["test-repo"] },
);
expect(CodingStandardsService.applyCodingStandardToRepositories).toHaveBeenCalledWith(
"gh", "test-org", 200, { link: [], unlink: ["test-repo"] },
);
const output = getAllOutput();
expect(output).toContain("will stop following");
expect(output).toContain("Security");
expect(output).toContain("OWASP10");
expect(output).toContain("imported successfully");
});
it("should report errors for failing tools", async () => {
vi.mocked(AnalysisService.configureTool).mockRejectedValue(
new Error("Conflict"),
);
const program = createProgram();
await program.parseAsync([
"node", "test", "tools", "gh", "test-org", "test-repo",
"--import", tmpConfigPath, "-y",
]);
const output = getAllOutput();
expect(output).toContain("error");
});
});
});