@@ -14,6 +14,11 @@ import (
1414 "github.com/stretchr/testify/require"
1515)
1616
17+ const (
18+ DIR_PERM = 0755 // rwxr-xr-x
19+ FILE_PERM = 0644 // rw-r--r--
20+ )
21+
1722func TestCreateCommand (t * testing.T ) {
1823 tests := []struct {
1924 name string
@@ -145,7 +150,7 @@ func TestAllTemplatesCreation(t *testing.T) {
145150 appPath := filepath .Join (tmpDir , appName )
146151
147152 // Create app directory
148- err := os .MkdirAll (appPath , 0755 )
153+ err := os .MkdirAll (appPath , DIR_PERM )
149154 require .NoError (t , err , "failed to create app directory" )
150155
151156 // Copy template files without installing dependencies
@@ -352,6 +357,61 @@ func TestCreateCommand_RequiredToolMissing(t *testing.T) {
352357 }
353358}
354359
360+ // TestCreateCommand_DirectoryOverwrite tests that overwriting an existing directory
361+ // properly removes old content and creates new content
362+ func TestCreateCommand_DirectoryOverwrite (t * testing.T ) {
363+ tmpDir := t .TempDir ()
364+ appName := "test-app"
365+ appPath := filepath .Join (tmpDir , appName )
366+
367+ orgDir , err := os .Getwd ()
368+ require .NoError (t , err )
369+
370+ err = os .Chdir (tmpDir )
371+ require .NoError (t , err )
372+
373+ t .Cleanup (func () {
374+ os .Chdir (orgDir )
375+ })
376+
377+ // Initialize directory with some files
378+ err = os .MkdirAll (appPath , DIR_PERM )
379+ require .NoError (t , err , "failed to create initial directory" )
380+
381+ // Create some initial files that should be removed after overwrite
382+ oldFile1 := filepath .Join (appPath , "old-file-1.txt" )
383+ oldSubDir := filepath .Join (appPath , "old-subdir" )
384+
385+ err = os .WriteFile (oldFile1 , []byte ("old content 1" ), FILE_PERM )
386+ require .NoError (t , err , "failed to create old file 1" )
387+
388+ err = os .MkdirAll (oldSubDir , DIR_PERM )
389+ require .NoError (t , err , "failed to create old subdirectory" )
390+
391+ // Verify initial files exist
392+ assert .FileExists (t , oldFile1 , "old file 1 should exist before overwrite" )
393+ assert .DirExists (t , oldSubDir , "old subdirectory should exist before overwrite" )
394+
395+ // Manually remove the directory and create the new app
396+ err = os .RemoveAll (appPath )
397+ require .NoError (t , err , "failed to remove existing directory" )
398+
399+ c := CreateCmd {}
400+ err = c .Create (context .Background (), CreateInput {
401+ Name : appName ,
402+ Language : create .LanguageTypeScript ,
403+ Template : "sample-app" ,
404+ })
405+ require .NoError (t , err , "failed to create new app" )
406+
407+ // Verify old files are gone
408+ assert .NoFileExists (t , oldFile1 , "old file 1 should not exist after overwrite" )
409+ assert .NoDirExists (t , oldSubDir , "old subdirectory should not exist after overwrite" )
410+
411+ // Verify new template files exist
412+ assert .FileExists (t , filepath .Join (appPath , "index.ts" ), "new index.ts should exist" )
413+ }
414+
355415func getTemplateInfo () []struct {
356416 name string
357417 language string
0 commit comments