blob: 7815aab9d5461bc39255200db06419e6d6eab4b2 [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 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 Weksteen92f703b2020-06-22 13:28:02 +020053 }
54
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020055 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 Weksteen92f703b2020-06-22 13:28:02 +020091 }
92}