Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vscode/engine] getGitLog 함수 관련 테스트 코드 구현 #689

Merged
merged 19 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 180 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/vscode/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const config = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "/out/"],
verbose: true,
rootDir: "./",
};

export default config;
5 changes: 4 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"pretest": "npm run compile-tests && npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"lint:fix": "eslint src --ext ts --fix",
"test": "node ./out/test/runTest.js"
"test": "jest"
},
"dependencies": {
"@githru-vscode-ext/analysis-engine": "^0.7.0",
Expand All @@ -90,6 +90,7 @@
},
"devDependencies": {
"@types/glob": "^7.2.0",
"@types/jest": "^29.5.12",
"@types/mocha": "^9.1.1",
"@types/node": "14.x",
"@types/vscode": "^1.67.0",
Expand All @@ -107,8 +108,10 @@
"eslint-plugin-unused-imports": "^3.0.0",
"formdata-polyfill": "^4.0.10",
"glob": "^8.0.1",
"jest": "^29.7.0",
"mocha": "^9.2.2",
"prettier": "^3.0.1",
"ts-jest": "^29.2.5",
"ts-loader": "^9.2.8",
"typescript": "^4.6.4",
"webpack": "^5.70.0",
Expand Down
61 changes: 61 additions & 0 deletions packages/vscode/src/test/utils/getGitLog.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as cp from "child_process";

import { getGitLog } from "../../utils/git.util";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗. 이 부분은 바로 상대경로를 쓰는게 아니라, engine pkg lib를 import 해서 쓰셔야 해요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

�넵 해당 부분 수정하도록 하겠습니다!

Copy link
Contributor Author

@novice1993 novice1993 Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ytaek

영택님 코멘트 주셨던 부분 중 아래의 2가지는 수정하여 반영하였는데

  1. mock 데이터 활용 방식으로 test 코드 수정
  2. jest.config.ts 파일 수정 (test가 특정 디렉토리에 한정되지 않도록 전체 경로로 변경)

해당 이슈 (engine 모듈 import 수정) 는 어떻게 해결해야할지 잘 모르습니다..!

getGitLog 함수를 "@githru-vscode-ext/analysis-engine"에서 import 하려고 하니,
해당 패키지에서 export 하지 않는다고 나오는 상황입니다!

getGitLog 함수는 vscode/src/utils/git.util.ts 파일에서 선언된 함수라서 상대 경로로 import 하는 게 맞지 않나 생각이 되는데
(https://github.com/githru/githru-vscode-ext/blob/main/packages/vscode/src/utils/git.util.ts)

혹시 제가 잘못 이해하고 있는 거라면 어떻게 수정하는게 좋을지 코멘트 부탁드립니다..!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉... 제가 상대경로 위치를 잘 못 봣었네요 ㅜ.ㅜ 죄송합니당.
../../ 부분이 package 바깥으로 가는 경로인 걸로 착각했습니다 😭😭😭😭😭


const generateMockGitLogData = (index: number) => `
commit ${index}1234567890abcdef1234567890abcdef${index}5678 (HEAD -> main)
Author: Mock User ${index} <mock${index}@example.com>
AuthorDate: Mon Sep ${index} 21:42:00 2023 +0000
Commit: Mock Committer ${index} <committer${index}@example.com>
CommitDate: Mon Sep ${index} 21:43:00 2023 +0000

Commit message ${index}
`;

jest.mock("child_process");
const mockSpawn = cp.spawn as jest.Mock;

let mockSpawnCallCount = 0;

mockSpawn.mockImplementation(() => {
return {
stdout: {
on: jest.fn((event, callback) => {
if (event === "data") {
const mockData = generateMockGitLogData(mockSpawnCallCount);
callback(Buffer.from(mockData));
mockSpawnCallCount++;
}
if (event === "close") {
callback();
}
}),
},
stderr: {
on: jest.fn((event, callback) => {
callback(Buffer.from("mocked error message"));
}),
},
on: jest.fn((event, callback) => {
if (event === "exit") {
callback(0);
}
}),
};
});

describe("getGitLog util test", () => {
afterEach(() => {
mockSpawnCallCount = 0; // initailize call count
});

it("should return the combined git log output from number of threads", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😊👍

const result = await getGitLog("git", "/mocked/path/to/repo");

const expectedData = Array.from({ length: mockSpawnCallCount }) // Create an array with length equal to call count
.map((_, index) => generateMockGitLogData(index)) // Insert mock data into the array for each index
.join(""); // Concatenate all mock data into a single string

return expect(result).toEqual(expectedData);
});
});
2 changes: 1 addition & 1 deletion packages/vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"outDir": "dist",
"lib": ["ES2020"],
"sourceMap": true,
"rootDir": "src",
"rootDir": "./src",
"strict": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
Expand Down
Loading