Skip to content

Commit 10ee02e

Browse files
committed
fix test case
1 parent 2364c6c commit 10ee02e

File tree

4 files changed

+23
-87
lines changed

4 files changed

+23
-87
lines changed

lib/config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const homedir = process.env.HOME || require('node:os').homedir();
22
const path = require('node:path');
33
const fs = require('node:fs');
4+
const chalk = require('chalk');
45
const configDir = path.resolve(homedir, '.config', 'fanyi');
56
const configPath = path.resolve(configDir, '.fanyirc');
67

8+
// 初始化一个带颜色的 chalk 实例
9+
const chalkInstance = new chalk.Instance({ level: 3 });
10+
711
const config = {
812
async load(path = configPath) {
913
const emptyObj = {};
@@ -21,9 +25,14 @@ const config = {
2125
async write(options = {}, path = configPath) {
2226
const defaultOptions = await config.load(path);
2327
const mergedOptions = { ...defaultOptions, ...options };
24-
const content = JSON.stringify(mergedOptions);
28+
const content = JSON.stringify(mergedOptions, null, 2);
2529
fs.existsSync(configDir) || fs.mkdirSync(configDir, { recursive: true });
26-
return fs.writeFileSync(path, content);
30+
fs.writeFileSync(path, content);
31+
console.log(
32+
`${chalkInstance.bgGreen(JSON.stringify(options))} config saved at ${chalkInstance.gray(path)}:`,
33+
);
34+
console.log();
35+
console.log(chalkInstance.greenBright(content));
2736
},
2837
};
2938

lib/searchHistory.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ function getDaySplit(someDay) {
4343

4444
exports.searchList = (args) => {
4545
const { someDay, recentDays, showFile } = args;
46+
console.log();
47+
console.log(chalk.gray('fanyi history:'));
48+
console.log();
4649
let targetContent;
4750
// 与配置放在一起
4851
fs.ensureFileSync(searchFilePath);

tests/__snapshots__/index.test.ts.snap

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,5 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`fanyi CLI > should be able to config global options 1`] = `
4-
"
5-
config --no-color ~ iciba.com
6-
7-
-----
8-
9-
"
10-
`;
11-
12-
exports[`fanyi CLI > should be able to config global options 2`] = `
13-
"
14-
config --color ~ iciba.com
15-
16-
-----
17-
18-
"
19-
`;
20-
21-
exports[`fanyi CLI > should be able to config global options 3`] = `
22-
"
23-
config --no-iciba ~ iciba.com
24-
25-
-----
26-
27-
"
28-
`;
29-
30-
exports[`fanyi CLI > should be able to config global options 4`] = `
31-
"
32-
config --iciba ~ iciba.com
33-
34-
-----
35-
36-
"
37-
`;
38-
39-
exports[`fanyi CLI > should print config list if config list is given 1`] = `
40-
"
41-
config list ~ iciba.com
42-
43-
-----
44-
45-
"
46-
`;
47-
483
exports[`fanyi CLI > should print help if -h is given 1`] = `
494
"Usage: fanyi [options] [command]
505
@@ -66,37 +21,6 @@ Examples:
6621
"
6722
`;
6823

69-
exports[`fanyi CLI > should print search history 1`] = `
70-
"2024-09-11
71-
xx
72-
abbr. without securities or warrants 无权或无保障(代号);
73-
74-
love
75-
vt.& vi. 爱,热爱;爱戴;喜欢;赞美,称赞;
76-
vt. 喜爱;喜好;喜欢;爱慕;
77-
n. 爱情,爱意;疼爱;热爱;爱人,所爱之物;
78-
word
79-
n. 单词;话语;诺言;消息;
80-
vt. 措辞,用词;用言语表达;
81-
vi. 讲话;
82-
config
83-
84-
hello
85-
int. 哈喽,喂;你好,您好;表示问候;打招呼;
86-
n. “喂”的招呼声或问候声;
87-
vi. 喊“喂”;
88-
helxlo
89-
90-
config list
91-
92-
config --no-color
93-
94-
config --color
95-
96-
97-
"
98-
`;
99-
10024
exports[`fanyi CLI > should print translation of the word 1`] = `
10125
"
10226
hello 英[ hə'ləʊ ] 美[ həˈloʊ ] ~ iciba.com

tests/index.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ describe('fanyi CLI', () => {
4646
});
4747

4848
it('should be able to config global options', async () => {
49-
const { stdout } = await runScript(['config --no-color']);
50-
expect(stdout).toMatchSnapshot();
51-
const { stdout: stdout2 } = await runScript(['config --color']);
52-
expect(stdout2).toMatchSnapshot();
53-
const { stdout: stdout3 } = await runScript(['config --no-iciba']);
54-
expect(stdout3).toMatchSnapshot();
55-
const { stdout: stdout4 } = await runScript(['config --iciba']);
56-
expect(stdout4).toMatchSnapshot();
49+
const { stdout } = await runScript(['config', '--no-color']);
50+
expect(stdout).toContain('{"color":false}');
51+
const { stdout: stdout2 } = await runScript(['config', '--color']);
52+
expect(stdout2).toContain('{"color":true}');
53+
const { stdout: stdout3 } = await runScript(['config', '--no-iciba']);
54+
expect(stdout3).toContain('{"iciba":false}');
55+
const { stdout: stdout4 } = await runScript(['config', '--iciba']);
56+
expect(stdout4).toContain('{"iciba":true}');
5757
});
5858

5959
it('should print search history', async () => {
6060
const { stdout } = await runScript(['list']);
61-
expect(stdout).toMatchSnapshot();
61+
expect(stdout).toContain('fanyi history:');
6262
});
6363
});

0 commit comments

Comments
 (0)