github.com/fredbi/git-chglog@v0.0.0-20190706071416-d35c598eac81/cmd/git-chglog/initializer_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestInitializer(t *testing.T) {
    13  	assert := assert.New(t)
    14  
    15  	stdout := &bytes.Buffer{}
    16  	stderr := &bytes.Buffer{}
    17  
    18  	mockFs := &mockFileSystem{
    19  		ReturnMkdirP: func(path string) error {
    20  			if path == filepath.FromSlash("/test/config") {
    21  				return nil
    22  			}
    23  			return errors.New("")
    24  		},
    25  		ReturnWriteFile: func(path string, content []byte) error {
    26  			if path == filepath.FromSlash("/test/config/config.yml") || path == filepath.FromSlash("/test/config/CHANGELOG.tpl.md") {
    27  				return nil
    28  			}
    29  			return errors.New("")
    30  		},
    31  	}
    32  
    33  	questioner := &mockQuestionerImpl{
    34  		ReturnAsk: func() (*Answer, error) {
    35  			return &Answer{
    36  				ConfigDir: "config",
    37  			}, nil
    38  		},
    39  	}
    40  
    41  	configBuilder := &mockConfigBuilderImpl{
    42  		ReturnBuild: func(ans *Answer) (string, error) {
    43  			if ans.ConfigDir == "config" {
    44  				return "config", nil
    45  			}
    46  			return "", errors.New("")
    47  		},
    48  	}
    49  
    50  	tplBuilder := &mockTemplateBuilderImpl{
    51  		ReturnBuild: func(ans *Answer) (string, error) {
    52  			if ans.ConfigDir == "config" {
    53  				return "template", nil
    54  			}
    55  			return "", errors.New("")
    56  		},
    57  	}
    58  
    59  	init := NewInitializer(
    60  		&InitContext{
    61  			WorkingDir: "/test",
    62  			Stdout:     stdout,
    63  			Stderr:     stderr,
    64  		},
    65  		mockFs,
    66  		questioner,
    67  		configBuilder,
    68  		func(t string) TemplateBuilder {
    69  			return tplBuilder
    70  		},
    71  	)
    72  
    73  	assert.Equal(ExitCodeOK, init.Run())
    74  	assert.Equal("", stderr.String())
    75  	assert.Contains(stdout.String(), "Configuration file and template")
    76  }