Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Abo-Omar-74
Copy link
Contributor

@Abo-Omar-74 Abo-Omar-74 commented Apr 6, 2025

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:

  • Extract validateConfigFlags() to handle command-line flag validation
  • Extract createCacheDirectory() with proper directory existence checking
  • Extract downloadGTFSBundles() for GTFS bundle downloading
  • Extract refreshGTFSBundles() for periodic GTFS updates
  • Extract refreshConfig() for periodic remote configuration updates
  • Add updateConfig() method to the application struct for thread-safe updates

Testing:

  • Add TestValidateConfigFlags
  • Add TestCreateCacheDirectory
  • Add TestDownloadGTFSBundles
  • Add TestRefreshGTFSBundles
  • Add TestRefreshConfig
  • Add TestUpdateConfig
  • Failure Cases for TestLoadConfigFromFile
  • Failure Cases for TestLoadConfigFromURL

Test coverage after these changes:

ok      watchdog.onebusaway.org/cmd/watchdog            coverage: 55.9% of statements
ok      watchdog.onebusaway.org/internal/metrics        coverage: 87.2% of statements
ok      watchdog.onebusaway.org/internal/models         coverage: 100.0% of statements
ok      watchdog.onebusaway.org/internal/server         coverage: 100.0% of statements
ok      watchdog.onebusaway.org/internal/utils          coverage: 96.9% of statements

…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
@aaronbrethorst aaronbrethorst requested a review from Copilot April 6, 2025 02:51
Copy link

@Copilot Copilot AI left a 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.

}
})

t.Run("Fails : if path is a file", func(t *testing.T) {
Copy link
Preview

Copilot AI Apr 6, 2025

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".

Suggested change
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.

logger.Error("Failed to create cache directory", "error", err)
os.Exit(1)
}
if err = createCacheDirectory(cacheDir , logger) ; err != nil {
Copy link
Preview

Copilot AI Apr 6, 2025

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'.

Suggested change
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
@Abo-Omar-74 Abo-Omar-74 force-pushed the feat/increase-test-coverage-2 branch from 295f3fc to f322208 Compare April 6, 2025 03:03
…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.
@Abo-Omar-74
Copy link
Contributor Author

Follow-up

Refactored cmd/watchdog/metrics.go to extract metrics collection into collectMetricsForServer() and added a unit test.

@coveralls
Copy link

Coverage Status

coverage: 74.051% (+9.7%) from 64.309%
when pulling ea13931 on Abo-Omar-74:feat/increase-test-coverage-2
into 960597e on OneBusAway:main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants