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 | } |