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