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