Skip to content

Commit fc382df

Browse files
Merge pull request #17 from laststance/feat/test-3
2 parents 1f3cca6 + e1f3b42 commit fc382df

File tree

3 files changed

+77
-21
lines changed

3 files changed

+77
-21
lines changed

tests/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ import * as gitGptCommit from '../index.js'
1111

1212
describe('Git GPT Commit', () => {
1313
let tempDir
14+
let originalDir
1415

1516
beforeEach(() => {
17+
// Store original directory
18+
originalDir = process.cwd()
19+
1620
// Set up a new test environment before each test
1721
tempDir = setupTestRepo()
1822
vi.clearAllMocks()
1923
})
2024

2125
afterEach(() => {
26+
// Change back to original directory before cleaning up
27+
process.chdir(originalDir)
28+
2229
// Clean up the test environment after each test
2330
cleanupTestRepo(tempDir)
2431
vi.clearAllMocks()

tests/setup-mocks.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ vi.mock('openai', () => {
2121
}
2222
})
2323

24-
// index.jsモジュール全体をモック
24+
// Mock the entire index.js module
2525
vi.mock('../index.js', () => {
2626
return {
2727
getGitSummary: vi.fn((options) => {
2828
try {
29-
// 実際のdiffコマンドを実行せず、ファイルの変更があるかチェック
29+
// Check if there are file changes without actually executing the diff command
3030
const gitStatus = require('child_process')
3131
.execSync('git status --porcelain')
3232
.toString()
@@ -35,8 +35,8 @@ vi.mock('../index.js', () => {
3535
throw new Error('No changes to commit')
3636
}
3737

38-
// モックされたdiffの内容を返す
39-
return `diff --git a/file1.js b/file1.js\nindex 123456..789012 100644\n--- a/file1.js\n+++ b/file1.js\n@@ -1,5 +1,8 @@\nfunction greet(name) {\n- return \`Hello, \${name}!\`;\n+ // 名前が空の場合のデフォルト値を追加\n+ const userName = name || 'Guest';\n+ return \`Hello, \${userName}!\`;\n }`
38+
// Return mocked diff content
39+
return `diff --git a/file1.js b/file1.js\nindex 123456..789012 100644\n--- a/file1.js\n+++ b/file1.js\n@@ -1,5 +1,8 @@\nfunction greet(name) {\n- return \`Hello, \${name}!\`;\n+ // Add default value when name is empty\n+ const userName = name || 'Guest';\n+ return \`Hello, \${userName}!\`;\n }`
4040
} catch (error) {
4141
throw new Error('Failed to get git summary')
4242
}
@@ -45,40 +45,40 @@ vi.mock('../index.js', () => {
4545
return 'Mock commit message'
4646
}),
4747
gitExtension: vi.fn(),
48-
// その他必要な関数やオブジェクト
48+
// Other necessary functions or objects
4949
}
5050
})
5151

52-
// fs モジュールをモック
52+
// Mock fs module
5353
vi.mock('fs', async () => {
5454
const actual = await vi.importActual('fs')
5555

5656
return {
5757
...actual,
5858
existsSync: vi.fn((path) => {
59-
// 特定のパスのみモックレスポンスを返す
59+
// Return mock response only for specific paths
6060
if (path.includes('.git-gpt-commit-config.json')) {
6161
return true
6262
}
63-
// それ以外は実際の実装を使用
63+
// Use actual implementation for others
6464
return actual.existsSync(path)
6565
}),
6666
readFileSync: vi.fn((path, options) => {
67-
// コンフィグファイルの場合、モックデータを返す
67+
// Return mock data for config file
6868
if (path.includes('.git-gpt-commit-config.json')) {
6969
return JSON.stringify({
7070
model: 'gpt-4o',
7171
language: 'English',
7272
})
7373
}
74-
// それ以外は実際の実装を使用
74+
// Use actual implementation for others
7575
return actual.readFileSync(path, options)
7676
}),
7777
writeFileSync: vi.fn(),
7878
}
7979
})
8080

81-
// commanderをモック
81+
// Mock commander
8282
vi.mock('commander', () => {
8383
const mockProgram = {
8484
command: vi.fn().mockReturnThis(),
@@ -95,32 +95,32 @@ vi.mock('commander', () => {
9595
}
9696
})
9797

98-
// child_processをモック
98+
// Mock child_process
9999
vi.mock('child_process', async () => {
100100
const actual = await vi.importActual('child_process')
101101

102102
return {
103103
...actual,
104104
execSync: vi.fn((command) => {
105105
if (typeof command === 'string') {
106-
// git statusコマンドの場合は変更があるとみなす
106+
// Treat as having changes for git status commands
107107
if (command.includes('git status')) {
108108
return Buffer.from('M file1.js')
109109
}
110110

111-
// git commitコマンドの場合はモック応答
111+
// Mock response for git commit commands
112112
if (command.includes('git commit')) {
113113
return Buffer.from('Commit successful')
114114
}
115115
}
116116

117-
// その他のコマンドは実際に実行
117+
// Actually execute other commands
118118
return actual.execSync(command)
119119
}),
120120
exec: vi.fn((command, callback) => {
121121
if (command.includes('git diff')) {
122122
const stdout =
123-
"diff --git a/file1.js b/file1.js\nindex 123456..789012 100644\n--- a/file1.js\n+++ b/file1.js\n@@ -1,5 +1,8 @@\nfunction greet(name) {\n- return `Hello, ${name}!`;\n+ // 名前が空の場合のデフォルト値を追加\n+ const userName = name || 'Guest';\n+ return `Hello, ${userName}!`;\n }"
123+
"diff --git a/file1.js b/file1.js\nindex 123456..789012 100644\n--- a/file1.js\n+++ b/file1.js\n@@ -1,5 +1,8 @@\nfunction greet(name) {\n- return \`Hello, \${name}!\`;\n+ // Add default value when name is empty\n+ const userName = name || 'Guest';\n+ return \`Hello, \${userName}!\`;\n }"
124124
callback(null, { stdout })
125125
} else {
126126
callback(null, { stdout: '' })
@@ -129,12 +129,12 @@ vi.mock('child_process', async () => {
129129
}
130130
})
131131

132-
// promptsモジュールをモック
132+
// Mock prompts module
133133
vi.mock('prompts', () => ({
134134
default: vi.fn().mockResolvedValue({ value: true }),
135135
}))
136136

137-
// process.exitをモック
137+
// Mock process.exit
138138
vi.stubGlobal('process', {
139139
...process,
140140
exit: vi.fn((code) => {

tests/setup.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,51 @@ if (fs.existsSync(testEnvPath)) {
1717
* @returns {string} Path to the created temporary directory
1818
*/
1919
export function setupTestRepo() {
20+
// Find the project root to access fixtures
21+
let projectRoot = process.cwd()
22+
let currentPath = projectRoot
23+
while (!fs.existsSync(path.join(currentPath, 'package.json'))) {
24+
const parentPath = path.dirname(currentPath)
25+
if (parentPath === currentPath) break
26+
currentPath = parentPath
27+
}
28+
if (fs.existsSync(path.join(currentPath, 'package.json'))) {
29+
projectRoot = currentPath
30+
}
31+
2032
// Create a temporary directory
2133
const tempDir = path.join(os.tmpdir(), `git-gpt-commit-test-${Date.now()}`)
2234
fs.mkdirSync(tempDir, { recursive: true })
2335

36+
// Create fixtures directory in the temp directory
37+
const fixturesDir = path.join(tempDir, 'fixtures')
38+
fs.mkdirSync(fixturesDir, { recursive: true })
39+
40+
// Copy fixture files from original project to test directory
41+
const sourceFixturesDir = path.join(projectRoot, 'fixtures')
42+
if (fs.existsSync(sourceFixturesDir)) {
43+
const files = fs.readdirSync(sourceFixturesDir)
44+
files.forEach((file) => {
45+
const sourcePath = path.join(sourceFixturesDir, file)
46+
const destPath = path.join(fixturesDir, file)
47+
48+
if (fs.statSync(sourcePath).isFile()) {
49+
fs.copyFileSync(sourcePath, destPath)
50+
} else if (fs.statSync(sourcePath).isDirectory()) {
51+
// Handle subdirectories like 'expected'
52+
fs.mkdirSync(destPath, { recursive: true })
53+
const subFiles = fs.readdirSync(sourcePath)
54+
subFiles.forEach((subFile) => {
55+
const subSourcePath = path.join(sourcePath, subFile)
56+
const subDestPath = path.join(destPath, subFile)
57+
if (fs.statSync(subSourcePath).isFile()) {
58+
fs.copyFileSync(subSourcePath, subDestPath)
59+
}
60+
})
61+
}
62+
})
63+
}
64+
2465
// Initialize Git repository
2566
process.chdir(tempDir)
2667
execSync('git init')
@@ -40,7 +81,17 @@ export function setupTestRepo() {
4081
* @returns {string} Path to the copied file
4182
*/
4283
export function copyFixture(fixtureName, destName = fixtureName) {
43-
// Find the project root by looking for package.json up the directory tree
84+
// First check if fixture exists in the current working directory
85+
const localFixturePath = path.join(process.cwd(), 'fixtures', fixtureName)
86+
const destPath = path.join(process.cwd(), destName)
87+
88+
// If fixture exists locally, use it
89+
if (fs.existsSync(localFixturePath)) {
90+
fs.copyFileSync(localFixturePath, destPath)
91+
return destPath
92+
}
93+
94+
// Otherwise, look for it in the project root
4495
let projectRoot = process.cwd()
4596
let currentPath = projectRoot
4697

@@ -59,7 +110,6 @@ export function copyFixture(fixtureName, destName = fixtureName) {
59110
}
60111

61112
const fixturePath = path.join(projectRoot, 'fixtures', fixtureName)
62-
const destPath = path.join(process.cwd(), destName)
63113

64114
if (!fs.existsSync(fixturePath)) {
65115
// Create a mock file if the fixture directory doesn't exist
@@ -94,6 +144,5 @@ export function modifyAndStageFile(filePath, content) {
94144
*/
95145
export function cleanupTestRepo(tempDir) {
96146
// Delete the directory after the test
97-
// テスト後にディレクトリを削除
98147
fs.rmSync(tempDir, { recursive: true, force: true })
99148
}

0 commit comments

Comments
 (0)