Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame^] | 1 | // Copyright 2019 The Android Open Source Project |
| 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 rust |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "testing" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | buildDir string |
| 27 | ) |
| 28 | |
| 29 | func setUp() { |
| 30 | var err error |
| 31 | buildDir, err = ioutil.TempDir("", "soong_rust_test") |
| 32 | if err != nil { |
| 33 | panic(err) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func tearDown() { |
| 38 | os.RemoveAll(buildDir) |
| 39 | } |
| 40 | |
| 41 | func TestMain(m *testing.M) { |
| 42 | run := func() int { |
| 43 | setUp() |
| 44 | defer tearDown() |
| 45 | |
| 46 | return m.Run() |
| 47 | } |
| 48 | |
| 49 | os.Exit(run()) |
| 50 | } |
| 51 | |
| 52 | func testRust(t *testing.T, bp string) *android.TestContext { |
| 53 | t.Helper() |
| 54 | config := android.TestArchConfig(buildDir, nil) |
| 55 | |
| 56 | t.Helper() |
| 57 | ctx := CreateTestContext(bp) |
| 58 | ctx.Register() |
| 59 | |
| 60 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 61 | android.FailIfErrored(t, errs) |
| 62 | _, errs = ctx.PrepareBuildActions(config) |
| 63 | android.FailIfErrored(t, errs) |
| 64 | |
| 65 | return ctx |
| 66 | } |
| 67 | |
| 68 | func testRustError(t *testing.T, pattern string, bp string) { |
| 69 | t.Helper() |
| 70 | config := android.TestArchConfig(buildDir, nil) |
| 71 | |
| 72 | ctx := CreateTestContext(bp) |
| 73 | ctx.Register() |
| 74 | |
| 75 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 76 | if len(errs) > 0 { |
| 77 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | _, errs = ctx.PrepareBuildActions(config) |
| 82 | if len(errs) > 0 { |
| 83 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
| 88 | } |
| 89 | |
| 90 | // Test that we can extract the lib name from a lib path. |
| 91 | func TestLibNameFromFilePath(t *testing.T) { |
| 92 | barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so") |
| 93 | libName := libNameFromFilePath(barPath) |
| 94 | expectedResult := "bar" |
| 95 | |
| 96 | if libName != expectedResult { |
| 97 | t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Test that we can extract the link path from a lib path. |
| 102 | func TestLinkPathFromFilePath(t *testing.T) { |
| 103 | barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so") |
| 104 | libName := linkPathFromFilePath(barPath) |
| 105 | expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/" |
| 106 | |
| 107 | if libName != expectedResult { |
| 108 | t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Test default crate names from module names are generated correctly. |
| 113 | func TestDefaultCrateName(t *testing.T) { |
| 114 | ctx := testRust(t, ` |
| 115 | rust_library_host_dylib { |
| 116 | name: "fizz-buzz", |
| 117 | srcs: ["foo.rs"], |
| 118 | }`) |
| 119 | module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64_dylib").Module().(*Module) |
| 120 | crateName := module.CrateName() |
| 121 | expectedResult := "fizz_buzz" |
| 122 | |
| 123 | if crateName != expectedResult { |
| 124 | t.Errorf("CrateName() returned the wrong default crate name; expected '%#v', got '%#v'", expectedResult, crateName) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Test to make sure dependencies are being picked up correctly. |
| 129 | func TestDepsTracking(t *testing.T) { |
| 130 | ctx := testRust(t, ` |
| 131 | rust_library_host_dylib { |
| 132 | name: "libfoo", |
| 133 | srcs: ["foo.rs"], |
| 134 | } |
| 135 | rust_library_host_rlib { |
| 136 | name: "libbar", |
| 137 | srcs: ["foo.rs"], |
| 138 | } |
| 139 | rust_proc_macro { |
| 140 | name: "libpm", |
| 141 | srcs: ["foo.rs"], |
| 142 | host_supported: true, |
| 143 | } |
| 144 | rust_binary_host { |
| 145 | name: "fizz-buzz", |
| 146 | dylibs: ["libfoo"], |
| 147 | rlibs: ["libbar"], |
| 148 | proc_macros: ["libpm"], |
| 149 | srcs: ["foo.rs"], |
| 150 | } |
| 151 | `) |
| 152 | module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module) |
| 153 | |
| 154 | // Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up. |
| 155 | if !android.InList("libfoo", module.Properties.AndroidMkDylibs) { |
| 156 | t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)") |
| 157 | } |
| 158 | |
| 159 | if !android.InList("libbar", module.Properties.AndroidMkRlibs) { |
| 160 | t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)") |
| 161 | } |
| 162 | |
| 163 | if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) { |
| 164 | t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)") |
| 165 | } |
| 166 | |
| 167 | } |