Skip to content

Commit

Permalink
config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenkoehler committed Feb 11, 2024
1 parent 1984bae commit 11bd491
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
13 changes: 7 additions & 6 deletions chdiff/chdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
_description string

//go:embed default-config.json
_defaultConfigJson string
_defaultConfigJson []byte
)

type cmdDigest struct {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (cmd *CmdVerify) Run(deps ChdiffDependencies) error {

func loadConfig() {
if err := json.Unmarshal(readConfigFile(), &common.Config); err != nil {
util.Fatal(err.Error())
util.Fatal("reading config: %s", err.Error())
}
util.SetLogLevelByName(common.Config.LogLevel)
util.Debug("%+v", common.Config)
Expand All @@ -120,13 +120,14 @@ func readConfigFile() []byte {
userhome, err := os.UserHomeDir()
if err != nil {
util.Warn("can't determine user home")
return []byte(_defaultConfigJson)
return _defaultConfigJson
}

data, err := os.ReadFile(filepath.Join(userhome, UserConfigFileName))
configFile := filepath.Join(userhome, UserConfigFileName)
data, err := os.ReadFile(configFile)
if err != nil {
util.Warn("can't read user config: %v", err)
return []byte(_defaultConfigJson)
os.WriteFile(configFile, _defaultConfigJson, 0744)
return _defaultConfigJson
}

return data
Expand Down
56 changes: 36 additions & 20 deletions chdiff/chdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ func (m *MockDependencies) KongExit() func(int) {
return m.Called().Get(0).(func(int))
}

type TestSuite struct {
type TSChdiff struct {
suite.Suite
Stdout *strings.Builder
Stderr *strings.Builder
Dependencies *MockDependencies
}

func TestSuiteRunner(t *testing.T) {
suite.Run(t, &TestSuite{})
suite.Run(t, &TSChdiff{})
}

func (s *TestSuite) SetupTest() {
func (s *TSChdiff) SetupTest() {
s.Stdout = &strings.Builder{}
s.Stderr = &strings.Builder{}
s.Dependencies = &MockDependencies{}
Expand All @@ -83,7 +83,7 @@ func (s *TestSuite) SetupTest() {
})
}

func (s *TestSuite) TestLoadConfig() {
func (s *TSChdiff) TestLoadConfig() {
s.T().Setenv("HOME", "../testdata/chdiff/userhome")
s.Dependencies.Mock.On("exit", mock.Anything).Return()

Expand All @@ -95,7 +95,22 @@ func (s *TestSuite) TestLoadConfig() {
"[D] {Exclude:{Absolute:[] Relative:[] Anywhere:[]} LogLevel:debug}")
}

func (s *TestSuite) TestLoadConfigBadUserHome() {
func (s *TSChdiff) TestLoadConfigCreateMissingFile() {
tempUserhome := s.T().TempDir()
s.T().Setenv("HOME", tempUserhome)
s.Dependencies.Mock.On("exit", mock.Anything).Return()

chdiff.Chdiff("TEST", []string{""}, s.Dependencies)

s.Dependencies.AssertExpectations(s.T())
assert.FileExists(s.T(), filepath.Join(tempUserhome, chdiff.UserConfigFileName))
assert.Contains(
s.T(), s.Stderr.String(),
// Attention: Kong's error message contains double space between commands
"error: expected one of \"create\", \"verify\"\n")
}

func (s *TSChdiff) TestLoadConfigBadUserhome() {
s.T().Setenv("HOME", "")
s.Dependencies.Mock.On("exit", mock.Anything).Return()

Expand All @@ -107,45 +122,46 @@ func (s *TestSuite) TestLoadConfigBadUserHome() {
"[W] can't determine user home")
}

func (s *TestSuite) TestLoadConfigBadJson() {
func (s *TSChdiff) TestLoadConfigBadJson() {
s.T().Setenv("HOME", "../testdata/chdiff/userhome-with-bad-config")
s.Dependencies.Mock.On("exit", mock.Anything).Return()

assert.PanicsWithError(s.T(), `/!\ invalid character 'i' looking for beginning of value`, func() {
chdiff.Chdiff("TEST", []string{""}, s.Dependencies)
})
assert.PanicsWithError(s.T(),
`/!\ reading config: invalid character 'i' looking for beginning of value`, func() {
chdiff.Chdiff("TEST", []string{""}, s.Dependencies)
})
}

func (s *TestSuite) TestNoCommand() {
func (s *TSChdiff) TestNoCommand() {
testErrorMessage(s,
[]string{""},
// Attention: Kong's error message contains double space between commands
"error: expected one of \"create\", \"verify\"\n")
}

func (s *TestSuite) TestUnknownCommand() {
func (s *TSChdiff) TestUnknownCommand() {
testErrorMessage(s,
[]string{"", "bad-command"},
"error: unexpected argument bad-command\n")
}

func (s *TestSuite) TestVerifyWithoutPath() {
func (s *TSChdiff) TestVerifyWithoutPath() {
testDigestVerify(s,
[]string{"", "v"},
".",
chdiff.DefaultDigestName,
digest.SHA256)
}

func (s *TestSuite) TestVerifyWithPath() {
func (s *TSChdiff) TestVerifyWithPath() {
testDigestVerify(s,
[]string{"", "v", "x"},
"x",
chdiff.DefaultDigestName,
digest.SHA256)
}

func (s *TestSuite) TestDigestVerifyMissingDigestFile() {
func (s *TSChdiff) TestDigestVerifyMissingDigestFile() {

absDataPath, _ := filepath.Abs("x")
absDigestFile := filepath.Join(absDataPath, chdiff.DefaultDigestName)
Expand All @@ -162,7 +178,7 @@ func (s *TestSuite) TestDigestVerifyMissingDigestFile() {
assert.Contains(s.T(), s.Stderr.String(), "[E] read error")
}

func (s *TestSuite) TestDigestCreateSHA256DefaultName() {
func (s *TSChdiff) TestDigestCreateSHA256DefaultName() {
absDataPath, _ := filepath.Abs("x")
absDigestFile := filepath.Join(absDataPath, chdiff.DefaultDigestName)

Expand All @@ -175,7 +191,7 @@ func (s *TestSuite) TestDigestCreateSHA256DefaultName() {
s.Dependencies.AssertExpectations(s.T())
}

func (s *TestSuite) TestDigestCreateSHA256ExplicitName() {
func (s *TSChdiff) TestDigestCreateSHA256ExplicitName() {
absDataPath, _ := filepath.Abs("x")
absDigestPath, _ := filepath.Abs("y")
absDigestFile := filepath.Join(absDigestPath, "explicit")
Expand All @@ -189,7 +205,7 @@ func (s *TestSuite) TestDigestCreateSHA256ExplicitName() {
s.Dependencies.AssertExpectations(s.T())
}

func (s *TestSuite) TestDigestCreateSHA512() {
func (s *TSChdiff) TestDigestCreateSHA512() {
absDataPath, _ := filepath.Abs("x")
absDigestFile := filepath.Join(absDataPath, chdiff.DefaultDigestName)

Expand All @@ -202,7 +218,7 @@ func (s *TestSuite) TestDigestCreateSHA512() {
s.Dependencies.AssertExpectations(s.T())
}

func (s *TestSuite) TestDigestCreateBadAlgorithm() {
func (s *TSChdiff) TestDigestCreateBadAlgorithm() {
s.Dependencies.Mock.On("exit", mock.Anything).Return()

chdiff.Chdiff("TEST", []string{"", "c", "-a", "WRONG", "x"}, s.Dependencies)
Expand All @@ -212,7 +228,7 @@ func (s *TestSuite) TestDigestCreateBadAlgorithm() {
}

func testErrorMessage(
s *TestSuite,
s *TSChdiff,
args []string,
expected string) {

Expand All @@ -225,7 +241,7 @@ func testErrorMessage(
}

func testDigestVerify(
s *TestSuite,
s *TSChdiff,
args []string,
dataPath, digestPath string,
algorithm digest.HashType) {
Expand Down

0 comments on commit 11bd491

Please sign in to comment.