package framework import ( "errors" "flag" "fmt" "path/filepath" "strings" ) // testContext is a private struct used to create a global test context leveraged // by all type testContext struct { Labels map[string]string SkipLabels map[string]string RepoRoot string } // Context is the public instance of our private context struct. All framework // instances and specific suite logic should interact with this object when // reading or updating test context var Context testContext // RegisterCommonFlags registers flags for the framework that are common to // all suites (e.g., parameters for Ginkgo or other config that effects // how the tests are ran) and sets some defaults for Ginkgo func RegisterCommonFlags(flags *flag.FlagSet) { flags.Func("labels", "only run tests with the provided comma separated labels", func(s string) error { Context.Labels = commaSepValues(s) return nil }) flags.Func("skip-labels", "run all tests except those with the provided comma separated labels", func(s string) error { Context.SkipLabels = commaSepValues(s) return nil }) flags.StringVar(&Context.RepoRoot, "repo-root", "", "absolute path to repository root") } // parses comma separated map values func commaSepValues(s string) map[string]string { a := strings.Split(s, ",") r := map[string]string{} for _, v := range a { if v != "" { kv := strings.Split(v, "=") // drop malformed input if len(kv) > 1 { r[kv[0]] = kv[1] } } } return r } // Validate checks that the base test context was provided valid values via // config func (c *testContext) Validate() { if c.Labels != nil && c.SkipLabels != nil { panic(errors.New("-labels and -skip-labels are mutually exclusive")) } } // ResolvePath resolves the provided path relative to the repository root. // If a repository root isnt provided via flag, it panics. func ResolvePath(p ...string) string { if Context.RepoRoot == "" { panic(fmt.Sprintf("cant resolve path to %s, -repo-root not provided", p)) } elements := append([]string{Context.RepoRoot}, p...) return filepath.Join(elements...) }