From 8fa16d9eef244f0e270d02319f9d11e3404dc903 Mon Sep 17 00:00:00 2001 From: Florian McKee <84742327+fmck3516@users.noreply.github.com> Date: Sun, 10 Dec 2023 15:19:14 -0600 Subject: [PATCH] issues/40: Increase unit test coverage for skippy-gradle --- .../gradle/collector/ClassFileCollector.java | 1 - .../collector/ClassFileCollectorTest.java | 70 ++++++++++++++++ .../gradle/collector/CollectorTestUtils.java | 65 +++++++++++++++ .../SkippifiedTestCollectorTest.java | 78 ++++++++++++++++++ .../CoverageBuildTasksAndArgumentsTest.java | 55 ++++++++++++ .../io/skippy/gradle/model/ClassFileTest.java | 15 +++- .../collector/sourceset1/NormalClass1.class | Bin 0 -> 285 bytes .../sourceset1/SkippifiedTest1.class | Bin 0 -> 662 bytes .../collector/sourceset2/NormalClass2.class | Bin 0 -> 285 bytes .../sourceset2/SkippifiedTest2.class | Bin 0 -> 662 bytes .../collector/sourceset3/NormalClass3.class | Bin 0 -> 285 bytes .../sourceset3/SkippifiedTest3.class | Bin 0 -> 662 bytes .../gradle/coveragebuild/LeftPadderTest.class | Bin 0 -> 846 bytes 13 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java create mode 100644 skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java create mode 100644 skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java create mode 100644 skippy-gradle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java create mode 100644 skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/NormalClass1.class create mode 100644 skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/SkippifiedTest1.class create mode 100644 skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/NormalClass2.class create mode 100644 skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/SkippifiedTest2.class create mode 100644 skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/NormalClass3.class create mode 100644 skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/SkippifiedTest3.class create mode 100644 skippy-gradle/src/test/resources/io/skippy/gradle/coveragebuild/LeftPadderTest.class diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java b/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java index 9a79e7b..54b5914 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/collector/ClassFileCollector.java @@ -49,7 +49,6 @@ public ClassFileCollector(Project project, SourceSetContainer sourceSetContainer this.sourceSetContainer = sourceSetContainer; } - /** * Collects all {@link ClassFile}s in the project. * diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java new file mode 100644 index 0000000..495650d --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/collector/ClassFileCollectorTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2023 the original author or authors. + * + * 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 + * + * https://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 io.skippy.gradle.collector; + +import io.skippy.gradle.model.ClassFile; +import org.gradle.api.Project; +import org.junit.jupiter.api.Test; + +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import static io.skippy.gradle.collector.CollectorTestUtils.mockSourceSetContainer; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link ClassFileCollector}. + * + * @author Florian McKee + */ +public class ClassFileCollectorTest { + + @Test + void testCollect() throws URISyntaxException { + + var currentDir = Paths.get(getClass().getResource("sourceset1").toURI()).toFile(); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(currentDir.getParentFile().getParentFile()); + + var classFileCollector = new ClassFileCollector(project, mockSourceSetContainer("sourceset1", "sourceset2")); + + var classFiles = classFileCollector.collect(); + assertEquals(4, classFiles.size()); + + var classFile0 = classFiles.get(0); + assertEquals("com.example.NormalClass1", classFile0.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset1/NormalClass1.class"), classFile0.getRelativePath()); + + var classFile1 = classFiles.get(1); + assertEquals("com.example.NormalClass2", classFile1.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset2/NormalClass2.class"), classFile1.getRelativePath()); + + var classFile2 = classFiles.get(2); + assertEquals("com.example.SkippifiedTest1", classFile2.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset1/SkippifiedTest1.class"), classFile2.getRelativePath()); + + var classFile3 = classFiles.get(3); + assertEquals("com.example.SkippifiedTest2", classFile3.getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset2/SkippifiedTest2.class"), classFile3.getRelativePath()); + + } + +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java b/skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java new file mode 100644 index 0000000..86820ba --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/collector/CollectorTestUtils.java @@ -0,0 +1,65 @@ +/* + * Copyright 2023 the original author or authors. + * + * 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 + * + * https://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 io.skippy.gradle.collector; + +import org.gradle.api.file.FileCollection; +import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; +import org.gradle.api.tasks.SourceSetOutput; + +import java.io.File; +import java.net.URISyntaxException; +import java.nio.file.Paths; +import java.util.Set; + +import static java.util.Arrays.asList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Utility method to mock a {@link SourceSet}. + * + * @author Florian McKee + */ +class CollectorTestUtils { + + static SourceSetContainer mockSourceSetContainer(String... sourceSetDirectories) { + var sourceSetContainer = mock(SourceSetContainer.class); + var sourceSets = asList(sourceSetDirectories).stream().map(CollectorTestUtils::mockSourceSet).toList(); + for (int i = 0; i < sourceSets.size(); i++) { + when(sourceSetContainer.getByName(sourceSetDirectories[i])).thenReturn(sourceSets.get(i)); + } + when(sourceSetContainer.iterator()).thenReturn(sourceSets.iterator()); + return sourceSetContainer; + } + + private static SourceSet mockSourceSet(String directory) { + try { + File outputDirectory = Paths.get(CollectorTestUtils.class.getResource(directory).toURI()).toFile(); + var sourceSet = mock(SourceSet.class); + var sourceSetOutput = mock(SourceSetOutput.class); + when(sourceSet.getOutput()).thenReturn(sourceSetOutput); + var classesDir = mock(FileCollection.class); + when(sourceSetOutput.getClassesDirs()).thenReturn(classesDir); + when(classesDir.getFiles()).thenReturn(Set.of(outputDirectory)); + return sourceSet; + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java new file mode 100644 index 0000000..fae1358 --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/collector/SkippifiedTestCollectorTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2023 the original author or authors. + * + * 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 + * + * https://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 io.skippy.gradle.collector; + +import io.skippy.gradle.SkippyPluginExtension; +import io.skippy.gradle.model.SourceSetWithTestTask; +import org.gradle.api.Project; +import org.junit.jupiter.api.Test; + +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static io.skippy.gradle.collector.CollectorTestUtils.mockSourceSetContainer; +import static java.util.Arrays.asList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link SkippifiedTestCollector}. + * + * @author Florian McKee + */ +public class SkippifiedTestCollectorTest { + + @Test + void testCollect() throws URISyntaxException { + var currentDir = Paths.get(getClass().getResource("sourceset1").toURI()).toFile(); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(currentDir.getParentFile().getParentFile()); + + var sourceSetContainer = mockSourceSetContainer("sourceset1", "sourceset2", "sourceset3"); + var classFileCollector = new ClassFileCollector(project, sourceSetContainer); + var skippyPluginExtension = mock(SkippyPluginExtension.class); + + when(skippyPluginExtension.getSourceSetsWithTestTasks()).thenReturn(asList( + new SourceSetWithTestTask("sourceset1", "test"), + new SourceSetWithTestTask("sourceset2", "integrationTest"), + new SourceSetWithTestTask("sourceset3", "functionalTest") + )); + var skippifiedTestCollector = new SkippifiedTestCollector(classFileCollector, sourceSetContainer, skippyPluginExtension); + + var skippifiedTests = skippifiedTestCollector.collect(); + + assertEquals(3, skippifiedTests.size()); + + var skippifiedTests0 = skippifiedTests.get(0); + assertEquals("com.example.SkippifiedTest1", skippifiedTests0.classFile().getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset1/SkippifiedTest1.class"), skippifiedTests0.classFile().getRelativePath()); + assertEquals("test", skippifiedTests0.testTask()); + + var skippifiedTests1 = skippifiedTests.get(1); + assertEquals("com.example.SkippifiedTest2", skippifiedTests1.classFile().getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset2/SkippifiedTest2.class"), skippifiedTests1.classFile().getRelativePath()); + assertEquals("integrationTest", skippifiedTests1.testTask()); + + var skippifiedTests2 = skippifiedTests.get(2); + assertEquals("com.example.SkippifiedTest3", skippifiedTests2.classFile().getFullyQualifiedClassName()); + assertEquals(Path.of("collector/sourceset3/SkippifiedTest3.class"), skippifiedTests2.classFile().getRelativePath()); + assertEquals("functionalTest", skippifiedTests2.testTask()); + } + +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java new file mode 100644 index 0000000..94e74b8 --- /dev/null +++ b/skippy-gradle/src/test/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArgumentsTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2023 the original author or authors. + * + * 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 + * + * https://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 io.skippy.gradle.coveragebuild; + +import io.skippy.gradle.model.ClassFile; +import io.skippy.gradle.model.SkippifiedTest; +import org.gradle.api.Project; +import org.junit.jupiter.api.Test; + +import java.net.URISyntaxException; +import java.nio.file.Paths; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link CoverageBuildTasksAndArguments}. + * + * @author Florian McKee + */ +public class CoverageBuildTasksAndArgumentsTest { + + @Test + void testForSkippifiedTest() throws URISyntaxException { + var classFile = Paths.get(getClass().getResource("LeftPadderTest.class").toURI()); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(classFile.toFile().getParentFile().getParentFile()); + var skippifiedTest = new SkippifiedTest(new ClassFile(project, classFile), "test"); + var tasksAndArgs = CoverageBuildTasksAndArguments.forSkippifiedTest(skippifiedTest); + assertThat(tasksAndArgs.tasks()).contains( + "test", + "jacocoTestReport" + ); + assertThat(tasksAndArgs.arguments()).contains( + "-PskippyCoverageBuild=true", + "-PskippyClassFile=coveragebuild/LeftPadderTest.class", + "-PskippyTestTask=test" + ); + } +} diff --git a/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java b/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java index 8f9b161..4fb5524 100644 --- a/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java +++ b/skippy-gradle/src/test/java/io/skippy/gradle/model/ClassFileTest.java @@ -16,16 +16,17 @@ package io.skippy.gradle.model; -import io.skippy.gradle.model.ClassFile; import org.gradle.api.Project; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import java.net.URISyntaxException; +import java.nio.file.Path; import java.nio.file.Paths; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Tests for {@link ClassFile}. @@ -44,6 +45,18 @@ void testGetFullyQualifiedClassName(String fileName, String expectedValue) throw assertEquals(expectedValue, new ClassFile(mock(Project.class), classFile).getFullyQualifiedClassName()); } + @ParameterizedTest + @CsvSource(value = { + "SourceFileTest1.class:model/SourceFileTest1.class", + "SourceFileTest2.class:model/SourceFileTest2.class" + }, delimiter = ':') + void testGetRelativePath(String fileName, String expectedValue) throws URISyntaxException { + var classFile = Paths.get(getClass().getResource(fileName).toURI()); + var project = mock(Project.class); + when(project.getProjectDir()).thenReturn(classFile.toFile().getParentFile().getParentFile()); + assertEquals(Path.of(expectedValue), new ClassFile(project, classFile).getRelativePath()); + } + @ParameterizedTest @CsvSource(value = { "SourceFileTest1.class:w7P+0X1Grw3y8nPkIceITQ==", diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/NormalClass1.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset1/NormalClass1.class new file mode 100644 index 0000000000000000000000000000000000000000..558ca665c7963640b826317af58ca08cfbbbe414 GIT binary patch literal 285 zcmZ`!Jx{|x41LZ=Q_@01r;3dYI&fkJx>RC{%0QLf2`9Mdl0!*~_*+a!4Ez9o6v9_A zuwYBie$Udg{q_Cv3E&1WVFSUtccZ6#E}Y1K*vg##yNV_?GWdb{gI@Uu99 zIP?efM^znfA|df*$uoX$Ja6o=|NguG1K=xGo2X#QL)AwOb%EKDx>Pb!$(j5)80pXo z)W1+d_M5=e#%8~P1}_GYYNClZ9(*4&XbG&D>`aad-pDp3t23!ml6!fsGfO7P1$yI~J4(}NOA2?bm zlXpuopwaO<-W{eN^4%u_TJX5+gGOVbG0!nqVS#-G-O?_?u`OnocrKqngjo3xMrE7w literal 0 HcmV?d00001 diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/NormalClass2.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/NormalClass2.class new file mode 100644 index 0000000000000000000000000000000000000000..8a977c3acafe9fb0b9ba9f15f79421af19861152 GIT binary patch literal 285 zcmZ`!Jx{|x41LZ=Q_@01r;3dYI&cyz(4`VnR0gW-PB_6ummEq`#NT2TZY9NfRO>2e~Av`-T5{wCp z$~8*Aa`SAp+PJP^ySCi-mxO5T9yDQ=n^tdjO{u#rmsXT{?kctgcgFKyF&dssPdLf{ zGrS_C_ioo!`qp@tK$zNdAYQ(M-b4tH3i>e+Vj?)*_p#JN KVT2>W0J9%|ggm|g literal 0 HcmV?d00001 diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/SkippifiedTest2.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset2/SkippifiedTest2.class new file mode 100644 index 0000000000000000000000000000000000000000..f1020578bcc4c672b33c542749d1db0b5583c65f GIT binary patch literal 662 zcmaJWVFSgHh+P-=!O!9Z z;?N(^A60d{iG;+LCC~W1@w~Ce{`>F#4}h;&ZK8rH4^Qc!_C1>*IV5CDU zQ2#;+*>3_<8=L(G8oU@ps);7vc<_D9pe3+ovNJgDV)$_*cwDRZl-cZim=#zGO)T}5iqlBT!wX7NIwd_k(z*RCQ2lO(TA&?JqJI?e zKxap45HZsUOsJy1%E;jfS+(cH^;d#dGT-KU&bXfH{ORg-Hi z2hU&KWwK23&=gsy56GQo?$Ob=T?YiVUXajNRwp@kPwuTZY9NfRO>2e~Av`-T5{wCp z$~8*Aa`SAp+PJP^ySCi-mxO5T9yDQ=n^tdjO{u#rmsXT{?kctgcgFKyF&dssPdLf{ zGrS_C_ioo!`qp@tK$zNdAYQ(M-b4tH3i>e+Vj?)*_p#JN KVT2>W0J9%~8a%-O literal 0 HcmV?d00001 diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/SkippifiedTest3.class b/skippy-gradle/src/test/resources/io/skippy/gradle/collector/sourceset3/SkippifiedTest3.class new file mode 100644 index 0000000000000000000000000000000000000000..a182b5a297d97900c57b97586e4f122e91e1d457 GIT binary patch literal 662 zcmaJWVF_)x(mZ6#E}Y1K*vg##yNV_?GWdb{gI@Uu99 zIP?efM^znfA|df*$uoX$Ja6o=|NguG1K=xGo2X#QL)AwOb%EKDx>Pb!$(j5)80pXo z)W1+d_M5=e#%8~P1}_GYYNClZ9(*4&XbG&D>`aad-pDp3t23!ml6!fsGfO7P1$yI~J4(}NOA2?bm zlXpuopwaO<-W{eN^4%u_TJX5+gGOVbG0!nqVS#-G-O?_?u`OnocrKqngjo3xPYIj& literal 0 HcmV?d00001 diff --git a/skippy-gradle/src/test/resources/io/skippy/gradle/coveragebuild/LeftPadderTest.class b/skippy-gradle/src/test/resources/io/skippy/gradle/coveragebuild/LeftPadderTest.class new file mode 100644 index 0000000000000000000000000000000000000000..a254282b94acbeb85085c105c758d4e89f3fb39e GIT binary patch literal 846 zcmZ`%O>fgc5Pcix3)dvUg+NN6DPK(rVo#g`NL5uNgawFH;lOE=EXf9EZR~YbeikPx z4*URq6k^tilptD5p52+*dGGDa{{Hjx7l0Qya8ZC$LD54AWroIFoC+QaJ>~C4b2&B) z<>yK(^OC{o_J$Qy8Cv6L!R47)#G<GBrn$PL0qet-@s}&14uxRd`g}$@ea5s8`VN za1A~~bF)5>pUrzQnaG5p5{rrT8TPxuTFJm9N>BT*d;iB=T*r+HnjUWA7DFdWrhJ}} zUb3+=GT|at{4h;rVic)l@I>w$f6YWl?mtA!EB7~ORuOyg9fqBYh1(V|6px~bWT*#9 z%eUELB$E>{3Mt|T(O85-ktpl0f<-e^MBffBhwL+K8G@$nESpkNI?jm6-w?R!4GrZ( zrj1(2p-L6e9BLgIvAT0RxVSG?II*~cC`-ojm9lEL*8{QXr*;esPcA`_XGZFjI?Ipk zZt_u?86lQVMVJv-D^L+nzoX##T1y8%z`p0{ zHd&hnT-+h+Ef3%>cF6-elr7}NZ{Xyhb^l7>Z#{05&vEq}+iRE_9RR&X2X(TI9Im-6 kS;093zlVE7ai30F+6S~2@G!TJ@@S2s`}B7|fsN7o3x|5negFUf literal 0 HcmV?d00001