-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: write a case for
TryParse
(#45)
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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) | ||
} |