sorting in tests

This commit is contained in:
Tyrel Souza 2019-05-14 17:16:59 -04:00 committed by Tyrel Souza
parent e323fc4e70
commit fcd11a3bd0
No known key found for this signature in database
GPG Key ID: 5A9394D4C30AEAC0
3 changed files with 38 additions and 26 deletions

23
main.go
View File

@ -6,7 +6,6 @@ import (
"os"
"strings"
"github.com/bmatcuk/doublestar"
"github.com/urfave/cli"
)
@ -48,25 +47,3 @@ func scan(c *cli.Context) error {
// TODO, actually scan, don't just print names
return nil
}
func getAllManifests(directory string) []string {
matchingFiles := make([]string, 0)
for _, glob := range manifestGlobs() {
pathGlob := fmt.Sprintf("%s/**/%s", directory, glob)
files := getManifestGlobMatches(pathGlob)
if len(files) > 0 {
matchingFiles = append(matchingFiles, files...)
}
}
return matchingFiles
}
func getManifestGlobMatches(glob string) []string {
files, err := doublestar.Glob(glob)
if err != nil {
log.Fatal(err)
}
return files
}

View File

@ -1,6 +1,7 @@
package main
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
@ -13,19 +14,24 @@ func TestGetManifestGlobMatches(t *testing.T) {
"test/fixtures/a/b/c/d/e/f/g/requirements.txt",
}
got := getManifestGlobMatches("test/**/requirements.txt")
sort.Strings(expected)
sort.Strings(got)
assert.Equal(t, expected, got, "they should be equal")
}
func TestGetAllManifests(t *testing.T) {
all := []string{
expected := []string{
"test/fixtures/yarn.lock",
"test/fixtures/Gemfile",
"test/fixtures/subdir/requirements.txt",
"test/fixtures/a/b/c/d/e/f/g/requirements.txt",
"test/fixtures/subdir/requirements.txt",
}
got := getAllManifests("test/")
assert.Equal(t, all, got, "they should match")
sort.Strings(expected)
sort.Strings(got)
assert.Equal(t, expected, got, "they should match")
}

View File

@ -1,5 +1,12 @@
package main
import (
"fmt"
"log"
"github.com/bmatcuk/doublestar"
)
/*
When adding new manifest types, for new package managers
add a new entry here.
@ -104,3 +111,25 @@ func manifestGlobs() []string {
return globs
}
func getAllManifests(directory string) []string {
matchingFiles := make([]string, 0)
for _, glob := range manifestGlobs() {
pathGlob := fmt.Sprintf("%s/**/%s", directory, glob)
files := getManifestGlobMatches(pathGlob)
if len(files) > 0 {
matchingFiles = append(matchingFiles, files...)
}
}
return matchingFiles
}
func getManifestGlobMatches(glob string) []string {
files, err := doublestar.Glob(glob)
if err != nil {
log.Fatal(err)
}
return files
}