Skip to content

Commit

Permalink
Merge pull request #14 from naver/ksmail13/feat/allow_other_file_types
Browse files Browse the repository at this point in the history
Allow non java files
  • Loading branch information
ksmail13 authored Nov 25, 2019
2 parents ca907c7 + 93dfdc0 commit c5dc3e3
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 130 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: java
jdk:
- oraclejdk8
- oraclejdk11
- openjdk8

- openjdk11
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.naver.nid</groupId>
<artifactId>coverchecker</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>

<packaging>jar</packaging>

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/naver/nid/cover/CoverChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -52,13 +55,15 @@ public CoverChecker(ObjectFactory objectManager) {
objectManager.getReporter());
}

public void check(Parameter param) {
public boolean check(Parameter param) {
try {
RawDiffReader diffReader = createDiffReader(param);

log.info("Check new line of code coverage by {}", coverageParser.getClass().getSimpleName());
CompletableFuture<List<FileCoverageReport>> coverage = executeByBackground((Function<String, List<FileCoverageReport>>) coverageParser::parse)
.apply(param.getCoveragePath());
CompletableFuture<List<FileCoverageReport>> coverage = param.getCoveragePath().stream()
.map(s -> executeByBackground((Function<String, List<FileCoverageReport>>) coverageParser::parse).apply(s))
.reduce((f1, f2) -> f1.thenCombine(f2, (r1, r2) -> Stream.concat(r1.stream(), r2.stream()).collect(Collectors.toList())))
.orElseThrow(() -> new IllegalStateException("No Coverage Report"));

log.info("read diff by {}", diffReader.getClass().getSimpleName());
CompletableFuture<List<Diff>> diff = executeByBackground(diffParser::parse)
Expand All @@ -69,12 +74,14 @@ public void check(Parameter param) {

reporter.report(check);
log.info("check result {}", check.result());
return true;
} catch (Exception e) {
NewCoverageCheckReport failResult = NewCoverageCheckReport.builder()
.error(e)
.build();

reporter.report(failResult);
return false;
}
}

Expand Down
46 changes: 25 additions & 21 deletions src/main/java/com/naver/nid/cover/checker/NewCoverageChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* diff 정보와 coverage report를 이용해 신규 코드에 대한 커버리지를 측정
* <p>코드의 변경점과 coverage 리포트를 이용해 신규 코드에 대한 커버리지를 측정</p>
*
* <p>NewCoverageChecker calculate test code coverage that updated.</p>
*/
public class NewCoverageChecker {
private static final Logger logger = LoggerFactory.getLogger(NewCoverageChecker.class);

/**
* coverage report 와 diff 를 확인하여 신규 코드의 테스트 코드의 커버 정도를 확인한다.
* <p>coverage report 와 diff 를 확인하여 신규 코드의 테스트 코드의 커버 정도를 확인한다.</p>
*
* <p>The method Check will calculate test coverage for new code for compare test coverage report and code diff.</p>
*
* @param coverage 파일 커버리지 리포트 결과
* @param diff 구 버전 파일과 신 버전 파일의 차이
Expand All @@ -46,35 +51,22 @@ public class NewCoverageChecker {
*/
public NewCoverageCheckReport check(List<FileCoverageReport> coverage, List<Diff> diff, int threshold, int fileThreshold) {
Map<String, List<Line>> diffMap = diff.stream()
.filter(d -> !d.getFileName().startsWith("src/test/java/"))
.filter(d -> d.getFileName().endsWith(".java"))
.filter(d -> !d.getFileName().startsWith("src/test"))
.filter(d -> !d.getDiffSectionList().isEmpty())
.peek(d -> logger.debug("diff file {}", d.getFileName()))
.peek(d -> d.setFileName(d.getFileName().replaceFirst("src/main/java/", "")))
.collect(Collectors.toMap(Diff::getFileName
, d -> d.getDiffSectionList().stream()
.flatMap(s -> s.getLineList().stream())
.filter(l -> l.getType() == ModifyType.ADD)
.collect(Collectors.toList())
, (u1, u2) -> {
ArrayList<Line> diffs = new ArrayList<>();
diffs.addAll(u1);
diffs.addAll(u2);

return diffs;
}));
, (u1, u2) -> Stream.concat(u1.stream(), u2.stream()).collect(Collectors.toList())));

// TODO redesign for multi module
Map<String, List<LineCoverageReport>> coverageMap = coverage.stream()
.peek(r -> logger.debug("file coverage {}", r.getFileName()))
.collect(Collectors.toMap(FileCoverageReport::getFileName
, FileCoverageReport::getLineCoverageReportList
, (u1, u2) -> {
ArrayList<LineCoverageReport> diffs = new ArrayList<>();
diffs.addAll(u1);
diffs.addAll(u2);

return diffs;
}));
, (u1, u2) -> Stream.concat(u1.stream(), u2.stream()).collect(Collectors.toList())));


NewCoverageCheckReport result = combine(coverageMap, diffMap);
Expand All @@ -96,11 +88,23 @@ private NewCoverageCheckReport combine(Map<String, List<LineCoverageReport>> cov
int coveredLineCount = 0;

Set<String> files = new HashSet<>(coverageReport.keySet());
files.retainAll(newCodeLines.keySet());

List<NewCoveredFile> coveredFileList = new ArrayList<>();
for (String file : files) {
List<Line> diffList = newCodeLines.get(file);
// TODO 다른 모듈의 동일 패키지 동일 파일 이름일 경우에 대한 처리 필요

// 코드 커버리지의 끝 경로가 같은 경우에 대해 검색
List<Line> diffList = newCodeLines.entrySet().stream()
.filter(e -> e.getKey().endsWith(file))
.findFirst()
.map(Map.Entry::getValue)
.orElse(Collections.emptyList());

if (diffList.isEmpty()) {
logger.debug("file({}) is not changed", file);
continue;
}

List<LineCoverageReport> lineCoverageReports = coverageReport.get(file);

logger.debug("check file {}", file);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/naver/nid/cover/util/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@Getter
@ToString
@Builder
Expand All @@ -27,7 +29,7 @@ public class Parameter {
private int fileThreshold;
private String githubToken;
private String diffPath;
private String coveragePath;
private List<String> coveragePath;
private String githubUrl;
private String repo;
private String diffType;
Expand Down
87 changes: 46 additions & 41 deletions src/main/java/com/naver/nid/cover/util/ParameterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.util.Arrays;

public class ParameterParser {
private static final Logger logger = LoggerFactory.getLogger(ParameterParser.class);
Expand Down Expand Up @@ -53,7 +54,7 @@ public Parameter getParam(String... commandArgs) {
.fileThreshold(getFileThreshold(cmd))
.githubToken(cmd.getOptionValue("g"))
.diffPath(cmd.getOptionValue("d"))
.coveragePath(cmd.getOptionValue("c"))
.coveragePath(Arrays.asList(cmd.getOptionValues("c")))
.githubUrl(cmd.getOptionValue("u", IGitHubConstants.HOST_API))
.repo(cmd.getOptionValue("r"))
.diffType(cmd.getOptionValue("diff-type"))
Expand Down Expand Up @@ -86,62 +87,66 @@ private String getPrNumber(CommandLine cmd) {
private Options executeOption() {
Options commandOptions = new Options();

Option diffPath = new Option("d", DIFF_OPTION, true, "diff file path(absolute recommend)");
commandOptions.addOption(diffPath);
commandOptions.addOption(Option.builder("d")
.longOpt(DIFF_OPTION)
.hasArg()
.desc("diff file path(absolute recommend)")
.build());

Option githubToken = new Option("g", GITHUB_TOKEN_OPTION, true, "github oauth token");
githubToken.setRequired(true);
commandOptions.addOption(githubToken);
commandOptions.addOption(Option.builder("dt")
.longOpt("diff-type")
.hasArg()
.desc("diff type (github | file)")
.build());

commandOptions.addOption(Option.builder("g")
.longOpt(GITHUB_TOKEN_OPTION)
.required()
.hasArg()
.desc("github oauth token")
.build());

Option githubUrl = Option.builder("u")
.desc("The url when you working on github enterprise url. default is api.github.com").hasArg()
commandOptions.addOption(Option.builder("u")
.longOpt("github-url")
.build();
commandOptions.addOption(githubUrl);
.hasArg()
.desc("The url when you working on github enterprise url. default is api.github.com")
.build());

Option githubPrNum = Option.builder("p")
.desc("github pr number").hasArg()
commandOptions.addOption(Option.builder("p")
.longOpt("pr")
.build();
commandOptions.addOption(githubPrNum);
.hasArg()
.desc("github pr number")
.build());

Option githubRepo = Option.builder("r").required()
.desc("github repo").hasArg()
commandOptions.addOption(Option.builder("r").required()
.longOpt("repo")
.build();
commandOptions.addOption(githubRepo);

Option thresholdOption = Option.builder("t").required()
.desc("coverage pass threshold").hasArg()
.longOpt(THRESHOLD_OPTION).type(Integer.class)
.build();
commandOptions.addOption(thresholdOption);
.hasArg()
.desc("github repo")
.build());

Option coverReportPath = Option.builder("c")
.longOpt(COVERAGE_PATH_OPTION).hasArg()
commandOptions.addOption(Option.builder("c")
.longOpt(COVERAGE_PATH_OPTION)
.hasArg()
.required()
.desc("coverage report path(absolute recommend)").build();
commandOptions.addOption(coverReportPath);
.desc("coverage report path(absolute recommend)")
.build());

Option coverReportType = Option.builder(COVERAGE_TYPE_OPTION)
commandOptions.addOption(Option.builder(COVERAGE_TYPE_OPTION)
.hasArg()
.desc("coverage report type (jacoco | cobertura) default is jacoco")
.build();
commandOptions.addOption(coverReportType);
.build());

Option fileThresholdOption = Option.builder("ft")
.longOpt("file-threshold")
commandOptions.addOption(Option.builder("t").required()
.longOpt(THRESHOLD_OPTION).type(Integer.class)
.hasArg()
.desc("coverage report type (jacoco | cobertura) default is jacoco")
.build();
commandOptions.addOption(fileThresholdOption);
.desc("coverage pass threshold")
.build());

Option diffType = Option.builder("dt")
.longOpt("diff-type")
commandOptions.addOption(Option.builder("ft")
.longOpt("file-threshold")
.hasArg()
.desc("diff type (github | file)")
.build();
commandOptions.addOption(diffType);
.desc("coverage report type (jacoco | cobertura) default is jacoco")
.build());

return commandOptions;
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<include resource="pattern.xml" />

<logger name="com.naver.nid.cover.parser.diff" level="INFO" />
<logger name="com.naver.nid.cover.parser.coverage" level="INFO" />
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/pattern.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<included>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level \(%file:%line\) %msg%n</pattern>
</encoder>
</appender>
</included>
Loading

0 comments on commit c5dc3e3

Please sign in to comment.