-
Notifications
You must be signed in to change notification settings - Fork 15
refactor(main): extract helper functions and add comprehensive tests (#45) #53
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
base: main
Are you sure you want to change the base?
refactor(main): extract helper functions and add comprehensive tests (#45) #53
Conversation
…med helper functions with proper error handling: - `validateConfigFlags()` handles command-line flag validation - `createCacheDirectory()` checks and creates cache directory if missing - `downloadGTFSBundles()` handles initial GTFS bundle downloading - `refreshGTFSBundles()` manages periodic GTFS updates - `refreshConfig()` manages periodic remote configuration refresh - Added `updateConfig()` method to Application struct for thread-safe config updates
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.
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
cmd/watchdog/main_test.go
Outdated
} | ||
}) | ||
|
||
t.Run("Fails : if path is a file", func(t *testing.T) { |
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 removing the extra space before the colon in the subtest name for consistency, e.g. change "Fails : if path is a file" to "Fails: if path is a file".
t.Run("Fails : if path is a file", func(t *testing.T) { | |
t.Run("Fails: if path is a file", func(t *testing.T) { |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
cmd/watchdog/main.go
Outdated
logger.Error("Failed to create cache directory", "error", err) | ||
os.Exit(1) | ||
} | ||
if err = createCacheDirectory(cacheDir , logger) ; err != nil { |
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] Remove the extra space before the comma in the function call arguments and after the closing parenthesis, e.g. change 'cacheDir , logger' to 'cacheDir, logger'.
if err = createCacheDirectory(cacheDir , logger) ; err != nil { | |
if err = createCacheDirectory(cacheDir, logger); err != nil { |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
- `TestValidateConfigFlags`: tests multiple flag combinations for validation - `TestCreateCacheDirectory`: covers directory creation, existing paths, and error handling - `TestDownloadGTFSBundles`: validates GTFS bundle downloading logic - `TestRefreshGTFSBundles`: tests periodic bundle refresh functionality - `TestRefreshConfig`: uses a mock server to test remote config refresh - `TestUpdateConfig`: verifies thread-safe config updates
295f3fc
to
f322208
Compare
…d add unit test for collectMetricsForServer - Extract metrics collection into collectMetricsForServer. - Add a basic unit test to verify the function collects metrics for a server and registers them with Prometheus, using a fresh registry to avoid conflicts.
Follow-upRefactored |
Summary
This PR refactors the main function in
main.go
into named helper functions with proper error handling and adds comprehensive unit tests for each function to enhance test coverage, as outlined in issue #45 .Changes
Refactoring:
validateConfigFlags()
to handle command-line flag validationcreateCacheDirectory()
with proper directory existence checkingdownloadGTFSBundles()
for GTFS bundle downloadingrefreshGTFSBundles()
for periodic GTFS updatesrefreshConfig()
for periodic remote configuration updatesupdateConfig()
method to the application struct for thread-safe updatesTesting:
TestValidateConfigFlags
TestCreateCacheDirectory
TestDownloadGTFSBundles
TestRefreshGTFSBundles
TestRefreshConfig
TestUpdateConfig
TestLoadConfigFromFile
TestLoadConfigFromURL
Test coverage after these changes: