...
1 package framework
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "path/filepath"
8
9 "github.com/bazelbuild/rules_go/go/runfiles"
10 "github.com/peterbourgon/ff/v3"
11
12 "edge-infra.dev/pkg/lib/build/bazel"
13 "edge-infra.dev/test/framework/config"
14 )
15
16 var (
17 cfgFlagName = "test-config"
18 cfgPath = "test/config.json"
19 cfgFlag string
20 )
21
22
23
24
25
26
27
28
29
30 func HandleFlags() {
31
32 config.Flags.StringVar(&cfgFlag, cfgFlagName, resolveCfgPath(), "path to test configuration file")
33
34
35 config.CopyFlags(config.Flags, flag.CommandLine)
36
37 RegisterCommonFlags(flag.CommandLine)
38
39 if err := ff.Parse(flag.CommandLine, os.Args[1:],
40 ff.WithConfigFileFlag(cfgFlagName),
41 ff.WithConfigFileParser(ff.JSONParser),
42 ff.WithAllowMissingConfigFile(true),
43 ff.WithIgnoreUndefined(true),
44 ); err != nil {
45 panic(err)
46 }
47
48 Context.Validate()
49 }
50
51
52
53
54 func resolveCfgPath() string {
55 if bazel.IsBazelRun() || bazel.IsBazelTest() {
56 rfilesPath := filepath.Join("edge_infa", cfgPath)
57 path, err := runfiles.Rlocation(rfilesPath)
58 if err != nil {
59 panic(fmt.Sprintf(
60 "failed to locate test config file %s in Bazel sandbox: %v",
61 rfilesPath, err,
62 ))
63 }
64 return path
65 }
66 cwd, err := os.Getwd()
67 if err != nil {
68 panic(fmt.Sprintf("failed to determine cwd: %v", err))
69 }
70 return filepath.Join(bazel.FindRepoRootOrDie(cwd), cfgPath)
71 }
72
View as plain text