add tests

This commit is contained in:
Tyrel Souza 2019-05-13 23:26:13 -04:00 committed by Tyrel Souza
parent 4f6db49d41
commit 44d7199e56
No known key found for this signature in database
GPG Key ID: 5A9394D4C30AEAC0
4 changed files with 148 additions and 106 deletions

117
main.go
View File

@ -32,125 +32,30 @@ func main() {
func upload(c *cli.Context) error { func upload(c *cli.Context) error {
directory := c.Args().First() directory := c.Args().First()
fmt.Println(strings.Join(getAllManifests(directory), ", "))
// TODO, actually upload, don't just print names
return nil
}
func getAllManifests(directory string) []string {
matchingFiles := make([]string, 0) matchingFiles := make([]string, 0)
for _, glob := range manifestGlobs() { for _, glob := range manifestGlobs() {
globPattern := []string{directory, "**", glob} pathGlob := fmt.Sprintf("%s/**/%s", directory, glob)
pathGlob := strings.Join(globPattern, "/") files := getManifestGlobMatches(pathGlob)
files := getManifestMatches(pathGlob)
if len(files) > 0 { if len(files) > 0 {
matchingFiles = append(matchingFiles, files...) matchingFiles = append(matchingFiles, files...)
} }
} }
return matchingFiles
fmt.Println(strings.Join(matchingFiles, ", "))
return nil
} }
func getManifestMatches(glob string) []string { func getManifestGlobMatches(glob string) []string {
files, err := doublestar.Glob(glob) files, err := doublestar.Glob(glob)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
return files return files
} }
func manifestGlobs() []string {
globs := make([]string, 0)
// Hackage
globs = append(globs, "*.cabal")
// npm
globs = append(globs, "package.json")
globs = append(globs, "package-lock.json")
globs = append(globs, "npm-shrinkwrap.json")
globs = append(globs, "yarn.lock")
// Maven
globs = append(globs, "pom.xml")
globs = append(globs, "ivy.xml")
globs = append(globs, "build.gradle")
// RubyGems
globs = append(globs, "Gemfile")
globs = append(globs, "Gemfile.lock")
globs = append(globs, "gems.rb")
globs = append(globs, "gems.locked")
globs = append(globs, "*.gemspec")
// Packagist
globs = append(globs, "composer.json")
globs = append(globs, "composer.lock")
// PyPi
globs = append(globs, "setup.py")
globs = append(globs, "req*.txt")
globs = append(globs, "req*.pip")
globs = append(globs, "requirements/*.txt")
globs = append(globs, "requirements/*.pip")
globs = append(globs, "Pipfile")
globs = append(globs, "Pipfile.lock")
// Nuget
globs = append(globs, "packages.config")
globs = append(globs, "Project.json")
globs = append(globs, "Project.lock.json")
globs = append(globs, "*.nuspec")
globs = append(globs, "paket.lock")
globs = append(globs, "*.csproj")
// Bower
globs = append(globs, "bower.json")
// CPAN
globs = append(globs, "META.json")
globs = append(globs, "META.yml")
// CocoaPods
globs = append(globs, "Podfile")
globs = append(globs, "Podfile.lock")
globs = append(globs, "*.podspec")
// Clojars
globs = append(globs, "project.clj")
// Meteor
globs = append(globs, "versions.json")
// CRAN
globs = append(globs, "DESCRIPTION")
// Cargo
globs = append(globs, "Cargo.toml")
globs = append(globs, "Cargo.lock")
// Hex
globs = append(globs, "mix.exs")
globs = append(globs, "mix.lock")
// Swift
globs = append(globs, "Package.swift")
// Pub
globs = append(globs, "pubspec.yaml")
globs = append(globs, "pubspec.lock")
// Carthage
globs = append(globs, "Cartfile")
globs = append(globs, "Cartfile.private")
globs = append(globs, "Cartfile.resolved")
// Dub
globs = append(globs, "dub.json")
globs = append(globs, "dub.sdl")
// Julia
globs = append(globs, "REQUIRE")
// Shards
globs = append(globs, "shard.yml")
globs = append(globs, "shard.lock")
// Go
globs = append(globs, "glide.yaml")
globs = append(globs, "glide.lock")
globs = append(globs, "Godeps")
globs = append(globs, "Godeps/Godeps.json")
globs = append(globs, "vendor/manifest")
globs = append(globs, "vendor/vendor.json")
globs = append(globs, "Gopkg.toml")
globs = append(globs, "Gopkg.lock")
// Elm
globs = append(globs, "elm-package.json")
globs = append(globs, "elm_dependencies.json")
globs = append(globs, "elm-stuff/exact-dependencies.json")
// Haxelib
globs = append(globs, "haxelib.json")
// Hackage
globs = append(globs, "*.cabal")
globs = append(globs, "cabal.config")
return globs
}

31
main_test.go Normal file
View File

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

106
manifests.go Normal file
View File

@ -0,0 +1,106 @@
package main
/*
When adding new manifest types, for new package managers
add a new entry here.
*/
func manifestGlobs() []string {
globs := make([]string, 0)
// Hackage
globs = append(globs, "*.cabal")
// npm
globs = append(globs, "package.json")
globs = append(globs, "package-lock.json")
globs = append(globs, "npm-shrinkwrap.json")
globs = append(globs, "yarn.lock")
// Maven
globs = append(globs, "pom.xml")
globs = append(globs, "ivy.xml")
globs = append(globs, "build.gradle")
// RubyGems
globs = append(globs, "Gemfile")
globs = append(globs, "Gemfile.lock")
globs = append(globs, "gems.rb")
globs = append(globs, "gems.locked")
globs = append(globs, "*.gemspec")
// Packagist
globs = append(globs, "composer.json")
globs = append(globs, "composer.lock")
// PyPi
globs = append(globs, "setup.py")
globs = append(globs, "req*.txt")
globs = append(globs, "req*.pip")
globs = append(globs, "requirements/*.txt")
globs = append(globs, "requirements/*.pip")
globs = append(globs, "Pipfile")
globs = append(globs, "Pipfile.lock")
// Nuget
globs = append(globs, "packages.config")
globs = append(globs, "Project.json")
globs = append(globs, "Project.lock.json")
globs = append(globs, "*.nuspec")
globs = append(globs, "paket.lock")
globs = append(globs, "*.csproj")
// Bower
globs = append(globs, "bower.json")
// CPAN
globs = append(globs, "META.json")
globs = append(globs, "META.yml")
// CocoaPods
globs = append(globs, "Podfile")
globs = append(globs, "Podfile.lock")
globs = append(globs, "*.podspec")
// Clojars
globs = append(globs, "project.clj")
// Meteor
globs = append(globs, "versions.json")
// CRAN
globs = append(globs, "DESCRIPTION")
// Cargo
globs = append(globs, "Cargo.toml")
globs = append(globs, "Cargo.lock")
// Hex
globs = append(globs, "mix.exs")
globs = append(globs, "mix.lock")
// Swift
globs = append(globs, "Package.swift")
// Pub
globs = append(globs, "pubspec.yaml")
globs = append(globs, "pubspec.lock")
// Carthage
globs = append(globs, "Cartfile")
globs = append(globs, "Cartfile.private")
globs = append(globs, "Cartfile.resolved")
// Dub
globs = append(globs, "dub.json")
globs = append(globs, "dub.sdl")
// Julia
globs = append(globs, "REQUIRE")
// Shards
globs = append(globs, "shard.yml")
globs = append(globs, "shard.lock")
// Go
globs = append(globs, "glide.yaml")
globs = append(globs, "glide.lock")
globs = append(globs, "Godeps")
globs = append(globs, "Godeps/Godeps.json")
globs = append(globs, "vendor/manifest")
globs = append(globs, "vendor/vendor.json")
globs = append(globs, "Gopkg.toml")
globs = append(globs, "Gopkg.lock")
// Elm
globs = append(globs, "elm-package.json")
globs = append(globs, "elm_dependencies.json")
globs = append(globs, "elm-stuff/exact-dependencies.json")
// Haxelib
globs = append(globs, "haxelib.json")
// Hackage
globs = append(globs, "*.cabal")
globs = append(globs, "cabal.config")
return globs
}

0
test/fixtures/notamanifest vendored Normal file
View File