diff options
author | 2017-07-11 14:17:50 -0700 | |
---|---|---|
committer | 2017-07-11 15:23:51 -0700 | |
commit | 091525e15ca573f6f8bd50be8c00e6ce93f9eed2 (patch) | |
tree | e003fc306102fa99d21bfe00e2e7a65509c37861 | |
parent | 9b58749f30d397ad05ff2d35e4d081016a2ebbee (diff) |
Parse environment options from the command line
Make supports specifying all types of variables on the command line
(using =, :=, +=, and other variable references. When running soong_ui
through make/makeparallel these all effectively become environment
variables.
So in preparation to remove the Make wrapper, support a simplified form
of this syntax, roughly equivalent to what the shell supports if
specified before the command (<NAME>=<VALUE>).
Test: m -j blueprint_tools
Change-Id: I08fa2b86710f282e619b0cc324a3e5bbaf62d26e
-rw-r--r-- | ui/build/config.go | 2 | ||||
-rw-r--r-- | ui/build/config_test.go | 69 |
2 files changed, 71 insertions, 0 deletions
diff --git a/ui/build/config.go b/ui/build/config.go index 925b15385..a451cfe5f 100644 --- a/ui/build/config.go +++ b/ui/build/config.go @@ -176,6 +176,8 @@ func (c *configImpl) parseArgs(ctx Context, args []string) { } else { ctx.Fatalln("Unknown option:", arg) } + } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 { + c.environ.Set(k, v) } else { c.arguments = append(c.arguments, arg) } diff --git a/ui/build/config_test.go b/ui/build/config_test.go index b2a4dd81a..e4eab9442 100644 --- a/ui/build/config_test.go +++ b/ui/build/config_test.go @@ -103,3 +103,72 @@ func TestConfigParseArgsJK(t *testing.T) { }) } } + +func TestConfigParseArgsVars(t *testing.T) { + ctx := testContext() + + testCases := []struct { + env []string + args []string + + expectedEnv []string + remaining []string + }{ + {}, + { + env: []string{"A=bc"}, + + expectedEnv: []string{"A=bc"}, + }, + { + args: []string{"abc"}, + + remaining: []string{"abc"}, + }, + + { + args: []string{"A=bc"}, + + expectedEnv: []string{"A=bc"}, + }, + { + env: []string{"A=a"}, + args: []string{"A=bc"}, + + expectedEnv: []string{"A=bc"}, + }, + + { + env: []string{"A=a"}, + args: []string{"A=", "=b"}, + + expectedEnv: []string{"A="}, + remaining: []string{"=b"}, + }, + } + + for _, tc := range testCases { + t.Run(strings.Join(tc.args, " "), func(t *testing.T) { + defer logger.Recover(func(err error) { + t.Fatal(err) + }) + + e := Environment(tc.env) + c := &configImpl{ + environ: &e, + } + c.parseArgs(ctx, tc.args) + + if !reflect.DeepEqual([]string(*c.environ), tc.expectedEnv) { + t.Errorf("for env=%q args=%q, environment:\nwant: %q\n got: %q\n", + tc.env, tc.args, + tc.expectedEnv, []string(*c.environ)) + } + if !reflect.DeepEqual(c.arguments, tc.remaining) { + t.Errorf("for env=%q args=%q, remaining arguments:\nwant: %q\n got: %q\n", + tc.env, tc.args, + tc.remaining, c.arguments) + } + }) + } +} |