blob: 786dc15376b55f8820e5793a3babcdd60045e66d [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) {
Colin Cross323dc602020-09-18 14:25:31 -070024 t.Parallel()
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()
49
50 fs := map[string][]byte{
51 // Reuse the same blueprint file for subdirectories.
52 "external/Android.bp": []byte(bp),
53 "hardware/Android.bp": []byte(bp),
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020054 }
55
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020056 var clippyLintTests = []struct {
57 modulePath string
58 fooFlags string
59 }{
60 {"", "${config.ClippyDefaultLints}"},
61 {"external/", ""},
62 {"hardware/", "${config.ClippyVendorLints}"},
63 }
64
65 for _, tc := range clippyLintTests {
66 t.Run("path="+tc.modulePath, func(t *testing.T) {
67
68 config := android.TestArchConfig(buildDir, nil, bp, fs)
69 ctx := CreateTestContext()
70 ctx.Register(config)
71 _, errs := ctx.ParseFileList(".", []string{tc.modulePath + "Android.bp"})
72 android.FailIfErrored(t, errs)
73 _, errs = ctx.PrepareBuildActions(config)
74 android.FailIfErrored(t, errs)
75
76 r := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
77 if r.Args["clippyFlags"] != tc.fooFlags {
78 t.Errorf("Incorrect flags for libfoo: %q, want %q", r.Args["clippyFlags"], tc.fooFlags)
79 }
80
81 r = ctx.ModuleForTests("libbar", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
82 if r.Args["clippyFlags"] != "${config.ClippyDefaultLints}" {
83 t.Errorf("Incorrect flags for libbar: %q, want %q", r.Args["clippyFlags"], "${config.ClippyDefaultLints}")
84 }
85
86 r = ctx.ModuleForTests("libfoobar", "android_arm64_armv8-a_dylib").MaybeRule("clippy")
87 if r.Rule != nil {
88 t.Errorf("libfoobar is setup to use clippy when explicitly disabled: clippyFlags=%q", r.Args["clippyFlags"])
89 }
90
91 })
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020092 }
93}