blob: 242e3afb0f784bf880cf07faaf6d9fc83e7a2d9d [file] [log] [blame]
Dan Willemsen9b587492017-07-10 22:13:00 -07001// 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
15package build
16
17import (
18 "bytes"
19 "context"
20 "reflect"
21 "strings"
22 "testing"
23
24 "android/soong/ui/logger"
Dan Willemsenb82471a2018-05-17 16:37:09 -070025 "android/soong/ui/terminal"
Dan Willemsen9b587492017-07-10 22:13:00 -070026)
27
28func testContext() Context {
29 return Context{&ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -070030 Context: context.Background(),
31 Logger: logger.New(&bytes.Buffer{}),
32 Writer: terminal.NewWriter(terminal.NewCustomStdio(&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{})),
Dan Willemsen9b587492017-07-10 22:13:00 -070033 }}
34}
35
36func TestConfigParseArgsJK(t *testing.T) {
37 ctx := testContext()
38
39 testCases := []struct {
40 args []string
41
42 parallel int
43 keepGoing int
44 remaining []string
45 }{
46 {nil, -1, -1, nil},
47
48 {[]string{"-j"}, -1, -1, nil},
49 {[]string{"-j1"}, 1, -1, nil},
50 {[]string{"-j1234"}, 1234, -1, nil},
51
52 {[]string{"-j", "1"}, 1, -1, nil},
53 {[]string{"-j", "1234"}, 1234, -1, nil},
54 {[]string{"-j", "1234", "abc"}, 1234, -1, []string{"abc"}},
55 {[]string{"-j", "abc"}, -1, -1, []string{"abc"}},
56 {[]string{"-j", "1abc"}, -1, -1, []string{"1abc"}},
57
58 {[]string{"-k"}, -1, 0, nil},
59 {[]string{"-k0"}, -1, 0, nil},
60 {[]string{"-k1"}, -1, 1, nil},
61 {[]string{"-k1234"}, -1, 1234, nil},
62
63 {[]string{"-k", "0"}, -1, 0, nil},
64 {[]string{"-k", "1"}, -1, 1, nil},
65 {[]string{"-k", "1234"}, -1, 1234, nil},
66 {[]string{"-k", "1234", "abc"}, -1, 1234, []string{"abc"}},
67 {[]string{"-k", "abc"}, -1, 0, []string{"abc"}},
68 {[]string{"-k", "1abc"}, -1, 0, []string{"1abc"}},
69
70 // TODO: These are supported in Make, should we support them?
71 //{[]string{"-kj"}, -1, 0},
72 //{[]string{"-kj8"}, 8, 0},
73
74 // -jk is not valid in Make
75 }
76
77 for _, tc := range testCases {
78 t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
79 defer logger.Recover(func(err error) {
80 t.Fatal(err)
81 })
82
83 c := &configImpl{
84 parallel: -1,
85 keepGoing: -1,
86 }
87 c.parseArgs(ctx, tc.args)
88
89 if c.parallel != tc.parallel {
90 t.Errorf("for %q, parallel:\nwant: %d\n got: %d\n",
91 strings.Join(tc.args, " "),
92 tc.parallel, c.parallel)
93 }
94 if c.keepGoing != tc.keepGoing {
95 t.Errorf("for %q, keep going:\nwant: %d\n got: %d\n",
96 strings.Join(tc.args, " "),
97 tc.keepGoing, c.keepGoing)
98 }
99 if !reflect.DeepEqual(c.arguments, tc.remaining) {
100 t.Errorf("for %q, remaining arguments:\nwant: %q\n got: %q\n",
101 strings.Join(tc.args, " "),
102 tc.remaining, c.arguments)
103 }
104 })
105 }
106}
Dan Willemsen091525e2017-07-11 14:17:50 -0700107
108func TestConfigParseArgsVars(t *testing.T) {
109 ctx := testContext()
110
111 testCases := []struct {
112 env []string
113 args []string
114
115 expectedEnv []string
116 remaining []string
117 }{
118 {},
119 {
120 env: []string{"A=bc"},
121
122 expectedEnv: []string{"A=bc"},
123 },
124 {
125 args: []string{"abc"},
126
127 remaining: []string{"abc"},
128 },
129
130 {
131 args: []string{"A=bc"},
132
133 expectedEnv: []string{"A=bc"},
134 },
135 {
136 env: []string{"A=a"},
137 args: []string{"A=bc"},
138
139 expectedEnv: []string{"A=bc"},
140 },
141
142 {
143 env: []string{"A=a"},
144 args: []string{"A=", "=b"},
145
146 expectedEnv: []string{"A="},
147 remaining: []string{"=b"},
148 },
149 }
150
151 for _, tc := range testCases {
152 t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
153 defer logger.Recover(func(err error) {
154 t.Fatal(err)
155 })
156
157 e := Environment(tc.env)
158 c := &configImpl{
159 environ: &e,
160 }
161 c.parseArgs(ctx, tc.args)
162
163 if !reflect.DeepEqual([]string(*c.environ), tc.expectedEnv) {
164 t.Errorf("for env=%q args=%q, environment:\nwant: %q\n got: %q\n",
165 tc.env, tc.args,
166 tc.expectedEnv, []string(*c.environ))
167 }
168 if !reflect.DeepEqual(c.arguments, tc.remaining) {
169 t.Errorf("for env=%q args=%q, remaining arguments:\nwant: %q\n got: %q\n",
170 tc.env, tc.args,
171 tc.remaining, c.arguments)
172 }
173 })
174 }
175}