Skip to content

Commit

Permalink
More test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Bencodes committed Jul 15, 2022
1 parent 4f307f9 commit c7ea2f0
Showing 1 changed file with 119 additions and 15 deletions.
134 changes: 119 additions & 15 deletions src/test/shell/bazel/android/android_local_test_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,31 @@ android_library(
name = 'lib',
manifest = 'AndroidManifest.xml',
exports_manifest = 0,
srcs = ['Bar.java'],
srcs = ['Collatz.java'],
visibility = ["//visibility:public"],
)
EOF
cat > java/com/bin/AndroidManifest.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
<manifest package='com.bin' xmlns:android="http://schemas.android.com/apk/res/android" />
EOF
cat > java/com/bin/Bar.java <<EOF
cat > java/com/bin/Collatz.java <<EOF
package com.bin;
public class Bar { }
public class Collatz {
public static int getCollatzFinal(int n) {
if (n == 1) {
return 1;
}
if (n % 2 == 0) {
return getCollatzFinal(n / 2);
} else {
return getCollatzFinal(n * 3 + 1);
}
}
}
EOF
cat > java/com/bin/res/values/values.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
Expand All @@ -80,9 +94,9 @@ java_library(
)
android_local_test(
name = 'test',
srcs = ['BarTest.java'],
srcs = ['TestCollatz.java'],
manifest = 'AndroidManifest.xml',
test_class = "com.bin.BarTest",
test_class = "com.bin.TestCollatz",
deps = ['//java/com/bin:lib', ':robolectric-deps'],
)
EOF
Expand All @@ -91,29 +105,59 @@ EOF
<manifest package='com.bin.test' xmlns:android='http://schemas.android.com/apk/res/android'>
</manifest>
EOF
cat > javatests/com/bin/BarTest.java <<EOF
cat > javatests/com/bin/TestCollatz.java <<EOF
package com.bin;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class BarTest {
public class TestCollatz {
@Test
public void testBar() {
new Bar();
public void testGetCollatzFinal() {
assertEquals(Collatz.getCollatzFinal(1), 1);
assertEquals(Collatz.getCollatzFinal(5), 1);
assertEquals(Collatz.getCollatzFinal(10), 1);
assertEquals(Collatz.getCollatzFinal(21), 1);
}
}
EOF
}

# Asserts that the coverage file exists by looking at the current $TEST_log.
# The method fails if TEST_log does not contain any coverage report for a passed test.
function assert_coverage_file_exists() {
# Returns the path of the code coverage report that was generated by Bazel by
# looking at the current $TEST_log. The method fails if TEST_log does not
# contain any coverage report for a passed test.
function get_coverage_file_path_from_test_log() {
local ending_part="$(sed -n -e '/PASSED/,$p' "$TEST_log")"

local coverage_file_path=$(grep -Eo "/[/a-zA-Z0-9\.\_\-]+\.dat$" <<< "$ending_part")
[[ -e "$coverage_file_path" ]] || fail "Coverage output file does not exist!"
echo "$coverage_file_path"
}

# Asserts if the given expected coverage result is included in the given output
# file.
#
# - expected_coverage The expected result that must be included in the output.
# - output_file The location of the coverage output file.
function assert_coverage_result() {
local expected_coverage="${1}"; shift
local output_file="${1}"; shift

# Replace newlines with commas to facilitate the assertion.
local expected_coverage_no_newlines="$( echo "$expected_coverage" | tr '\n' ',' )"
local output_file_no_newlines="$( cat "$output_file" | tr '\n' ',' )"

(echo "$output_file_no_newlines" \
| grep -F "$expected_coverage_no_newlines") \
|| fail "Expected coverage result
<$expected_coverage>
was not found in actual coverage report:
<$( cat "$output_file" )>"
}

function test_hello_world_android_local_test() {
function test_android_local_test() {
create_new_workspace
setup_android_sdk_support
setup_android_local_test_env
Expand All @@ -123,7 +167,7 @@ function test_hello_world_android_local_test() {
//javatests/com/bin:test &>$TEST_log || fail "Tests for //javatests/com/bin:test failed"
}

function test_hello_world_android_local_test_with_coverage() {
function test_android_local_test_with_coverage() {
create_new_workspace
setup_android_sdk_support
setup_android_local_test_env
Expand All @@ -132,7 +176,67 @@ function test_hello_world_android_local_test_with_coverage() {
bazel coverage --test_output=all \
//javatests/com/bin:test &>$TEST_log || fail "Test with coverage for //javatests/com/bin:test failed"

assert_coverage_file_exists
local coverage_file_path="$( get_coverage_file_path_from_test_log )"
local expected_result="SF:java/com/bin/Collatz.java
FN:3,com/bin/Collatz::<init> ()V
FN:6,com/bin/Collatz::getCollatzFinal (I)I
FNDA:0,com/bin/Collatz::<init> ()V
FNDA:1,com/bin/Collatz::getCollatzFinal (I)I
FNF:2
FNH:1
BRDA:6,0,0,1
BRDA:6,0,1,1
BRDA:9,0,0,1
BRDA:9,0,1,1
BRF:4
BRH:4
DA:3,0
DA:6,1
DA:7,1
DA:9,1
DA:10,1
DA:12,1
LH:5
LF:6
end_of_record"

assert_coverage_result "$expected_result" "$coverage_file_path"
}

function test_android_local_test_with_combined_coverage() {
create_new_workspace
setup_android_sdk_support
setup_android_local_test_env

bazel clean
bazel coverage --test_output=all --coverage_report_generator=@bazel_tools//tools/test:coverage_report_generator --combined_report=lcov \
//javatests/com/bin:test &>$TEST_log || fail "Test with coverage for //javatests/com/bin:test failed"

local coverage_file_path="./bazel-out/_coverage/_coverage_report.dat"
local expected_result="SF:java/com/bin/Collatz.java
FN:3,com/bin/Collatz::<init> ()V
FN:6,com/bin/Collatz::getCollatzFinal (I)I
FNDA:0,com/bin/Collatz::<init> ()V
FNDA:1,com/bin/Collatz::getCollatzFinal (I)I
FNF:2
FNH:1
BRDA:6,0,0,1
BRDA:6,0,1,1
BRDA:9,0,0,1
BRDA:9,0,1,1
BRF:4
BRH:4
DA:3,0
DA:6,1
DA:7,1
DA:9,1
DA:10,1
DA:12,1
LH:5
LF:6
end_of_record"

assert_coverage_result "$expected_result" "$coverage_file_path"
}

run_suite "android_local_test integration tests"

0 comments on commit c7ea2f0

Please sign in to comment.