v.io/jiri@v0.0.0-20160715023856-abfb8b131290/profiles/target_test.go (about) 1 // Copyright 2015 The Vanadium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package profiles_test 6 7 import ( 8 "flag" 9 "fmt" 10 "os" 11 "runtime" 12 "strings" 13 "testing" 14 15 "v.io/jiri/profiles" 16 ) 17 18 func ExampleProfileTarget() { 19 var target profiles.Target 20 flags := flag.NewFlagSet("test", flag.ContinueOnError) 21 profiles.RegisterTargetAndEnvFlags(flags, &target) 22 flags.Parse([]string{"--target=arm-linux", "--env=A=B,C=D", "--env=E=F"}) 23 fmt.Println(target.String()) 24 fmt.Println(target.DebugString()) 25 // Output: 26 // arm-linux@ 27 // arm-linux@ dir: --env=A=B,C=D,E=F envvars:[] 28 } 29 30 func TestProfileTargetArgs(t *testing.T) { 31 for i, c := range []struct { 32 arg, arch, os, version string 33 err bool 34 }{ 35 {"a-b", "a", "b", "", false}, 36 {"a-b@3", "a", "b", "3", false}, 37 {"", "", "", "", true}, 38 {"a", "", "", "", true}, 39 {"a-", "", "", "", true}, 40 {"-a", "", "", "", true}, 41 {"a-", "", "", "", true}, 42 } { 43 target := &profiles.Target{} 44 if err := target.Set(c.arg); err != nil { 45 if !c.err { 46 t.Errorf("%d: %v", i, err) 47 } 48 continue 49 } 50 if got, want := target.Arch(), c.arch; got != want { 51 t.Errorf("%d: got %v, want %v", i, got, want) 52 } 53 if got, want := target.OS(), c.os; got != want { 54 t.Errorf("%d: got %v, want %v", i, got, want) 55 } 56 } 57 } 58 59 func TestProfileEnvArgs(t *testing.T) { 60 for i, c := range []struct { 61 args []string 62 env string 63 err bool 64 }{ 65 {[]string{}, "", false}, 66 {[]string{"a="}, "a=", false}, 67 {[]string{"a=b"}, "a=b", false}, 68 {[]string{"a=b,c=d"}, "a=b,c=d", false}, 69 {[]string{"a=b", "c=d"}, "a=b,c=d", false}, 70 {[]string{"=b"}, "", true}, 71 {[]string{"b"}, "", true}, 72 } { 73 target := &profiles.Target{} 74 for _, arg := range c.args { 75 if err := target.Env.Set(arg); err != nil { 76 if !c.err { 77 t.Errorf("%d: %v", i, err) 78 } 79 continue 80 } 81 } 82 if got, want := target.Env.String(), c.env; got != want { 83 t.Errorf("%d: got %v, want %v", i, got, want) 84 } 85 } 86 } 87 88 func TestCopyCommandLineEnv(t *testing.T) { 89 env := "A=B,C=D" 90 target, _ := profiles.NewTarget("a=a-o", env) 91 clenv := target.CommandLineEnv() 92 if got, want := strings.Join(clenv.Vars, ","), env; got != want { 93 t.Errorf("got %v, want %v", got, want) 94 } 95 // Make sure we can't mutate the command line env stored in target. 96 clenv.Vars[0] = "oops" 97 clenv2 := target.CommandLineEnv() 98 if got, want := strings.Join(clenv2.Vars, ","), env; got != want { 99 t.Errorf("got %v, want %v", got, want) 100 } 101 } 102 103 func TestTargetEquality(t *testing.T) { 104 type s []string 105 for i, c := range []struct { 106 args1, args2 []string 107 equal bool 108 }{ 109 {s{""}, s{""}, true}, 110 {s{"--t=a-b"}, s{"--t=a-b"}, true}, 111 {s{"--t=a-b@foo"}, s{"--t=a-b@foo"}, true}, 112 {s{"--t=a-b@foo"}, s{"--t=a-b"}, false}, 113 {s{"--t=a-b", "-e=a=b,c=d"}, s{"--t=a-b", "--e=c=d,a=b"}, true}, 114 {s{"--t=a-b", "-e=a=b,c=d"}, s{"--t=a-b", "--e=a=b,c=d"}, true}, 115 {s{"--t=a-b", "-e=a=b,c=d"}, s{"--t=a-b", "--e=c=d", "--e=a=b"}, true}, 116 {s{"--t=a-b"}, s{"--t=c-b"}, false}, 117 {s{"--t=a-b", "-e=a=b,c=d"}, s{"--t=c-b"}, false}, 118 } { 119 t1, t2 := &profiles.Target{}, &profiles.Target{} 120 t1.Env.Vars = []string{"Oh"} // Env vars don't matter. 121 fl1 := flag.NewFlagSet("test1", flag.ContinueOnError) 122 fl2 := flag.NewFlagSet("test2", flag.ContinueOnError) 123 fl1.Var(t1, "t", t1.Usage()) 124 fl1.Var(&t1.Env, "e", t1.Env.Usage()) 125 fl2.Var(t2, "t", t2.Usage()) 126 fl2.Var(&t2.Env, "e", t2.Env.Usage()) 127 if err := fl1.Parse(c.args1); err != nil { 128 t.Errorf("%d: %v\n", i, err) 129 continue 130 } 131 if err := fl2.Parse(c.args2); err != nil { 132 t.Errorf("%d: %v\n", i, err) 133 continue 134 } 135 if got, want := t1.Match(t2), c.equal; got != want { 136 t.Errorf("%v --- %v", t1, t2) 137 t.Errorf("%d: got %v, want %v", i, got, want) 138 } 139 } 140 } 141 142 func TestTargetVersion(t *testing.T) { 143 t1, t2 := &profiles.Target{}, &profiles.Target{} 144 t1.Set("cpu,os") 145 t2.Set("cpu,os") 146 if got, want := t1.Match(t2), true; got != want { 147 t.Errorf("got %v, want %v", got, want) 148 } 149 t2.Set("cpu,os@var") 150 if got, want := t1.Match(t2), true; got != want { 151 t.Errorf("got %v, want %v", got, want) 152 } 153 t2.Set("cpu,os") 154 t1.Set("cpu,os@baz") 155 if got, want := t1.Match(t2), false; got != want { 156 t.Errorf("got %v, want %v", got, want) 157 } 158 } 159 160 func TestDefaults(t *testing.T) { 161 t1 := &profiles.Target{} 162 native := fmt.Sprintf("%s-%s@", runtime.GOARCH, runtime.GOOS) 163 if got, want := t1.String(), native; got != want { 164 t.Errorf("got %v, want %v", got, want) 165 } 166 t1.Set("cpu-os") 167 if got, want := t1.String(), "cpu-os@"; got != want { 168 t.Errorf("got %v, want %v", got, want) 169 } 170 } 171 172 func TestFindTarget(t *testing.T) { 173 t1 := &profiles.Target{} 174 t1.Set("a-o") 175 ts := []*profiles.Target{t1} 176 prev := os.Getenv("GOARCH") 177 if len(prev) > 0 { 178 // Clear GOARCH so that DefaultTarget is not set. 179 os.Setenv("GOARCH", "") 180 defer os.Setenv("GOARCH", prev) 181 } 182 def := profiles.DefaultTarget() 183 if got, want := profiles.FindTargetWithDefault(ts, &def), t1; !got.Match(want) { 184 t.Errorf("got %v, want %v", got, want) 185 } 186 t2 := &profiles.Target{} 187 t2.Set("a-o1") 188 ts = append(ts, t2) 189 if got := profiles.FindTarget(ts, &def); got != nil { 190 t.Errorf("got %v, want nil", got) 191 } 192 193 w := &profiles.Target{} 194 w.Set("a-o") 195 if got, want := profiles.FindTarget(ts, w), t1; !got.Match(want) { 196 t.Errorf("got %v, want %v", got, want) 197 } 198 199 w.Set("a-o1") 200 if got, want := profiles.FindTarget(ts, w), t2; !got.Match(want) { 201 t.Errorf("got %v, want %v", got, want) 202 } 203 204 w.Set("c-d") 205 if got := profiles.FindTarget(ts, w); got != nil { 206 t.Errorf("got %v, want nil", got) 207 } 208 } 209 210 func TestOrderedTargets(t *testing.T) { 211 for i, c := range []struct { 212 a, b string 213 r bool 214 }{ 215 {"x-b", "y-b", true}, 216 {"x-b", "w-b", false}, 217 {"x-b", "x-a", false}, 218 {"x-b", "x-c", true}, 219 {"x-b", "x-b@1", true}, 220 {"x-b@1", "x-b@", false}, 221 {"x-b@1", "x-b@2", false}, 222 {"x-b@12", "x-b@2", true}, 223 {"x-b@2", "x-b@1", true}, 224 {"x-b@1.2", "x-b@1.1", true}, 225 {"x-b@1.2.c", "x-b@1.2.b", true}, 226 {"x-b@1.2.1", "x-b@1.2", true}, 227 {"x-b@1.2.1.3", "x-b@1.2", true}, 228 {"x-b@2.2", "x-b@1.2.3.4", true}, 229 {"x-b", "x-b", false}, 230 } { 231 a, err := profiles.NewTarget(c.a, "") 232 if err != nil { 233 t.Fatal(err) 234 } 235 b, _ := profiles.NewTarget(c.b, "") 236 if err != nil { 237 t.Fatal(err) 238 } 239 if got, want := a.Less(&b), c.r; got != want { 240 t.Errorf("%d: got %v, want %v", i, got, want) 241 } 242 } 243 244 ol := profiles.Targets{} 245 data := []string{"a-b@2", "x-y", "a-b@12", "a-b@3", "a-b@0", "x-y@3", "x-y@2"} 246 for _, s := range data { 247 target, err := profiles.NewTarget(s) 248 if err != nil { 249 t.Fatal(err) 250 } 251 ol = profiles.InsertTarget(ol, &target) 252 } 253 if got, want := len(ol), len(data); got != want { 254 t.Fatalf("got %v, want %v", got, want) 255 } 256 for i, _ := range ol[:len(ol)-1] { 257 j := i + 1 258 if !ol.Less(i, j) { 259 t.Errorf("%v is not less than %v", ol[i], ol[j]) 260 } 261 } 262 if got, want := ol[0].String(), "a-b@12"; got != want { 263 t.Fatalf("got %v, want %v", got, want) 264 } 265 if got, want := ol[len(ol)-1].String(), "x-y@2"; got != want { 266 t.Fatalf("got %v, want %v", got, want) 267 } 268 t2, _ := profiles.NewTarget("a-b@12") 269 ol = profiles.RemoveTarget(ol, &t2) 270 }