Skip to content

Commit

Permalink
Merge pull request #467 from zenn-dev/canary
Browse files Browse the repository at this point in the history
release v0.1.147
  • Loading branch information
cm-wada-yusuke authored Oct 3, 2023
2 parents 21ae651 + e92a90d commit 8e70c21
Show file tree
Hide file tree
Showing 58 changed files with 847 additions and 2,180 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"packages": [
"packages/*"
],
"version": "0.1.146",
"version": "0.1.147-alpha.1",
"npmClient": "pnpm"
}
12 changes: 0 additions & 12 deletions packages/zenn-cli/jest.config.client.js

This file was deleted.

31 changes: 0 additions & 31 deletions packages/zenn-cli/jest.config.server.js

This file was deleted.

12 changes: 4 additions & 8 deletions packages/zenn-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenn-cli",
"version": "0.1.146",
"version": "0.1.147-alpha.1",
"description": "Preview Zenn content locally.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -30,19 +30,16 @@
"fix:eslint": "eslint --fix 'src/**/*.{ts,tsx}'",
"strict:lint": "eslint 'src/**/*.{ts,tsx}' --max-warnings 0",
"test": "run-s test:client test:server",
"test:client": "jest --config=jest.config.client.js",
"test:server": "jest --config=jest.config.server.js",
"test:client": "vitest run --config vitest.client.config.ts",
"test:server": "vitest run --config vitest.server.config.ts",
"exec:zenn": "node ./dist/server/zenn.js"
},
"devDependencies": {
"@swc/core": "1.2.205",
"@swc/jest": "^0.2.24",
"@types/configstore": "^6.0.0",
"@types/connect-history-api-fallback": "^1.3.5",
"@types/emoji-regex": "^9.2.0",
"@types/express": "^4.17.17",
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.4.0",
"@types/js-yaml": "^4.0.5",
"@types/node": "^18.13.0",
"@types/node-fetch": "^2.6.2",
Expand Down Expand Up @@ -74,8 +71,6 @@
"gray-matter": "^4.0.3",
"history": "^5.3.0",
"image-size": "^1.0.2",
"jest": "^29.4.2",
"jest-environment-jsdom": "^29.4.2",
"js-yaml": "^4.1.0",
"node-fetch": "^2.6.9",
"node-loader": "^2.0.0",
Expand All @@ -95,6 +90,7 @@
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"vite": "^4.2.1",
"vitest": "^0.34.4",
"wait-on": "^7.0.1",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
Expand Down
7 changes: 4 additions & 3 deletions packages/zenn-cli/src/client/__tests__/lib/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { describe, test, expect } from 'vitest';
import { encodeUrlPeriod, decodeUrlPeriod } from '../../lib/helper';

describe('pair of encodeUrlPeriod and decodeUrlPeriod', () => {
test('return original text `a.b.c` throgh encoding and decoding', () => {
describe('encodeUrlPeriod decodeUrlPeriod を使った処理のテスト', () => {
test('エンコードしてデコードすると元のテキスト"a.b.c"を返す', () => {
const original = 'a.b.c';
const encoded = encodeUrlPeriod(original);
expect(encoded).not.toContain('.');
const decoded = decodeUrlPeriod(encoded);
expect(decoded).toEqual(original);
});
test('return original text `2E.` throgh encoding and decoding', () => {
test('エンコードしてデコードすると元のテキスト"2E."を返す', () => {
const original = '2E.';
const encoded = encodeUrlPeriod(original);
expect(encoded).not.toContain('.');
Expand Down
21 changes: 11 additions & 10 deletions packages/zenn-cli/src/common/__tests__/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
import { describe, test, expect } from 'vitest';
import * as helper from '../helper';

describe('validateSlug', () => {
test('should return true with 12 valid characters', () => {
describe('validateSlug() のテスト', () => {
test('12文字の有効な文字列には true を返す', () => {
const result = helper.validateSlug('abcd-efg_123');
expect(result).toBe(true);
});

test('should return false with 12 invalid characters', () => {
test('12文字の無効な文字列には false を返す', () => {
expect(helper.validateSlug('abcd-efg%12')).toBe(false);
expect(helper.validateSlug('abcd-efg/12')).toBe(false);
expect(helper.validateSlug('abcd-efg"12')).toBe(false);
});

test('should return false with 11 valid characters', () => {
test('11文字の有効な文字列には false を返す', () => {
const result = helper.validateSlug('abcd-efg_12');
expect(result).toBe(false);
});

test('should return false with 50 valid characters', () => {
test('50文字の有効な文字列には false を返す', () => {
const result = helper.validateSlug(
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx'
);
expect(result).toBe(true);
});

test('should return false with 51 valid characters', () => {
test('51文字の有効な文字列には false を返す', () => {
const result = helper.validateSlug(
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'
);
expect(result).toBe(false);
});
});

describe('validateChapterSlug', () => {
test('should return true with 12 valid characters', () => {
describe('validateChapterSlug()のテスト', () => {
test('12文字の有効な文字列には true を返す', () => {
const result = helper.validateSlug('abcd-efg_123');
expect(result).toBe(true);
});

test('should return false with single valid character', () => {
test('1文字の有効な文字列には false を返す', () => {
const result = helper.validateSlug('a');
expect(result).toBe(false);
});

test('should return false with 51 valid characters', () => {
test('51文字の有効な文字列には false を返す', () => {
const result = helper.validateSlug(
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'
);
Expand Down
7 changes: 4 additions & 3 deletions packages/zenn-cli/src/server/__tests__/commands/help.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import { exec } from '../../commands/help';
import { commandListText } from '../../lib/messages';

describe('cli exec help', () => {
describe('helpコマンドのテスト', () => {
beforeEach(() => {
console.log = jest.fn();
console.log = vi.fn();
});

test('should log help message', () => {
test('ヘルプメッセージを表示する', () => {
exec([]);
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(commandListText)
Expand Down
10 changes: 6 additions & 4 deletions packages/zenn-cli/src/server/__tests__/commands/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { vi, describe, test, expect, beforeEach, SpyInstance } from 'vitest';
import { exec } from '../../commands/index';
import * as Log from '../../lib/log';
import { commandListText } from '../../lib/messages';
import * as notify from '../../lib/notify-update';

describe('CLIのデフォルトの挙動のテスト', () => {
let notifyNeedUpdateCLIMock: jest.SpyInstance<Promise<void>>;
let notifyNeedUpdateCLIMock: SpyInstance<any[], Promise<void>>;

beforeEach(() => {
// mock
console.log = jest.fn();
jest.spyOn(Log, 'error').mockImplementation();
notifyNeedUpdateCLIMock = jest
console.log = vi.fn();
console.error = vi.fn();
vi.spyOn(Log, 'error');
notifyNeedUpdateCLIMock = vi
.spyOn(notify, 'notifyNeedUpdateCLI')
.mockResolvedValue();
});
Expand Down
19 changes: 10 additions & 9 deletions packages/zenn-cli/src/server/__tests__/commands/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import path from 'path';
import { exec } from '../../commands/init';
import * as helper from '../../lib/helper';
import { initHelpText } from '../../lib/messages';

describe('cli exec init', () => {
describe('initコマンドのテスト', () => {
beforeEach(() => {
// mock
jest.spyOn(helper, 'generateFileIfNotExist').mockImplementation();
console.log = jest.fn();
console.error = jest.fn();
vi.spyOn(helper, 'generateFileIfNotExist').mockReturnValue(undefined);
console.log = vi.fn();
console.error = vi.fn();
});

test('should call generateFileIfNotExist for directories', () => {
test('ディレクトリに対して generateFileIfNotExist を実行する', () => {
exec([]);
expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringContaining(path.join(process.cwd(), 'articles/.keep')),
Expand All @@ -23,30 +24,30 @@ describe('cli exec init', () => {
);
});

test('should call generateFileIfNotExist for .gitignore', () => {
test('.gitignore に対して generateFileIfNotExist を実行する', () => {
exec([]);
expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringContaining(path.join(process.cwd(), '.gitignore')),
expect.stringContaining(['node_modules', '.DS_Store'].join('\n'))
);
});

test('should call generateFileIfNotExist for README', () => {
test('README に対して generateFileIfNotExist を実行する', () => {
exec([]);
expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringContaining(path.join(process.cwd(), 'README.md')),
expect.stringContaining('Zenn CLI')
);
});

test('should log success message', () => {
test('成功メッセージを表示する', () => {
exec([]);
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining('🎉 Done!')
);
});

test('should log help text with --help', () => {
test('--helpでもヘルプメッセージを表示する', () => {
exec(['--help']);
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(initHelpText)
Expand Down
35 changes: 13 additions & 22 deletions packages/zenn-cli/src/server/__tests__/commands/new-article.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import path from 'path';
import { exec } from '../../commands/new-article';
import * as helper from '../../lib/helper';
import * as Log from '../../lib/log';
import { newArticleHelpText } from '../../lib/messages';

describe('cli exec new:article', () => {
describe('new:articleコマンドのテスト', () => {
const expectedArticlesDirpath = path.join(process.cwd(), 'articles');

beforeEach(() => {
// mock
jest.spyOn(helper, 'generateFileIfNotExist').mockImplementation();
jest.spyOn(Log, 'error').mockImplementation();
console.log = jest.fn();
vi.spyOn(helper, 'generateFileIfNotExist').mockReturnValue(undefined);
vi.spyOn(Log, 'error').mockReturnValue(undefined);
console.log = vi.fn();
});

test('should call generateFileIfNotExist with proper arguments', () => {
test('有効な引数に generateFileIfNotExist を実行する', () => {
exec(['--emoji', '💭']);

expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
Expand All @@ -33,31 +34,31 @@ describe('cli exec new:article', () => {
);
});

test('should call generateFileIfNotExist with specified title', () => {
test('指定されたタイトルで generateFileIfNotExist を実行する', () => {
exec(['--title', 'A"B/C']);
expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringContaining(expectedArticlesDirpath),
expect.stringContaining(`title: "A\\"B/C"`)
);
});

test('should call generateFileIfNotExist with specified published value true', () => {
test('`published: true` で generateFileIfNotExist を実行する', () => {
exec(['--published', 'true']);
expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringContaining(expectedArticlesDirpath),
expect.stringContaining(`published: true`)
);
});

test('should call generateFileIfNotExist with specified published value false', () => {
test('`published: false` で generateFileIfNotExist を実行する', () => {
exec(['--published', 'false']);
expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringContaining(expectedArticlesDirpath),
expect.stringContaining(`published: false`)
);
});

test('should call generateFileIfNotExist with the path including slug', () => {
test('指定した slug を含むパスで generateFileIfNotExist を実行する', () => {
const slug = 'example-article';
exec(['--slug', slug]);

Expand All @@ -67,32 +68,22 @@ describe('cli exec new:article', () => {
);
});

test('should call generateFileIfNotExist with the path including slug', () => {
const slug = 'example-article';
exec(['--slug', slug]);

expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringMatching(`${expectedArticlesDirpath}/${slug}.md`),
expect.stringContaining(`---`)
);
});

test('should call generateFileIfNotExist with specified publication name', () => {
test('指定された Publication 名で generateFileIfNotExist を実行する', () => {
exec(['--publication-name', 'myPublication']);
expect(helper.generateFileIfNotExist).toHaveBeenCalledWith(
expect.stringContaining(expectedArticlesDirpath),
expect.stringContaining(`publication_name: myPublication`)
);
});

test('should log help text with --help', () => {
test('--help オプションを渡すとヘルプメッセージを表示する', () => {
exec(['--help']);
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining(newArticleHelpText)
);
});

test('should log error with invalid slug', () => {
test('無効な slug が渡されたらエラーメッセージを表示する', () => {
exec(['--slug', 'invalid/slug']);
expect(Log.error).toHaveBeenCalledWith(
expect.stringContaining(
Expand Down
Loading

0 comments on commit 8e70c21

Please sign in to comment.