Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple plugin test #87

Merged
merged 11 commits into from
Feb 4, 2020
42 changes: 42 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
kotlin("jvm") version "1.3.61"
id("com.gradle.plugin-publish") version "0.10.1"
id("io.gitlab.arturbosch.detekt") version "1.4.0"
id("maven-publish")
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}

java {
Expand Down Expand Up @@ -53,6 +54,43 @@ pluginBundle {
}
}

configure<PublishingExtension> {
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
publications {
create<MavenPublication>("default") {
from(components.findByName("java"))

pom {
groupId = PublishingVersions.PLUGIN_GROUP
artifactId = PublishingVersions.PLUGIN_ARTIFACT
version = PublishingVersions.PLUGIN_VERSION

name.set("Swagger Gradle Codegen")
packaging = "jar"
description.set("Swagger Gradle Codegen")
url.set("https://github.com/Yelp/swagger-gradle-codegen")

scm {
url.set("https://github.com/Yelp/swagger-gradle-codegen")
}

licenses {
license {
name.set("Apache")
url.set("https://github.com/Yelp/swagger-gradle-codegen/blob/master/LICENSE")
}
}
}
}
}

repositories {
maven {
name = "pluginTest"
url = uri("file://${rootProject.buildDir}/localMaven")
}
}
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}

detekt {
toolVersion = "1.4.0"
input = files("src/")
Expand All @@ -69,3 +107,7 @@ tasks.jacocoTestReport {
tasks.check {
dependsOn(tasks.jacocoTestReport)
}

tasks.withType<Test> {
dependsOn("publishDefaultPublicationToPluginTestRepository")
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can safely replace with:

tasks.withType<Test> {
    dependsOn("publishToMavenLocal")
}

And use MavenLocal for the whole testing scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that too much since mavenLocal is global to the machine and might contain other artifacts that will be pulled in tests inadvertently. Doing everything inside the repo makes sure everything is contained.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing everything inside the repo makes sure everything is contained.

I see your point, but then having this in the test gradle file would probably suffice as well:

    mavenLocal {
      content {
        includeGroup("com.yelp.codegen")
      }
    }

This will make sure mavenLocal is used only for com.yelp.codegen and nothing else.

Copy link
Contributor Author

@martinbonnin martinbonnin Feb 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then there's the other side of the thing. Using mavenLocal will add the plugin under test to the global repo, which a user might end up pulling a few weeks later inadvertently when wanting to pull something else from mavenLocal.

Overall, I find adding

configure<PublishingExtension> {
    repositories {
        maven {
            name = "pluginTest"
            url = uri("file://${rootProject.buildDir}/localMaven")
        }
    }
}

is a ok price to pay for proper isolation.

}
28 changes: 28 additions & 0 deletions plugin/src/test/java/com/yelp/plugin/PluginTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.yelp.plugin

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Assert
import org.junit.Test
import java.io.File


class PluginTests {
@Test
fun `basic plugin test`() {
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
val tmpDir = File(".", "build/testProject")
tmpDir.deleteRecursively()

println(File(".").absolutePath)
File(".", "src/test/testProject").copyRecursively(tmpDir)

val result = GradleRunner.create().withProjectDir(tmpDir)
.forwardStdOutput(System.out.writer())
.forwardStdError(System.err.writer())
.withArguments("generateSwagger")
.build()

Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":generateSwagger")?.outcome)
}
}
26 changes: 26 additions & 0 deletions plugin/src/test/testProject/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import com.yelp.codegen.plugin.GenerateTaskConfiguration

buildscript {
repositories {
maven {
cortinico marked this conversation as resolved.
Show resolved Hide resolved
url = uri("../../../build/localMaven")
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}
jcenter()
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}
dependencies {
// Always take the latest version from the local test repo
// This is not super robust and we could find a way to communicate the version to the testProject
// by copying the root buildSrc to the testProject or extracting the version in a properties file
classpath("com.yelp.codegen:plugin:+")
}
}

apply(plugin = "com.yelp.codegen.plugin")

configure<GenerateTaskConfiguration> {
platform = "kotlin"
packageName = "com.yelp.codegen.samples"
// this file is in rootDir/plugin/build/testProject/
inputFile = file("../../../samples/junit-tests/junit_tests_specs.json")
outputDir = file("./build/generetadSources")
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions plugin/src/test/testProject/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name="testProject"
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved