Skip to content

Commit 8713ecd

Browse files
Merge pull request #7514 from Shopify/tester-remove-git-test-fs-mocks-1108447129653858516
[Tests] Remove filesystem mocks in git.test.ts
2 parents 0a2b099 + 814970c commit 8713ecd

1 file changed

Lines changed: 48 additions & 64 deletions

File tree

packages/cli-kit/src/public/node/git.test.ts

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
import * as git from './git.js'
2-
import {
3-
appendFileSync,
4-
fileExists,
5-
fileExistsSync,
6-
glob,
7-
inTemporaryDirectory,
8-
isDirectory,
9-
readFileSync,
10-
writeFileSync,
11-
} from './fs.js'
2+
import {fileExistsSync, inTemporaryDirectory, mkdirSync, readFileSync, writeFileSync} from './fs.js'
123
import {hasGit, isTerminalInteractive} from './context/local.js'
134
import {beforeEach, describe, expect, test, vi} from 'vitest'
145
import {execa} from 'execa'
156

167
vi.mock('execa')
178
vi.mock('./context/local.js')
18-
vi.mock('./fs.js', async () => {
19-
const fs = await vi.importActual('./fs.js')
20-
return {
21-
...fs,
22-
appendFileSync: vi.fn(),
23-
fileExists: vi.fn(),
24-
isDirectory: vi.fn(),
25-
glob: vi.fn(),
26-
}
27-
})
289

2910
const mockedExeca = vi.mocked(execa)
3011

@@ -133,60 +114,64 @@ describe('downloadRepository()', async () => {
133114
})
134115

135116
test('throws when destination exists as a file', async () => {
136-
await expect(async () => {
117+
await inTemporaryDirectory(async (tmpDir) => {
137118
const repoUrl = 'http://repoUrl'
138-
const destination = 'destination'
139-
vi.mocked(fileExists).mockResolvedValue(true)
140-
vi.mocked(isDirectory).mockResolvedValue(false)
119+
const destination = `${tmpDir}/file.txt`
120+
writeFileSync(destination, '')
141121

142-
await git.downloadGitRepository({repoUrl, destination})
143-
}).rejects.toThrowError(/Can't clone to/)
122+
await expect(async () => {
123+
await git.downloadGitRepository({repoUrl, destination})
124+
}).rejects.toThrowError(/Can't clone to/)
125+
})
144126
})
145127

146128
test('throws when destination directory is not empty', async () => {
147-
await expect(async () => {
129+
await inTemporaryDirectory(async (tmpDir) => {
148130
const repoUrl = 'http://repoUrl'
149-
const destination = 'destination'
150-
vi.mocked(fileExists).mockResolvedValue(true)
151-
vi.mocked(isDirectory).mockResolvedValue(true)
152-
vi.mocked(glob).mockResolvedValue(['file1.txt', 'file2.txt'])
131+
const destination = `${tmpDir}/dir`
132+
mkdirSync(destination)
133+
writeFileSync(`${destination}/file1.txt`, '')
153134

154-
await git.downloadGitRepository({repoUrl, destination})
155-
}).rejects.toThrowError(/already exists and is not empty/)
135+
await expect(async () => {
136+
await git.downloadGitRepository({repoUrl, destination})
137+
}).rejects.toThrowError(/already exists and is not empty/)
138+
})
156139
})
157140

158141
test('throws when destination contains only hidden files', async () => {
159-
await expect(async () => {
142+
await inTemporaryDirectory(async (tmpDir) => {
160143
const repoUrl = 'http://repoUrl'
161-
const destination = 'destination'
162-
vi.mocked(fileExists).mockResolvedValue(true)
163-
vi.mocked(isDirectory).mockResolvedValue(true)
164-
vi.mocked(glob).mockResolvedValue(['.git', '.DS_Store'])
144+
const destination = `${tmpDir}/dir`
145+
mkdirSync(destination)
146+
writeFileSync(`${destination}/.git`, '')
165147

166-
await git.downloadGitRepository({repoUrl, destination})
167-
}).rejects.toThrowError(/already exists and is not empty/)
148+
await expect(async () => {
149+
await git.downloadGitRepository({repoUrl, destination})
150+
}).rejects.toThrowError(/already exists and is not empty/)
151+
})
168152
})
169153

170154
test('succeeds when destination directory is empty', async () => {
171-
const repoUrl = 'http://repoUrl'
172-
const destination = 'destination'
173-
vi.mocked(fileExists).mockResolvedValue(true)
174-
vi.mocked(isDirectory).mockResolvedValue(true)
175-
vi.mocked(glob).mockResolvedValue([])
155+
await inTemporaryDirectory(async (tmpDir) => {
156+
const repoUrl = 'http://repoUrl'
157+
const destination = `${tmpDir}/dir`
158+
mkdirSync(destination)
176159

177-
await git.downloadGitRepository({repoUrl, destination})
160+
await git.downloadGitRepository({repoUrl, destination})
178161

179-
expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
162+
expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
163+
})
180164
})
181165

182166
test('succeeds when destination does not exist', async () => {
183-
const repoUrl = 'http://repoUrl'
184-
const destination = 'destination'
185-
vi.mocked(fileExists).mockResolvedValue(false)
167+
await inTemporaryDirectory(async (tmpDir) => {
168+
const repoUrl = 'http://repoUrl'
169+
const destination = `${tmpDir}/nonexistent`
186170

187-
await git.downloadGitRepository({repoUrl, destination})
171+
await git.downloadGitRepository({repoUrl, destination})
188172

189-
expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
173+
expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
174+
})
190175
})
191176
})
192177

@@ -203,18 +188,17 @@ describe('initializeRepository()', () => {
203188

204189
describe('createGitIgnore()', () => {
205190
test('writes to a file in the provided directory', async () => {
206-
const mockedAppendSync = vi.fn()
207-
vi.mocked(appendFileSync).mockImplementation(mockedAppendSync)
208-
const directory = '/unit/test'
209-
const template = {
210-
section: ['first', 'second'],
211-
}
212-
213-
git.createGitIgnore(directory, template)
214-
215-
expect(mockedAppendSync).toHaveBeenCalledOnce()
216-
expect(mockedAppendSync.mock.lastCall?.[0]).toBe(`${directory}/.gitignore`)
217-
expect(mockedAppendSync.mock.lastCall?.[1]).toBe('# section\nfirst\nsecond\n\n')
191+
await inTemporaryDirectory(async (tmpDir) => {
192+
const template = {
193+
section: ['first', 'second'],
194+
}
195+
196+
git.createGitIgnore(tmpDir, template)
197+
198+
const gitIgnorePath = `${tmpDir}/.gitignore`
199+
expect(fileExistsSync(gitIgnorePath)).toBe(true)
200+
expect(readFileSync(gitIgnorePath).toString()).toBe('# section\nfirst\nsecond\n\n')
201+
})
218202
})
219203
})
220204

0 commit comments

Comments
 (0)