Skip to content
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

fix: announce when a config file is invalid and exit with a non-zero code #1242

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cmd/osv-scanner/__snapshots__/main_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,29 @@ overriding license for package Packagist/league/flysystem/1.0.8 with 0BSD

---

[TestRun/config_file_is_invalid - 1]
Scanning dir ./fixtures/config-invalid
Scanned <rootdir>/fixtures/config-invalid/composer.lock file and found 1 package

---

[TestRun/config_file_is_invalid - 2]
Ignored invalid config file at: <rootdir>/fixtures/config-invalid/osv-scanner.toml

---

[TestRun/config_file_is_invalid#01 - 1]
Scanning dir ./fixtures/config-invalid
Scanned <rootdir>/fixtures/config-invalid/composer.lock file and found 1 package
Config file <rootdir>/fixtures/config-invalid/osv-scanner.toml is invalid because: toml: line 1: expected '.' or '=', but got '!' instead

---

[TestRun/config_file_is_invalid#01 - 2]
Ignored invalid config file at: <rootdir>/fixtures/config-invalid/osv-scanner.toml

---

[TestRun/cyclonedx_1.4_output - 1]
{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
Expand Down
51 changes: 51 additions & 0 deletions cmd/osv-scanner/fixtures/config-invalid/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/osv-scanner/fixtures/config-invalid/osv-scanner.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!
11 changes: 11 additions & 0 deletions cmd/osv-scanner/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ func TestRun(t *testing.T) {
args: []string{"", "--config=./fixtures/osv-scanner-composite-config.toml", "--experimental-licenses", "MIT", "./fixtures/locks-many", "./fixtures/locks-insecure"},
exit: 1,
},
// invalid config file
{
name: "config file is invalid",
args: []string{"", "./fixtures/config-invalid"},
exit: 127,
},
{
name: "config file is invalid",
args: []string{"", "--verbosity", "verbose", "./fixtures/config-invalid"},
exit: 127,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
10 changes: 8 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -175,6 +176,11 @@ func (c *ConfigManager) Get(r reporter.Reporter, targetPath string) Config {
if configErr == nil {
r.Infof("Loaded filter from: %s\n", config.LoadPath)
} else {
// anything other than the config file not existing is most likely due to an invalid config file
if !errors.Is(configErr, os.ErrNotExist) {
r.Errorf("Ignored invalid config file at: %s\n", configPath)
r.Verbosef("Config file %s is invalid because: %v\n", configPath, configErr)
}
// If config doesn't exist, use the default config
config = c.DefaultConfig
}
Expand Down Expand Up @@ -211,12 +217,12 @@ func tryLoadConfig(configPath string) (Config, error) {

_, err := toml.NewDecoder(file).Decode(&config)
if err != nil {
return Config{}, fmt.Errorf("failed to parse config file: %w", err)
return Config{}, err
}
config.LoadPath = configPath

return config, nil
}

return Config{}, fmt.Errorf("no config file found on this path: %s", configPath)
return Config{}, err
}