@@ -6,20 +6,26 @@ package cmd_test
66
77import (
88 "bytes"
9+ "errors"
910 "fmt"
11+ "io/ioutil"
12+ "path"
1013 "testing"
1114 "time"
1215
1316 "github.com/elastic/elastic-agent-changelog-tool/cmd"
1417 "github.com/elastic/elastic-agent-changelog-tool/internal/changelog"
1518 "github.com/elastic/elastic-agent-changelog-tool/internal/changelog/fragment"
19+ "github.com/elastic/elastic-agent-changelog-tool/internal/settings"
1620 "github.com/spf13/afero"
1721 "github.com/spf13/viper"
1822 "github.com/stretchr/testify/require"
1923 "gopkg.in/yaml.v3"
2024)
2125
22- func TestBuildCmd_default (t * testing.T ) {
26+ func TestBuildCmd (t * testing.T ) {
27+ settings .Init ()
28+
2329 testFs := afero .NewMemMapFs ()
2430 c := fragment .NewCreator (testFs , viper .GetString ("fragment_location" ))
2531 err := c .Create ("foo" )
@@ -31,21 +37,37 @@ func TestBuildCmd_default(t *testing.T) {
3137
3238 cmd := cmd .BuildCmd (testFs )
3339
40+ expectedVersion := "0.0.0"
41+ cmd .SetArgs ([]string {
42+ fmt .Sprintf ("--version=%s" , expectedVersion ),
43+ })
44+
3445 b := new (bytes.Buffer )
3546 cmd .SetOut (b )
3647
3748 err = cmd .Execute ()
3849 require .Nil (t , err )
3950
40- content , err := afero .ReadFile (testFs , viper .GetString ("changelog_destination" ))
51+ changelogFile := path .Join (viper .GetString ("changelog_destination" ), viper .GetString ("changelog_filename" ))
52+ content , err := afero .ReadFile (testFs , changelogFile )
4153 require .Nil (t , err )
4254
4355 ch := changelog.Changelog {}
4456 err = yaml .Unmarshal (content , & ch )
4557 require .Nil (t , err )
4658
47- fmt .Println (ch )
48-
49- require .Equal (t , "8.2.1" , ch .Version )
59+ require .Equal (t , expectedVersion , ch .Version )
5060 require .Len (t , ch .Entries , 2 )
5161}
62+
63+ func TestBuildCmd_missingFlag (t * testing.T ) {
64+ testFs := afero .NewMemMapFs ()
65+ cmd := cmd .BuildCmd (testFs )
66+
67+ cmd .SetOut (ioutil .Discard )
68+ cmd .SetErr (ioutil .Discard )
69+
70+ err := cmd .Execute ()
71+ expectedErr := errors .New ("required flag(s) \" version\" not set" )
72+ require .Error (t , expectedErr , err )
73+ }
0 commit comments