Skip to content
Merged
Changes from all commits
Commits
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
112 changes: 48 additions & 64 deletions packages/cli-kit/src/public/node/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import * as git from './git.js'
import {
appendFileSync,
fileExists,
fileExistsSync,
glob,
inTemporaryDirectory,
isDirectory,
readFileSync,
writeFileSync,
} from './fs.js'
import {fileExistsSync, inTemporaryDirectory, mkdirSync, readFileSync, writeFileSync} from './fs.js'
import {hasGit, isTerminalInteractive} from './context/local.js'
import {beforeEach, describe, expect, test, vi} from 'vitest'
import {execa} from 'execa'

vi.mock('execa')
vi.mock('./context/local.js')
vi.mock('./fs.js', async () => {
const fs = await vi.importActual('./fs.js')
return {
...fs,
appendFileSync: vi.fn(),
fileExists: vi.fn(),
isDirectory: vi.fn(),
glob: vi.fn(),
}
})

const mockedExeca = vi.mocked(execa)

Expand Down Expand Up @@ -133,60 +114,64 @@ describe('downloadRepository()', async () => {
})

test('throws when destination exists as a file', async () => {
await expect(async () => {
await inTemporaryDirectory(async (tmpDir) => {
const repoUrl = 'http://repoUrl'
const destination = 'destination'
vi.mocked(fileExists).mockResolvedValue(true)
vi.mocked(isDirectory).mockResolvedValue(false)
const destination = `${tmpDir}/file.txt`
writeFileSync(destination, '')

await git.downloadGitRepository({repoUrl, destination})
}).rejects.toThrowError(/Can't clone to/)
await expect(async () => {
await git.downloadGitRepository({repoUrl, destination})
}).rejects.toThrowError(/Can't clone to/)
})
})

test('throws when destination directory is not empty', async () => {
await expect(async () => {
await inTemporaryDirectory(async (tmpDir) => {
const repoUrl = 'http://repoUrl'
const destination = 'destination'
vi.mocked(fileExists).mockResolvedValue(true)
vi.mocked(isDirectory).mockResolvedValue(true)
vi.mocked(glob).mockResolvedValue(['file1.txt', 'file2.txt'])
const destination = `${tmpDir}/dir`
mkdirSync(destination)
writeFileSync(`${destination}/file1.txt`, '')

await git.downloadGitRepository({repoUrl, destination})
}).rejects.toThrowError(/already exists and is not empty/)
await expect(async () => {
await git.downloadGitRepository({repoUrl, destination})
}).rejects.toThrowError(/already exists and is not empty/)
})
})

test('throws when destination contains only hidden files', async () => {
await expect(async () => {
await inTemporaryDirectory(async (tmpDir) => {
const repoUrl = 'http://repoUrl'
const destination = 'destination'
vi.mocked(fileExists).mockResolvedValue(true)
vi.mocked(isDirectory).mockResolvedValue(true)
vi.mocked(glob).mockResolvedValue(['.git', '.DS_Store'])
const destination = `${tmpDir}/dir`
mkdirSync(destination)
writeFileSync(`${destination}/.git`, '')

await git.downloadGitRepository({repoUrl, destination})
}).rejects.toThrowError(/already exists and is not empty/)
await expect(async () => {
await git.downloadGitRepository({repoUrl, destination})
}).rejects.toThrowError(/already exists and is not empty/)
})
})

test('succeeds when destination directory is empty', async () => {
const repoUrl = 'http://repoUrl'
const destination = 'destination'
vi.mocked(fileExists).mockResolvedValue(true)
vi.mocked(isDirectory).mockResolvedValue(true)
vi.mocked(glob).mockResolvedValue([])
await inTemporaryDirectory(async (tmpDir) => {
const repoUrl = 'http://repoUrl'
const destination = `${tmpDir}/dir`
mkdirSync(destination)

await git.downloadGitRepository({repoUrl, destination})
await git.downloadGitRepository({repoUrl, destination})

expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
})
})

test('succeeds when destination does not exist', async () => {
const repoUrl = 'http://repoUrl'
const destination = 'destination'
vi.mocked(fileExists).mockResolvedValue(false)
await inTemporaryDirectory(async (tmpDir) => {
const repoUrl = 'http://repoUrl'
const destination = `${tmpDir}/nonexistent`

await git.downloadGitRepository({repoUrl, destination})
await git.downloadGitRepository({repoUrl, destination})

expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
expect(mockedExeca).toHaveBeenCalledWith('git', ['clone', '--recurse-submodules', repoUrl, destination])
})
})
})

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

describe('createGitIgnore()', () => {
test('writes to a file in the provided directory', async () => {
const mockedAppendSync = vi.fn()
vi.mocked(appendFileSync).mockImplementation(mockedAppendSync)
const directory = '/unit/test'
const template = {
section: ['first', 'second'],
}

git.createGitIgnore(directory, template)

expect(mockedAppendSync).toHaveBeenCalledOnce()
expect(mockedAppendSync.mock.lastCall?.[0]).toBe(`${directory}/.gitignore`)
expect(mockedAppendSync.mock.lastCall?.[1]).toBe('# section\nfirst\nsecond\n\n')
await inTemporaryDirectory(async (tmpDir) => {
const template = {
section: ['first', 'second'],
}

git.createGitIgnore(tmpDir, template)

const gitIgnorePath = `${tmpDir}/.gitignore`
expect(fileExistsSync(gitIgnorePath)).toBe(true)
expect(readFileSync(gitIgnorePath).toString()).toBe('# section\nfirst\nsecond\n\n')
})
})
})

Expand Down
Loading