github.com/rivy-go/git-changelog@v0.0.0-20240424224517-b86e6ab57773/cmd/git-changelog/cli_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 "path/filepath" 8 "regexp" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 13 changelog "github.com/rivy-go/git-changelog/internal/changelog" 14 ) 15 16 func TestCLIForStdout(t *testing.T) { 17 assert := assert.New(t) 18 assert.True(true) 19 20 stdout := &bytes.Buffer{} 21 stderr := &bytes.Buffer{} 22 mockFS := &mockFileSystem{} 23 24 configLoader := &mockConfigLoaderImpl{ 25 ReturnLoad: func(path string) (*Config, error) { 26 if path != "/.changelog/config.yml" { 27 return nil, errors.New("") 28 } 29 return &Config{ 30 Bin: "/custom/bin/git", 31 }, nil 32 }, 33 } 34 35 generator := &mockGeneratorImpl{ 36 ReturnGenerate: func(w io.Writer, query string, config *changelog.Config) error { 37 if config.Bin != "/custom/bin/git" { 38 return errors.New("") 39 } 40 w.Write([]byte("success!!")) 41 return nil 42 }, 43 } 44 45 c := NewCLI( 46 &CLIContext{ 47 WorkingDir: "/", 48 ConfigPath: "/.changelog/config.yml", 49 OutputPath: "", 50 Stdout: stdout, 51 Stderr: stderr, 52 }, 53 mockFS, 54 configLoader, 55 generator, 56 ) 57 58 assert.Equal(ExitCodeOK, c.Run()) 59 assert.Equal("", stderr.String()) 60 assert.Equal("success!!", stdout.String()) 61 } 62 63 func TestCLIForFile(t *testing.T) { 64 assert := assert.New(t) 65 assert.True(true) 66 67 stdout := &bytes.Buffer{} 68 stderr := &bytes.Buffer{} 69 70 mockFS := &mockFileSystem{ 71 ReturnMkdirP: func(path string) error { 72 if filepath.ToSlash(path) != "/dir/to" { 73 return errors.New("") 74 } 75 return nil 76 }, 77 ReturnCreate: func(name string) (File, error) { 78 if filepath.ToSlash(name) != "/dir/to/CHANGELOG.tpl" { 79 return nil, errors.New("") 80 } 81 return &mockFile{ 82 ReturnWrite: func(b []byte) (int, error) { 83 if string(b) != "success!!" { 84 return 0, errors.New("") 85 } 86 return 0, nil 87 }, 88 }, nil 89 }, 90 } 91 92 configLoader := &mockConfigLoaderImpl{ 93 ReturnLoad: func(path string) (*Config, error) { 94 if filepath.ToSlash(path) != "/.changelog/config.yml" { 95 return nil, errors.New("") 96 } 97 return &Config{ 98 Bin: "/custom/bin/git", 99 }, nil 100 }, 101 } 102 103 generator := &mockGeneratorImpl{ 104 ReturnGenerate: func(w io.Writer, query string, config *changelog.Config) error { 105 if filepath.ToSlash(config.Bin) != "/custom/bin/git" { 106 return errors.New("") 107 } 108 w.Write([]byte("success!!")) 109 return nil 110 }, 111 } 112 113 c := NewCLI( 114 &CLIContext{ 115 WorkingDir: "/", 116 ConfigPath: "/.changelog/config.yml", 117 OutputPath: "/dir/to/CHANGELOG.tpl", 118 Stdout: stdout, 119 Stderr: stderr, 120 }, 121 mockFS, 122 configLoader, 123 generator, 124 ) 125 126 assert.Equal(ExitCodeOK, c.Run()) 127 assert.Equal("", stderr.String()) 128 out := regexp.MustCompile("\x1b\\[[^a-z]*[a-z]").ReplaceAllString(stdout.String(), "") 129 assert.Contains(out, "Generate of \"/dir/to/CHANGELOG.tpl\"") 130 }