|
1 | 1 | import * as git from './git.js' |
2 | | -import {appendFileSync, fileExistsSync, inTemporaryDirectory, readFileSync, writeFileSync} from './fs.js' |
| 2 | +import { |
| 3 | + appendFileSync, |
| 4 | + fileExists, |
| 5 | + fileExistsSync, |
| 6 | + glob, |
| 7 | + inTemporaryDirectory, |
| 8 | + isDirectory, |
| 9 | + readFileSync, |
| 10 | + writeFileSync, |
| 11 | +} from './fs.js' |
3 | 12 | import {hasGit} from './context/local.js' |
4 | 13 | import {beforeEach, describe, expect, test, vi} from 'vitest' |
5 | 14 | import simpleGit from 'simple-git' |
@@ -38,6 +47,9 @@ vi.mock('./fs.js', async () => { |
38 | 47 | return { |
39 | 48 | ...fs, |
40 | 49 | appendFileSync: vi.fn(), |
| 50 | + fileExists: vi.fn(), |
| 51 | + isDirectory: vi.fn(), |
| 52 | + glob: vi.fn(), |
41 | 53 | } |
42 | 54 | }) |
43 | 55 |
|
@@ -151,6 +163,83 @@ describe('downloadRepository()', async () => { |
151 | 163 | expect(mockedClone).toHaveBeenCalledWith('http://repoUrl', destination, options) |
152 | 164 | expect(mockCheckout).toHaveBeenCalledWith(expectedLatestTag) |
153 | 165 | }) |
| 166 | + |
| 167 | + test('throws when destination exists as a file', async () => { |
| 168 | + await expect(async () => { |
| 169 | + // Given |
| 170 | + const repoUrl = 'http://repoUrl' |
| 171 | + const destination = 'destination' |
| 172 | + vi.mocked(fileExists).mockResolvedValue(true) |
| 173 | + vi.mocked(isDirectory).mockResolvedValue(false) |
| 174 | + |
| 175 | + // When |
| 176 | + await git.downloadGitRepository({repoUrl, destination}) |
| 177 | + |
| 178 | + // Then |
| 179 | + }).rejects.toThrowError(/Can't clone to/) |
| 180 | + }) |
| 181 | + |
| 182 | + test('throws when destination directory is not empty', async () => { |
| 183 | + await expect(async () => { |
| 184 | + // Given |
| 185 | + const repoUrl = 'http://repoUrl' |
| 186 | + const destination = 'destination' |
| 187 | + vi.mocked(fileExists).mockResolvedValue(true) |
| 188 | + vi.mocked(isDirectory).mockResolvedValue(true) |
| 189 | + vi.mocked(glob).mockResolvedValue(['file1.txt', 'file2.txt']) |
| 190 | + |
| 191 | + // When |
| 192 | + await git.downloadGitRepository({repoUrl, destination}) |
| 193 | + |
| 194 | + // Then |
| 195 | + }).rejects.toThrowError(/already exists and is not empty/) |
| 196 | + }) |
| 197 | + |
| 198 | + test('throws when destination contains only hidden files', async () => { |
| 199 | + await expect(async () => { |
| 200 | + // Given |
| 201 | + const repoUrl = 'http://repoUrl' |
| 202 | + const destination = 'destination' |
| 203 | + vi.mocked(fileExists).mockResolvedValue(true) |
| 204 | + vi.mocked(isDirectory).mockResolvedValue(true) |
| 205 | + vi.mocked(glob).mockResolvedValue(['.git', '.DS_Store']) |
| 206 | + |
| 207 | + // When |
| 208 | + await git.downloadGitRepository({repoUrl, destination}) |
| 209 | + |
| 210 | + // Then |
| 211 | + }).rejects.toThrowError(/already exists and is not empty/) |
| 212 | + }) |
| 213 | + |
| 214 | + test('succeeds when destination directory is empty', async () => { |
| 215 | + // Given |
| 216 | + const repoUrl = 'http://repoUrl' |
| 217 | + const destination = 'destination' |
| 218 | + const options: any = {'--recurse-submodules': null} |
| 219 | + vi.mocked(fileExists).mockResolvedValue(true) |
| 220 | + vi.mocked(isDirectory).mockResolvedValue(true) |
| 221 | + vi.mocked(glob).mockResolvedValue([]) |
| 222 | + |
| 223 | + // When |
| 224 | + await git.downloadGitRepository({repoUrl, destination}) |
| 225 | + |
| 226 | + // Then |
| 227 | + expect(mockedClone).toHaveBeenCalledWith(repoUrl, destination, options) |
| 228 | + }) |
| 229 | + |
| 230 | + test('succeeds when destination does not exist', async () => { |
| 231 | + // Given |
| 232 | + const repoUrl = 'http://repoUrl' |
| 233 | + const destination = 'destination' |
| 234 | + const options: any = {'--recurse-submodules': null} |
| 235 | + vi.mocked(fileExists).mockResolvedValue(false) |
| 236 | + |
| 237 | + // When |
| 238 | + await git.downloadGitRepository({repoUrl, destination}) |
| 239 | + |
| 240 | + // Then |
| 241 | + expect(mockedClone).toHaveBeenCalledWith(repoUrl, destination, options) |
| 242 | + }) |
154 | 243 | }) |
155 | 244 |
|
156 | 245 | describe('initializeRepository()', () => { |
|
0 commit comments