-
Notifications
You must be signed in to change notification settings - Fork 881
Deprecate config.yaml as valid config source; Add unit regression for correct config paths #1640
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
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
51398ab
Add pwd to config search path iff it contains a config
AidanDelaney b88f11d
refactor: update to deprecate behavior pre v1.0.0
spiffcs 59f9061
chore: add nolint directive for SA
spiffcs 592deff
test: add config discovery regressions
spiffcs 2b0b596
test: add unit test cases for config coverage
spiffcs 898fc45
chore: refactor tests to account for XDG path
spiffcs e9025e3
chore: update hidden to full
spiffcs 7cf22a2
chore: update scope cli options to map cli default
spiffcs dc0ce0e
chore: restructure so xdg can be in same table
spiffcs 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| 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, <XDG_CONFIG_HOME>/syft/config.yaml | ||
| // DEPRECATED: .config.yaml is currently supported by | ||
|
spiffcs marked this conversation as resolved.
Outdated
|
||
| tests := []struct { | ||
| name 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-explicit-config", app.File) | ||
| }, | ||
| }, | ||
| { | ||
| name: "current working directory named config", | ||
| setup: func(t *testing.T) string { | ||
| 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-wd-named-config", app.File) | ||
| }, | ||
| }, | ||
| { | ||
| 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 { | ||
| 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 { | ||
| // 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 "" | ||
| }, | ||
| assertions: func(t *testing.T, app *Application) { | ||
| assert.Equal(t, "test-home-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) | ||
| if err != nil { | ||
| t.Fatalf("failed to load application config: %+v", err) | ||
| } | ||
| test.assertions(t, application) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // 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) | ||
| } | ||
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,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-explicit-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: "" |
25 changes: 25 additions & 0 deletions
25
internal/config/test-fixtures/config-dir-test/.syft/config.yaml
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,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: "" |
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,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: "" |
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,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: "" |
25 changes: 25 additions & 0 deletions
25
internal/config/test-fixtures/config-xdg-dir-test/.syft/config.yaml
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,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: "" |
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.
Uh oh!
There was an error while loading. Please reload this page.