Skip to content

Commit 434f896

Browse files
committed
πŸ› fix: do not include agent if scope is only Test
1 parent 7e7c76e commit 434f896

File tree

13 files changed

+102
-1
lines changed

13 files changed

+102
-1
lines changed

β€Žsrc/main/scala/com/lightbend/sbt/javaagent/JavaAgent.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ object JavaAgent extends JavaAgent {
4444
val inCompile = scope.compile || confs.contains(Compile.name) || confs.contains(Provided.name)
4545
val inRun = scope.run || inCompile || confs.contains(Runtime.name)
4646
val inTest = scope.test || confs.contains(Test.name)
47-
val inDist = scope.dist
47+
// Note: in order to not be a breaking change, dist scope defaults to true unless scope is test only
48+
val inDist = scope.dist && !(confs.contains(Test.name) && confs.size == 1)
4849
val configuration = if (inCompile) Provided else AgentConfig
4950
val reconfiguredModule = Modules.withConfigurations(module, Some(configuration.name))
5051
val configuredScope = AgentScope(compile = inCompile, test = inTest, run = inRun, dist = inDist)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
lazy val agentTest = project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging)
2+
3+
javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props(
4+
"project.version"
5+
) % "dist;test"
6+
7+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version"))
2+
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
object Main extends App
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Test {
2+
@org.junit.Test def test(): Unit = ()
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> stage
2+
> check
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
TaskKey[Unit]("check") := {
2+
3+
assert(
4+
(Test / javaOptions).value exists (s =>
5+
s.contains("-javaagent:") && s.contains("maxwell")
6+
),
7+
"Test / javaOptions do not contain 'maxwell' agent"
8+
)
9+
10+
assert(
11+
!((Test / fullClasspath).value exists (f =>
12+
f.data.name.contains("maxwell")
13+
)),
14+
"maxwell test agent is available on the test run class path"
15+
)
16+
17+
// Check that the agent is included in dist
18+
19+
assert(
20+
(Universal / mappings).value exists { case (file, path) =>
21+
path == "maxwell/maxwell.jar"
22+
},
23+
"dist mappings do not include 'maxwell/maxwell.jar'"
24+
)
25+
26+
import scala.sys.process._
27+
val output =
28+
((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!!
29+
30+
assert(
31+
output contains "Agent 86",
32+
"output does not include 'Agent 86'"
33+
)
34+
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
lazy val agentTest = project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging)
2+
3+
javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props(
4+
"project.version"
5+
) % "test"
6+
7+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version"))
2+
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
object Main extends App

0 commit comments

Comments
Β (0)