clippy_test.go 2.6 KB

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