Skip to content

Commit

Permalink
Add support for "integration test" sbt#10
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherif Ebady committed May 30, 2018
1 parent 90ef573 commit a49b25e
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright (c) 2012-2014 Joachim Hofer & contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package de.johoop.testngplugin

import sbt._

private[testngplugin] abstract class BaseTestNGPlugin extends AutoPlugin {

object autoImport {
val testNGVersion = SettingKey[String](
"testng-version",
"the version of TestNG to use")

val testNGOutputDirectory = SettingKey[String](
"testng-output-directory",
"the directory where the test results will be written to by TestNG")

val testNGParameters = SettingKey[Seq[String]](
"testng-parameters",
"additional parameters to TestNG")

val testNGSuites = SettingKey[Seq[String]](
"testng-suites",
"the suite definition files (YAML or XML) that will be run by TestNG")

val testNGInterfaceVersion = SettingKey[String](
"testngInterfaceVersion")

val testNGSnakeyamlVersion = SettingKey[String](
"testngSnakeyamlVersion",
"the version of Snakeyaml to use")
}

lazy val testngSources: Array[Byte] = {
val artifactId = TestNGPluginBuildInfo.interfaceName + "_2.12"
val src = url(s"https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/${TestNGPluginBuildInfo.organization}/${artifactId}/${TestNGPluginBuildInfo.version}/srcs/${artifactId}-sources.jar")
IO.withTemporaryDirectory { dir =>
val f = dir / "temp.jar"
sbt.io.Using.urlInputStream(src) { in =>
IO.transfer(in, f)
}
IO.readBytes(f)
}
}

override def requires = plugins.JvmPlugin

}
77 changes: 77 additions & 0 deletions plugin/src/main/scala/de/johoop/testngplugin/TestNGItPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Copyright (c) 2018 Joachim Hofer & contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package de.johoop.testngplugin

import java.io.ByteArrayInputStream
import sbt._
import sbt.Keys._

object TestNGItPlugin extends BaseTestNGPlugin {

import autoImport._

override lazy val projectSettings: Seq[Def.Setting[_]] = Seq(
resolvers += Resolver.sbtPluginRepo("releases"), // why is that necessary, and why like that?

testNGVersion := (testNGVersion ?? TestNGPluginBuildInfo.testngVersion).value,
testNGSnakeyamlVersion := (testNGSnakeyamlVersion ?? "1.17").value,
testNGInterfaceVersion := (testNGInterfaceVersion ?? TestNGPluginBuildInfo.version).value,
testNGOutputDirectory := (crossTarget.value / "testng").absolutePath,
testNGParameters := Seq(),
testNGSuites := Seq(((resourceDirectory in IntegrationTest).value / "testng.yaml").absolutePath),

libraryDependencies ++= Seq(
"org.testng" % "testng" % testNGVersion.value % "test,it",
"org.yaml" % "snakeyaml" % testNGSnakeyamlVersion.value % "test,it"
),

libraryDependencies += {
if (TestNGPluginBuildInfo.preCompiledInterfaceVersions.contains(scalaBinaryVersion.value)) {
TestNGPluginBuildInfo.organization %% TestNGPluginBuildInfo.interfaceName % testNGInterfaceVersion.value % "test,it"
} else {
"org.scala-sbt" % "test-interface" % "1.0" % "test,it"
}
},

sourceGenerators in IntegrationTest += Def.task {
val dir = (sourceManaged in IntegrationTest).value
if (TestNGPluginBuildInfo.preCompiledInterfaceVersions.contains(scalaBinaryVersion.value)) {
Nil
} else {
IO.unzipStream(new ByteArrayInputStream(testngSources), dir).toList.filter(_.getName endsWith "scala")
}
},

testFrameworks += TestNGFrameworkID,

testOptions += Tests.Argument(
TestNGFrameworkID, ("-d" +: testNGOutputDirectory.value +: testNGParameters.value) ++ testNGSuites.value: _*
)
)

lazy val TestNGFrameworkID = new TestFramework("de.johoop.testnginterface.TestNGFramework")

}
43 changes: 2 additions & 41 deletions plugin/src/main/scala/de/johoop/testngplugin/TestNGPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,12 @@ import java.io.ByteArrayInputStream
import sbt._
import sbt.Keys._

object TestNGPlugin extends AutoPlugin {

object autoImport {
val testNGVersion = SettingKey[String](
"testng-version",
"the version of TestNG to use")

val testNGOutputDirectory = SettingKey[String](
"testng-output-directory",
"the directory where the test results will be written to by TestNG")

val testNGParameters = SettingKey[Seq[String]](
"testng-parameters",
"additional parameters to TestNG")

val testNGSuites = SettingKey[Seq[String]](
"testng-suites",
"the suite definition files (YAML or XML) that will be run by TestNG")

val testNGInterfaceVersion = SettingKey[String](
"testngInterfaceVersion")

val testNGSnakeyamlVersion = SettingKey[String](
"testngSnakeyamlVersion",
"the version of Snakeyaml to use")
}
object TestNGPlugin extends BaseTestNGPlugin {

import autoImport._

private[this] lazy val testngSources: Array[Byte] = {
val artifactId = TestNGPluginBuildInfo.interfaceName + "_2.12"
val src = url(s"https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/${TestNGPluginBuildInfo.organization}/${artifactId}/${TestNGPluginBuildInfo.version}/srcs/${artifactId}-sources.jar")
IO.withTemporaryDirectory { dir =>
val f = dir / "temp.jar"
sbt.io.Using.urlInputStream(src) { in =>
IO.transfer(in, f)
}
IO.readBytes(f)
}
}

override def requires = plugins.JvmPlugin

override lazy val projectSettings: Seq[Def.Setting[_]] = Seq(
resolvers += Resolver.sbtPluginRepo("releases"), // why is that necessary, and why like that?
resolvers += Resolver.sbtPluginRepo("releases"), // why is that necessary, and why like that?

testNGVersion := (testNGVersion ?? TestNGPluginBuildInfo.testngVersion).value,
testNGSnakeyamlVersion := (testNGSnakeyamlVersion ?? "1.17").value,
Expand Down
9 changes: 9 additions & 0 deletions plugin/src/sbt-test/testng/it/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
lazy val root = (project in file("."))
.configs(IntegrationTest)
.settings(
Defaults.itSettings
)

enablePlugins(TestNGItPlugin)

crossScalaVersions := Seq("2.10.6", "2.11.11", "2.12.3")
1 change: 1 addition & 0 deletions plugin/src/sbt-test/testng/it/project/plugin.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("de.johoop" % "sbt-testng-plugin" % System.getProperty("plugin.version"))
5 changes: 5 additions & 0 deletions plugin/src/sbt-test/testng/it/src/it/resources/testng.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Foo
tests:
- name: Baz
classes:
- example.ITest1
17 changes: 17 additions & 0 deletions plugin/src/sbt-test/testng/it/src/it/scala/example/ITest1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package example;

import org.testng.annotations.Test
import java.nio.file.Files
import java.nio.file.Paths
import scala.util.Properties

class ITest1 {

@Test
def foo(): Unit = {
val version = Properties.versionNumberString
val path = Paths.get(version + ".txt")
Files.write(path, version.getBytes("UTF-8"))
}

}
4 changes: 4 additions & 0 deletions plugin/src/sbt-test/testng/it/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
> + it:test
$ exists 2.10.6.txt
$ exists 2.11.11.txt
$ exists 2.12.3.txt

0 comments on commit a49b25e

Please sign in to comment.