...
1 package framework
2
3 import (
4 "errors"
5 "flag"
6 "fmt"
7 "path/filepath"
8 "strings"
9 )
10
11
12
13 type testContext struct {
14 Labels map[string]string
15 SkipLabels map[string]string
16 RepoRoot string
17 }
18
19
20
21
22 var Context testContext
23
24
25
26
27 func RegisterCommonFlags(flags *flag.FlagSet) {
28 flags.Func("labels", "only run tests with the provided comma separated labels", func(s string) error {
29 Context.Labels = commaSepValues(s)
30 return nil
31 })
32
33 flags.Func("skip-labels", "run all tests except those with the provided comma separated labels", func(s string) error {
34 Context.SkipLabels = commaSepValues(s)
35 return nil
36 })
37
38 flags.StringVar(&Context.RepoRoot, "repo-root", "", "absolute path to repository root")
39 }
40
41
42 func commaSepValues(s string) map[string]string {
43 a := strings.Split(s, ",")
44 r := map[string]string{}
45 for _, v := range a {
46 if v != "" {
47 kv := strings.Split(v, "=")
48
49 if len(kv) > 1 {
50 r[kv[0]] = kv[1]
51 }
52 }
53 }
54 return r
55 }
56
57
58
59 func (c *testContext) Validate() {
60 if c.Labels != nil && c.SkipLabels != nil {
61 panic(errors.New("-labels and -skip-labels are mutually exclusive"))
62 }
63 }
64
65
66
67 func ResolvePath(p ...string) string {
68 if Context.RepoRoot == "" {
69 panic(fmt.Sprintf("cant resolve path to %s, -repo-root not provided", p))
70 }
71
72 elements := append([]string{Context.RepoRoot}, p...)
73
74 return filepath.Join(elements...)
75 }
76
View as plain text