Skip to content

Commit

Permalink
fix: now fallback .sauced.yaml contents get read (#135)
Browse files Browse the repository at this point in the history
* fix: now the repository root .sauced.yaml gets it's contents read

* test: now there is a check for the fallback config file contents

* test: updated tests for checking existing config file

* removed unnecesary log statements
  • Loading branch information
nickytonline authored Sep 5, 2024
1 parent 90b8320 commit fd658e5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const DefaultConfigPath = "~/.sauced.yaml"
// empty path is provided. If none is found in the user's home directory, it tries to load
// ".sauced.yaml" from the fallback path, which is the root path of a repository.
func LoadConfig(path string, repoRootPathConfig string) (*Spec, error) {
println("Config path loading from -c flag", path)

config := &Spec{}

if path == DefaultConfigPath || path == "" {
Expand All @@ -41,6 +43,12 @@ func LoadConfig(path string, repoRootPathConfig string) (*Spec, error) {
if err != nil {
return nil, fmt.Errorf("error reading config file from %s or %s", absPath, repoRootPathConfig)
}

data, err = os.ReadFile(repoRootPathConfig)

if err != nil {
return nil, fmt.Errorf("error reading config file from %s", repoRootPathConfig)
}
} else {
return nil, fmt.Errorf("error reading config file: %w", err)
}
Expand Down
54 changes: 52 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,30 @@ func TestLoadConfig(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
configFilePath := filepath.Join(tmpDir, ".sauced.yaml")
require.NoError(t, os.WriteFile(configFilePath, []byte("key: value"), 0644))

fileContents := `# Configuration for attributing commits with emails to GitHub user profiles
# Used during codeowners generation.
# List the emails associated with the given username.
# The commits associated with these emails will be attributed to
# the username in this yaml map. Any number of emails may be listed.
attribution:
brandonroberts:
- [email protected]
jpmcb:
- [email protected]`

require.NoError(t, os.WriteFile(configFilePath, []byte(fileContents), 0644))

config, err := LoadConfig(configFilePath, "")
assert.NoError(t, err)
assert.NotNil(t, config)

// Assert that config contains all the Attributions in fileContents
assert.Equal(t, 2, len(config.Attributions))

// Check specific attributions
assert.Equal(t, []string{"[email protected]"}, config.Attributions["brandonroberts"])
assert.Equal(t, []string{"[email protected]"}, config.Attributions["jpmcb"])
})

t.Run("Non-existent file", func(t *testing.T) {
Expand All @@ -35,14 +54,45 @@ func TestLoadConfig(t *testing.T) {

t.Run("Non-existent file with fallback", func(t *testing.T) {
t.Parallel()
fileContents := `# Configuration for attributing commits with emails to GitHub user profiles
# Used during codeowners generation.
# List the emails associated with the given username.
# The commits associated with these emails will be attributed to
# the username in this yaml map. Any number of emails may be listed.
attribution:
brandonroberts:
- [email protected]
jpmcb:
- [email protected]
nickytonline:
- [email protected]
- [email protected]
zeucapua:
- [email protected]`

tmpDir := t.TempDir()
fallbackPath := filepath.Join(tmpDir, ".sauced.yaml")
require.NoError(t, os.WriteFile(fallbackPath, []byte("key: fallback"), 0644))
require.NoError(t, os.WriteFile(fallbackPath, []byte(fileContents), 0644))

// Print out the contents of the file we just wrote
_, err := os.ReadFile(fallbackPath)
require.NoError(t, err)

nonExistentPath := filepath.Join(tmpDir, "non-existent.yaml")

config, err := LoadConfig(nonExistentPath, fallbackPath)

assert.NoError(t, err)
assert.NotNil(t, config)

// Assert that config contains all the Attributions in fileContents
assert.Equal(t, 4, len(config.Attributions))

// Check specific attributions
assert.Equal(t, []string{"[email protected]"}, config.Attributions["brandonroberts"])
assert.Equal(t, []string{"[email protected]"}, config.Attributions["jpmcb"])
assert.Equal(t, []string{"[email protected]", "[email protected]"}, config.Attributions["nickytonline"])
assert.Equal(t, []string{"[email protected]"}, config.Attributions["zeucapua"])
})

t.Run("Default path", func(t *testing.T) {
Expand Down

0 comments on commit fd658e5

Please sign in to comment.