Skip to content

Commit

Permalink
fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Sep 11, 2024
1 parent 2364c6c commit 10ee02e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 87 deletions.
13 changes: 11 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const homedir = process.env.HOME || require('node:os').homedir();
const path = require('node:path');
const fs = require('node:fs');
const chalk = require('chalk');
const configDir = path.resolve(homedir, '.config', 'fanyi');
const configPath = path.resolve(configDir, '.fanyirc');

// 初始化一个带颜色的 chalk 实例
const chalkInstance = new chalk.Instance({ level: 3 });

const config = {
async load(path = configPath) {
const emptyObj = {};
Expand All @@ -21,9 +25,14 @@ const config = {
async write(options = {}, path = configPath) {
const defaultOptions = await config.load(path);
const mergedOptions = { ...defaultOptions, ...options };
const content = JSON.stringify(mergedOptions);
const content = JSON.stringify(mergedOptions, null, 2);
fs.existsSync(configDir) || fs.mkdirSync(configDir, { recursive: true });
return fs.writeFileSync(path, content);
fs.writeFileSync(path, content);
console.log(
`${chalkInstance.bgGreen(JSON.stringify(options))} config saved at ${chalkInstance.gray(path)}:`,
);
console.log();
console.log(chalkInstance.greenBright(content));
},
};

Expand Down
3 changes: 3 additions & 0 deletions lib/searchHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ function getDaySplit(someDay) {

exports.searchList = (args) => {
const { someDay, recentDays, showFile } = args;
console.log();
console.log(chalk.gray('fanyi history:'));
console.log();
let targetContent;
// 与配置放在一起
fs.ensureFileSync(searchFilePath);
Expand Down
76 changes: 0 additions & 76 deletions tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`fanyi CLI > should be able to config global options 1`] = `
"
config --no-color ~ iciba.com
-----
"
`;

exports[`fanyi CLI > should be able to config global options 2`] = `
"
config --color ~ iciba.com
-----
"
`;

exports[`fanyi CLI > should be able to config global options 3`] = `
"
config --no-iciba ~ iciba.com
-----
"
`;

exports[`fanyi CLI > should be able to config global options 4`] = `
"
config --iciba ~ iciba.com
-----
"
`;

exports[`fanyi CLI > should print config list if config list is given 1`] = `
"
config list ~ iciba.com
-----
"
`;

exports[`fanyi CLI > should print help if -h is given 1`] = `
"Usage: fanyi [options] [command]
Expand All @@ -66,37 +21,6 @@ Examples:
"
`;

exports[`fanyi CLI > should print search history 1`] = `
"2024-09-11
xx
abbr. without securities or warrants 无权或无保障(代号);
love
vt.& vi. 爱,热爱;爱戴;喜欢;赞美,称赞;
vt. 喜爱;喜好;喜欢;爱慕;
n. 爱情,爱意;疼爱;热爱;爱人,所爱之物;
word
n. 单词;话语;诺言;消息;
vt. 措辞,用词;用言语表达;
vi. 讲话;
config
hello
int. 哈喽,喂;你好,您好;表示问候;打招呼;
n. “喂”的招呼声或问候声;
vi. 喊“喂”;
helxlo
config list
config --no-color
config --color
"
`;

exports[`fanyi CLI > should print translation of the word 1`] = `
"
hello 英[ hə'ləʊ ] 美[ həˈloʊ ] ~ iciba.com
Expand Down
18 changes: 9 additions & 9 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ describe('fanyi CLI', () => {
});

it('should be able to config global options', async () => {
const { stdout } = await runScript(['config --no-color']);
expect(stdout).toMatchSnapshot();
const { stdout: stdout2 } = await runScript(['config --color']);
expect(stdout2).toMatchSnapshot();
const { stdout: stdout3 } = await runScript(['config --no-iciba']);
expect(stdout3).toMatchSnapshot();
const { stdout: stdout4 } = await runScript(['config --iciba']);
expect(stdout4).toMatchSnapshot();
const { stdout } = await runScript(['config', '--no-color']);
expect(stdout).toContain('{"color":false}');
const { stdout: stdout2 } = await runScript(['config', '--color']);
expect(stdout2).toContain('{"color":true}');
const { stdout: stdout3 } = await runScript(['config', '--no-iciba']);
expect(stdout3).toContain('{"iciba":false}');
const { stdout: stdout4 } = await runScript(['config', '--iciba']);
expect(stdout4).toContain('{"iciba":true}');
});

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

0 comments on commit 10ee02e

Please sign in to comment.