func configureLogging() { if flagDebug { log.SetGlobalLogLevel(log.DEBUG) } else if flagQuiet { log.SetGlobalLogLevel(log.ERROR) } else if flagVerbose { log.SetGlobalLogLevel(log.INFO) } else { log.SetGlobalLogLevel(log.WARN) } }
func TestVersionSpecRank(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) for _, item := range []vsRankTest{ vsRankTest{"=1", "=5", false, false}, vsRankTest{">1", "=5", false, true}, vsRankTest{"<7", "=5", false, true}, vsRankTest{"=1", "=1", true, true}, vsRankTest{">6", "<4", false, false}, vsRankTest{">2", ">4", false, true}, vsRankTest{">2.0", ">4.0", false, true}, vsRankTest{">2.0.0", ">4.0.0", false, true}, } { var err error var vsA, vsB *VersionSpec if vsA, err = ParseVersionSpec(item.A); err != nil { t.Errorf("Error parsing version spec: '%v': %v", item.A, err) } if vsB, err = ParseVersionSpec(item.B); err != nil { t.Errorf("Error parsing version spec: '%v': %v", item.B, err) } if vsA.Outranks(vsB) != item.ResultA { t.Errorf("'%v' outrank '%v' == %v, expected %v", item.A, item.B, !item.ResultA, item.ResultA) } if vsB.Outranks(vsA) != item.ResultB { t.Errorf("'%v' outrank '%v' == %v, expected %v", item.B, item.A, !item.ResultB, item.ResultB) } } }
func TestVersionSatisfaction(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) for _, item := range []vsSatisfyTest{ vsSatisfyTest{"=1", "5", false}, vsSatisfyTest{">7", "5", false}, vsSatisfyTest{">1", "5", true}, vsSatisfyTest{"<6", "2", true}, vsSatisfyTest{">=2", "2", true}, vsSatisfyTest{">=1.*.*", "2", true}, vsSatisfyTest{">=1.0.*", "2.1", true}, vsSatisfyTest{">=1.0.*", "2.1.10", true}, vsSatisfyTest{"=1.0.*", "1.0.10", true}, vsSatisfyTest{"=1.0.*", "1.0", true}, vsSatisfyTest{"1.0", "1.0", true}, vsSatisfyTest{"1.0", "v1.0", true}, } { var err error var vs *VersionSpec var ver *Version if vs, err = ParseVersionSpec(item.Vspec); err != nil { t.Errorf("Error parsing version spec: '%v': %v", item.Vspec, err) } if ver, err = ParseVersion(item.Ver); err != nil { t.Errorf("Error parsing version: '%v': %v", item.Ver, err) } if vs.IsSatisfiedBy(ver) != item.Result { t.Errorf("'%v' satisfied by '%v' == %v, expected %v", item.Vspec, item.Ver, !item.Result, item.Result) } } }
func TestLoadEmptyRewriteRules(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) rules, err := LoadRewriteRules("testfiles/empty.grapnelrc.toml") if err != nil { t.Errorf("Error loading rules") t.Errorf("%v", err) return } if len(rules) != 0 { t.Errorf("Expected no rules to be parsed from config file") return } }
func TestLibResolveDeps(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) // construct a test for a basic resolver for type 'test' resolver := &Resolver{ LibSources: map[string]LibSource{ "test": &testSCM{}, }, } // dependency set for test testDeps := []*Dependency{ &Dependency{ Import: "dependency1", VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), Type: "test", }, &Dependency{ Import: "dependency2", Url: url.MustParse("foo://somewhere.com/baz/gorf"), VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), }, &Dependency{ Import: "dependency3", Url: url.MustParse("http://foobar.com/baz/gorf"), VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), }, } // target library set resolved := make(map[string]*Library) // create a pre-resolved entry dep := testDeps[0] resolved[dep.Import] = &Library{ Version: &Version{1, 0, 0}, } // test resolution if deps, err := resolver.LibResolveDeps(resolved, testDeps); err != nil { t.Errorf("%v", err) } else if len(deps) != 2 { t.Error("Expected 2 deps: got %v instead", len(deps)) } else if deps[0].Import != testDeps[1].Import { t.Error("Result 0 should be %v not %v", testDeps[1].Import, deps[0].Import) } else if deps[1].Import != testDeps[2].Import { t.Error("Result 1 should be %v not %v", testDeps[2].Import, deps[1].Import) } }
func TestDeduplicateDeps(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) // construct a test for a basic resolver for type 'test' resolver := &Resolver{ LibSources: map[string]LibSource{ "test": &testSCM{}, }, } // dependency set for test testDeps := []*Dependency{ &Dependency{ Import: "dependency1", VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), Type: "test", }, &Dependency{ Import: "dependency2", Url: url.MustParse("foo://somewhere.com/baz/gorf"), VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), }, &Dependency{ Import: "dependency3", Url: url.MustParse("http://foobar.com/baz/gorf"), VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), }, } targetLen := len(testDeps) // ... duplicated testDeps = append(testDeps, testDeps...) // positive test results, err := resolver.DeduplicateDeps(testDeps) if err != nil { t.Errorf("%v", err) } if len(results) != targetLen { t.Errorf("Expected length of %v; got %v instead", targetLen, len(results)) } }
func TestParseVersion(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) // positive tests for k, v := range map[string]*Version{ "1.0": NewVersion(1, 0, -1), "1.1": NewVersion(1, 1, -1), "1.1.1": NewVersion(1, 1, 1), " v1.5 ": NewVersion(1, 5, -1), " release100.0 ": NewVersion(100, 0, -1), "release.r60 ": NewVersion(60, -1, -1), "r2015.01.24": NewVersion(2015, 1, 24), " 5 ": NewVersion(5, -1, -1), } { version, err := ParseVersion(k) if err != nil { t.Errorf("Error parsing version: '%v': %v", k, err) } if v.Major != version.Major { t.Errorf("Major values don't match for '%v': %v vs %v", k, v.Major, version.Major) } if v.Minor != version.Minor { t.Errorf("Minor values don't match for '%v': %v vs %v", k, v.Minor, version.Minor) } if v.Subminor != version.Subminor { t.Errorf("Subminor values don't match for '%v': %v vs %v", k, v.Subminor, version.Subminor) } } // negative tests for _, item := range []string{ "1.1.1.1", "7dbad25113954256a925a5a1f7348b92f196b295", } { if _, err := ParseVersion(item); err == nil { t.Errorf("Bad version parsed okay: %v", item) } } }
func TestParseVersionSpec(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) // positive tests for k, v := range map[string]*VersionSpec{ ">1.0": NewVersionSpec(OpGt, 1, 0, -1), "=1.1": NewVersionSpec(OpEq, 1, 1, -1), "<1.1.1": NewVersionSpec(OpLt, 1, 1, 1), " <= 1.*.* ": NewVersionSpec(OpLte, 1, -1, -1), " >= 100.0.* ": NewVersionSpec(OpGte, 100, 0, -1), " 5 ": NewVersionSpec(OpEq, 5, -1, -1), } { version, err := ParseVersionSpec(k) if err != nil { t.Errorf("Error parsing version: '%v': %v", k, err) } if v.Oper != version.Oper { t.Errorf("Operators don't match for '%v': %v vs %v", k, v.Oper, version.Oper) } if v.Major != version.Major { t.Errorf("Major values don't match for '%v': %v vs %v", k, v.Major, version.Major) } if v.Minor != version.Minor { t.Errorf("Minor values don't match for '%v': %v vs %v", k, v.Minor, version.Minor) } if v.Subminor != version.Subminor { t.Errorf("Subminor values don't match for '%v': %v vs %v", k, v.Subminor, version.Subminor) } } // negative tests for _, item := range []string{ "v1.0", "1.0xyz", "1.1.1.1", } { if _, err := ParseVersionSpec(item); err == nil { t.Errorf("Bad version parsed okay: %v", item) } } }
func TestResolveDependencies(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) // construct a test for a basic resolver for type 'test' resolver := &Resolver{ LibSources: map[string]LibSource{ "test": &testSCM{}, }, } // dependency set for test testDeps := []*Dependency{ &Dependency{ Import: "dependency1", VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), Type: "test", }, &Dependency{ Import: "dependency2", VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), Type: "test", }, &Dependency{ Import: "dependency3", VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), Type: "test", }, } // test using the test data libs, err := resolver.ResolveDependencies(testDeps) if err != nil { t.Errorf("Error resolving dependencies: %v", err) } if len(libs) != len(testDeps) { t.Errorf("Error resolving dependencies. Expected %v entries, got %v instead.", len(testDeps), len(libs)) } }
func TestResolver(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) // construct a test for a basic resolver for type 'test' resolver := &Resolver{ LibSources: map[string]LibSource{ "test": &testSCM{}, }, } // positive tests for ii, dep := range []*Dependency{ &Dependency{ Import: "dependency1", VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), Type: "test", }, } { if _, err := resolver.Resolve(dep); err != nil { t.Errorf("Error resolving dependency %v: %v", ii, err) t.Log("Dep: ", dep.Flatten()) } } // negative tests for ii, dep := range []*Dependency{ &Dependency{ Import: "dependency2", Url: url.MustParse("foo://somewhere.com/baz/gorf"), VersionSpec: NewVersionSpec(OpEq, 1, 0, -1), }, } { if _, err := resolver.Resolve(dep); err == nil { t.Errorf("Error ignoring dependency %v", ii) t.Log("Dep: ", dep.Flatten()) } } }
func InitTestLogging() { log.SetGlobalLogLevel(log.DEBUG) log.SetFlags(0) }
func TestLoadRewriteRules(t *testing.T) { log.SetGlobalLogLevel(log.DEBUG) testRules := []*RewriteRule{ &RewriteRule{ Matches: MatchMap{ "import": regexp.MustCompile(`.+`), "url": regexp.MustCompile(`^$`), }, Replacements: ReplaceMap{ "url": template.Must(RewriteTemplate(`http://{{.import}}`)), }, }, // generic rewrite for missing import &RewriteRule{ Matches: MatchMap{ "import": regexp.MustCompile(`^$`), "url": regexp.MustCompile(`.+`), }, Replacements: ReplaceMap{ "import": template.Must(RewriteTemplate(`{{.host}}/{{.path}}`)), }, }, // rewrite rules for misc git resolvers TypeResolverRule("scheme", `git`, `git`), TypeResolverRule("path", `.*\.git`, `git`), TypeResolverRule("import", `github.com/.*`, `git`), TypeResolverRule("host", `github.com`, `git`), // rewrite rules for gopkg.in &RewriteRule{ Matches: MatchMap{ "host": regexp.MustCompile(`gopkg\.in`), }, Replacements: ReplaceMap{ "branch": template.Must(RewriteTemplate(`{{replace .path "^.*\\.(.*)$" "$1"}}`)), "path": template.Must(RewriteTemplate(`{{replace .path "^(.*)\\..*$" "$1"}}`)), "host": template.Must(RewriteTemplate(`github.com`)), "type": template.Must(RewriteTemplate(`git`)), }, }, } rules, err := LoadRewriteRules("testfiles/grapnelrc.toml") if err != nil { t.Errorf("Error loading rules") t.Errorf("%v", err) return } if len(rules) != len(testRules) { t.Errorf("Array length for rules do not match: %d vs %d", len(rules), len(testRules)) return } for idx, testRule := range testRules { rule := rules[idx] if len(rule.Matches) != len(testRule.Matches) { t.Errorf("Match array length for test %d do not match", idx) t.Logf("testRule: %v", testRule.Matches) t.Logf("rule: %v", rule.Matches) } if len(rule.Replacements) != len(testRule.Replacements) { t.Errorf("Replacement array length for test %d do not match", idx) t.Logf("testRule: %v", testRule.Replacements) t.Logf("rule: %v", rule.Replacements) } for key, testMatch := range testRule.Matches { if match, ok := rule.Matches[key]; !ok { t.Errorf("Test %d - Rule match key %s is missing", idx, key) } else if !reflect.DeepEqual(match, testMatch) { t.Errorf("Test %d - Rule match for key %s does not equal expected value", idx, key) } } /* TODO: figure out some way to verify the templates have parsed and match for key, testReplace := range testRule.Replacements { if replace, ok := rule.Replacements[key]; !ok { t.Errorf("Test %d - Rule replace key %s is missing", idx, key) } else if !reflect.DeepEqual(replace, testReplace) { t.Errorf("Test %d - Rule replace for key %s does not equal expected value", idx, key) } } */ } }