Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 1 | // Copyright 2020 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 | "testing" |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame^] | 19 | |
| 20 | "android/soong/android" |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func TestClippy(t *testing.T) { |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame^] | 24 | |
| 25 | bp := ` |
| 26 | // foo uses the default value of clippy_lints |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 27 | rust_library { |
| 28 | name: "libfoo", |
| 29 | srcs: ["foo.rs"], |
| 30 | crate_name: "foo", |
| 31 | } |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame^] | 32 | // bar forces the use of the "android" lint set |
| 33 | rust_library { |
| 34 | name: "libbar", |
| 35 | srcs: ["foo.rs"], |
| 36 | crate_name: "bar", |
| 37 | clippy_lints: "android", |
| 38 | } |
| 39 | // foobar explicitly disable clippy |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 40 | rust_library { |
| 41 | name: "libfoobar", |
| 42 | srcs: ["foo.rs"], |
| 43 | crate_name: "foobar", |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame^] | 44 | clippy_lints: "none", |
| 45 | }` |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 46 | |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame^] | 47 | bp = bp + GatherRequiredDepsForTest() |
| 48 | |
| 49 | fs := map[string][]byte{ |
| 50 | // Reuse the same blueprint file for subdirectories. |
| 51 | "external/Android.bp": []byte(bp), |
| 52 | "hardware/Android.bp": []byte(bp), |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame^] | 55 | var clippyLintTests = []struct { |
| 56 | modulePath string |
| 57 | fooFlags string |
| 58 | }{ |
| 59 | {"", "${config.ClippyDefaultLints}"}, |
| 60 | {"external/", ""}, |
| 61 | {"hardware/", "${config.ClippyVendorLints}"}, |
| 62 | } |
| 63 | |
| 64 | for _, tc := range clippyLintTests { |
| 65 | t.Run("path="+tc.modulePath, func(t *testing.T) { |
| 66 | |
| 67 | config := android.TestArchConfig(buildDir, nil, bp, fs) |
| 68 | ctx := CreateTestContext() |
| 69 | ctx.Register(config) |
| 70 | _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"}) |
| 71 | android.FailIfErrored(t, errs) |
| 72 | _, errs = ctx.PrepareBuildActions(config) |
| 73 | android.FailIfErrored(t, errs) |
| 74 | |
| 75 | r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("clippy") |
| 76 | if r.Args["clippyFlags"] != tc.fooFlags { |
| 77 | t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["clippyFlags"], tc.fooFlags) |
| 78 | } |
| 79 | |
| 80 | r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("clippy") |
| 81 | if r.Args["clippyFlags"] != "${config.ClippyDefaultLints}" { |
| 82 | t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["clippyFlags"], "${config.ClippyDefaultLints}") |
| 83 | } |
| 84 | |
| 85 | r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("clippy") |
| 86 | if r.Rule != nil { |
| 87 | t.Errorf("libfoobar is setup to use clippy when explicitly disabled: clippyFlags=%q", r.Args["clippyFlags"]) |
| 88 | } |
| 89 | |
| 90 | }) |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 91 | } |
| 92 | } |