github.com/Ryooooooga/zouch@v0.3.9/pkg/commands/add_test.go (about) 1 package commands_test 2 3 import ( 4 "os" 5 "path" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func prepareTestDir(t *testing.T) string { 12 tempDir := t.TempDir() 13 14 _ = os.Mkdir(path.Join(tempDir, "test-dir"), 0755) 15 _ = os.WriteFile(path.Join(tempDir, "test.txt"), []byte("test"), 0644) 16 _ = os.WriteFile(path.Join(tempDir, "test2.txt"), []byte("test2"), 0644) 17 _ = os.WriteFile(path.Join(tempDir, "test-dir/hello.txt"), []byte("hello"), 0644) 18 19 return tempDir 20 } 21 22 func TestAdd(t *testing.T) { 23 scenarios := []struct { 24 testname string 25 files []string 26 force bool 27 expectedTemplates []string 28 }{ 29 { 30 testname: "test2.txt and hello.txt", 31 files: []string{"test2.txt", "./test-dir/hello.txt"}, 32 force: false, 33 expectedTemplates: []string{"_.txt", "hello.txt", "main.go", "test.txt", "test2.txt"}, 34 }, 35 { 36 testname: "force", 37 files: []string{"test.txt", "test-dir/hello.txt"}, 38 force: true, 39 expectedTemplates: []string{"_.txt", "hello.txt", "main.go", "test.txt"}, 40 }, 41 } 42 43 for _, s := range scenarios { 44 t.Run(s.testname, func(t *testing.T) { 45 tempDir := prepareTestDir(t) 46 _ = os.Chdir(tempDir) 47 48 cmd := newTestCommand(t, false, s.force) 49 50 err := cmd.Add(s.files) 51 assert.Nil(t, err) 52 53 templateFiles, err := cmd.Templates.ListTemplates() 54 assert.Nil(t, err) 55 assert.Equal(t, s.expectedTemplates, templateFiles) 56 }) 57 } 58 } 59 60 func TestFailAdd(t *testing.T) { 61 scenarios := []struct { 62 testname string 63 files []string 64 force bool 65 }{ 66 { 67 testname: "empty", 68 files: []string{}, 69 force: false, 70 }, 71 { 72 testname: "directory", 73 files: []string{"test-dir"}, 74 force: true, 75 }, 76 { 77 testname: "not exists", 78 files: []string{"NO_SUCH_FILE"}, 79 force: false, 80 }, 81 { 82 testname: "template exists", 83 files: []string{"test.txt"}, 84 force: false, 85 }, 86 } 87 88 for _, s := range scenarios { 89 t.Run(s.testname, func(t *testing.T) { 90 tempDir := prepareTestDir(t) 91 _ = os.Chdir(tempDir) 92 93 cmd := newTestCommand(t, false, s.force) 94 95 err := cmd.Add(s.files) 96 assert.Error(t, err) 97 }) 98 } 99 }