11import * 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'
123import { hasGit , isTerminalInteractive } from './context/local.js'
134import { beforeEach , describe , expect , test , vi } from 'vitest'
145import { execa } from 'execa'
156
167vi . mock ( 'execa' )
178vi . 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
2910const 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 ( / C a n ' t c l o n e t o / )
122+ await expect ( async ( ) => {
123+ await git . downloadGitRepository ( { repoUrl, destination} )
124+ } ) . rejects . toThrowError ( / C a n ' t c l o n e t o / )
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 ( / a l r e a d y e x i s t s a n d i s n o t e m p t y / )
135+ await expect ( async ( ) => {
136+ await git . downloadGitRepository ( { repoUrl, destination} )
137+ } ) . rejects . toThrowError ( / a l r e a d y e x i s t s a n d i s n o t e m p t y / )
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 ( / a l r e a d y e x i s t s a n d i s n o t e m p t y / )
148+ await expect ( async ( ) => {
149+ await git . downloadGitRepository ( { repoUrl, destination} )
150+ } ) . rejects . toThrowError ( / a l r e a d y e x i s t s a n d i s n o t e m p t y / )
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
204189describe ( '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