From 51398abc87f3dd3710446ed9553c531fbf75b444 Mon Sep 17 00:00:00 2001 From: Aidan Delaney Date: Thu, 2 Mar 2023 05:51:32 +0000 Subject: [PATCH 1/9] Add pwd to config search path iff it contains a config Only add the pwd to the config search path if and only if it contains a config file that we expect. This avoids incorrectly finding config files that may be specific to applictions other than syft. fixes: #1634 Signed-off-by: Aidan Delaney --- internal/config/application.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/config/application.go b/internal/config/application.go index b5a74b6380f..a58fc61a37d 100644 --- a/internal/config/application.go +++ b/internal/config/application.go @@ -3,6 +3,7 @@ package config import ( "errors" "fmt" + "os" "path" "reflect" "sort" @@ -223,13 +224,16 @@ func loadConfig(v *viper.Viper, configPath string) error { // start searching for valid configs in order... // 1. look for ..yaml (in the current directory) - v.AddConfigPath(".") - v.SetConfigName("." + internal.ApplicationName) - if err = v.ReadInConfig(); err == nil { - v.Set("config", v.ConfigFileUsed()) - return nil - } else if !errors.As(err, &viper.ConfigFileNotFoundError{}) { - return fmt.Errorf("unable to parse config=%q: %w", v.ConfigFileUsed(), err) + confFilePath := "." + internal.ApplicationName + if _, err := os.Stat(confFilePath); err == nil { + v.AddConfigPath(".") + v.SetConfigName(confFilePath) + if err = v.ReadInConfig(); err == nil { + v.Set("config", v.ConfigFileUsed()) + return nil + } else if !errors.As(err, &viper.ConfigFileNotFoundError{}) { + return fmt.Errorf("unable to parse config=%q: %w", v.ConfigFileUsed(), err) + } } // 2. look for ./config.yaml (in the current directory) From b88f11d64c1c0a56c54201c9e79a9711d557100f Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 10:20:54 -0400 Subject: [PATCH 2/9] refactor: update to deprecate behavior pre v1.0.0 Signed-off-by: Christopher Phillips --- internal/config/application.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/config/application.go b/internal/config/application.go index a58fc61a37d..9d921c23b4d 100644 --- a/internal/config/application.go +++ b/internal/config/application.go @@ -225,9 +225,18 @@ func loadConfig(v *viper.Viper, configPath string) error { // start searching for valid configs in order... // 1. look for ..yaml (in the current directory) confFilePath := "." + internal.ApplicationName + + // TODO: Remove this before v1.0.0 + // See syft #1634 + v.AddConfigPath(".") + v.SetConfigName(confFilePath) + + // check if config.yaml exists in the current directory + // DEPRECATED: this will be removed in v1.0.0 + if _, err := os.Stat("config.yaml"); err == nil { + log.Warn("DEPRECATED: config.yaml as a configuration file is deprecated and will be removed as an option in v1.0.0, please rename to .syft.yaml") + } if _, err := os.Stat(confFilePath); err == nil { - v.AddConfigPath(".") - v.SetConfigName(confFilePath) if err = v.ReadInConfig(); err == nil { v.Set("config", v.ConfigFileUsed()) return nil From 59f906121e87b1d2a0663881c05f5e8e3ae47beb Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 10:33:50 -0400 Subject: [PATCH 3/9] chore: add nolint directive for SA Signed-off-by: Christopher Phillips --- internal/config/application.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/config/application.go b/internal/config/application.go index 9d921c23b4d..4530d8e0297 100644 --- a/internal/config/application.go +++ b/internal/config/application.go @@ -209,6 +209,7 @@ func (cfg Application) String() string { return string(appaStr) } +// nolint:funlen func loadConfig(v *viper.Viper, configPath string) error { var err error // use explicitly the given user config From 592deff81088f7943c927b689114edbc9f20bbf5 Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 12:02:21 -0400 Subject: [PATCH 4/9] test: add config discovery regressions Signed-off-by: Christopher Phillips --- internal/config/application.go | 5 +- internal/config/application_test.go | 92 +++++++++++++++++++ internal/config/cataloger_options.go | 1 + internal/config/test-fixtures/.syft.yaml | 25 +++++ .../config-dir-test/.syft/config.yaml | 25 +++++ .../test-fixtures/config-home-test/.syft.yaml | 25 +++++ .../config-home-test/.syft/config.yaml | 25 +++++ 7 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 internal/config/application_test.go create mode 100644 internal/config/test-fixtures/.syft.yaml create mode 100644 internal/config/test-fixtures/config-dir-test/.syft/config.yaml create mode 100644 internal/config/test-fixtures/config-home-test/.syft.yaml create mode 100644 internal/config/test-fixtures/config-home-test/.syft/config.yaml diff --git a/internal/config/application.go b/internal/config/application.go index 4530d8e0297..bb210d81b05 100644 --- a/internal/config/application.go +++ b/internal/config/application.go @@ -235,9 +235,10 @@ func loadConfig(v *viper.Viper, configPath string) error { // check if config.yaml exists in the current directory // DEPRECATED: this will be removed in v1.0.0 if _, err := os.Stat("config.yaml"); err == nil { - log.Warn("DEPRECATED: config.yaml as a configuration file is deprecated and will be removed as an option in v1.0.0, please rename to .syft.yaml") + log.Warn("DEPRECATED: ./config.yaml as a configuration file is deprecated and will be removed as an option in v1.0.0, please rename to .syft.yaml") } - if _, err := os.Stat(confFilePath); err == nil { + + if _, err := os.Stat(confFilePath + ".yaml"); err == nil { if err = v.ReadInConfig(); err == nil { v.Set("config", v.ConfigFileUsed()) return nil diff --git a/internal/config/application_test.go b/internal/config/application_test.go new file mode 100644 index 00000000000..44f843e02e5 --- /dev/null +++ b/internal/config/application_test.go @@ -0,0 +1,92 @@ +package config + +import ( + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestApplicationConfig(t *testing.T) { + // config is picked up at desired configuration paths + // VALID: .syft.yaml, .syft/config.yaml, ~/.syft.yaml, /syft/config.yaml + // DEPRECATED: .config.yaml is currently supported by + tests := []struct { + name string + path string + setup func(t *testing.T) string + assertions func(t *testing.T, app *Application) + }{ + { + name: "explicit config", + setup: func(t *testing.T) string { + return "./test-fixtures/.syft.yaml" + }, // no-op for explicit config + assertions: func(t *testing.T, app *Application) { + assert.Equal(t, "test-config", app.File) + }, + }, + { + name: "current working directory file config", + setup: func(t *testing.T) string { + err := os.Chdir("./test-fixtures") // change application cwd to test-fixtures + if err != nil { + t.Fatalf("%s failed to change cwd: %+v", t.Name(), err) + } + return "" + }, + assertions: func(t *testing.T, app *Application) { + assert.Equal(t, "test-config", app.File) + }, + }, + { + name: "current working directory dir config", + setup: func(t *testing.T) string { + err := os.Chdir("./test-fixtures/config-dir-test") // change application cwd to test-fixtures + if err != nil { + t.Fatalf("%s failed to change cwd: %+v", t.Name(), err) + } + return "" + }, + assertions: func(t *testing.T, app *Application) { + assert.Equal(t, "test-dir-config", app.File) + }, + }, + { + name: "home directory file config", + setup: func(t *testing.T) string { + t.Setenv("HOME", "./test-fixtures/config-home-test") // set HOME to testdata + return "" + }, + assertions: func(t *testing.T, app *Application) { + assert.Equal(t, "test-home-config", app.File) + }, + }, + //{ // I don't think this is a valid test case, as the XDG_CONFIG_HOME cannot be hit using the current homedir implementation + // // check $XDG_CONFIG_HOME then fall back to checking $XDG_CONFIG_DIRS + // name: "xdg config /config.yaml", + // setup: func(t *testing.T) string { + // // NOTE: we need to temporarily unset HOME or we never reach the XDG_CONFIG_HOME check + // t.Setenv("HOME", "") // set HOME to testdata + // t.Setenv("XDG_CONFIG_HOME", "./test-fixtures/config-home-test") // set HOME to testdata + // return "" + // }, + // assertions: func(t *testing.T, app *Application) { + // assert.Equal(t, "test-home-XDG-config", app.File) + // }, + //}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + application := &Application{} + viperInstance := viper.New() + + configPath := test.setup(t) + err := application.LoadAllValues(viperInstance, configPath) + if err != nil { + t.Fatalf("failed to load application config: %+v", err) + } + test.assertions(t, application) + }) + } +} diff --git a/internal/config/cataloger_options.go b/internal/config/cataloger_options.go index 1fb2992b44c..cd92f37d25d 100644 --- a/internal/config/cataloger_options.go +++ b/internal/config/cataloger_options.go @@ -16,6 +16,7 @@ type catalogerOptions struct { func (cfg catalogerOptions) loadDefaultValues(v *viper.Viper) { v.SetDefault("package.cataloger.enabled", true) + v.SetDefault("package.cataloger.scope", "squashed") } func (cfg *catalogerOptions) parseConfigValues() error { diff --git a/internal/config/test-fixtures/.syft.yaml b/internal/config/test-fixtures/.syft.yaml new file mode 100644 index 00000000000..6b5d0fb86c8 --- /dev/null +++ b/internal/config/test-fixtures/.syft.yaml @@ -0,0 +1,25 @@ +output: "table" + +# suppress all output (except for the SBOM report) +# same as -q ; SYFT_QUIET env var +quiet: false + +# same as --file; write output report to a file (default is to write to stdout) +file: "test-config" + +# enable/disable checking for application updates on startup +# same as SYFT_CHECK_FOR_APP_UPDATE env var +check-for-app-update: true + +log: + # use structured logging + # same as SYFT_LOG_STRUCTURED env var + structured: false + + # the log level; note: detailed logging suppress the ETUI + # same as SYFT_LOG_LEVEL env var + level: "error" + + # location to write the log file (default is not to have a log file) + # same as SYFT_LOG_FILE env var + file: "" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-dir-test/.syft/config.yaml b/internal/config/test-fixtures/config-dir-test/.syft/config.yaml new file mode 100644 index 00000000000..dcc0dc3962f --- /dev/null +++ b/internal/config/test-fixtures/config-dir-test/.syft/config.yaml @@ -0,0 +1,25 @@ +output: "table" + +# suppress all output (except for the SBOM report) +# same as -q ; SYFT_QUIET env var +quiet: false + +# same as --file; write output report to a file (default is to write to stdout) +file: "test-dir-config" + +# enable/disable checking for application updates on startup +# same as SYFT_CHECK_FOR_APP_UPDATE env var +check-for-app-update: true + +log: + # use structured logging + # same as SYFT_LOG_STRUCTURED env var + structured: false + + # the log level; note: detailed logging suppress the ETUI + # same as SYFT_LOG_LEVEL env var + level: "error" + + # location to write the log file (default is not to have a log file) + # same as SYFT_LOG_FILE env var + file: "" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-home-test/.syft.yaml b/internal/config/test-fixtures/config-home-test/.syft.yaml new file mode 100644 index 00000000000..98d9535b7fd --- /dev/null +++ b/internal/config/test-fixtures/config-home-test/.syft.yaml @@ -0,0 +1,25 @@ +output: "table" + +# suppress all output (except for the SBOM report) +# same as -q ; SYFT_QUIET env var +quiet: false + +# same as --file; write output report to a file (default is to write to stdout) +file: "test-home-config" + +# enable/disable checking for application updates on startup +# same as SYFT_CHECK_FOR_APP_UPDATE env var +check-for-app-update: true + +log: + # use structured logging + # same as SYFT_LOG_STRUCTURED env var + structured: false + + # the log level; note: detailed logging suppress the ETUI + # same as SYFT_LOG_LEVEL env var + level: "error" + + # location to write the log file (default is not to have a log file) + # same as SYFT_LOG_FILE env var + file: "" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-home-test/.syft/config.yaml b/internal/config/test-fixtures/config-home-test/.syft/config.yaml new file mode 100644 index 00000000000..04b332dc998 --- /dev/null +++ b/internal/config/test-fixtures/config-home-test/.syft/config.yaml @@ -0,0 +1,25 @@ +output: "table" + +# suppress all output (except for the SBOM report) +# same as -q ; SYFT_QUIET env var +quiet: false + +# same as --file; write output report to a file (default is to write to stdout) +file: "test-home-XDG-config" + +# enable/disable checking for application updates on startup +# same as SYFT_CHECK_FOR_APP_UPDATE env var +check-for-app-update: true + +log: + # use structured logging + # same as SYFT_LOG_STRUCTURED env var + structured: false + + # the log level; note: detailed logging suppress the ETUI + # same as SYFT_LOG_LEVEL env var + level: "error" + + # location to write the log file (default is not to have a log file) + # same as SYFT_LOG_FILE env var + file: "" \ No newline at end of file From 2b0b5965c682ef522ec5d5e8650bbbbb848be4ae Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 13:22:48 -0400 Subject: [PATCH 5/9] test: add unit test cases for config coverage Signed-off-by: Christopher Phillips --- internal/config/application.go | 10 +-- internal/config/application_test.go | 62 +++++++++++++------ internal/config/test-fixtures/.syft.yaml | 2 +- .../test-fixtures/config-wd-file/.syft.yaml | 25 ++++++++ .../.syft/config.yaml | 0 5 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 internal/config/test-fixtures/config-wd-file/.syft.yaml rename internal/config/test-fixtures/{config-home-test => config-xdg-dir-test}/.syft/config.yaml (100%) diff --git a/internal/config/application.go b/internal/config/application.go index bb210d81b05..0cee7c2b3d1 100644 --- a/internal/config/application.go +++ b/internal/config/application.go @@ -270,12 +270,14 @@ func loadConfig(v *viper.Viper, configPath string) error { } } - // 4. look for /config.yaml in xdg locations (starting with xdg home config dir, then moving upwards) - v.AddConfigPath(path.Join(xdg.ConfigHome, internal.ApplicationName)) + // 4. look for ./config.yaml in xdg locations (starting with xdg home config dir, then moving upwards) + + v.SetConfigName("config") + configPath = path.Join(xdg.ConfigHome, "."+internal.ApplicationName) + v.AddConfigPath(configPath) for _, dir := range xdg.ConfigDirs { - v.AddConfigPath(path.Join(dir, internal.ApplicationName)) + v.AddConfigPath(path.Join(dir, "."+internal.ApplicationName)) } - v.SetConfigName("config") if err = v.ReadInConfig(); err == nil { v.Set("config", v.ConfigFileUsed()) return nil diff --git a/internal/config/application_test.go b/internal/config/application_test.go index 44f843e02e5..8f4504b9927 100644 --- a/internal/config/application_test.go +++ b/internal/config/application_test.go @@ -1,19 +1,21 @@ package config import ( + "github.com/adrg/xdg" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "os" + "path" "testing" ) +// TODO: set negative case when config.yaml is no longer a valid option func TestApplicationConfig(t *testing.T) { // config is picked up at desired configuration paths // VALID: .syft.yaml, .syft/config.yaml, ~/.syft.yaml, /syft/config.yaml // DEPRECATED: .config.yaml is currently supported by tests := []struct { name string - path string setup func(t *testing.T) string assertions func(t *testing.T, app *Application) }{ @@ -23,24 +25,24 @@ func TestApplicationConfig(t *testing.T) { return "./test-fixtures/.syft.yaml" }, // no-op for explicit config assertions: func(t *testing.T, app *Application) { - assert.Equal(t, "test-config", app.File) + assert.Equal(t, "test-explicit-config", app.File) }, }, { - name: "current working directory file config", + name: "current working directory named config", setup: func(t *testing.T) string { - err := os.Chdir("./test-fixtures") // change application cwd to test-fixtures + err := os.Chdir("./test-fixtures/config-wd-file") // change application cwd to test-fixtures if err != nil { t.Fatalf("%s failed to change cwd: %+v", t.Name(), err) } return "" }, assertions: func(t *testing.T, app *Application) { - assert.Equal(t, "test-config", app.File) + assert.Equal(t, "test-wd-named-config", app.File) }, }, { - name: "current working directory dir config", + name: "current working directory syft dir config", setup: func(t *testing.T) string { err := os.Chdir("./test-fixtures/config-dir-test") // change application cwd to test-fixtures if err != nil { @@ -55,6 +57,8 @@ func TestApplicationConfig(t *testing.T) { { name: "home directory file config", setup: func(t *testing.T) string { + // Because Setenv affects the whole process, it cannot be used in parallel tests or + // tests with parallel ancestors: see separate XDG test for consequence of this t.Setenv("HOME", "./test-fixtures/config-home-test") // set HOME to testdata return "" }, @@ -62,27 +66,19 @@ func TestApplicationConfig(t *testing.T) { assert.Equal(t, "test-home-config", app.File) }, }, - //{ // I don't think this is a valid test case, as the XDG_CONFIG_HOME cannot be hit using the current homedir implementation - // // check $XDG_CONFIG_HOME then fall back to checking $XDG_CONFIG_DIRS - // name: "xdg config /config.yaml", - // setup: func(t *testing.T) string { - // // NOTE: we need to temporarily unset HOME or we never reach the XDG_CONFIG_HOME check - // t.Setenv("HOME", "") // set HOME to testdata - // t.Setenv("XDG_CONFIG_HOME", "./test-fixtures/config-home-test") // set HOME to testdata - // return "" - // }, - // assertions: func(t *testing.T, app *Application) { - // assert.Equal(t, "test-home-XDG-config", app.File) - // }, - //}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + t.Fatalf("failed to get working directory: %+v", err) + } + defer os.Chdir(wd) // reset working directory after test application := &Application{} viperInstance := viper.New() configPath := test.setup(t) - err := application.LoadAllValues(viperInstance, configPath) + err = application.LoadAllValues(viperInstance, configPath) if err != nil { t.Fatalf("failed to load application config: %+v", err) } @@ -90,3 +86,29 @@ func TestApplicationConfig(t *testing.T) { }) } } + +// NOTE: this has to be separate for now because of t.Setenv behavior +// if this was included in the above table test then HOMEDIR would always +// be set; we would never fall through to the XDG case +func TestApplication_LoadAllValues_XDG(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + t.Fatalf("failed to get working directory: %+v", err) + } + defer os.Chdir(wd) // reset working directory after test + application := &Application{} + viperInstance := viper.New() + + // NOTE: we need to temporarily unset HOME or we never reach the XDG_CONFIG_HOME check + t.Setenv("HOME", "/foo/bar") + configDir := path.Join(wd, "./test-fixtures/config-xdg-dir-test") // set HOME to testdata + t.Setenv("XDG_CONFIG_DIRS", configDir) + xdg.Reload() + + err = application.LoadAllValues(viperInstance, "") + if err != nil { + t.Fatalf("failed to load application config: %+v", err) + } + + assert.Equal(t, "test-home-XDG-config", application.File) +} diff --git a/internal/config/test-fixtures/.syft.yaml b/internal/config/test-fixtures/.syft.yaml index 6b5d0fb86c8..ddf55ee5913 100644 --- a/internal/config/test-fixtures/.syft.yaml +++ b/internal/config/test-fixtures/.syft.yaml @@ -5,7 +5,7 @@ output: "table" quiet: false # same as --file; write output report to a file (default is to write to stdout) -file: "test-config" +file: "test-explicit-config" # enable/disable checking for application updates on startup # same as SYFT_CHECK_FOR_APP_UPDATE env var diff --git a/internal/config/test-fixtures/config-wd-file/.syft.yaml b/internal/config/test-fixtures/config-wd-file/.syft.yaml new file mode 100644 index 00000000000..10dd0207040 --- /dev/null +++ b/internal/config/test-fixtures/config-wd-file/.syft.yaml @@ -0,0 +1,25 @@ +output: "table" + +# suppress all output (except for the SBOM report) +# same as -q ; SYFT_QUIET env var +quiet: false + +# same as --file; write output report to a file (default is to write to stdout) +file: "test-wd-named-config" + +# enable/disable checking for application updates on startup +# same as SYFT_CHECK_FOR_APP_UPDATE env var +check-for-app-update: true + +log: + # use structured logging + # same as SYFT_LOG_STRUCTURED env var + structured: false + + # the log level; note: detailed logging suppress the ETUI + # same as SYFT_LOG_LEVEL env var + level: "error" + + # location to write the log file (default is not to have a log file) + # same as SYFT_LOG_FILE env var + file: "" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-home-test/.syft/config.yaml b/internal/config/test-fixtures/config-xdg-dir-test/.syft/config.yaml similarity index 100% rename from internal/config/test-fixtures/config-home-test/.syft/config.yaml rename to internal/config/test-fixtures/config-xdg-dir-test/.syft/config.yaml From 898fc452e014046ce1630e66d36c6473c2b0b435 Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 14:30:18 -0400 Subject: [PATCH 6/9] chore: refactor tests to account for XDG path Signed-off-by: Christopher Phillips --- internal/config/application_test.go | 28 ++++++++++++++----- .../test-fixtures/config-home-test/.syft.yaml | 25 ----------------- .../.syft/config.yaml | 0 .../config-home-test/config-file/.syft.yaml | 2 ++ 4 files changed, 23 insertions(+), 32 deletions(-) delete mode 100644 internal/config/test-fixtures/config-home-test/.syft.yaml rename internal/config/test-fixtures/{config-xdg-dir-test => config-home-test}/.syft/config.yaml (100%) create mode 100644 internal/config/test-fixtures/config-home-test/config-file/.syft.yaml diff --git a/internal/config/application_test.go b/internal/config/application_test.go index 8f4504b9927..0169e44b15d 100644 --- a/internal/config/application_test.go +++ b/internal/config/application_test.go @@ -1,12 +1,13 @@ package config import ( - "github.com/adrg/xdg" - "github.com/spf13/viper" - "github.com/stretchr/testify/assert" "os" "path" "testing" + + "github.com/adrg/xdg" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" ) // TODO: set negative case when config.yaml is no longer a valid option @@ -18,6 +19,7 @@ func TestApplicationConfig(t *testing.T) { name string setup func(t *testing.T) string assertions func(t *testing.T, app *Application) + Cleanup func(t *testing.T) }{ { name: "explicit config", @@ -27,6 +29,7 @@ func TestApplicationConfig(t *testing.T) { assertions: func(t *testing.T, app *Application) { assert.Equal(t, "test-explicit-config", app.File) }, + Cleanup: func(t *testing.T) {}, }, { name: "current working directory named config", @@ -40,6 +43,7 @@ func TestApplicationConfig(t *testing.T) { assertions: func(t *testing.T, app *Application) { assert.Equal(t, "test-wd-named-config", app.File) }, + Cleanup: func(t *testing.T) {}, }, { name: "current working directory syft dir config", @@ -53,18 +57,29 @@ func TestApplicationConfig(t *testing.T) { assertions: func(t *testing.T, app *Application) { assert.Equal(t, "test-dir-config", app.File) }, + Cleanup: func(t *testing.T) {}, }, { name: "home directory file config", setup: func(t *testing.T) string { // Because Setenv affects the whole process, it cannot be used in parallel tests or // tests with parallel ancestors: see separate XDG test for consequence of this - t.Setenv("HOME", "./test-fixtures/config-home-test") // set HOME to testdata + t.Setenv("HOME", "./test-fixtures/config-home-test") + err := os.Link("./test-fixtures/config-home-test/config-file/.syft.yaml", "./test-fixtures/config-home-test/.syft.yaml") + if err != nil { + t.Fatalf("%s failed to link home config: %+v", t.Name(), err) + } return "" }, assertions: func(t *testing.T, app *Application) { assert.Equal(t, "test-home-config", app.File) }, + Cleanup: func(t *testing.T) { + err := os.Remove("./test-fixtures/config-home-test/.syft.yaml") // + if err != nil { + t.Fatalf("%s failed to remove home config link: %+v", t.Name(), err) + } + }, }, } for _, test := range tests { @@ -83,6 +98,7 @@ func TestApplicationConfig(t *testing.T) { t.Fatalf("failed to load application config: %+v", err) } test.assertions(t, application) + test.Cleanup(t) }) } } @@ -99,9 +115,7 @@ func TestApplication_LoadAllValues_XDG(t *testing.T) { application := &Application{} viperInstance := viper.New() - // NOTE: we need to temporarily unset HOME or we never reach the XDG_CONFIG_HOME check - t.Setenv("HOME", "/foo/bar") - configDir := path.Join(wd, "./test-fixtures/config-xdg-dir-test") // set HOME to testdata + configDir := path.Join(wd, "./test-fixtures/config-home-test") // set HOME to testdata t.Setenv("XDG_CONFIG_DIRS", configDir) xdg.Reload() diff --git a/internal/config/test-fixtures/config-home-test/.syft.yaml b/internal/config/test-fixtures/config-home-test/.syft.yaml deleted file mode 100644 index 98d9535b7fd..00000000000 --- a/internal/config/test-fixtures/config-home-test/.syft.yaml +++ /dev/null @@ -1,25 +0,0 @@ -output: "table" - -# suppress all output (except for the SBOM report) -# same as -q ; SYFT_QUIET env var -quiet: false - -# same as --file; write output report to a file (default is to write to stdout) -file: "test-home-config" - -# enable/disable checking for application updates on startup -# same as SYFT_CHECK_FOR_APP_UPDATE env var -check-for-app-update: true - -log: - # use structured logging - # same as SYFT_LOG_STRUCTURED env var - structured: false - - # the log level; note: detailed logging suppress the ETUI - # same as SYFT_LOG_LEVEL env var - level: "error" - - # location to write the log file (default is not to have a log file) - # same as SYFT_LOG_FILE env var - file: "" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-xdg-dir-test/.syft/config.yaml b/internal/config/test-fixtures/config-home-test/.syft/config.yaml similarity index 100% rename from internal/config/test-fixtures/config-xdg-dir-test/.syft/config.yaml rename to internal/config/test-fixtures/config-home-test/.syft/config.yaml diff --git a/internal/config/test-fixtures/config-home-test/config-file/.syft.yaml b/internal/config/test-fixtures/config-home-test/config-file/.syft.yaml new file mode 100644 index 00000000000..c74c67437e4 --- /dev/null +++ b/internal/config/test-fixtures/config-home-test/config-file/.syft.yaml @@ -0,0 +1,2 @@ +# same as --file; write output report to a file (default is to write to stdout) +file: "test-home-config" \ No newline at end of file From e9025e3c68d94d399068e4a3ccb871e9f0f4f01a Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 14:36:58 -0400 Subject: [PATCH 7/9] chore: update hidden to full Signed-off-by: Christopher Phillips --- internal/config/application_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/config/application_test.go b/internal/config/application_test.go index 0169e44b15d..492e0811ffe 100644 --- a/internal/config/application_test.go +++ b/internal/config/application_test.go @@ -14,7 +14,7 @@ import ( func TestApplicationConfig(t *testing.T) { // config is picked up at desired configuration paths // VALID: .syft.yaml, .syft/config.yaml, ~/.syft.yaml, /syft/config.yaml - // DEPRECATED: .config.yaml is currently supported by + // DEPRECATED: config.yaml is currently supported by tests := []struct { name string setup func(t *testing.T) string From 7cf22a2e5d29a683a7a9d4028bcf0a7910acb053 Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 14:43:58 -0400 Subject: [PATCH 8/9] chore: update scope cli options to map cli default Signed-off-by: Christopher Phillips --- internal/config/cataloger_options.go | 1 - internal/config/test-fixtures/.syft.yaml | 26 +++---------------- .../config-dir-test/.syft/config.yaml | 26 +++---------------- .../config-home-test/.syft/config.yaml | 26 +++---------------- .../config-home-test/config-file/.syft.yaml | 5 +++- .../test-fixtures/config-wd-file/.syft.yaml | 26 +++---------------- 6 files changed, 17 insertions(+), 93 deletions(-) diff --git a/internal/config/cataloger_options.go b/internal/config/cataloger_options.go index cd92f37d25d..1fb2992b44c 100644 --- a/internal/config/cataloger_options.go +++ b/internal/config/cataloger_options.go @@ -16,7 +16,6 @@ type catalogerOptions struct { func (cfg catalogerOptions) loadDefaultValues(v *viper.Viper) { v.SetDefault("package.cataloger.enabled", true) - v.SetDefault("package.cataloger.scope", "squashed") } func (cfg *catalogerOptions) parseConfigValues() error { diff --git a/internal/config/test-fixtures/.syft.yaml b/internal/config/test-fixtures/.syft.yaml index ddf55ee5913..9da17f95b54 100644 --- a/internal/config/test-fixtures/.syft.yaml +++ b/internal/config/test-fixtures/.syft.yaml @@ -1,25 +1,7 @@ -output: "table" - -# suppress all output (except for the SBOM report) -# same as -q ; SYFT_QUIET env var -quiet: false - # same as --file; write output report to a file (default is to write to stdout) file: "test-explicit-config" +package: + cataloger: + scope: "squashed" -# enable/disable checking for application updates on startup -# same as SYFT_CHECK_FOR_APP_UPDATE env var -check-for-app-update: true - -log: - # use structured logging - # same as SYFT_LOG_STRUCTURED env var - structured: false - - # the log level; note: detailed logging suppress the ETUI - # same as SYFT_LOG_LEVEL env var - level: "error" - - # location to write the log file (default is not to have a log file) - # same as SYFT_LOG_FILE env var - file: "" \ No newline at end of file + # same as --scope; limit the scope of the cataloger to only the specified types \ No newline at end of file diff --git a/internal/config/test-fixtures/config-dir-test/.syft/config.yaml b/internal/config/test-fixtures/config-dir-test/.syft/config.yaml index dcc0dc3962f..0122d78dc01 100644 --- a/internal/config/test-fixtures/config-dir-test/.syft/config.yaml +++ b/internal/config/test-fixtures/config-dir-test/.syft/config.yaml @@ -1,25 +1,5 @@ -output: "table" - -# suppress all output (except for the SBOM report) -# same as -q ; SYFT_QUIET env var -quiet: false - # same as --file; write output report to a file (default is to write to stdout) file: "test-dir-config" - -# enable/disable checking for application updates on startup -# same as SYFT_CHECK_FOR_APP_UPDATE env var -check-for-app-update: true - -log: - # use structured logging - # same as SYFT_LOG_STRUCTURED env var - structured: false - - # the log level; note: detailed logging suppress the ETUI - # same as SYFT_LOG_LEVEL env var - level: "error" - - # location to write the log file (default is not to have a log file) - # same as SYFT_LOG_FILE env var - file: "" \ No newline at end of file +package: + cataloger: + scope: "squashed" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-home-test/.syft/config.yaml b/internal/config/test-fixtures/config-home-test/.syft/config.yaml index 04b332dc998..3c64be119ec 100644 --- a/internal/config/test-fixtures/config-home-test/.syft/config.yaml +++ b/internal/config/test-fixtures/config-home-test/.syft/config.yaml @@ -1,25 +1,5 @@ -output: "table" - -# suppress all output (except for the SBOM report) -# same as -q ; SYFT_QUIET env var -quiet: false - # same as --file; write output report to a file (default is to write to stdout) file: "test-home-XDG-config" - -# enable/disable checking for application updates on startup -# same as SYFT_CHECK_FOR_APP_UPDATE env var -check-for-app-update: true - -log: - # use structured logging - # same as SYFT_LOG_STRUCTURED env var - structured: false - - # the log level; note: detailed logging suppress the ETUI - # same as SYFT_LOG_LEVEL env var - level: "error" - - # location to write the log file (default is not to have a log file) - # same as SYFT_LOG_FILE env var - file: "" \ No newline at end of file +package: + cataloger: + scope: "squashed" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-home-test/config-file/.syft.yaml b/internal/config/test-fixtures/config-home-test/config-file/.syft.yaml index c74c67437e4..04b886ba5f4 100644 --- a/internal/config/test-fixtures/config-home-test/config-file/.syft.yaml +++ b/internal/config/test-fixtures/config-home-test/config-file/.syft.yaml @@ -1,2 +1,5 @@ # same as --file; write output report to a file (default is to write to stdout) -file: "test-home-config" \ No newline at end of file +file: "test-home-config" +package: + cataloger: + scope: "squashed" \ No newline at end of file diff --git a/internal/config/test-fixtures/config-wd-file/.syft.yaml b/internal/config/test-fixtures/config-wd-file/.syft.yaml index 10dd0207040..6971cbee52f 100644 --- a/internal/config/test-fixtures/config-wd-file/.syft.yaml +++ b/internal/config/test-fixtures/config-wd-file/.syft.yaml @@ -1,25 +1,5 @@ -output: "table" - -# suppress all output (except for the SBOM report) -# same as -q ; SYFT_QUIET env var -quiet: false - # same as --file; write output report to a file (default is to write to stdout) file: "test-wd-named-config" - -# enable/disable checking for application updates on startup -# same as SYFT_CHECK_FOR_APP_UPDATE env var -check-for-app-update: true - -log: - # use structured logging - # same as SYFT_LOG_STRUCTURED env var - structured: false - - # the log level; note: detailed logging suppress the ETUI - # same as SYFT_LOG_LEVEL env var - level: "error" - - # location to write the log file (default is not to have a log file) - # same as SYFT_LOG_FILE env var - file: "" \ No newline at end of file +package: + cataloger: + scope: "squashed" \ No newline at end of file From dc0ce0e26d84ba9ca493bfb44f27e5cea0f24640 Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 20 Mar 2023 15:00:51 -0400 Subject: [PATCH 9/9] chore: restructure so xdg can be in same table Signed-off-by: Christopher Phillips --- internal/config/application_test.go | 43 ++++++++++++----------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/internal/config/application_test.go b/internal/config/application_test.go index 492e0811ffe..a5108a5081c 100644 --- a/internal/config/application_test.go +++ b/internal/config/application_test.go @@ -81,9 +81,27 @@ func TestApplicationConfig(t *testing.T) { } }, }, + { + name: "XDG file config", + setup: func(t *testing.T) string { + wd, err := os.Getwd() + if err != nil { + t.Fatalf("%s: failed to get working directory: %+v", t.Name(), err) + } + configDir := path.Join(wd, "./test-fixtures/config-home-test") // set HOME to testdata + t.Setenv("XDG_CONFIG_DIRS", configDir) + xdg.Reload() + return "" + }, + assertions: func(t *testing.T, app *Application) { + assert.Equal(t, "test-home-XDG-config", app.File) + }, + Cleanup: func(t *testing.T) {}, + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { + defer test.Cleanup(t) wd, err := os.Getwd() if err != nil { t.Fatalf("failed to get working directory: %+v", err) @@ -98,31 +116,6 @@ func TestApplicationConfig(t *testing.T) { t.Fatalf("failed to load application config: %+v", err) } test.assertions(t, application) - test.Cleanup(t) }) } } - -// NOTE: this has to be separate for now because of t.Setenv behavior -// if this was included in the above table test then HOMEDIR would always -// be set; we would never fall through to the XDG case -func TestApplication_LoadAllValues_XDG(t *testing.T) { - wd, err := os.Getwd() - if err != nil { - t.Fatalf("failed to get working directory: %+v", err) - } - defer os.Chdir(wd) // reset working directory after test - application := &Application{} - viperInstance := viper.New() - - configDir := path.Join(wd, "./test-fixtures/config-home-test") // set HOME to testdata - t.Setenv("XDG_CONFIG_DIRS", configDir) - xdg.Reload() - - err = application.LoadAllValues(viperInstance, "") - if err != nil { - t.Fatalf("failed to load application config: %+v", err) - } - - assert.Equal(t, "test-home-XDG-config", application.File) -}