Skip to content

Commit

Permalink
test: use path.Join to ensure OS-specific path separators are used
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Dec 16, 2022
1 parent 573f2ca commit 0a20993
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 82 deletions.
4 changes: 2 additions & 2 deletions internal/configer/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gopkg.in/yaml.v2"
"net/url"
"os"
"path"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -132,7 +132,7 @@ func Find(r *reporter.Reporter, pathToDirectory string) (Config, error) {
func Load(r *reporter.Reporter, pathToConfig string) (Config, error) {
var raw rawConfig

pathToConfig = path.Clean(pathToConfig)
pathToConfig = filepath.Clean(pathToConfig)

raw.FilePath = pathToConfig

Expand Down
21 changes: 11 additions & 10 deletions internal/configer/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/g-rath/osv-detector/internal/configer"
"github.com/g-rath/osv-detector/internal/reporter"
"path/filepath"
"reflect"
"strings"
"testing"
Expand All @@ -26,7 +27,7 @@ func TestFind_NoConfig(t *testing.T) {

r, _, _ := newReporter(t)

config, err := configer.Find(r, "fixtures/no-config")
config, err := configer.Find(r, filepath.FromSlash("fixtures/no-config"))

if err != nil {
t.Errorf("Find() error = %v, expected nothing", err)
Expand All @@ -47,9 +48,9 @@ func TestFind_ExtYml(t *testing.T) {
r, _, _ := newReporter(t)

expectedIgnores := []string{"GHSA-1", "GHSA-2", "GHSA-3"}
expectedFilePath := "fixtures/ext-yml/.osv-detector.yml"
expectedFilePath := filepath.FromSlash("fixtures/ext-yml/.osv-detector.yml")

config, err := configer.Find(r, "fixtures/ext-yml")
config, err := configer.Find(r, filepath.FromSlash("fixtures/ext-yml"))

if err != nil {
t.Errorf("Find() error = %v, expected nothing", err)
Expand All @@ -69,9 +70,9 @@ func TestFind_ExtYml_Invalid(t *testing.T) {

r, _, _ := newReporter(t)

expectedFilePath := "fixtures/ext-yml-invalid/.osv-detector.yml"
expectedFilePath := filepath.FromSlash("fixtures/ext-yml-invalid/.osv-detector.yml")

config, err := configer.Find(r, "fixtures/ext-yml-invalid")
config, err := configer.Find(r, filepath.FromSlash("fixtures/ext-yml-invalid"))

if err == nil {
t.Errorf("Find() did not error, which was unexpected")
Expand All @@ -92,9 +93,9 @@ func TestFind_ExtYaml(t *testing.T) {
r, _, _ := newReporter(t)

expectedIgnores := []string{"GHSA-4", "GHSA-5", "GHSA-6"}
expectedFilePath := "fixtures/ext-yaml/.osv-detector.yaml"
expectedFilePath := filepath.FromSlash("fixtures/ext-yaml/.osv-detector.yaml")

config, err := configer.Find(r, "fixtures/ext-yaml")
config, err := configer.Find(r, filepath.FromSlash("fixtures/ext-yaml"))

if err != nil {
t.Errorf("Find() error = %v, expected nothing", err)
Expand All @@ -114,9 +115,9 @@ func TestFind_ExtYaml_Invalid(t *testing.T) {

r, _, _ := newReporter(t)

expectedFilePath := "fixtures/ext-yaml-invalid/.osv-detector.yaml"
expectedFilePath := filepath.FromSlash("fixtures/ext-yaml-invalid/.osv-detector.yaml")

config, err := configer.Find(r, "fixtures/ext-yaml-invalid")
config, err := configer.Find(r, filepath.FromSlash("fixtures/ext-yaml-invalid"))

if err == nil {
t.Errorf("Find() did not error, which was unexpected")
Expand All @@ -136,7 +137,7 @@ func TestLoad(t *testing.T) {

r, _, stderr := newReporter(t)

fixturePath := "fixtures/extra-databases/.osv-detector.yml"
fixturePath := filepath.FromSlash("fixtures/extra-databases/.osv-detector.yml")

config, err := configer.Load(r, fixturePath)

Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/g-rath/osv-detector/pkg/lockfile"
"io"
"os"
"path"
"path/filepath"
"sort"
)

Expand Down Expand Up @@ -263,7 +263,7 @@ func findLockfiles(r *reporter.Reporter, pathToLockOrDirectory string, parseAs s
}
}

lockfiles = append(lockfiles, path.Join(pathToLockOrDirectory, dir.Name()))
lockfiles = append(lockfiles, filepath.Join(pathToLockOrDirectory, dir.Name()))
}
}
} else {
Expand Down Expand Up @@ -300,7 +300,7 @@ func findAllLockfiles(r *reporter.Reporter, pathsToCheck []string, parseAs strin
}

for _, p := range lps {
paths = append(paths, path.Clean(p))
paths = append(paths, filepath.Clean(p))
}
}

Expand Down Expand Up @@ -417,7 +417,7 @@ func readAllLockfiles(

for _, pathToLock := range pathsToLocks {
if checkForLocalConfig {
base := path.Dir(pathToLock)
base := filepath.Dir(pathToLock)
con, err := configer.Find(r, base)

if err != nil {
Expand Down

0 comments on commit 0a20993

Please sign in to comment.