Skip to content

Commit

Permalink
feat: Implement the test coverage feature (#1637) (#1639)
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored Apr 5, 2024
1 parent 650af8a commit 2a83336
Show file tree
Hide file tree
Showing 36 changed files with 988 additions and 71 deletions.
6 changes: 0 additions & 6 deletions .azure-pipelines/vscode-java-test-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ extends:
command: custom
verbose: false
customCommand: run build-plugin
- task: Npm@1
displayName: npm run compile
inputs:
command: custom
verbose: false
customCommand: run compile
- task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@2
displayName: ESRP CodeSigning
inputs:
Expand Down
6 changes: 0 additions & 6 deletions .azure-pipelines/vscode-java-test-rc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ extends:
command: custom
verbose: false
customCommand: run build-plugin
- task: Npm@1
displayName: npm run compile
inputs:
command: custom
verbose: false
customCommand: run compile
- task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@2
displayName: ESRP CodeSigning
inputs:
Expand Down
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,4 @@ node_modules
resources/templates/css/**
resources/templates/js/**
resources/templates/fonts/**
dist
**/vscode.d.ts
**/vscode.proposed.d.ts
dist
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
"**/archetype-resources/**",
"**/META-INF/maven/**",
"**/test/test-projects/**"
]
],
"java.checkstyle.version": "8.18",
"java.checkstyle.configuration": "${workspaceFolder}/java-extension/build-tools/src/main/resources/checkstyle/checkstyle.xml",
}
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extension.bundle.ts
javaConfig.json
.github/**
.azure-pipelines/**
vscode.d.ts

# Ignore output of code sign
server/*.md
32 changes: 26 additions & 6 deletions ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ This project incorporates components from the projects listed below. The origina
5. Eskibear/vscode-extension-telemetry-wrapper (https://github.com/Eskibear/vscode-extension-telemetry-wrapper)
6. google/gson (https://github.com/google/gson)
7. isaacs/node-lru-cache (https://github.com/isaacs/node-lru-cache)
8. jprichardson/node-fs-extra (https://github.com/jprichardson/node-fs-extra)
9. junit-team/junit5 (https://github.com/junit-team/junit5)
10. lodash/lodash (https://github.com/lodash/lodash)
11. microsoft/vscode-languageserver-node (https://github.com/microsoft/vscode-languageserver-node)
12. ota4j-team/opentest4j (https://github.com/ota4j-team/opentest4j)
13. sindresorhus/get-port (https://github.com/sindresorhus/get-port)
8. jacoco/jacoco (https://github.com/jacoco/jacoco)
9. jprichardson/node-fs-extra (https://github.com/jprichardson/node-fs-extra)
10. junit-team/junit5 (https://github.com/junit-team/junit5)
11. lodash/lodash (https://github.com/lodash/lodash)
12. microsoft/vscode-languageserver-node (https://github.com/microsoft/vscode-languageserver-node)
13. ota4j-team/opentest4j (https://github.com/ota4j-team/opentest4j)
14. sindresorhus/get-port (https://github.com/sindresorhus/get-port)

%% Apache Commons Lang NOTICES AND INFORMATION BEGIN HERE
=========================================
Expand Down Expand Up @@ -976,6 +977,25 @@ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
=========================================
END OF isaacs/node-lru-cache NOTICES AND INFORMATION

%% jacoco/jacoco NOTICES AND INFORMATION BEGIN HERE
=========================================
License
=======

Copyright (c) 2009, 2023 Mountainminds GmbH & Co. KG and Contributors

The JaCoCo Java Code Coverage Library and all included documentation is made
available by Mountainminds GmbH & Co. KG, Munich. Except indicated below, the
Content is provided to you under the terms and conditions of the Eclipse Public
License Version 2.0 ("EPL"). A copy of the EPL is available at
[https://www.eclipse.org/legal/epl-2.0/](https://www.eclipse.org/legal/epl-2.0/).

Please visit
[http://www.jacoco.org/jacoco/trunk/doc/license.html](http://www.jacoco.org/jacoco/trunk/doc/license.html)
for the complete license information including third party licenses and trademarks.
=========================================
END OF jacoco/jacoco NOTICES AND INFORMATION

%% jprichardson/node-fs-extra NOTICES AND INFORMATION BEGIN HERE
=========================================
(The MIT License)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>coverage-test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1709104275722</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>coverage-test</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.project;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class Sample {

void sample(boolean x) {
Foo foo = getFoo();
if (x) {
foo = new Foo(); // <-- BOTH THIS LINE
}
Bar bar = new Bar();
Baz baz = new Baz();
baz.from(bar::bar)
.as(Sample::toArray) // <-- AND THIS LINE
.to(foo::foo);
}

static String[] toArray(List<String> s) {
return s.toArray(String[]::new);
}

static Foo getFoo() {
return new Foo();
}

static class Foo {
void foo(String... foo) {
}
}

static class Bar {
List<String> bar() {
return List.of("bar");
}
}

static class Baz {
Baz from(Supplier<List<String>> from) {
return this;
}

Baz as(Function<List<String>, String[]> as) {
return this;
}

Baz to(Consumer<String[]> to) {
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.project;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

class SampleTests {

@Test
void test() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*******************************************************************************
* Copyright (c) 2024 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package com.microsoft.java.test.plugin.coverage;

import com.microsoft.java.test.plugin.AbstractProjectsManagerBasedTest;
import com.microsoft.java.test.plugin.coverage.model.LineCoverage;
import com.microsoft.java.test.plugin.coverage.model.MethodCoverage;
import com.microsoft.java.test.plugin.coverage.model.SourceFileCoverage;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.junit.Test;

import java.io.File;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertTrue;

public class CoverageHandlerTest extends AbstractProjectsManagerBasedTest {

@Test
public void testGetCoverageDetail() throws Exception {
importProjects(Collections.singleton("coverage-test"));
final IJavaProject javaProject = ProjectUtils.getJavaProject("coverage-test");
final String basePath = new File("projects/coverage-test").getAbsolutePath();
final CoverageHandler coverageHandler = new CoverageHandler(javaProject, basePath);
final List<SourceFileCoverage> coverageDetail = coverageHandler.getCoverageDetail(new NullProgressMonitor());
for (final SourceFileCoverage fileCoverage : coverageDetail) {
for (final LineCoverage lineCoverage : fileCoverage.getLineCoverages()) {
assertTrue(lineCoverage.getLineNumber() > 0);
}

for (final MethodCoverage methodCoverage : fileCoverage.getMethodCoverages()) {
assertTrue(methodCoverage.getLineNumber() > 0);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ Require-Bundle: org.eclipse.jdt.core,
junit-platform-suite-engine;bundle-version="1.8.1",
org.apiguardian.api;bundle-version="1.0.0",
org.apache.commons.lang3;bundle-version="3.1.0",
com.google.gson;bundle-version="2.7.0"
com.google.gson;bundle-version="2.7.0",
org.objectweb.asm;bundle-version="9.6.0",
org.jacoco.core;bundle-version="0.8.11"
Export-Package: com.microsoft.java.test.plugin.launchers;x-friends:="com.microsoft.java.test.plugin.test",
com.microsoft.java.test.plugin.model;x-friends:="com.microsoft.java.test.plugin.test"
com.microsoft.java.test.plugin.model;x-friends:="com.microsoft.java.test.plugin.test",
com.microsoft.java.test.plugin.coverage;x-friends:="com.microsoft.java.test.plugin.test",
com.microsoft.java.test.plugin.coverage.model;x-friends:="com.microsoft.java.test.plugin.test"
Bundle-ClassPath: .
1 change: 1 addition & 0 deletions java-extension/com.microsoft.java.test.plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<command id="vscode.java.test.resolvePath" />
<command id="vscode.java.test.findTestLocation" />
<command id="vscode.java.test.navigateToTestOrTarget" />
<command id="vscode.java.test.jacoco.getCoverageDetail" />
</delegateCommandHandler>
</extension>
</plugin>
Loading

0 comments on commit 2a83336

Please sign in to comment.