Skip to content

Commit

Permalink
invoking gradle works
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Aug 16, 2023
1 parent 56aa3b2 commit 13fc528
Show file tree
Hide file tree
Showing 27 changed files with 698 additions and 58 deletions.
26 changes: 26 additions & 0 deletions .run/All Tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All Tests" type="TestNG">
<shortenClasspath name="NONE" />
<useClassPathOnly />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="ALTERNATIVE_JRE_PATH" value="11" />
<option name="SUITE_NAME" value="" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="GROUP_NAME" value="" />
<option name="TEST_OBJECT" value="PACKAGE" />
<option name="VM_PARAMETERS" value="-ea " />
<option name="PARAMETERS" value="" />
<option name="OUTPUT_DIRECTORY" value="" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="wholeProject" />
</option>
<option name="PROPERTIES_FILE" value="" />
<properties />
<listeners />
<method v="2">
<option name="Maven.BeforeRunTask" enabled="true" file="$PROJECT_DIR$//pom.xml" goal="install -DskipTests" />
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.antwerkz.build.maven

import org.testng.Assert.assertTrue
import org.testng.annotations.Test

class GradleInvocationTest : MavenTester() {
@Test
fun invokeGradle() {
val testDir = initProject("projects/gradleInvoke")
val (_, welcome) = setupAndInvoke(testDir)
var string = welcome.toLogFormat()
assertTrue(welcome.contains("[INFO] > hello, graven!"), string)

val (_, packaging) = setupAndInvoke(testDir, goals = listOf("package"))
string = packaging.toLogFormat()
assertTrue(packaging.contains("[INFO] > hello, graven!"), string)
assertTrue(packaging.contains("[INFO] > working hard"), string)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package com.antwerkz.build.maven

import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.io.PrintStream
import java.lang.System.getProperty
import java.nio.charset.StandardCharsets
import java.util.Properties
import java.util.function.Predicate
import java.util.regex.Pattern
import org.apache.maven.model.Model
import org.apache.maven.model.io.xpp3.MavenXpp3Reader
import org.apache.maven.shared.invoker.DefaultInvocationRequest
import org.apache.maven.shared.invoker.DefaultInvoker
import org.apache.maven.shared.invoker.InvocationRequest
import org.apache.maven.shared.invoker.InvocationResult
import org.apache.maven.shared.invoker.Invoker
import org.apache.maven.shared.invoker.InvokerLogger
import org.apache.maven.shared.invoker.PrintStreamLogger
import org.codehaus.plexus.util.FileUtils.copyDirectoryStructure
import org.slf4j.LoggerFactory
import org.testng.Assert.assertFalse
Expand Down Expand Up @@ -124,10 +130,41 @@ open class MavenTester {
return invoker
}

protected fun setupAndInvoke(
testDir: File,
goals: List<String> = listOf("test-compile"),
quiet: Boolean = false,
params: Properties = Properties()
): Pair<InvocationResult, List<String>> {
val output = mutableListOf<String>()
val request: InvocationRequest = DefaultInvocationRequest()
request.isBatchMode = true
request.isDebug = false
request.isShowErrors = true
request.properties = params
request.setQuiet(quiet)
request.goals = goals
request.setOutputHandler { line -> output += line }

val invoker = initInvoker(testDir)
invoker.logger =
PrintStreamLogger(
PrintStream(
FileOutputStream(File(testDir, "maven-${testDir.name}.log")),
true,
"UTF-8"
),
InvokerLogger.DEBUG
)
return invoker.execute(request) to output
}

val env: Map<String, String>
get() {
val env = mutableMapOf<String, String>()
getProperty("mavenOpts")?.let { env["MAVEN_OPTS"] = it }
return env
}
}

fun List<String>.toLogFormat() = joinToString("\n", prefix = "\n")
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package com.antwerkz.build.maven

import java.io.File
import java.io.FileOutputStream
import java.io.PrintStream
import java.nio.charset.Charset
import java.util.Properties
import org.apache.maven.shared.invoker.DefaultInvocationRequest
import org.apache.maven.shared.invoker.InvocationRequest
import org.apache.maven.shared.invoker.InvocationResult
import org.apache.maven.shared.invoker.InvokerLogger
import org.apache.maven.shared.invoker.PrintStreamLogger
import org.testng.Assert.assertEquals
import org.testng.Assert.assertTrue
import org.testng.annotations.Test

class VersionReplacementTest : MavenTester() {
class ReplacementMojoTest : MavenTester() {
@Test
fun doubleQuoteUpdates() {
val testDir = initProject("projects/doubleQuotes")

val result = setupAndInvoke(testDir)
val (result, output) = setupAndInvoke(testDir)

assertEquals(result.exitCode, 0)
assertEquals(result.exitCode, 0, output.toLogFormat())
val lines = File(testDir, "build.gradle.kts").readLines(Charset.forName("UTF-8"))

assertTrue(lines.any { it.contains("classpath(\"org.apache.maven:maven-model:3.9.1\")") })
Expand All @@ -37,9 +29,9 @@ class VersionReplacementTest : MavenTester() {
fun noRegex() {
val testDir = initProject("projects/noRegex")

val result = setupAndInvoke(testDir)
val (result, output) = setupAndInvoke(testDir)

assertEquals(result.exitCode, 0)
assertEquals(result.exitCode, 0, output.toLogFormat())
val lines = File(testDir, "build.gradle.kts").readLines(Charset.forName("UTF-8"))

assertTrue(lines.any { it.contains("classpath(\"org.apache.maven:maven-model:2.3.1\")") })
Expand All @@ -55,7 +47,7 @@ class VersionReplacementTest : MavenTester() {
fun properties() {
val testDir = initProject("projects/properties")

val result = setupAndInvoke(testDir)
val (result, output) = setupAndInvoke(testDir)

assertEquals(result.exitCode, 0)
val lines = File(testDir, "gradle.properties").readLines(Charset.forName("UTF-8"))
Expand All @@ -68,9 +60,9 @@ class VersionReplacementTest : MavenTester() {
fun singleQuoteUpdates() {
val testDir = initProject("projects/singleQuotes")

val result = setupAndInvoke(testDir)
val (result, output) = setupAndInvoke(testDir)

assertEquals(result.exitCode, 0)
assertEquals(result.exitCode, 0, output.toLogFormat())
val lines = File(testDir, "build.gradle").readLines(Charset.forName("UTF-8"))

assertTrue(
Expand All @@ -83,26 +75,4 @@ class VersionReplacementTest : MavenTester() {
)
assertTrue(lines.any { it.contains("kotlin('jvm') version '1.8.10'") })
}

private fun setupAndInvoke(testDir: File, params: Properties = Properties()): InvocationResult {
val invoker = initInvoker(testDir)

val request: InvocationRequest = DefaultInvocationRequest()
request.isBatchMode = true
request.isDebug = false
request.isShowErrors = true
request.properties = params
request.goals = listOf("test-compile")
val logger =
PrintStreamLogger(
PrintStream(
FileOutputStream(File(testDir, "maven-${testDir.name}.log")),
true,
"UTF-8"
),
InvokerLogger.DEBUG
)
invoker.logger = logger
return invoker.execute(request)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<executions>
<execution>
<goals>
<goal>graven</goal>
<goal>sync</goal>
</goals>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.antwerkz.build

class DoubleQuotes
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
buildscript {
dependencies {
classpath("org.apache.maven:maven-model:2.3.1")
classpath("com.fasterxml.jackson.core:jackson-databind:1.0.0")
}
}

plugins {
id("dev.morphia.critter") version "${findProperty("critter.version")}"
kotlin("jvm") version "1.6.0"
`maven-publish`
}

repositories {
mavenLocal()
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
}

dependencies {
implementation("dev.morphia.morphia:morphia-core:1.2.3")
testImplementation("org.testng:testng:${findProperty("testng.version")}")
testImplementation("org.testcontainers:mongodb:${findProperty("testcontainers.version")}")
}

tasks.register("welcome") {
doLast {
println("hello, graven!")
}
}

tasks.register("packaging") {
doLast {
println("working hard")
}
}

tasks {
test {
useTestNG()
}

critter {
}
}

tasks.withType(JavaCompile::class.java) {
options.compilerArgs = listOf("-parameters")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 0.1.0-SNAPSHOT
morphia.version = 123
critter.version = 123
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Loading

0 comments on commit 13fc528

Please sign in to comment.