2021-05-06 15:31:18 +02:00
|
|
|
// Copyright 2021 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
package android
|
2021-05-06 15:31:18 +02:00
|
|
|
|
|
|
|
import (
|
2022-03-25 15:55:40 +01:00
|
|
|
"android/soong/bazel"
|
2021-05-06 15:31:18 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExpandVars(t *testing.T) {
|
2022-03-25 15:55:40 +01:00
|
|
|
android_arm64_config := TestConfig("out", nil, "", nil)
|
|
|
|
android_arm64_config.BuildOS = Android
|
|
|
|
android_arm64_config.BuildArch = Arm64
|
2021-10-14 20:08:38 +02:00
|
|
|
|
2021-05-06 15:31:18 +02:00
|
|
|
testCases := []struct {
|
2021-05-20 15:40:14 +02:00
|
|
|
description string
|
2022-03-25 15:55:40 +01:00
|
|
|
config Config
|
|
|
|
stringScope ExportedStringVariables
|
|
|
|
stringListScope ExportedStringListVariables
|
|
|
|
configVars ExportedConfigDependingVariables
|
2021-05-20 15:40:14 +02:00
|
|
|
toExpand string
|
|
|
|
expectedValues []string
|
2021-05-06 15:31:18 +02:00
|
|
|
}{
|
|
|
|
{
|
2021-05-20 15:40:14 +02:00
|
|
|
description: "no expansion for non-interpolated value",
|
|
|
|
toExpand: "foo",
|
|
|
|
expectedValues: []string{"foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "single level expansion for string var",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringScope: ExportedStringVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
toExpand: "${foo}",
|
|
|
|
expectedValues: []string{"bar"},
|
|
|
|
},
|
2022-02-16 15:02:48 +01:00
|
|
|
{
|
|
|
|
description: "single level expansion with short-name for string var",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringScope: ExportedStringVariables{
|
2022-02-16 15:02:48 +01:00
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
toExpand: "${config.foo}",
|
|
|
|
expectedValues: []string{"bar"},
|
|
|
|
},
|
2021-05-20 15:40:14 +02:00
|
|
|
{
|
|
|
|
description: "single level expansion string list var",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringListScope: ExportedStringListVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"foo": []string{"bar"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
toExpand: "${foo}",
|
|
|
|
expectedValues: []string{"bar"},
|
|
|
|
},
|
2021-05-20 15:40:14 +02:00
|
|
|
{
|
|
|
|
description: "mixed level expansion for string list var",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringScope: ExportedStringVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"foo": "${bar}",
|
|
|
|
"qux": "hello",
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
stringListScope: ExportedStringListVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"bar": []string{"baz", "${qux}"},
|
|
|
|
},
|
|
|
|
toExpand: "${foo}",
|
2021-10-14 20:08:38 +02:00
|
|
|
expectedValues: []string{"baz hello"},
|
2021-05-20 15:40:14 +02:00
|
|
|
},
|
2021-05-06 15:31:18 +02:00
|
|
|
{
|
|
|
|
description: "double level expansion",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringListScope: ExportedStringListVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"foo": []string{"${bar}"},
|
|
|
|
"bar": []string{"baz"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
toExpand: "${foo}",
|
|
|
|
expectedValues: []string{"baz"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "double level expansion with a literal",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringListScope: ExportedStringListVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"a": []string{"${b}", "c"},
|
|
|
|
"b": []string{"d"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
toExpand: "${a}",
|
2021-10-14 20:08:38 +02:00
|
|
|
expectedValues: []string{"d c"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "double level expansion, with two variables in a string",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringListScope: ExportedStringListVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"a": []string{"${b} ${c}"},
|
|
|
|
"b": []string{"d"},
|
|
|
|
"c": []string{"e"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
toExpand: "${a}",
|
2021-10-14 20:08:38 +02:00
|
|
|
expectedValues: []string{"d e"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "triple level expansion with two variables in a string",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringListScope: ExportedStringListVariables{
|
2021-05-20 15:40:14 +02:00
|
|
|
"a": []string{"${b} ${c}"},
|
|
|
|
"b": []string{"${c}", "${d}"},
|
|
|
|
"c": []string{"${d}"},
|
|
|
|
"d": []string{"foo"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
toExpand: "${a}",
|
2021-10-14 20:08:38 +02:00
|
|
|
expectedValues: []string{"foo foo foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "expansion with config depending vars",
|
2022-03-25 15:55:40 +01:00
|
|
|
configVars: ExportedConfigDependingVariables{
|
|
|
|
"a": func(c Config) string { return c.BuildOS.String() },
|
|
|
|
"b": func(c Config) string { return c.BuildArch.String() },
|
2021-10-14 20:08:38 +02:00
|
|
|
},
|
|
|
|
config: android_arm64_config,
|
|
|
|
toExpand: "${a}-${b}",
|
|
|
|
expectedValues: []string{"android-arm64"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "double level multi type expansion",
|
2022-03-25 15:55:40 +01:00
|
|
|
stringListScope: ExportedStringListVariables{
|
2021-10-14 20:08:38 +02:00
|
|
|
"platform": []string{"${os}-${arch}"},
|
|
|
|
"const": []string{"const"},
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
configVars: ExportedConfigDependingVariables{
|
|
|
|
"os": func(c Config) string { return c.BuildOS.String() },
|
|
|
|
"arch": func(c Config) string { return c.BuildArch.String() },
|
|
|
|
"foo": func(c Config) string { return "foo" },
|
2021-10-14 20:08:38 +02:00
|
|
|
},
|
|
|
|
config: android_arm64_config,
|
|
|
|
toExpand: "${const}/${platform}/${foo}",
|
|
|
|
expectedValues: []string{"const/android-arm64/foo"},
|
2021-05-06 15:31:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.description, func(t *testing.T) {
|
2021-10-14 20:08:38 +02:00
|
|
|
output, _ := expandVar(testCase.config, testCase.toExpand, testCase.stringScope, testCase.stringListScope, testCase.configVars)
|
2021-05-06 15:31:18 +02:00
|
|
|
if len(output) != len(testCase.expectedValues) {
|
|
|
|
t.Errorf("Expected %d values, got %d", len(testCase.expectedValues), len(output))
|
|
|
|
}
|
|
|
|
for i, actual := range output {
|
|
|
|
expectedValue := testCase.expectedValues[i]
|
|
|
|
if actual != expectedValue {
|
|
|
|
t.Errorf("Actual value '%s' doesn't match expected value '%s'", actual, expectedValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 16:41:48 +02:00
|
|
|
|
|
|
|
func TestBazelToolchainVars(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2022-03-25 15:55:40 +01:00
|
|
|
config Config
|
|
|
|
vars ExportedVariables
|
2021-08-02 16:41:48 +02:00
|
|
|
expectedOut string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "exports strings",
|
2022-03-25 15:55:40 +01:00
|
|
|
vars: ExportedVariables{
|
|
|
|
exportedStringVars: ExportedStringVariables{
|
2021-08-02 16:41:48 +02:00
|
|
|
"a": "b",
|
|
|
|
"c": "d",
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
expectedOut: bazel.GeneratedBazelFileWarning + `
|
2021-08-02 16:41:48 +02:00
|
|
|
|
|
|
|
_a = "b"
|
|
|
|
|
|
|
|
_c = "d"
|
|
|
|
|
|
|
|
constants = struct(
|
|
|
|
a = _a,
|
|
|
|
c = _c,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exports string lists",
|
2022-03-25 15:55:40 +01:00
|
|
|
vars: ExportedVariables{
|
|
|
|
exportedStringListVars: ExportedStringListVariables{
|
2021-08-02 16:41:48 +02:00
|
|
|
"a": []string{"b1", "b2"},
|
|
|
|
"c": []string{"d1", "d2"},
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
expectedOut: bazel.GeneratedBazelFileWarning + `
|
2021-08-02 16:41:48 +02:00
|
|
|
|
|
|
|
_a = [
|
|
|
|
"b1",
|
|
|
|
"b2",
|
|
|
|
]
|
|
|
|
|
|
|
|
_c = [
|
|
|
|
"d1",
|
|
|
|
"d2",
|
|
|
|
]
|
|
|
|
|
|
|
|
constants = struct(
|
|
|
|
a = _a,
|
|
|
|
c = _c,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exports string lists dicts",
|
2022-03-25 15:55:40 +01:00
|
|
|
vars: ExportedVariables{
|
|
|
|
exportedStringListDictVars: ExportedStringListDictVariables{
|
|
|
|
"a": map[string][]string{"b1": {"b2"}},
|
|
|
|
"c": map[string][]string{"d1": {"d2"}},
|
2021-08-02 16:41:48 +02:00
|
|
|
},
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
expectedOut: bazel.GeneratedBazelFileWarning + `
|
2021-08-02 16:41:48 +02:00
|
|
|
|
|
|
|
_a = {
|
2022-02-03 14:42:10 +01:00
|
|
|
"b1": ["b2"],
|
2021-08-02 16:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_c = {
|
2022-02-03 14:42:10 +01:00
|
|
|
"d1": ["d2"],
|
2021-08-02 16:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constants = struct(
|
|
|
|
a = _a,
|
|
|
|
c = _c,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
2022-02-16 15:02:48 +01:00
|
|
|
name: "exports dict with var refs",
|
2022-03-25 15:55:40 +01:00
|
|
|
vars: ExportedVariables{
|
|
|
|
exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{
|
2022-02-16 15:02:48 +01:00
|
|
|
"a": map[string]string{"b1": "${b2}"},
|
|
|
|
"c": map[string]string{"d1": "${config.d2}"},
|
|
|
|
},
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
expectedOut: bazel.GeneratedBazelFileWarning + `
|
2022-02-16 15:02:48 +01:00
|
|
|
|
|
|
|
_a = {
|
|
|
|
"b1": _b2,
|
|
|
|
}
|
|
|
|
|
|
|
|
_c = {
|
|
|
|
"d1": _d2,
|
|
|
|
}
|
|
|
|
|
|
|
|
constants = struct(
|
|
|
|
a = _a,
|
|
|
|
c = _c,
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sorts across types with variable references last",
|
2022-03-25 15:55:40 +01:00
|
|
|
vars: ExportedVariables{
|
|
|
|
exportedStringVars: ExportedStringVariables{
|
2021-08-02 16:41:48 +02:00
|
|
|
"b": "b-val",
|
|
|
|
"d": "d-val",
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
exportedStringListVars: ExportedStringListVariables{
|
2021-08-02 16:41:48 +02:00
|
|
|
"c": []string{"c-val"},
|
|
|
|
"e": []string{"e-val"},
|
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
exportedStringListDictVars: ExportedStringListDictVariables{
|
|
|
|
"a": map[string][]string{"a1": {"a2"}},
|
|
|
|
"f": map[string][]string{"f1": {"f2"}},
|
2021-08-02 16:41:48 +02:00
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{
|
2022-02-16 15:02:48 +01:00
|
|
|
"aa": map[string]string{"b1": "${b}"},
|
|
|
|
"cc": map[string]string{"d1": "${config.d}"},
|
|
|
|
},
|
2021-08-02 16:41:48 +02:00
|
|
|
},
|
2022-03-25 15:55:40 +01:00
|
|
|
expectedOut: bazel.GeneratedBazelFileWarning + `
|
2021-08-02 16:41:48 +02:00
|
|
|
|
|
|
|
_a = {
|
2022-02-03 14:42:10 +01:00
|
|
|
"a1": ["a2"],
|
2021-08-02 16:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_b = "b-val"
|
|
|
|
|
2022-02-03 14:42:10 +01:00
|
|
|
_c = ["c-val"]
|
2021-08-02 16:41:48 +02:00
|
|
|
|
|
|
|
_d = "d-val"
|
|
|
|
|
2022-02-03 14:42:10 +01:00
|
|
|
_e = ["e-val"]
|
2021-08-02 16:41:48 +02:00
|
|
|
|
|
|
|
_f = {
|
2022-02-03 14:42:10 +01:00
|
|
|
"f1": ["f2"],
|
2021-08-02 16:41:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 15:02:48 +01:00
|
|
|
_aa = {
|
|
|
|
"b1": _b,
|
|
|
|
}
|
|
|
|
|
|
|
|
_cc = {
|
|
|
|
"d1": _d,
|
|
|
|
}
|
|
|
|
|
2021-08-02 16:41:48 +02:00
|
|
|
constants = struct(
|
|
|
|
a = _a,
|
|
|
|
b = _b,
|
|
|
|
c = _c,
|
|
|
|
d = _d,
|
|
|
|
e = _e,
|
|
|
|
f = _f,
|
2022-02-16 15:02:48 +01:00
|
|
|
aa = _aa,
|
|
|
|
cc = _cc,
|
2021-08-02 16:41:48 +02:00
|
|
|
)`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-25 15:55:40 +01:00
|
|
|
out := BazelToolchainVars(tc.config, tc.vars)
|
2021-08-02 16:41:48 +02:00
|
|
|
if out != tc.expectedOut {
|
|
|
|
t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 17:33:26 +01:00
|
|
|
|
|
|
|
func TestSplitStringKeepingQuotedSubstring(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
s string
|
|
|
|
delimiter byte
|
|
|
|
split []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "empty string returns single empty string",
|
|
|
|
s: "",
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string with single space returns two empty strings",
|
|
|
|
s: " ",
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string with two spaces returns three empty strings",
|
|
|
|
s: " ",
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string with four words returns four word string",
|
|
|
|
s: "hello world with words",
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"hello",
|
|
|
|
"world",
|
|
|
|
"with",
|
|
|
|
"words",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string with words and nested quote returns word strings and quote string",
|
|
|
|
s: `hello "world with" words`,
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"hello",
|
|
|
|
`"world with"`,
|
|
|
|
"words",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string with escaped quote inside real quotes",
|
|
|
|
s: `hello \"world "with\" words"`,
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"hello",
|
|
|
|
`"world`,
|
|
|
|
`"with" words"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string with words and escaped quotes returns word strings",
|
|
|
|
s: `hello \"world with\" words`,
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"hello",
|
|
|
|
`"world`,
|
|
|
|
`with"`,
|
|
|
|
"words",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string which is single quoted substring returns only substring",
|
|
|
|
s: `"hello world with words"`,
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
`"hello world with words"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string starting with quote returns quoted string",
|
|
|
|
s: `"hello world with" words`,
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
`"hello world with"`,
|
|
|
|
"words",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "string with starting quote and no ending quote returns quote to end of string",
|
|
|
|
s: `hello "world with words`,
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"hello",
|
|
|
|
`"world with words`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "quoted string is treated as a single \"word\" unless separated by delimiter",
|
|
|
|
s: `hello "world"with words`,
|
|
|
|
delimiter: ' ',
|
|
|
|
split: []string{
|
|
|
|
"hello",
|
|
|
|
`"world"with`,
|
|
|
|
"words",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
split := splitStringKeepingQuotedSubstring(tc.s, tc.delimiter)
|
|
|
|
if len(split) != len(tc.split) {
|
|
|
|
t.Fatalf("number of split string elements (%d) differs from expected (%d): split string (%v), expected (%v)",
|
|
|
|
len(split), len(tc.split), split, tc.split,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
for i := range split {
|
|
|
|
if split[i] != tc.split[i] {
|
|
|
|
t.Errorf("split string element (%d), %v, differs from expected, %v", i, split[i], tc.split[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|