-
Notifications
You must be signed in to change notification settings - Fork 30
fix: PGHOST, PGPORT dotenv #41
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
Changes from all commits
Commits
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
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,114 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetEnvWithDefault(t *testing.T) { | ||
| // Test with existing env var | ||
| os.Setenv("TEST_STRING", "test-value") | ||
| if GetEnvWithDefault("TEST_STRING", "default") != "test-value" { | ||
| t.Errorf("Expected GetEnvWithDefault to return 'test-value', got '%s'", GetEnvWithDefault("TEST_STRING", "default")) | ||
| } | ||
|
|
||
| // Test with missing env var | ||
| os.Unsetenv("MISSING_VAR") | ||
| if GetEnvWithDefault("MISSING_VAR", "default") != "default" { | ||
| t.Errorf("Expected GetEnvWithDefault to return 'default', got '%s'", GetEnvWithDefault("MISSING_VAR", "default")) | ||
| } | ||
|
|
||
| // Test with empty env var (should return default) | ||
| os.Setenv("EMPTY_VAR", "") | ||
| if GetEnvWithDefault("EMPTY_VAR", "default") != "default" { | ||
| t.Errorf("Expected GetEnvWithDefault to return 'default' for empty var, got '%s'", GetEnvWithDefault("EMPTY_VAR", "default")) | ||
| } | ||
|
|
||
| // Cleanup | ||
| os.Unsetenv("TEST_STRING") | ||
| os.Unsetenv("EMPTY_VAR") | ||
| } | ||
|
|
||
| func TestGetEnvIntWithDefault(t *testing.T) { | ||
| // Test with valid int env var | ||
| os.Setenv("TEST_INT", "12345") | ||
| if GetEnvIntWithDefault("TEST_INT", 0) != 12345 { | ||
| t.Errorf("Expected GetEnvIntWithDefault to return 12345, got %d", GetEnvIntWithDefault("TEST_INT", 0)) | ||
| } | ||
|
|
||
| // Test with invalid int value (should return default) | ||
| os.Setenv("TEST_INVALID_INT", "not-a-number") | ||
| if GetEnvIntWithDefault("TEST_INVALID_INT", 999) != 999 { | ||
| t.Errorf("Expected GetEnvIntWithDefault to return default 999, got %d", GetEnvIntWithDefault("TEST_INVALID_INT", 999)) | ||
| } | ||
|
|
||
| // Test with missing env var | ||
| os.Unsetenv("MISSING_INT_VAR") | ||
| if GetEnvIntWithDefault("MISSING_INT_VAR", 777) != 777 { | ||
| t.Errorf("Expected GetEnvIntWithDefault to return default 777, got %d", GetEnvIntWithDefault("MISSING_INT_VAR", 777)) | ||
| } | ||
|
|
||
| // Test with empty env var (should return default) | ||
| os.Setenv("EMPTY_INT_VAR", "") | ||
| if GetEnvIntWithDefault("EMPTY_INT_VAR", 888) != 888 { | ||
| t.Errorf("Expected GetEnvIntWithDefault to return default 888 for empty var, got %d", GetEnvIntWithDefault("EMPTY_INT_VAR", 888)) | ||
| } | ||
|
|
||
| // Cleanup | ||
| os.Unsetenv("TEST_INT") | ||
| os.Unsetenv("TEST_INVALID_INT") | ||
| os.Unsetenv("EMPTY_INT_VAR") | ||
| } | ||
|
|
||
| func TestPreRunEWithEnvVars(t *testing.T) { | ||
| // Setup test environment | ||
| os.Setenv("PGDATABASE", "test-db") | ||
| os.Setenv("PGUSER", "test-user") | ||
| os.Setenv("PGHOST", "test-host") | ||
| os.Setenv("PGPORT", "1234") | ||
| os.Setenv("PGAPPNAME", "test-app") | ||
|
|
||
| // Test variables to be populated | ||
| var db, user, host, appName string | ||
| var port int | ||
|
|
||
| // Create a mock command that simulates flags not being changed | ||
| // In real usage, cobra.Command would handle this, but for testing we'll call the function directly | ||
| preRunFunc := PreRunEWithEnvVarsAndConnectionAndApp(&db, &user, &host, &port, &appName) | ||
|
|
||
| // We can't easily test this without a real cobra.Command, but we can test the underlying logic | ||
| // by directly calling the helper functions which are used in the PreRun function | ||
|
|
||
| // Test that environment variables are read correctly | ||
| if GetEnvWithDefault("PGDATABASE", "") != "test-db" { | ||
| t.Errorf("Expected PGDATABASE to be 'test-db', got '%s'", GetEnvWithDefault("PGDATABASE", "")) | ||
| } | ||
|
|
||
| if GetEnvWithDefault("PGUSER", "") != "test-user" { | ||
| t.Errorf("Expected PGUSER to be 'test-user', got '%s'", GetEnvWithDefault("PGUSER", "")) | ||
| } | ||
|
|
||
| if GetEnvWithDefault("PGHOST", "") != "test-host" { | ||
| t.Errorf("Expected PGHOST to be 'test-host', got '%s'", GetEnvWithDefault("PGHOST", "")) | ||
| } | ||
|
|
||
| if GetEnvIntWithDefault("PGPORT", 0) != 1234 { | ||
| t.Errorf("Expected PGPORT to be 1234, got %d", GetEnvIntWithDefault("PGPORT", 0)) | ||
| } | ||
|
|
||
| if GetEnvWithDefault("PGAPPNAME", "") != "test-app" { | ||
| t.Errorf("Expected PGAPPNAME to be 'test-app', got '%s'", GetEnvWithDefault("PGAPPNAME", "")) | ||
| } | ||
|
|
||
| // Cleanup | ||
| os.Unsetenv("PGDATABASE") | ||
| os.Unsetenv("PGUSER") | ||
| os.Unsetenv("PGHOST") | ||
| os.Unsetenv("PGPORT") | ||
| os.Unsetenv("PGAPPNAME") | ||
|
|
||
| // Verify preRunFunc was created (basic sanity check) | ||
| if preRunFunc == nil { | ||
| t.Error("PreRunEWithEnvVarsAndConnectionAndApp should return a non-nil function") | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using a more explicit approach by creating a struct to hold the optional parameters instead of passing multiple nil values. This would make the function calls clearer and reduce the risk of passing parameters in the wrong order.