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
15package rust
16
17import (
18	"strings"
19	"testing"
20
21	"android/soong/android"
22)
23
24// Test that coverage flags are being correctly generated.
25func TestCoverageFlags(t *testing.T) {
26	ctx := testRustCov(t, `
27		rust_library {
28			name: "libfoo_cov",
29			srcs: ["foo.rs"],
30			crate_name: "foo",
31		}
32		rust_binary {
33			name: "fizz_cov",
34			srcs: ["foo.rs"],
35		}
36        rust_binary {
37			name: "buzzNoCov",
38			srcs: ["foo.rs"],
39			native_coverage: false,
40		}
41		rust_library {
42			name: "libbar_nocov",
43			srcs: ["foo.rs"],
44			crate_name: "bar",
45			native_coverage: false,
46		}`)
47
48	// Make sure native_coverage: false isn't creating a coverage variant.
49	if android.InList("android_arm64_armv8-a_dylib_cov", ctx.ModuleVariantsForTests("libbar_nocov")) {
50		t.Fatalf("coverage variant created for module 'libbar_nocov' with native coverage disabled")
51	}
52
53	// Just test the dylib variants unless the library coverage logic changes to distinguish between the types.
54	libfooCov := ctx.ModuleForTests("libfoo_cov", "android_arm64_armv8-a_dylib_cov").Rule("rustc")
55	libbarNoCov := ctx.ModuleForTests("libbar_nocov", "android_arm64_armv8-a_dylib").Rule("rustc")
56	fizzCov := ctx.ModuleForTests("fizz_cov", "android_arm64_armv8-a_cov").Rule("rustc")
57	buzzNoCov := ctx.ModuleForTests("buzzNoCov", "android_arm64_armv8-a").Rule("rustc")
58
59	rustcCoverageFlags := []string{"-C instrument-coverage", " -g "}
60	for _, flag := range rustcCoverageFlags {
61		missingErrorStr := "missing rustc flag '%s' for '%s' module with coverage enabled; rustcFlags: %#v"
62		containsErrorStr := "contains rustc flag '%s' for '%s' module with coverage disabled; rustcFlags: %#v"
63
64		if !strings.Contains(fizzCov.Args["rustcFlags"], flag) {
65			t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["rustcFlags"])
66		}
67		if !strings.Contains(libfooCov.Args["rustcFlags"], flag) {
68			t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["rustcFlags"])
69		}
70		if strings.Contains(buzzNoCov.Args["rustcFlags"], flag) {
71			t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["rustcFlags"])
72		}
73		if strings.Contains(libbarNoCov.Args["rustcFlags"], flag) {
74			t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["rustcFlags"])
75		}
76	}
77
78	linkCoverageFlags := []string{"-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw", " -g "}
79	for _, flag := range linkCoverageFlags {
80		missingErrorStr := "missing rust linker flag '%s' for '%s' module with coverage enabled; rustcFlags: %#v"
81		containsErrorStr := "contains rust linker flag '%s' for '%s' module with coverage disabled; rustcFlags: %#v"
82
83		if !strings.Contains(fizzCov.Args["linkFlags"], flag) {
84			t.Fatalf(missingErrorStr, flag, "fizz_cov", fizzCov.Args["linkFlags"])
85		}
86		if !strings.Contains(libfooCov.Args["linkFlags"], flag) {
87			t.Fatalf(missingErrorStr, flag, "libfoo_cov dylib", libfooCov.Args["linkFlags"])
88		}
89		if strings.Contains(buzzNoCov.Args["linkFlags"], flag) {
90			t.Fatalf(containsErrorStr, flag, "buzzNoCov", buzzNoCov.Args["linkFlags"])
91		}
92		if strings.Contains(libbarNoCov.Args["linkFlags"], flag) {
93			t.Fatalf(containsErrorStr, flag, "libbar_cov", libbarNoCov.Args["linkFlags"])
94		}
95	}
96
97}
98
99func TestCoverageDeps(t *testing.T) {
100	ctx := testRustCov(t, `
101		rust_binary {
102			name: "fizz",
103			srcs: ["foo.rs"],
104		}`)
105
106	fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a_cov").Rule("rustc")
107	if !strings.Contains(fizz.Args["linkFlags"], "libprofile-clang-extras.a") {
108		t.Fatalf("missing expected coverage 'libprofile-clang-extras' dependency in linkFlags: %#v", fizz.Args["linkFlags"])
109	}
110}
111