| Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
|  | 15 | package build | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "bytes" | 
|  | 19 | "context" | 
|  | 20 | "reflect" | 
|  | 21 | "strings" | 
|  | 22 | "testing" | 
|  | 23 |  | 
|  | 24 | "android/soong/ui/logger" | 
|  | 25 | ) | 
|  | 26 |  | 
|  | 27 | func testContext() Context { | 
|  | 28 | return Context{&ContextImpl{ | 
|  | 29 | Context:        context.Background(), | 
|  | 30 | Logger:         logger.New(&bytes.Buffer{}), | 
|  | 31 | StdioInterface: NewCustomStdio(&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}), | 
|  | 32 | }} | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | func TestConfigParseArgsJK(t *testing.T) { | 
|  | 36 | ctx := testContext() | 
|  | 37 |  | 
|  | 38 | testCases := []struct { | 
|  | 39 | args []string | 
|  | 40 |  | 
|  | 41 | parallel  int | 
|  | 42 | keepGoing int | 
|  | 43 | remaining []string | 
|  | 44 | }{ | 
|  | 45 | {nil, -1, -1, nil}, | 
|  | 46 |  | 
|  | 47 | {[]string{"-j"}, -1, -1, nil}, | 
|  | 48 | {[]string{"-j1"}, 1, -1, nil}, | 
|  | 49 | {[]string{"-j1234"}, 1234, -1, nil}, | 
|  | 50 |  | 
|  | 51 | {[]string{"-j", "1"}, 1, -1, nil}, | 
|  | 52 | {[]string{"-j", "1234"}, 1234, -1, nil}, | 
|  | 53 | {[]string{"-j", "1234", "abc"}, 1234, -1, []string{"abc"}}, | 
|  | 54 | {[]string{"-j", "abc"}, -1, -1, []string{"abc"}}, | 
|  | 55 | {[]string{"-j", "1abc"}, -1, -1, []string{"1abc"}}, | 
|  | 56 |  | 
|  | 57 | {[]string{"-k"}, -1, 0, nil}, | 
|  | 58 | {[]string{"-k0"}, -1, 0, nil}, | 
|  | 59 | {[]string{"-k1"}, -1, 1, nil}, | 
|  | 60 | {[]string{"-k1234"}, -1, 1234, nil}, | 
|  | 61 |  | 
|  | 62 | {[]string{"-k", "0"}, -1, 0, nil}, | 
|  | 63 | {[]string{"-k", "1"}, -1, 1, nil}, | 
|  | 64 | {[]string{"-k", "1234"}, -1, 1234, nil}, | 
|  | 65 | {[]string{"-k", "1234", "abc"}, -1, 1234, []string{"abc"}}, | 
|  | 66 | {[]string{"-k", "abc"}, -1, 0, []string{"abc"}}, | 
|  | 67 | {[]string{"-k", "1abc"}, -1, 0, []string{"1abc"}}, | 
|  | 68 |  | 
|  | 69 | // TODO: These are supported in Make, should we support them? | 
|  | 70 | //{[]string{"-kj"}, -1, 0}, | 
|  | 71 | //{[]string{"-kj8"}, 8, 0}, | 
|  | 72 |  | 
|  | 73 | // -jk is not valid in Make | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | for _, tc := range testCases { | 
|  | 77 | t.Run(strings.Join(tc.args, " "), func(t *testing.T) { | 
|  | 78 | defer logger.Recover(func(err error) { | 
|  | 79 | t.Fatal(err) | 
|  | 80 | }) | 
|  | 81 |  | 
|  | 82 | c := &configImpl{ | 
|  | 83 | parallel:  -1, | 
|  | 84 | keepGoing: -1, | 
|  | 85 | } | 
|  | 86 | c.parseArgs(ctx, tc.args) | 
|  | 87 |  | 
|  | 88 | if c.parallel != tc.parallel { | 
|  | 89 | t.Errorf("for %q, parallel:\nwant: %d\n got: %d\n", | 
|  | 90 | strings.Join(tc.args, " "), | 
|  | 91 | tc.parallel, c.parallel) | 
|  | 92 | } | 
|  | 93 | if c.keepGoing != tc.keepGoing { | 
|  | 94 | t.Errorf("for %q, keep going:\nwant: %d\n got: %d\n", | 
|  | 95 | strings.Join(tc.args, " "), | 
|  | 96 | tc.keepGoing, c.keepGoing) | 
|  | 97 | } | 
|  | 98 | if !reflect.DeepEqual(c.arguments, tc.remaining) { | 
|  | 99 | t.Errorf("for %q, remaining arguments:\nwant: %q\n got: %q\n", | 
|  | 100 | strings.Join(tc.args, " "), | 
|  | 101 | tc.remaining, c.arguments) | 
|  | 102 | } | 
|  | 103 | }) | 
|  | 104 | } | 
|  | 105 | } | 
| Dan Willemsen | 091525e | 2017-07-11 14:17:50 -0700 | [diff] [blame] | 106 |  | 
|  | 107 | func TestConfigParseArgsVars(t *testing.T) { | 
|  | 108 | ctx := testContext() | 
|  | 109 |  | 
|  | 110 | testCases := []struct { | 
|  | 111 | env  []string | 
|  | 112 | args []string | 
|  | 113 |  | 
|  | 114 | expectedEnv []string | 
|  | 115 | remaining   []string | 
|  | 116 | }{ | 
|  | 117 | {}, | 
|  | 118 | { | 
|  | 119 | env: []string{"A=bc"}, | 
|  | 120 |  | 
|  | 121 | expectedEnv: []string{"A=bc"}, | 
|  | 122 | }, | 
|  | 123 | { | 
|  | 124 | args: []string{"abc"}, | 
|  | 125 |  | 
|  | 126 | remaining: []string{"abc"}, | 
|  | 127 | }, | 
|  | 128 |  | 
|  | 129 | { | 
|  | 130 | args: []string{"A=bc"}, | 
|  | 131 |  | 
|  | 132 | expectedEnv: []string{"A=bc"}, | 
|  | 133 | }, | 
|  | 134 | { | 
|  | 135 | env:  []string{"A=a"}, | 
|  | 136 | args: []string{"A=bc"}, | 
|  | 137 |  | 
|  | 138 | expectedEnv: []string{"A=bc"}, | 
|  | 139 | }, | 
|  | 140 |  | 
|  | 141 | { | 
|  | 142 | env:  []string{"A=a"}, | 
|  | 143 | args: []string{"A=", "=b"}, | 
|  | 144 |  | 
|  | 145 | expectedEnv: []string{"A="}, | 
|  | 146 | remaining:   []string{"=b"}, | 
|  | 147 | }, | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | for _, tc := range testCases { | 
|  | 151 | t.Run(strings.Join(tc.args, " "), func(t *testing.T) { | 
|  | 152 | defer logger.Recover(func(err error) { | 
|  | 153 | t.Fatal(err) | 
|  | 154 | }) | 
|  | 155 |  | 
|  | 156 | e := Environment(tc.env) | 
|  | 157 | c := &configImpl{ | 
|  | 158 | environ: &e, | 
|  | 159 | } | 
|  | 160 | c.parseArgs(ctx, tc.args) | 
|  | 161 |  | 
|  | 162 | if !reflect.DeepEqual([]string(*c.environ), tc.expectedEnv) { | 
|  | 163 | t.Errorf("for env=%q args=%q, environment:\nwant: %q\n got: %q\n", | 
|  | 164 | tc.env, tc.args, | 
|  | 165 | tc.expectedEnv, []string(*c.environ)) | 
|  | 166 | } | 
|  | 167 | if !reflect.DeepEqual(c.arguments, tc.remaining) { | 
|  | 168 | t.Errorf("for env=%q args=%q, remaining arguments:\nwant: %q\n got: %q\n", | 
|  | 169 | tc.env, tc.args, | 
|  | 170 | tc.remaining, c.arguments) | 
|  | 171 | } | 
|  | 172 | }) | 
|  | 173 | } | 
|  | 174 | } |