Skip to content

Commit

Permalink
test: write a case for TryParse (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Mar 8, 2022
1 parent 16e3f42 commit 01dfa24
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions detector/parsers/parsers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package parsers_test

import (
"errors"
"io/ioutil"
"osv-detector/detector/parsers"
"strings"
"testing"
)

func expectNumberOfParsersCalled(t *testing.T, numberOfParsersCalled int) {
t.Helper()

directories, err := ioutil.ReadDir(".")

if err != nil {
t.Fatalf("unable to read current directory: ")
}

count := 0

for _, directory := range directories {
if strings.HasPrefix(directory.Name(), "parse-") &&
!strings.HasSuffix(directory.Name(), "_test.go") {
count++
}
}

if numberOfParsersCalled != count {
t.Errorf(
"Expected %d parsers to have been called, but had %d",
count,
numberOfParsersCalled,
)
}
}

func TestTryParse_FindsExpectedParsers(t *testing.T) {
t.Parallel()

lockfiles := []string{
"cargo.lock",
"package-lock.json",
"yarn.lock",
"pnpm-lock.yaml",
"composer.lock",
"Gemfile.lock",
"requirements.txt",
}

count := 0

for _, lockfile := range lockfiles {
_, err := parsers.TryParse("/path/to/my/"+lockfile, "")

if errors.Is(err, parsers.ErrParserNotFound) {
t.Errorf("No parser was found for %s", lockfile)
}

count++
}

expectNumberOfParsersCalled(t, count)
}

0 comments on commit 01dfa24

Please sign in to comment.