From 40811fb0ef381185bb5e9b0a7052c3f7fcb9ef64 Mon Sep 17 00:00:00 2001 From: Adam Byrne Date: Tue, 14 Jan 2025 14:11:40 +0000 Subject: [PATCH 1/3] moved main to cmd, fixed config validation --- cmd/launchrail/{launchrail.go => main.go} | 4 +-- config.yaml | 7 +++-- internal/config/config.go | 31 ++++++++++++++++++++++- internal/config/config_test.go | 5 ++-- internal/config/schema.go | 2 +- main.go | 9 ------- 6 files changed, 39 insertions(+), 19 deletions(-) rename cmd/launchrail/{launchrail.go => main.go} (97%) delete mode 100644 main.go diff --git a/cmd/launchrail/launchrail.go b/cmd/launchrail/main.go similarity index 97% rename from cmd/launchrail/launchrail.go rename to cmd/launchrail/main.go index bfcb6eb..587a0d5 100644 --- a/cmd/launchrail/launchrail.go +++ b/cmd/launchrail/main.go @@ -1,4 +1,4 @@ -package launchrail +package main import ( "github.com/bxrne/launchrail/internal/config" @@ -9,7 +9,7 @@ import ( "github.com/bxrne/launchrail/pkg/thrustcurves" ) -func Root() { +func main() { log := logger.GetLogger() log.Debug("Starting...") diff --git a/config.yaml b/config.yaml index 84f59b1..e340c85 100644 --- a/config.yaml +++ b/config.yaml @@ -13,10 +13,9 @@ options: openrocket_file: "./testdata/openrocket/l1.ork" launchrail: length: 3.0 - angle: 0.0 - orientation: 0.0 + angle: 2.0 + orientation: 1.0 launchsite: latitude: 37.7749 longitude: -122.4194 - altitude: 0.0 - + altitude: 1.0 diff --git a/internal/config/config.go b/internal/config/config.go index 02e80af..e32d0e8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,7 +8,7 @@ import ( "github.com/spf13/viper" ) -// GetConfig returns the singleton instance of the configuration. +// GetConfig returns the application configuration func GetConfig() (*Config, error) { var cfg *Config v := viper.New() @@ -31,6 +31,7 @@ func GetConfig() (*Config, error) { return cfg, nil } +// Validate checks the config to error on empty field func (cfg *Config) Validate() error { if cfg.App.Name == "" { return fmt.Errorf("app.name is required") @@ -44,6 +45,10 @@ func (cfg *Config) Validate() error { return fmt.Errorf("logging.level is required") } + if cfg.External.OpenRocketVersion == "" { + return fmt.Errorf("external.openrocket_version is required") + } + if cfg.Options.MotorDesignation == "" { return fmt.Errorf("options.motor_designation is required") } @@ -56,5 +61,29 @@ func (cfg *Config) Validate() error { return fmt.Errorf("options.openrocket_file is invalid: %s", err) } + if cfg.Options.Launchrail.Length == 0 { + return fmt.Errorf("options.launchrail.length is required") + } + + if cfg.Options.Launchrail.Angle == 0 { + return fmt.Errorf("options.launchrail.angle is required") + } + + if cfg.Options.Launchrail.Orientation == 0 { + return fmt.Errorf("options.launchrail.orientation is required") + } + + if cfg.Options.Launchsite.Latitude == 0 { + return fmt.Errorf("options.launchsite.latitude is required") + } + + if cfg.Options.Launchsite.Longitude == 0 { + return fmt.Errorf("options.launchsite.longitude is required") + } + + if cfg.Options.Launchsite.Altitude == 0 { + return fmt.Errorf("options.launchsite.altitude is required") + } + return nil } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 922066c..2b566a6 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -137,7 +137,7 @@ func TestValidateConfigMissingLoggingLevel(t *testing.T) { }) } -// TEST: GIVEN a configuration file with missing external.openrocket_version WHEN GetConfig is called THEN no error is returned +// TEST: GIVEN a configuration file with missing external.openrocket_version WHEN GetConfig is called THEN error is returned func TestValidateConfigMissingOpenRocketVersion(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() @@ -147,7 +147,8 @@ func TestValidateConfigMissingOpenRocketVersion(t *testing.T) { cfg.External.OpenRocketVersion = "" err = cfg.Validate() - if err != nil { + expected := "external.openrocket_version is required" + if err == nil && err.Error() != expected { t.Errorf("Expected no error, got: %s", err) } diff --git a/internal/config/schema.go b/internal/config/schema.go index 7352284..82539a2 100644 --- a/internal/config/schema.go +++ b/internal/config/schema.go @@ -30,7 +30,7 @@ type Config struct { } `mapstructure:"options"` } -// Marshal to map structure for logging. +// String returns the configuration as a map of strings, useful for testing. func (c *Config) String() map[string]string { marshalled := make(map[string]string) marshalled["app.name"] = c.App.Name diff --git a/main.go b/main.go deleted file mode 100644 index e8c4cd8..0000000 --- a/main.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import ( - "github.com/bxrne/launchrail/cmd/launchrail" -) - -func main() { - launchrail.Root() -} From aaf93e04e3ac4ce42d9bb0f7fb2e9ae61164627e Mon Sep 17 00:00:00 2001 From: Adam Byrne Date: Tue, 14 Jan 2025 14:41:28 +0000 Subject: [PATCH 2/3] config tests --- internal/config/config.go | 27 ++- internal/config/config_test.go | 218 ++++++++++++++++++----- internal/http_client/mock_client_test.go | 1 + 3 files changed, 194 insertions(+), 52 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index e32d0e8..3453755 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,33 +4,50 @@ import ( "errors" "fmt" "os" + "sync" "github.com/spf13/viper" ) -// GetConfig returns the application configuration +var ( + once sync.Once + cfg *Config +) + +// GetConfig returns the application configuration as a singleton func GetConfig() (*Config, error) { - var cfg *Config v := viper.New() v.SetConfigName("config") v.SetConfigType("yaml") v.AddConfigPath(".") if err := v.ReadInConfig(); err != nil { - return nil, errors.New("failed to read config file") + cfg = nil + return nil, fmt.Errorf("failed to read config file: %s", err) } if err := v.Unmarshal(&cfg); err != nil { - return nil, errors.New("failed to unmarshal config") + cfg = nil + return nil, fmt.Errorf("failed to unmarshal config: %s", err) } if err := cfg.Validate(); err != nil { - return nil, err + cfg = nil + return nil, fmt.Errorf("failed to validate config: %s", err) + } + + if cfg == nil { + return nil, errors.New("failed to load configuration") } return cfg, nil } +// Reset resets the configuration singleton, useful for testing +func Reset() { + cfg = nil +} + // Validate checks the config to error on empty field func (cfg *Config) Validate() error { if cfg.App.Name == "" { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 2b566a6..9186196 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -2,6 +2,7 @@ package config_test import ( "os" + "strings" "testing" "github.com/bxrne/launchrail/internal/config" @@ -26,6 +27,9 @@ func withWorkingDir(t *testing.T, dir string, testFunc func()) { } }() + // Reset the configuration + config.Reset() + // Run the test function testFunc() } @@ -52,27 +56,49 @@ func TestGetConfigInvalidConfigFile(t *testing.T) { t.Error("Expected an error, got nil") } - if err.Error() != "failed to read config file" { - t.Errorf("Expected error to be 'failed to read config file', got: %s", err) + expected := "failed to read config file:" + if !strings.HasPrefix(err.Error(), expected) { + t.Errorf("Expected error to start with %s, got: %s", expected, err) } }) } // TEST: GIVEN a bad config file WHEN GetConfig is called THEN the error 'failed to unmarshal config' is returned func TestGetConfigBadConfigFile(t *testing.T) { - withWorkingDir(t, "../../testdata/config/bad/", func() { + withWorkingDir(t, "../../testdata/config/bad", func() { _, err := config.GetConfig() if err == nil { t.Error("Expected an error, got nil") } - if err.Error() != "failed to unmarshal config" { - t.Errorf("Expected error to be 'failed to unmarshal config', got: %s", err) + + expected := "failed to unmarshal config:" + if !strings.HasPrefix(err.Error(), expected) { + t.Errorf("Expected error to start with %s, got: %s", expected, err) + } + }) +} + +// TEST: GIVEN a config WHEN another config is requested THEN the config is a singleton +func TestGetConfigSingleton(t *testing.T) { + withWorkingDir(t, "../..", func() { + cfg1, err := config.GetConfig() + if err != nil { + t.Errorf("Expected no error, got: %s", err) + } + + cfg2, err := config.GetConfig() + if err != nil { + t.Errorf("Expected no error, got: %s", err) + } + + if cfg1 != cfg2 { + t.Error("Expected config to be a singleton") } }) } -// TEST: GIVEN a configuration file with missing app.name WHEN Validate is called THEN an error is returned -func TestValidateConfigMissingAppName(t *testing.T) { +// TEST: GIVEN a config with missing app.name WHEN Validate is called THEN an error is returned +func TestGetConfigMissingFields(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() if err != nil { @@ -85,16 +111,15 @@ func TestValidateConfigMissingAppName(t *testing.T) { t.Error("Expected an error, got nil") } - if err.Error() != "app.name is required" { - t.Errorf("Expected error to be 'app.name is required', got: %s", err) + expected := "app.name is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) } - - cfg.App.Name = "launchrail-test" // Reset app.name }) } -// TEST: GIVEN a configuration file with missing app.version WHEN GetConfig is called THEN an error is returned -func TestValidateConfigMissingAppVersion(t *testing.T) { +// TEST: GIVEN a config with missing app.version WHEN Validate is called THEN an error is returned +func TestGetConfigMissingVersion(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() if err != nil { @@ -107,16 +132,15 @@ func TestValidateConfigMissingAppVersion(t *testing.T) { t.Error("Expected an error, got nil") } - if err.Error() != "app.version is required" { - t.Errorf("Expected error to be 'app.version is required', got: %s", err) + expected := "app.version is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) } - - cfg.App.Version = "0.0.0" // Reset app.version }) } -// TEST: GIVEN a configuration file with missing logging.level WHEN GetConfig is called THEN an error is returned -func TestValidateConfigMissingLoggingLevel(t *testing.T) { +// TEST: GIVEN a config with missing logging.level WHEN Validate is called THEN an error is returned +func TestGetConfigMissingLoggingLevel(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() if err != nil { @@ -129,16 +153,15 @@ func TestValidateConfigMissingLoggingLevel(t *testing.T) { t.Error("Expected an error, got nil") } - if err.Error() != "logging.level is required" { - t.Errorf("Expected error to be 'logging.level is required', got: %s", err) + expected := "logging.level is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) } - - cfg.Logging.Level = "info" // Reset logging.level }) } -// TEST: GIVEN a configuration file with missing external.openrocket_version WHEN GetConfig is called THEN error is returned -func TestValidateConfigMissingOpenRocketVersion(t *testing.T) { +// TEST: GIVEN a config with external.openrocket_version WHEN Validate is called THEN no error is returned +func TestGetConfigExternalOpenRocketVersion(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() if err != nil { @@ -147,80 +170,181 @@ func TestValidateConfigMissingOpenRocketVersion(t *testing.T) { cfg.External.OpenRocketVersion = "" err = cfg.Validate() + if err == nil { + t.Error("Expected an error, got nil") + } + expected := "external.openrocket_version is required" - if err == nil && err.Error() != expected { + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } + }) +} + +// TEST: GIVEN a config with missing options.motor_designation WHEN Validate is called THEN an error is returned +func TestGetConfigMissingMotorDesignation(t *testing.T) { + withWorkingDir(t, "../..", func() { + cfg, err := config.GetConfig() + if err != nil { t.Errorf("Expected no error, got: %s", err) } - cfg.External.OpenRocketVersion = "15.03" // Reset external.openrocket_version + cfg.Options.MotorDesignation = "" + err = cfg.Validate() + if err == nil { + t.Error("Expected an error, got nil") + } + + expected := "options.motor_designation is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } }) } -// TEST: GIVEN a configuration file with missing options.motor_designation WHEN GetConfig is called THEN an error is returned -func TestValidateConfigMissingMotorDesignation(t *testing.T) { +// TEST: GIVEN a config with missing options.openrocket_file WHEN Validate is called THEN an error is returned +func TestGetConfigMissingOpenRocketFile(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() if err != nil { t.Errorf("Expected no error, got: %s", err) } - cfg.Options.MotorDesignation = "" + cfg.Options.OpenRocketFile = "" err = cfg.Validate() if err == nil { t.Error("Expected an error, got nil") } - if err.Error() != "options.motor_designation is required" { - t.Errorf("Expected error to be 'options.motor_designation is required', got: %s", err) + expected := "options.openrocket_file is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } + }) +} + +// TEST: GIVEN a config with missing options.launchrail.length WHEN Validate is called THEN no error is returned +func TestGetConfigMissingLaunchrailLength(t *testing.T) { + withWorkingDir(t, "../..", func() { + cfg, err := config.GetConfig() + if err != nil { + t.Errorf("Expected no error, got: %s", err) } - cfg.Options.MotorDesignation = "A8" // Reset options.motor_designation + cfg.Options.Launchrail.Length = 0 + err = cfg.Validate() + if err == nil { + t.Error("Expected an error, got nil") + } + + expected := "options.launchrail.length is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } }) } -// TEST: GIVEN a configuration file with missing options.open_rocket_file WHEN GetConfig is called THEN an error is returned -func TestValidateConfigMissingOpenRocketFile(t *testing.T) { +// TEST: GIVEN a config with missing options.launchrail.angle WHEN Validate is called THEN no error is returned +func TestGetConfigMissingLaunchrailAngle(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() if err != nil { t.Errorf("Expected no error, got: %s", err) } - cfg.Options.OpenRocketFile = "" + cfg.Options.Launchrail.Angle = 0 err = cfg.Validate() if err == nil { t.Error("Expected an error, got nil") } - if err.Error() != "options.openrocket_file is required" { - t.Errorf("Expected error to be 'options.openrocket_file is required', got: %s", err) + expected := "options.launchrail.angle is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } + }) +} + +// TEST: GIVEN a config with missing options.launchrail.orientation WHEN Validate is called THEN no error is returned +func TestGetConfigMissingLaunchrailOrientation(t *testing.T) { + withWorkingDir(t, "../..", func() { + cfg, err := config.GetConfig() + if err != nil { + t.Errorf("Expected no error, got: %s", err) + } + + cfg.Options.Launchrail.Orientation = 0 + err = cfg.Validate() + if err == nil { + t.Error("Expected an error, got nil") } - cfg.Options.OpenRocketFile = "./testdata/openrocket/l1.ork" // Reset options.open_rocket_file + expected := "options.launchrail.orientation is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } }) } -// TEST: GIVEN a configuration file with invalid options.open_rocket_file WHEN GetConfig is called THEN an error is returned -func TestValidateConfigInvalidOpenRocketFile(t *testing.T) { +// TEST: GIVEN a config with missing options.launchsite.latitude WHEN Validate is called THEN no error is returned +func TestGetConfigMissingLaunchsiteLatitude(t *testing.T) { withWorkingDir(t, "../..", func() { cfg, err := config.GetConfig() if err != nil { t.Errorf("Expected no error, got: %s", err) } - cfg.Options.OpenRocketFile = "test/resources/invalid.ork" + cfg.Options.Launchsite.Latitude = 0 err = cfg.Validate() if err == nil { t.Error("Expected an error, got nil") } - unixErr := "options.openrocket_file is invalid: stat test/resources/invalid.ork: no such file or directory" - winErr := "options.openrocket_file is invalid: CreateFile test/resources/invalid.ork: The system cannot find the path specified." + expected := "options.launchsite.latitude is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } + }) +} - if err.Error() != unixErr && err.Error() != winErr { - t.Errorf("Expected error to be '%s' or '%s', got: %s", unixErr, winErr, err) +// TEST: GIVEN a config with missing options.launchsite.longitude WHEN Validate is called THEN no error is returned +func TestGetConfigMissingLaunchsiteLongitude(t *testing.T) { + withWorkingDir(t, "../..", func() { + cfg, err := config.GetConfig() + if err != nil { + t.Errorf("Expected no error, got: %s", err) + } + + cfg.Options.Launchsite.Longitude = 0 + err = cfg.Validate() + if err == nil { + t.Error("Expected an error, got nil") + } + + expected := "options.launchsite.longitude is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) } + }) +} - cfg.Options.OpenRocketFile = "test/resources/rocket.ork" // Reset options.open_rocket_file +// TEST: GIVEN a config with missing options.launchsite.altitude WHEN Validate is called THEN no error is returned +func TestGetConfigMissingLaunchsiteAltitude(t *testing.T) { + withWorkingDir(t, "../..", func() { + cfg, err := config.GetConfig() + if err != nil { + t.Errorf("Expected no error, got: %s", err) + } + + cfg.Options.Launchsite.Altitude = 0 + err = cfg.Validate() + if err == nil { + t.Error("Expected an error, got nil") + } + + expected := "options.launchsite.altitude is required" + if err.Error() != expected { + t.Errorf("Expected %s, got %s", expected, err) + } }) } diff --git a/internal/http_client/mock_client_test.go b/internal/http_client/mock_client_test.go index 87ad9aa..3de5777 100644 --- a/internal/http_client/mock_client_test.go +++ b/internal/http_client/mock_client_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) +// TEST: GIVEN a valid configuration file WHEN GetConfig is called THEN no error is returned func TestMockHTTPClient_Post(t *testing.T) { tests := []struct { name string From 37f5f32735f3a50f1f22892cddb64c69f66d7037 Mon Sep 17 00:00:00 2001 From: Adam Byrne Date: Tue, 14 Jan 2025 15:16:27 +0000 Subject: [PATCH 3/3] config and tests --- internal/config/config.go | 18 +-------- internal/config/config_test.go | 72 +++++++++++++--------------------- sonar-project.properties | 2 +- 3 files changed, 30 insertions(+), 62 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 3453755..97c6f0c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,53 +1,39 @@ package config import ( - "errors" "fmt" "os" - "sync" "github.com/spf13/viper" ) var ( - once sync.Once - cfg *Config + cfg *Config ) // GetConfig returns the application configuration as a singleton func GetConfig() (*Config, error) { + v := viper.New() v.SetConfigName("config") v.SetConfigType("yaml") v.AddConfigPath(".") if err := v.ReadInConfig(); err != nil { - cfg = nil return nil, fmt.Errorf("failed to read config file: %s", err) } if err := v.Unmarshal(&cfg); err != nil { - cfg = nil return nil, fmt.Errorf("failed to unmarshal config: %s", err) } if err := cfg.Validate(); err != nil { - cfg = nil return nil, fmt.Errorf("failed to validate config: %s", err) } - if cfg == nil { - return nil, errors.New("failed to load configuration") - } - return cfg, nil } -// Reset resets the configuration singleton, useful for testing -func Reset() { - cfg = nil -} - // Validate checks the config to error on empty field func (cfg *Config) Validate() error { if cfg.App.Name == "" { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 9186196..8641114 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -2,14 +2,13 @@ package config_test import ( "os" - "strings" "testing" "github.com/bxrne/launchrail/internal/config" ) // Helper to change directory and reset after test -func withWorkingDir(t *testing.T, dir string, testFunc func()) { +func withWorkingDir(t *testing.T, dir string, testFunc func(cfg *config.Config, err error)) { originalDir, err := os.Getwd() if err != nil { t.Fatalf("Failed to get current directory: %s", err) @@ -27,17 +26,14 @@ func withWorkingDir(t *testing.T, dir string, testFunc func()) { } }() - // Reset the configuration - config.Reset() - - // Run the test function - testFunc() + // WARN: Run the test function with the configuration and handle its error within + cfg, err := config.GetConfig() + testFunc(cfg, err) } // TEST: GIVEN a valid configuration file WHEN GetConfig is called THEN no error is returned func TestGetConfig(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -50,38 +46,36 @@ func TestGetConfig(t *testing.T) { // TEST: GIVEN an invalid config file WHEN GetConfig is called THEN the error 'failed to read config file' is returned func TestGetConfigInvalidConfigFile(t *testing.T) { - withWorkingDir(t, ".", func() { - _, err := config.GetConfig() + withWorkingDir(t, ".", func(cfg *config.Config, err error) { if err == nil { t.Error("Expected an error, got nil") } expected := "failed to read config file:" - if !strings.HasPrefix(err.Error(), expected) { - t.Errorf("Expected error to start with %s, got: %s", expected, err) + if err.Error()[:len(expected)] != expected { + t.Errorf("Expected %s, got %s", expected, err) } }) } // TEST: GIVEN a bad config file WHEN GetConfig is called THEN the error 'failed to unmarshal config' is returned func TestGetConfigBadConfigFile(t *testing.T) { - withWorkingDir(t, "../../testdata/config/bad", func() { - _, err := config.GetConfig() + withWorkingDir(t, "../../testdata/config/bad", func(cfg *config.Config, err error) { if err == nil { t.Error("Expected an error, got nil") } - expected := "failed to unmarshal config:" - if !strings.HasPrefix(err.Error(), expected) { - t.Errorf("Expected error to start with %s, got: %s", expected, err) + expected := "failed to unmarshal config" + if err.Error()[:len(expected)] != expected { + t.Errorf("Expected %s, got %s", expected, err) } + }) } // TEST: GIVEN a config WHEN another config is requested THEN the config is a singleton func TestGetConfigSingleton(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg1, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -91,7 +85,7 @@ func TestGetConfigSingleton(t *testing.T) { t.Errorf("Expected no error, got: %s", err) } - if cfg1 != cfg2 { + if cfg != cfg2 { t.Error("Expected config to be a singleton") } }) @@ -99,8 +93,7 @@ func TestGetConfigSingleton(t *testing.T) { // TEST: GIVEN a config with missing app.name WHEN Validate is called THEN an error is returned func TestGetConfigMissingFields(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -120,8 +113,7 @@ func TestGetConfigMissingFields(t *testing.T) { // TEST: GIVEN a config with missing app.version WHEN Validate is called THEN an error is returned func TestGetConfigMissingVersion(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -141,8 +133,7 @@ func TestGetConfigMissingVersion(t *testing.T) { // TEST: GIVEN a config with missing logging.level WHEN Validate is called THEN an error is returned func TestGetConfigMissingLoggingLevel(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -162,8 +153,7 @@ func TestGetConfigMissingLoggingLevel(t *testing.T) { // TEST: GIVEN a config with external.openrocket_version WHEN Validate is called THEN no error is returned func TestGetConfigExternalOpenRocketVersion(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -183,8 +173,7 @@ func TestGetConfigExternalOpenRocketVersion(t *testing.T) { // TEST: GIVEN a config with missing options.motor_designation WHEN Validate is called THEN an error is returned func TestGetConfigMissingMotorDesignation(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -204,8 +193,7 @@ func TestGetConfigMissingMotorDesignation(t *testing.T) { // TEST: GIVEN a config with missing options.openrocket_file WHEN Validate is called THEN an error is returned func TestGetConfigMissingOpenRocketFile(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -225,8 +213,7 @@ func TestGetConfigMissingOpenRocketFile(t *testing.T) { // TEST: GIVEN a config with missing options.launchrail.length WHEN Validate is called THEN no error is returned func TestGetConfigMissingLaunchrailLength(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -246,8 +233,7 @@ func TestGetConfigMissingLaunchrailLength(t *testing.T) { // TEST: GIVEN a config with missing options.launchrail.angle WHEN Validate is called THEN no error is returned func TestGetConfigMissingLaunchrailAngle(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -267,8 +253,7 @@ func TestGetConfigMissingLaunchrailAngle(t *testing.T) { // TEST: GIVEN a config with missing options.launchrail.orientation WHEN Validate is called THEN no error is returned func TestGetConfigMissingLaunchrailOrientation(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -288,8 +273,7 @@ func TestGetConfigMissingLaunchrailOrientation(t *testing.T) { // TEST: GIVEN a config with missing options.launchsite.latitude WHEN Validate is called THEN no error is returned func TestGetConfigMissingLaunchsiteLatitude(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -309,8 +293,7 @@ func TestGetConfigMissingLaunchsiteLatitude(t *testing.T) { // TEST: GIVEN a config with missing options.launchsite.longitude WHEN Validate is called THEN no error is returned func TestGetConfigMissingLaunchsiteLongitude(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } @@ -330,8 +313,7 @@ func TestGetConfigMissingLaunchsiteLongitude(t *testing.T) { // TEST: GIVEN a config with missing options.launchsite.altitude WHEN Validate is called THEN no error is returned func TestGetConfigMissingLaunchsiteAltitude(t *testing.T) { - withWorkingDir(t, "../..", func() { - cfg, err := config.GetConfig() + withWorkingDir(t, "../..", func(cfg *config.Config, err error) { if err != nil { t.Errorf("Expected no error, got: %s", err) } diff --git a/sonar-project.properties b/sonar-project.properties index db35a8b..b383990 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -7,5 +7,5 @@ sonar.sources=. sonar.exclusions=**/*_test.go,**/vendor/**,**/cmd/**,*.xml sonar.tests=. sonar.test.inclusions=**/*_test.go -sonar.test.exclusions=**/vendor/** +sonar.test.exclusions=**/vendor/**, **/cmd/** sonar.go.coverage.reportPaths=coverage.out