Skip to content

Commit d1c7cc5

Browse files
authored
test: Enhance CLI tests for output directory handling and add downloa… (#65)
* test: Enhance CLI tests for output directory handling and add downloadFiles tests * lint * chore: Update test:ci script to include experimental test module mocks * fix: download-files.test.js
1 parent b0ee40a commit d1c7cc5

3 files changed

Lines changed: 99 additions & 10 deletions

File tree

cli/test/cli.test.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-nocheck
2-
import * as assert from 'node:assert';
2+
import assert from 'node:assert/strict';
33
import { execFile } from 'node:child_process';
44
import { existsSync } from 'node:fs';
55
import * as fs from 'node:fs/promises';
@@ -11,11 +11,9 @@ import { projectRoot } from '../index.mjs';
1111
const execFileAsync = promisify(execFile);
1212
describe('CLI', () => {
1313
describe('default', () => {
14-
test('should accept default command with no flags and download the cursorrules', async () => {
14+
test('should accept default command with no flags', async () => {
1515
const { stdout, stderr } = await execFileAsync('node', ['./cli/index.mjs']);
16-
if (existsSync(path.join(projectRoot, 'output', '.cursor/rules'))) {
17-
assert.ok(stdout.includes('Success'));
18-
}
16+
assert.ok(stdout.includes('Loading @usrrname/cursorrules ...'));
1917
});
2018
after(async () => {
2119
try {
@@ -52,6 +50,14 @@ describe('CLI', () => {
5250

5351
describe('output', () => {
5452

53+
test('should accept --output flag for output directory and download the cursorrules', async () => {
54+
const { stdout, stderr } = await execFileAsync('node', ['./cli/index.mjs', '--output', 'output']);
55+
56+
if (existsSync(path.join(projectRoot, 'output'))) {
57+
assert.ok(stdout.includes('Success'));
58+
}
59+
})
60+
5561
test('should accept -o flag for output directory and download the cursorrules', async () => {
5662
const { stdout, stderr } = await execFileAsync('node', ['./cli/index.mjs', '-o', 'output']);
5763

@@ -105,22 +111,24 @@ describe('CLI', () => {
105111
console.error(`Error: ${err}`);
106112
}
107113
})
108-
describe('output directory Validation', () => {
114+
115+
describe('output directory validation', () => {
109116
const testBaseDir = path.join(projectRoot, 'test-output-validation');
110117

111-
beforeEach(async () => {
118+
beforeEach(async (t) => {
112119
await fs.mkdir(testBaseDir, { recursive: true });
113120
});
114121

115-
afterEach(async () => {
122+
afterEach(async (t) => {
116123
if (existsSync(testBaseDir)) {
117124
await fs.rm(testBaseDir, { recursive: true, force: true });
118125
}
119126
});
120127

121-
test('should accept a valid output directory name within project root', async () => {
128+
test('should accept a valid output directory name within project root', async (t) => {
122129
const validDir = testBaseDir;
123130
const { stdout } = await execFileAsync('node', ['./cli/index.mjs', '-o', validDir]);
131+
124132
assert.ok(stdout.includes('Success'));
125133
assert.ok(existsSync(validDir));
126134
});

cli/test/download-files.test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// @ts-nocheck
2+
import { strict as assert } from 'node:assert';
3+
import { execFile } from 'node:child_process';
4+
import { existsSync } from 'node:fs';
5+
import * as fs from 'node:fs/promises';
6+
import * as path from 'node:path';
7+
import { afterEach, before, describe, mock, test } from 'node:test';
8+
import { promisify } from 'node:util';
9+
import { projectRoot } from '../index.mjs';
10+
const execFileAsync = promisify(execFile);
11+
describe('downloadFiles', () => {
12+
let downloadFilesMock;
13+
let downloadSelectedFilesMock;
14+
let validDir = '';
15+
16+
before(async (t) => {
17+
const downloadFilesModule = await import('../utils/download-files.mjs');
18+
19+
downloadFilesMock = mock.fn((...args) => downloadFilesModule.downloadFiles(...args));
20+
downloadSelectedFilesMock = mock.fn((...args) => downloadFilesModule.downloadSelectedFiles(...args));
21+
22+
mock?.module?.('../utils/download-files.mjs', () => {
23+
return {
24+
cache: true,
25+
downloadFiles: downloadFilesMock,
26+
downloadSelectedFiles: downloadSelectedFilesMock,
27+
}
28+
});
29+
validDir = path.join(projectRoot, 'test-download-files');
30+
});
31+
32+
afterEach(async (t) => {
33+
if (existsSync(validDir)) {
34+
await fs.rm(validDir, { recursive: true, force: true });
35+
}
36+
});
37+
38+
test('should call downloadFiles with valid directory', async (t) => {
39+
try {
40+
41+
const { stdout } = await execFileAsync('node', ['./cli/index.mjs', '-o', validDir]);
42+
assert.ok(stdout.includes('Downloading'));
43+
44+
assert.call(downloadFilesMock.mockImplementationOnce((validDir) => {
45+
assert.ok(existsSync(validDir));
46+
assert.strictEqual(t.error?.code, 1)
47+
}));
48+
49+
50+
} catch (error) {
51+
52+
}
53+
})
54+
55+
test('should call downloadSelectedFiles with valid directory', async (t) => {
56+
57+
try {
58+
const mockSelectedFiles = {
59+
'standards': [
60+
'test-rule-1.mdc',
61+
'test-rule-2.mdc',
62+
],
63+
}
64+
65+
const { stdout } = await execFileAsync('node', ['./cli/index.mjs', '-io', validDir]);
66+
assert.call(downloadSelectedFilesMock?.mockImplementationOnce((validDir, mockSelectedFiles) => {
67+
assert.strictEqual(validDir, validDir);
68+
assert.strictEqual(mockSelectedFiles, mockSelectedFiles);
69+
}));
70+
71+
72+
assert.ok(stdout.includes('Copied'));
73+
assert.ok(existsSync(validDir));
74+
assert.equal(downloadSelectedFilesMock?.mock.calls?.length, 1);
75+
76+
} catch (error) {
77+
assert.strictEqual(error.code, 1);
78+
}
79+
80+
})
81+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"clear": "npm unpublish --registry http://localhost:4873/ --scope=@usrrname",
2323
"lint": "tsc --noEmit --project tsconfig.json",
2424
"test": "node --test --watch",
25-
"test:ci": "node --test cli/test/*"
25+
"test:ci": "node --test cli/test/* --experimental-test-module-mocks"
2626
},
2727
"repository": {
2828
"type": "git",

0 commit comments

Comments
 (0)