blob: bd3bfb1514d2105d2ae0caa2f675578d21d9ffe6 [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"
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020021)
22
23func TestClippy(t *testing.T) {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020024
25 bp := `
26 // foo uses the default value of clippy_lints
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020027 rust_library {
28 name: "libfoo",
29 srcs: ["foo.rs"],
30 crate_name: "foo",
31 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020032 // 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 Weksteen92f703b2020-06-22 13:28:02 +020040 rust_library {
41 name: "libfoobar",
42 srcs: ["foo.rs"],
43 crate_name: "foobar",
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020044 clippy_lints: "none",
45 }`
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020046
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020047 var clippyLintTests = []struct {
48 modulePath string
49 fooFlags string
50 }{
51 {"", "${config.ClippyDefaultLints}"},
52 {"external/", ""},
53 {"hardware/", "${config.ClippyVendorLints}"},
54 }
55
56 for _, tc := range clippyLintTests {
57 t.Run("path="+tc.modulePath, func(t *testing.T) {
58
Paul Duffin9e0c3f92021-03-30 22:45:21 +010059 result := android.GroupFixturePreparers(
60 prepareForRustTest,
61 // Test with the blueprint file in different directories.
62 android.FixtureAddTextFile(tc.modulePath+"Android.bp", bp),
63 ).RunTest(t)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020064
Paul Duffin9e0c3f92021-03-30 22:45:21 +010065 r := result.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
66 android.AssertStringEquals(t, "libfoo flags", tc.fooFlags, r.Args["clippyFlags"])
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020067
Paul Duffin9e0c3f92021-03-30 22:45:21 +010068 r = result.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
69 android.AssertStringEquals(t, "libbar flags", "${config.ClippyDefaultLints}", r.Args["clippyFlags"])
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020070
Paul Duffin9e0c3f92021-03-30 22:45:21 +010071 r = result.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020072 if r.Rule != nil {
73 t.Errorf("libfoobar is setup to use clippy when explicitly disabled: clippyFlags=%q", r.Args["clippyFlags"])
74 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020075 })
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020076 }
77}