Skip to content

Commit

Permalink
Refactored tests and removed the bug (Previously, resources projects …
Browse files Browse the repository at this point in the history
…were not building automatically, resulting failure of tests)
  • Loading branch information
ABHAY0O7 committed Jul 26, 2021
1 parent 35b2606 commit 6ab6de9
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 74 deletions.
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ depclean-maven-plugin/target

depclean-core/depclean-core.iml
depclean-core/target/generated-sources/
depclean-gradle-plugin/.gradle/4.10.2/vcsMetadata-1/
depclean-gradle-plugin/build/tmp/compileJava/
depclean-gradle-plugin/depclean-gradle-plugin.iml
depclean-gradle-plugin/example/.gradle/4.10.2/vcsMetadata-1/
depclean-gradle-plugin/example/build/dependencies/
depclean-gradle-plugin/example/build/tmp/compileJava/
depclean-gradle-plugin/target/generated-sources/
depclean-maven-plugin/depclean-maven-plugin.iml
depclean-maven-plugin/target/generated-sources/
depclean-maven-plugin/target/generated-test-sources/
Expand Down
Binary file not shown.
Empty file.
Binary file added .gradle/7.0.2/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/7.0.2/fileHashes/fileHashes.lock
Binary file not shown.
Empty file added .gradle/7.0.2/gc.properties
Empty file.
Binary file added .gradle/checksums/checksums.lock
Binary file not shown.
Empty file added .gradle/vcs-1/gc.properties
Empty file.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package se.kth.depclean

import org.gradle.api.tasks.TaskExecutionException
import org.apache.maven.BuildFailureException
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.gradle.testkit.runner.TaskOutcome.*
import se.kth.depclean.util.FileUtils
import spock.lang.Specification

import static org.junit.jupiter.api.Assertions.assertEquals
import static org.gradle.testkit.runner.TaskOutcome.*
import static org.junit.jupiter.api.Assertions.assertTrue

class DepCleanGradleFT extends Specification {
Expand All @@ -31,11 +31,14 @@ class DepCleanGradleFT extends Specification {
.withArguments("debloat")
.buildAndFail()
} catch (Exception e) {
assertEquals(e, TaskExecutionException)
assertEquals(e, BuildFailureException)
}
}

File allDependenciesUnused = new File("src/Test/resources-fts/all_dependencies_unused")
String projectPath1 = "src/Test/resources-fts/all_dependencies_unused"
File allDependenciesUnused = new File(projectPath1)
File originalOutputFile1 = new File(projectPath1 + "/originalOutputFile.txt")
File expectedOutputFile1 = new File(projectPath1 + "/expectedOutputFile.txt")
@Test
@DisplayName("Test that depclean gradle plugin runs on a project which has only unused dependencies.")
def "all_dependencies_unused"() {
Expand All @@ -44,30 +47,22 @@ class DepCleanGradleFT extends Specification {

when:
project.plugins.apply("se.kth.castor.depclean-gradle-plugin")
BuildResult result = GradleRunner.create()
.withProjectDir(allDependenciesUnused)
.withArguments("debloat")
.build()
BuildResult buildResult = createRunner(allDependenciesUnused, "build")
BuildResult debloatResult = createRunner(allDependenciesUnused, "debloat")

then:
assertEquals(SUCCESS, result.task(":debloat").getOutcome())
String[] expectedOutput = {
"USED DIRECT DEPENDENCIES [0]:"
"USED INHERITED DEPENDENCIES [0]:"
"USED TRANSITIVE DEPENDENCIES [0]:"
"POTENTIALLY UNUSED DIRECT DEPENDENCIES [1]:"
"\tcom.fasterxml.jackson.core:jackson-databind:2.12.2:compile (1 MB)"
"POTENTIALLY UNUSED INHERITED DEPENDENCIES [0]:"
"POTENTIALLY UNUSED TRANSITIVE DEPENDENCIES [2]:"
"\tcom.fasterxml.jackson.core:jackson-core:2.12.2:compile (356 KB)"
"\tcom.fasterxml.jackson.core:jackson-annotations:2.12.2:compile (73 KB)"
}
for (String expectedOutputLine : expectedOutput) {
result.output.stripIndent().trim().contains(expectedOutputLine.stripIndent().trim())
}
assertEquals(SUCCESS, buildResult.task(":build").getOutcome())
assertEquals(SUCCESS, debloatResult.task(":debloat").getOutcome())

originalOutputFile1.write(debloatResult.getOutput())
assertTrue(compareOutputs(expectedOutputFile1, originalOutputFile1))
FileUtils.forceDelete(new File(projectPath1 + "/build"))
}

File allDependenciesUsed = new File("src/Test/resources-fts/all_dependencies_used")
String projectPath2 = "src/Test/resources-fts/all_dependencies_used"
File allDependenciesUsed = new File(projectPath2)
File originalOutputFile2 = new File(projectPath2 + "/originalOutputFile.txt")
File expectedOutputFile2 = new File(projectPath2 + "/expectedOutputFile.txt")
@Test
@DisplayName("Test that depclean gradle plugin runs on a project which has only used dependencies.")
def "all_dependencies_used"() {
Expand All @@ -76,34 +71,21 @@ class DepCleanGradleFT extends Specification {

when:
project.plugins.apply("se.kth.castor.depclean-gradle-plugin")
BuildResult result = GradleRunner.create()
.withProjectDir(allDependenciesUsed)
.withArguments("debloat")
.build()
BuildResult buildResult = createRunner(allDependenciesUsed, "build")
BuildResult debloatResult = createRunner(allDependenciesUsed, "debloat")

then:
assertEquals(SUCCESS, result.task(":debloat").getOutcome())
String[] expectedOutput = {
"USED DIRECT DEPENDENCIES [1]:"
"\tcom.fasterxml.jackson.core:jackson-databind:2.12.2:compile (1 MB)"
"USED INHERITED DEPENDENCIES [0]:"
"USED TRANSITIVE DEPENDENCIES [2]:"
"\tcom.fasterxml.jackson.core:jackson-annotations:2.12.2:compile (73 KB)"
"\tcom.fasterxml.jackson.core:jackson-core:2.12.2:compile (356 KB)"
"POTENTIALLY UNUSED DIRECT DEPENDENCIES [0]:"
"POTENTIALLY UNUSED INHERITED DEPENDENCIES [0]:"
"POTENTIALLY UNUSED TRANSITIVE DEPENDENCIES [0]:"
}
for (String expectedOutputLine : expectedOutput) {
result.output.stripIndent().trim().contains(expectedOutputLine.stripIndent().trim())
}
assertEquals(SUCCESS, buildResult.task(":build").getOutcome())
assertEquals(SUCCESS, debloatResult.task(":debloat").getOutcome())

originalOutputFile2.write(debloatResult.getOutput())
assertTrue(compareOutputs(expectedOutputFile2, originalOutputFile2))
FileUtils.forceDelete(new File(projectPath2 + "/build"))
}

File debloatedDependenciesIsCorrect =
new File("src/test/resources-fts/debloated_dependencies.gradle_is_correct")
String path = "src/test/resources-fts/debloated_dependencies.gradle_is_correct/" +
"debloated-dependencies.gradle";
File generatedDebloatedDependenciesDotGradle = new File(path);
String projectPath3 = "src/test/resources-fts/debloated_dependencies.gradle_is_correct"
File debloatedDependenciesIsCorrect = new File(projectPath3)
File generatedDebloatedDependenciesDotGradle = new File(projectPath3 + "/debloated-dependencies.gradle");
@Test
@DisplayName("Test that the depclean creates a proper debloated-dependencies.gradle file.")
def "debloated_dependencies.gradle_is_correct"() {
Expand All @@ -112,26 +94,46 @@ class DepCleanGradleFT extends Specification {

when:
project.plugins.apply("se.kth.castor.depclean-gradle-plugin")
BuildResult result = GradleRunner.create()
.withProjectDir(debloatedDependenciesIsCorrect)
.withArguments("debloat")
.build()
BuildResult buildResult = createRunner(debloatedDependenciesIsCorrect, "build")
BuildResult debloatResult = createRunner(debloatedDependenciesIsCorrect, "debloat")

then:
assertEquals(SUCCESS, result.task(":debloat").getOutcome())
String[] expectedOutput = {
"Starting debloating dependencies"
"Adding 0 used direct dependencies"
"Adding 1 used transitive dependencies as direct dependencies."
"Excluding 1 unused transitive dependencies one-by-one."
"Dependencies debloated successfully"
"debloated-dependencies.gradle file created in: " +
generatedDebloatedDependenciesDotGradle.getAbsolutePath()
}
for (String expectedOutputLine : expectedOutput) {
result.output.stripIndent().trim().contains(expectedOutputLine.stripIndent().trim())
}
assertEquals(SUCCESS, buildResult.task(":build").getOutcome())
assertEquals(SUCCESS, debloatResult.task(":debloat").getOutcome())

assertTrue(generatedDebloatedDependenciesDotGradle.exists())
FileUtils.forceDelete(new File(projectPath3 + "/build"))
}

private static BuildResult createRunner(File project, String argument) {
BuildResult result = GradleRunner.create()
.withProjectDir(project)
.withArguments(argument)
.build()
return result
}

private static boolean compareOutputs(File expectedOutputFile, File originalOutputFile) {

FileReader fileReader1 = new FileReader(expectedOutputFile)
FileReader fileReader2 = new FileReader(originalOutputFile)
BufferedReader reader1 = new BufferedReader(fileReader1)
BufferedReader reader2 = new BufferedReader(fileReader2)

String line1, line2
while (true) {
// Continue while there are equal lines
line1 = reader1.readLine()
line2 = reader2.readLine()

if (line1 == null) {
// End of file 1
return 1
}
if (!line1.trim().equalsIgnoreCase(line2.trim())) {
// Different lines, or end of file 2
return 0
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

> Task :debloat
Starting DepClean dependency analysis
-------------------------------------------------------
D E P C L E A N A N A L Y S I S R E S U L T S
-------------------------------------------------------
-------------------------------------------------------
USED DIRECT DEPENDENCIES [0]:
USED INHERITED DEPENDENCIES [0]:
USED TRANSITIVE DEPENDENCIES [0]:
POTENTIALLY UNUSED DIRECT DEPENDENCIES [1]:
com.fasterxml.jackson.core:jackson-databind:2.12.2:compile (1 MB)
POTENTIALLY UNUSED INHERITED DEPENDENCIES [0]:
POTENTIALLY UNUSED TRANSITIVE DEPENDENCIES [2]:
com.fasterxml.jackson.core:jackson-core:2.12.2:compile (356 KB)
com.fasterxml.jackson.core:jackson-annotations:2.12.2:compile (73 KB)
-------------------------------------------------------

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

> Task :debloat
Starting DepClean dependency analysis
-------------------------------------------------------
D E P C L E A N A N A L Y S I S R E S U L T S
-------------------------------------------------------
-------------------------------------------------------
USED DIRECT DEPENDENCIES [0]:
USED INHERITED DEPENDENCIES [0]:
USED TRANSITIVE DEPENDENCIES [0]:
POTENTIALLY UNUSED DIRECT DEPENDENCIES [1]:
com.fasterxml.jackson.core:jackson-databind:2.12.2:compile (1 MB)
POTENTIALLY UNUSED INHERITED DEPENDENCIES [0]:
POTENTIALLY UNUSED TRANSITIVE DEPENDENCIES [2]:
com.fasterxml.jackson.core:jackson-core:2.12.2:compile (356 KB)
com.fasterxml.jackson.core:jackson-annotations:2.12.2:compile (73 KB)
-------------------------------------------------------

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

> Task :debloat
Starting DepClean dependency analysis
-------------------------------------------------------
D E P C L E A N A N A L Y S I S R E S U L T S
-------------------------------------------------------
-------------------------------------------------------
USED DIRECT DEPENDENCIES [1]:
com.fasterxml.jackson.core:jackson-databind:2.12.2:compile (1 MB)
USED INHERITED DEPENDENCIES [0]:
USED TRANSITIVE DEPENDENCIES [2]:
com.fasterxml.jackson.core:jackson-core:2.12.2:compile (356 KB)
com.fasterxml.jackson.core:jackson-annotations:2.12.2:compile (73 KB)
POTENTIALLY UNUSED DIRECT DEPENDENCIES [0]:
POTENTIALLY UNUSED INHERITED DEPENDENCIES [0]:
POTENTIALLY UNUSED TRANSITIVE DEPENDENCIES [0]:
-------------------------------------------------------

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

> Task :debloat
Starting DepClean dependency analysis
-------------------------------------------------------
D E P C L E A N A N A L Y S I S R E S U L T S
-------------------------------------------------------
-------------------------------------------------------
USED DIRECT DEPENDENCIES [1]:
com.fasterxml.jackson.core:jackson-databind:2.12.2:compile (1 MB)
USED INHERITED DEPENDENCIES [0]:
USED TRANSITIVE DEPENDENCIES [2]:
com.fasterxml.jackson.core:jackson-core:2.12.2:compile (356 KB)
com.fasterxml.jackson.core:jackson-annotations:2.12.2:compile (73 KB)
POTENTIALLY UNUSED DIRECT DEPENDENCIES [0]:
POTENTIALLY UNUSED INHERITED DEPENDENCIES [0]:
POTENTIALLY UNUSED TRANSITIVE DEPENDENCIES [0]:
-------------------------------------------------------

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed

0 comments on commit 6ab6de9

Please sign in to comment.