Skip to content

Commit 77a0037

Browse files
authored
Merge pull request #53 from Nautilus-Cyberneering/issue-52-do-not-overwrite-git-local-config
Fix bug: tests overwrites git local config
2 parents 0eab4df + 525f169 commit 77a0037

File tree

6 files changed

+64
-65
lines changed

6 files changed

+64
-65
lines changed

__tests__/e2e/main.test.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import * as path from 'path'
33
import * as process from 'process'
44

55
import {
6-
createInitializedTempGitDir,
6+
createInitializedGitRepo,
77
createInitializedTempGnuPGHomeDir,
88
dummyPayload,
99
getLatestCommitHash,
1010
gitLogForLatestCommit
1111
} from '../../src/__tests__/helpers'
1212

13+
import {GitRepo} from '../../src/git-repo'
14+
1315
import {expect} from '@jest/globals'
1416
import {getErrorMessage} from '../../src/error'
1517
import {testConfiguration} from '../../src/__tests__/config'
@@ -32,11 +34,11 @@ function executeAction(env): string | Buffer {
3234
return output
3335
}
3436

35-
function createJob(gitRepoDir): string | Buffer {
37+
function createJob(gitRepo: GitRepo): string | Buffer {
3638
const env = {
3739
...process.env,
3840
INPUT_QUEUE_NAME: 'QUEUE-NAME',
39-
INPUT_GIT_REPO_DIR: gitRepoDir,
41+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
4042
INPUT_ACTION: 'create-job',
4143
INPUT_JOB_PAYLOAD: dummyPayload(),
4244
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
@@ -67,12 +69,12 @@ function getOutputVariable(
6769

6870
describe('GitHub Action', () => {
6971
it('should print the git repo dir at the beginning of the action execution', async () => {
70-
const gitRepoDir = await createInitializedTempGitDir()
72+
const gitRepo = await createInitializedGitRepo()
7173

7274
const env = {
7375
...process.env,
7476
INPUT_QUEUE_NAME: 'QUEUE-NAME',
75-
INPUT_GIT_REPO_DIR: gitRepoDir,
77+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
7678
INPUT_ACTION: 'next-job',
7779
INPUT_JOB_PAYLOAD: dummyPayload(),
7880
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
@@ -81,17 +83,17 @@ describe('GitHub Action', () => {
8183
const output = executeAction(env)
8284

8385
expect(output).toEqual(
84-
expect.stringContaining(`git_repo_dir: ${gitRepoDir}`)
86+
expect.stringContaining(`git_repo_dir: ${gitRepo.getDirPath()}`)
8587
)
8688
})
8789

8890
it('should return an error for invalid actions', async () => {
89-
const gitRepoDir = await createInitializedTempGitDir()
91+
const gitRepo = await createInitializedGitRepo()
9092

9193
const env = {
9294
...process.env,
9395
INPUT_QUEUE_NAME: 'QUEUE-NAME',
94-
INPUT_GIT_REPO_DIR: gitRepoDir,
96+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
9597
INPUT_ACTION: 'INVALID ACTION',
9698
INPUT_JOB_PAYLOAD: dummyPayload(),
9799
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
@@ -107,12 +109,12 @@ describe('GitHub Action', () => {
107109
})
108110

109111
it('should create a new job', async () => {
110-
const gitRepoDir = await createInitializedTempGitDir()
112+
const gitRepo = await createInitializedGitRepo()
111113

112114
const env = {
113115
...process.env,
114116
INPUT_QUEUE_NAME: 'QUEUE-NAME',
115-
INPUT_GIT_REPO_DIR: gitRepoDir,
117+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
116118
INPUT_ACTION: 'create-job',
117119
INPUT_JOB_PAYLOAD: dummyPayload(),
118120
INPUT_GIT_COMMIT_NO_GPG_SIGN: true
@@ -122,19 +124,19 @@ describe('GitHub Action', () => {
122124

123125
expect(getOutputVariable('job_created', output.toString())).toBe('true')
124126
expect(getOutputVariable('job_commit', output.toString())).toBe(
125-
getLatestCommitHash(gitRepoDir)
127+
getLatestCommitHash(gitRepo.getDirPath())
126128
)
127129
})
128130

129131
it('should get the next job', async () => {
130-
const gitRepoDir = await createInitializedTempGitDir()
132+
const gitRepo = await createInitializedGitRepo()
131133

132-
createJob(gitRepoDir)
134+
createJob(gitRepo)
133135

134136
const env = {
135137
...process.env,
136138
INPUT_QUEUE_NAME: 'QUEUE-NAME',
137-
INPUT_GIT_REPO_DIR: gitRepoDir,
139+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
138140
INPUT_ACTION: 'next-job',
139141
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
140142
}
@@ -145,19 +147,19 @@ describe('GitHub Action', () => {
145147
dummyPayload()
146148
)
147149
expect(getOutputVariable('job_commit', output.toString())).toBe(
148-
getLatestCommitHash(gitRepoDir)
150+
getLatestCommitHash(gitRepo.getDirPath())
149151
)
150152
})
151153

152154
it('should mark the pending job as started', async () => {
153-
const gitRepoDir = await createInitializedTempGitDir()
155+
const gitRepo = await createInitializedGitRepo()
154156

155-
createJob(gitRepoDir)
157+
createJob(gitRepo)
156158

157159
const env = {
158160
...process.env,
159161
INPUT_QUEUE_NAME: 'QUEUE-NAME',
160-
INPUT_GIT_REPO_DIR: gitRepoDir,
162+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
161163
INPUT_ACTION: 'start-job',
162164
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
163165
}
@@ -166,44 +168,44 @@ describe('GitHub Action', () => {
166168

167169
expect(getOutputVariable('job_started', output.toString())).toBe('true')
168170
expect(getOutputVariable('job_commit', output.toString())).toBe(
169-
getLatestCommitHash(gitRepoDir)
171+
getLatestCommitHash(gitRepo.getDirPath())
170172
)
171173
})
172174

173175
it('should mark the pending job as finished', async () => {
174-
const gitRepoDir = await createInitializedTempGitDir()
176+
const gitRepo = await createInitializedGitRepo()
175177

176-
createJob(gitRepoDir)
178+
createJob(gitRepo)
177179

178180
executeAction({
179181
...process.env,
180182
INPUT_QUEUE_NAME: 'QUEUE-NAME',
181-
INPUT_GIT_REPO_DIR: gitRepoDir,
183+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
182184
INPUT_ACTION: 'start-job',
183185
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
184186
})
185187

186188
const output = executeAction({
187189
...process.env,
188190
INPUT_QUEUE_NAME: 'QUEUE-NAME',
189-
INPUT_GIT_REPO_DIR: gitRepoDir,
191+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
190192
INPUT_ACTION: 'finish-job',
191193
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
192194
})
193195

194196
expect(getOutputVariable('job_finished', output.toString())).toBe('true')
195197
expect(getOutputVariable('job_commit', output.toString())).toBe(
196-
getLatestCommitHash(gitRepoDir)
198+
getLatestCommitHash(gitRepo.getDirPath())
197199
)
198200
})
199201

200202
it('should allow to overwrite commit author', async () => {
201-
const gitRepoDir = await createInitializedTempGitDir()
203+
const gitRepo = await createInitializedGitRepo()
202204

203205
const env = {
204206
...process.env,
205207
INPUT_QUEUE_NAME: 'QUEUE-NAME',
206-
INPUT_GIT_REPO_DIR: gitRepoDir,
208+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
207209
INPUT_ACTION: 'create-job',
208210
INPUT_JOB_PAYLOAD: dummyPayload(),
209211
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true',
@@ -212,23 +214,23 @@ describe('GitHub Action', () => {
212214

213215
executeAction(env)
214216

215-
const gitLogOutput = gitLogForLatestCommit(gitRepoDir)
217+
const gitLogOutput = gitLogForLatestCommit(gitRepo.getDirPath())
216218

217219
expect(gitLogOutput).toEqual(
218220
expect.stringContaining('Author: A committer <[email protected]>')
219221
)
220222
})
221223

222224
it('should allow to overwrite commit signing key', async () => {
223-
const gitRepoDir = await createInitializedTempGitDir()
225+
const gitRepo = await createInitializedGitRepo()
224226
const gnuPGHomeDir = await createInitializedTempGnuPGHomeDir()
225227
const signingKeyFingerprint =
226228
testConfiguration().gpg_signing_key.fingerprint
227229

228230
const env = {
229231
...process.env,
230232
INPUT_QUEUE_NAME: 'QUEUE-NAME',
231-
INPUT_GIT_REPO_DIR: gitRepoDir,
233+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
232234
INPUT_ACTION: 'create-job',
233235
INPUT_JOB_PAYLOAD: dummyPayload(),
234236
INPUT_GIT_COMMIT_GPG_SIGN: signingKeyFingerprint,
@@ -237,7 +239,7 @@ describe('GitHub Action', () => {
237239

238240
executeAction(env)
239241

240-
const gitLogOutput = gitLogForLatestCommit(gitRepoDir)
242+
const gitLogOutput = gitLogForLatestCommit(gitRepo.getDirPath())
241243

242244
expect(gitLogOutput).toEqual(
243245
expect.stringContaining(
@@ -247,20 +249,20 @@ describe('GitHub Action', () => {
247249
})
248250

249251
it('should allow to disable commit signing for a given commit', async () => {
250-
const gitRepoDir = await createInitializedTempGitDir()
252+
const gitRepo = await createInitializedGitRepo()
251253

252254
const env = {
253255
...process.env,
254256
INPUT_QUEUE_NAME: 'QUEUE-NAME',
255-
INPUT_GIT_REPO_DIR: gitRepoDir,
257+
INPUT_GIT_REPO_DIR: gitRepo.getDirPath(),
256258
INPUT_ACTION: 'create-job',
257259
INPUT_JOB_PAYLOAD: dummyPayload(),
258260
INPUT_GIT_COMMIT_NO_GPG_SIGN: 'true'
259261
}
260262

261263
executeAction(env)
262264

263-
const gitLogOutput = gitLogForLatestCommit(gitRepoDir)
265+
const gitLogOutput = gitLogForLatestCommit(gitRepo.getDirPath())
264266

265267
expect(!gitLogOutput.includes('gpg: Signature')).toBe(true)
266268

__tests__/unit/git-repo.test.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
import {createTempEmptyDir, newSimpleGit} from '../../src/__tests__/helpers'
1+
import {
2+
createTempEmptyDir,
3+
newSimpleGitWithCommitterIdentity
4+
} from '../../src/__tests__/helpers'
25

36
import {GitRepo} from '../../src/git-repo'
47
import {GitRepoDir} from '../../src/git-repo-dir'
5-
import {SimpleGit} from 'simple-git'
6-
import {testConfiguration} from '../../src/__tests__/config'
7-
8-
async function newSimpleGitWithCommitterIdentity(
9-
gitRepoDir: GitRepoDir
10-
): Promise<SimpleGit> {
11-
const git = await newSimpleGit(gitRepoDir.getDirPath())
12-
git.addConfig('user.name', testConfiguration().git.user.name)
13-
git.addConfig('user.email', testConfiguration().git.user.email)
14-
return git
15-
}
168

179
describe('GitRepo', () => {
1810
it('should be bound to a file system dir', async () => {

dist/index.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/helpers.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as cp from 'child_process'
22
import * as gpg from './gpg'
33
import * as openpgp from './openpgp'
44

5-
import simpleGit, {SimpleGit} from 'simple-git'
5+
import simpleGit, {SimpleGit, SimpleGitOptions} from 'simple-git'
66

77
import {GitRepo} from '../git-repo'
88
import {GitRepoDir} from '../git-repo-dir'
@@ -37,12 +37,16 @@ export async function newSimpleGit(baseDir: string): Promise<SimpleGit> {
3737
}
3838

3939
export async function newSimpleGitWithCommitterIdentity(
40-
gitRepoDir: string
40+
gitRepoDir: GitRepoDir
4141
): Promise<SimpleGit> {
42-
const git = await newSimpleGitWithoutCommitterIdentity(gitRepoDir)
43-
git.init()
44-
git.addConfig('user.name', testConfiguration().git.user.name)
45-
git.addConfig('user.email', testConfiguration().git.user.email)
42+
const options: Partial<SimpleGitOptions> = {
43+
baseDir: gitRepoDir.getDirPath(),
44+
config: [
45+
`user.name=${testConfiguration().git.user.name}`,
46+
`user.email=${testConfiguration().git.user.email}`
47+
]
48+
}
49+
const git = simpleGit(options)
4650
return git
4751
}
4852

@@ -53,17 +57,11 @@ export async function newSimpleGitWithoutCommitterIdentity(
5357
return git
5458
}
5559

56-
export async function createInitializedTempGitDir(): Promise<string> {
57-
const gitRepoDir = await createTempEmptyDir()
58-
const git = await newSimpleGit(gitRepoDir)
59-
await git.init()
60-
return gitRepoDir
61-
}
62-
6360
export async function createInitializedGitRepo(): Promise<GitRepo> {
64-
const gitRepoDirPath = await createTempEmptyDir()
65-
const git = await newSimpleGitWithCommitterIdentity(gitRepoDirPath)
66-
const gitRepo = new GitRepo(new GitRepoDir(gitRepoDirPath), git)
61+
const gitRepoDir = new GitRepoDir(await createTempEmptyDir())
62+
const git = await newSimpleGitWithCommitterIdentity(gitRepoDir)
63+
const gitRepo = new GitRepo(gitRepoDir, git)
64+
await gitRepo.init()
6765
return gitRepo
6866
}
6967

src/simple-git-factory.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import simpleGit, {SimpleGit} from 'simple-git'
1+
import simpleGit, {SimpleGit, SimpleGitOptions} from 'simple-git'
22
import {GitRepoDir} from './git-repo-dir'
33

44
export async function createGitInstance(
55
gitRepoDir: GitRepoDir
66
): Promise<SimpleGit> {
7-
const git: SimpleGit = simpleGit(gitRepoDir.getDirPath())
7+
const options: Partial<SimpleGitOptions> = {
8+
baseDir: gitRepoDir.getDirPath()
9+
}
10+
11+
const git: SimpleGit = simpleGit(options)
812

913
/*
1014
* We need to pass the env vars to the child git process

0 commit comments

Comments
 (0)