blob: e90564f63a9d80a8ef4794b339b774e20bb4781e [file] [log] [blame]
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001// 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
15package rust
16
17import (
18 "testing"
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020019
20 "android/soong/android"
Jiyong Park99644e92020-11-17 22:21:02 +090021 "android/soong/cc"
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020022)
23
24func TestClippy(t *testing.T) {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020025
26 bp := `
27 // foo uses the default value of clippy_lints
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020028 rust_library {
29 name: "libfoo",
30 srcs: ["foo.rs"],
31 crate_name: "foo",
32 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020033 // bar forces the use of the "android" lint set
34 rust_library {
35 name: "libbar",
36 srcs: ["foo.rs"],
37 crate_name: "bar",
38 clippy_lints: "android",
39 }
40 // foobar explicitly disable clippy
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020041 rust_library {
42 name: "libfoobar",
43 srcs: ["foo.rs"],
44 crate_name: "foobar",
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020045 clippy_lints: "none",
46 }`
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020047
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020048 bp = bp + GatherRequiredDepsForTest()
Jiyong Park99644e92020-11-17 22:21:02 +090049 bp = bp + cc.GatherRequiredDepsForTest(android.NoOsType)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020050
51 fs := map[string][]byte{
52 // Reuse the same blueprint file for subdirectories.
53 "external/Android.bp": []byte(bp),
54 "hardware/Android.bp": []byte(bp),
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 }
56
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020057 var clippyLintTests = []struct {
58 modulePath string
59 fooFlags string
60 }{
61 {"", "${config.ClippyDefaultLints}"},
62 {"external/", ""},
63 {"hardware/", "${config.ClippyVendorLints}"},
64 }
65
66 for _, tc := range clippyLintTests {
67 t.Run("path="+tc.modulePath, func(t *testing.T) {
68
Paul Duffin2c4ca8d2021-03-07 19:18:38 +000069 config := android.TestArchConfig(t.TempDir(), nil, bp, fs)
Colin Crossae8600b2020-10-29 17:09:13 -070070 ctx := CreateTestContext(config)
71 ctx.Register()
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020072 _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"})
73 android.FailIfErrored(t, errs)
74 _, errs = ctx.PrepareBuildActions(config)
75 android.FailIfErrored(t, errs)
76
77 r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
78 if r.Args["clippyFlags"] != tc.fooFlags {
79 t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["clippyFlags"], tc.fooFlags)
80 }
81
82 r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
83 if r.Args["clippyFlags"] != "${config.ClippyDefaultLints}" {
84 t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["clippyFlags"], "${config.ClippyDefaultLints}")
85 }
86
87 r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
88 if r.Rule != nil {
89 t.Errorf("libfoobar is setup to use clippy when explicitly disabled: clippyFlags=%q", r.Args["clippyFlags"])
90 }
91
92 })
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020093 }
94}