1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.coverageoutputgenerator;
import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.coverageoutputgenerator.Constants.TRACEFILE_EXTENSION;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Test for {@link Main}. */
@RunWith(JUnit4.class)
public class MainTest {
private static final int TEST_PARSE_PARALLELISM = 4;
@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule public TestName testName = new TestName();
private Path coverageDir;
@Before
public void createCoverageDirectory() throws IOException {
coverageDir = temporaryFolder.newFolder("coverage-dir").toPath();
}
@Test
public void testMainEmptyCoverageDir() {
assertThat(Main.getCoverageFilesInDir(coverageDir.toAbsolutePath().toString())).isEmpty();
}
@Test
public void testMainGetLcovTracefiles() throws IOException {
Path ccCoverageDir = Files.createTempDirectory(coverageDir, "cc_coverage");
Path javaCoverageDir = Files.createTempDirectory(coverageDir, "java_coverage");
Files.createTempFile(ccCoverageDir, "tracefile1", ".dat");
Files.createTempFile(javaCoverageDir, "tracefile2", ".dat");
List<File> coverageFiles = Main.getCoverageFilesInDir(coverageDir.toAbsolutePath().toString());
List<File> tracefiles = Main.getFilesWithExtension(coverageFiles, TRACEFILE_EXTENSION);
assertThat(tracefiles).hasSize(2);
}
@Test
public void testParallelParse_1KLoC_1KLcovFiles() throws Exception {
assertParallelParse(1024, 4, 256);
}
@Test
public void testParallelParse_1MLoC_4LcovFiles() throws Exception {
assertParallelParse(4, 1024, 1024);
}
@Test
public void testParallelParse_1MLoC_1LcovFiles() throws Exception {
assertParallelParse(1, 1024, 1024);
}
@Test
public void testEmptyInputProducesEmptyOutput() throws Exception {
Path output =
Paths.get(
temporaryFolder.getRoot().getAbsolutePath(),
testName.getMethodName() + ".coverage.dat");
int exitCode =
Main.runWithArgs(
"--coverage_dir", coverageDir.toAbsolutePath().toString(),
"--output_file", output.toAbsolutePath().toString());
assertThat(exitCode).isEqualTo(0);
assertThat(output.toFile().exists()).isTrue();
assertThat(output.toFile().length()).isEqualTo(0L);
}
@Test
public void testNonEmptyInputProducesNonEmptyOutput() throws Exception {
LcovMergerTestUtils.generateLcovFiles("test_data/simple_test", 8, 8, 8, coverageDir);
Path output =
Paths.get(
temporaryFolder.getRoot().getAbsolutePath(),
testName.getMethodName() + ".coverage.dat");
int exitCode =
Main.runWithArgs(
"--coverage_dir", coverageDir.toAbsolutePath().toString(),
"--output_file", output.toAbsolutePath().toString());
assertThat(exitCode).isEqualTo(0);
assertThat(output.toFile().exists()).isTrue();
assertThat(output.toFile().length()).isGreaterThan(0L);
}
private void assertParallelParse(int numLcovFiles, int numSourceFiles, int numLinesPerSourceFile)
throws Exception {
ByteArrayOutputStream sequentialOutput = new ByteArrayOutputStream();
ByteArrayOutputStream parallelOutput = new ByteArrayOutputStream();
LcovMergerTestUtils.generateLcovFiles(
"test_data/simple_test", numLcovFiles, numSourceFiles, numLinesPerSourceFile, coverageDir);
List<File> coverageFiles = Main.getCoverageFilesInDir(coverageDir.toAbsolutePath().toString());
Coverage sequentialCoverage = Main.parseFilesSequentially(coverageFiles, LcovParser::parse);
LcovPrinter.print(sequentialOutput, sequentialCoverage);
Coverage parallelCoverage =
Main.parseFilesInParallel(coverageFiles, LcovParser::parse, TEST_PARSE_PARALLELISM);
LcovPrinter.print(parallelOutput, parallelCoverage);
assertThat(parallelOutput.toString()).isEqualTo(sequentialOutput.toString());
}
}
|