11import * as git from './git.js'
2- import { appendFileSync } from './fs.js'
2+ import { appendFileSync , fileExistsSync , inTemporaryDirectory , readFileSync , writeFileSync } from './fs.js'
33import { hasGit } from './context/local.js'
44import { beforeEach , describe , expect , test , vi } from 'vitest'
55import simpleGit from 'simple-git'
@@ -25,9 +25,15 @@ const simpleGitProperties = {
2525 status : mockedGitStatus ,
2626}
2727
28- vi . mock ( './context/local.js' )
29- vi . mock ( './fs.js' )
3028vi . mock ( 'simple-git' )
29+ vi . mock ( './context/local.js' )
30+ vi . mock ( './fs.js' , async ( ) => {
31+ const fs = await vi . importActual ( './fs.js' )
32+ return {
33+ ...fs ,
34+ appendFileSync : vi . fn ( ) ,
35+ }
36+ } )
3137
3238beforeEach ( ( ) => {
3339 vi . mocked ( hasGit ) . mockResolvedValue ( true )
@@ -375,3 +381,67 @@ describe('isGitClean()', () => {
375381 expect ( simpleGit ) . toHaveBeenCalledWith ( { baseDir : directory } )
376382 } )
377383} )
384+
385+ describe ( 'addToGitIgnore()' , ( ) => {
386+ test ( 'does nothing when .gitignore does not exist' , async ( ) => {
387+ await inTemporaryDirectory ( async ( tmpDir ) => {
388+ // Given
389+ const gitIgnorePath = `${ tmpDir } /.gitignore`
390+
391+ // When
392+ git . addToGitIgnore ( tmpDir , '.shopify' )
393+
394+ // Then
395+ expect ( fileExistsSync ( gitIgnorePath ) ) . toBe ( false )
396+ } )
397+ } )
398+
399+ test ( 'does nothing when pattern already exists in .gitignore' , async ( ) => {
400+ await inTemporaryDirectory ( async ( tmpDir ) => {
401+ // Given
402+ const gitIgnorePath = `${ tmpDir } /.gitignore`
403+ const gitIgnoreContent = ' .shopify \nnode_modules\n'
404+
405+ writeFileSync ( gitIgnorePath , gitIgnoreContent )
406+
407+ // When
408+ git . addToGitIgnore ( tmpDir , '.shopify' )
409+
410+ // Then
411+ const actualContent = readFileSync ( gitIgnorePath ) . toString ( )
412+ expect ( actualContent ) . toBe ( gitIgnoreContent )
413+ } )
414+ } )
415+
416+ test ( 'appends pattern to .gitignore when file exists and pattern not present' , async ( ) => {
417+ await inTemporaryDirectory ( async ( tmpDir ) => {
418+ // Given
419+ const gitIgnorePath = `${ tmpDir } /.gitignore`
420+
421+ writeFileSync ( gitIgnorePath , 'node_modules\ndist' )
422+
423+ // When
424+ git . addToGitIgnore ( tmpDir , '.shopify' )
425+
426+ // Then
427+ const gitIgnoreContent = readFileSync ( gitIgnorePath ) . toString ( )
428+ expect ( gitIgnoreContent ) . toBe ( 'node_modules\ndist\n.shopify\n' )
429+ } )
430+ } )
431+
432+ test ( 'appends pattern to .gitignore when file exists and pattern not present without duplicating the last empty line' , async ( ) => {
433+ await inTemporaryDirectory ( async ( tmpDir ) => {
434+ // Given
435+ const gitIgnorePath = `${ tmpDir } /.gitignore`
436+
437+ writeFileSync ( gitIgnorePath , 'node_modules\ndist\n' )
438+
439+ // When
440+ git . addToGitIgnore ( tmpDir , '.shopify' )
441+
442+ // Then
443+ const gitIgnoreContent = readFileSync ( gitIgnorePath ) . toString ( )
444+ expect ( gitIgnoreContent ) . toBe ( 'node_modules\ndist\n.shopify\n' )
445+ } )
446+ } )
447+ } )
0 commit comments