-
Notifications
You must be signed in to change notification settings - Fork 1
ROX-35667: configurable user config path via ROXIE_USER_CONFIG_PATH #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+176
−5
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package paths | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| var ( | ||
| defaultUserConfigPath = func() string { | ||
| // Ok to panic here. The tests would fail one way or another anyway. | ||
| path, err := getDefaultUserConfigPath() | ||
| if err != nil { | ||
| panic(fmt.Sprintf("failed to obtain default user config path: %v", err)) | ||
| } | ||
| return path | ||
| }() | ||
| ) | ||
|
|
||
| func TestUserConfigPath(t *testing.T) { | ||
| existentUserConfig := createTempFile(t, "cfg.yaml") | ||
| tests := []struct { | ||
| name string | ||
| envValue string | ||
| setEnv bool | ||
| expected string | ||
| validateUserConfig bool | ||
| expectErr bool | ||
| }{ | ||
| { | ||
| name: "returns env var value when set and file exists with validation", | ||
| envValue: existentUserConfig, | ||
| setEnv: true, | ||
| expected: existentUserConfig, | ||
| validateUserConfig: true, | ||
| }, | ||
| { | ||
| name: "validation returns error when env var value set to non-existent file", | ||
| envValue: "/non-existent/roxie.yaml", | ||
| setEnv: true, | ||
| validateUserConfig: true, | ||
| expectErr: true, | ||
| }, | ||
| { | ||
| name: "returns env var value when set to non-existent file with validation off", | ||
| envValue: "/non-existent/roxie.yaml", | ||
| setEnv: true, | ||
| expected: "/non-existent/roxie.yaml", | ||
| validateUserConfig: false, | ||
| }, | ||
| { | ||
| name: "returns default path when env var is not set with validation", | ||
| setEnv: false, | ||
| expected: defaultUserConfigPath, | ||
| validateUserConfig: true, | ||
| }, | ||
| { | ||
| name: "returns default path when env var is set but empty", | ||
| envValue: "", | ||
| setEnv: true, | ||
| expected: defaultUserConfigPath, | ||
| validateUserConfig: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cleanEnvironment(t) | ||
|
|
||
| if tt.setEnv { | ||
| t.Setenv(envUserConfigPath, tt.envValue) | ||
| } | ||
| got, err := UserConfigPath(tt.validateUserConfig) | ||
| if tt.expectErr { | ||
| assert.Error(t, err, "expected UserConfigPath() to return error") | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.expected, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestUserConfigPathString(t *testing.T) { | ||
| existentUserConfig := createTempFile(t, "cfg.yaml") | ||
| tests := []struct { | ||
| name string | ||
| envValue string | ||
| setEnv bool | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "returns env var value when set and file does not exist", | ||
| envValue: "/nonexistent/path.yaml", | ||
| setEnv: true, | ||
| expected: "/nonexistent/path.yaml", | ||
| }, | ||
| { | ||
| name: "returns env var value when set and file exists", | ||
| envValue: existentUserConfig, | ||
| setEnv: true, | ||
| expected: existentUserConfig, | ||
| }, | ||
| { | ||
| name: "returns default path when env var is not set", | ||
| setEnv: false, | ||
| expected: defaultUserConfigPath, | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| { | ||
| name: "returns default path when env var set but empty", | ||
| envValue: "", | ||
| setEnv: true, | ||
| expected: defaultUserConfigPath, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cleanEnvironment(t) | ||
|
|
||
| if tt.setEnv { | ||
| t.Setenv(envUserConfigPath, tt.envValue) | ||
| } | ||
| assert.Equal(t, tt.expected, UserConfigPathString()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func createTempFile(t *testing.T, name string) string { | ||
| tmpdir := t.TempDir() | ||
| path := tmpdir + "/" + name | ||
| err := os.WriteFile(path, nil, 0o600) | ||
| require.NoErrorf(t, err, "failed to create temporary file %q", path) | ||
| return path | ||
| } | ||
|
|
||
| func cleanEnvironment(t *testing.T) { | ||
| // Clean environment for the duration of this test case. | ||
| // The os.Unsetenv() in combination with the cleanup handler installed by t.Setenv() | ||
| // guarantees that for the duration of the test, there is *no* variable set in the | ||
| // environment at all, not even an empty one. | ||
| t.Setenv(envUserConfigPath, "") | ||
| os.Unsetenv(envUserConfigPath) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.