Skip to content

🐛 fix: do not include agent if scope is only Test #37

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ This will automatically resolve the agent module, bundle the agent artifact in t

By default, sbt-javaagent will only add an agent to distributions. Agents can be optionally enabled for compile, run, or test.

> **Note**: starting from v0.2.0, if the agent is only in `test` scope, it won't be added do distributions. You can set the scope to `dist;test` to retain the previous behaviour.
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 assume this PR would be released as a new minor, feel free to change this line if you prefer to release it as bugfix.

Copy link
Member

Choose a reason for hiding this comment

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

We need to release at least a new minor version because in main branch we also dropped support for SBT 0.13.
Will take a look at this pr soon


The following scopes are supported:

* **dist** — bundle the agent in production distributions and add a `-javaagent` option to start scripts
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/com/lightbend/sbt/javaagent/JavaAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ object JavaAgent extends JavaAgent {
val inCompile = scope.compile || confs.contains(Compile.name) || confs.contains(Provided.name)
val inRun = scope.run || inCompile || confs.contains(Runtime.name)
val inTest = scope.test || confs.contains(Test.name)
val inDist = scope.dist
// Note: in order to not be a breaking change, dist scope defaults to true unless scope is test only
val inDist = scope.dist && !(confs.contains(Test.name) && confs.size == 1)
val configuration = if (inCompile) Provided else AgentConfig
val reconfiguredModule = Modules.withConfigurations(module, Some(configuration.name))
val configuredScope = AgentScope(compile = inCompile, test = inTest, run = inRun, dist = inDist)
Expand Down
7 changes: 7 additions & 0 deletions src/sbt-test/agent/test_and_dist/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
lazy val agentTest = project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging)

javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props(
"project.version"
) % "dist;test"

libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
2 changes: 2 additions & 0 deletions src/sbt-test/agent/test_and_dist/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version"))
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version"))
1 change: 1 addition & 0 deletions src/sbt-test/agent/test_and_dist/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object Main extends App
3 changes: 3 additions & 0 deletions src/sbt-test/agent/test_and_dist/src/test/scala/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Test {
@org.junit.Test def test(): Unit = ()
}
2 changes: 2 additions & 0 deletions src/sbt-test/agent/test_and_dist/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> stage
> check
35 changes: 35 additions & 0 deletions src/sbt-test/agent/test_and_dist/test.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
TaskKey[Unit]("check") := {

assert(
(Test / javaOptions).value exists (s =>
s.contains("-javaagent:") && s.contains("maxwell")
),
"Test / javaOptions do not contain 'maxwell' agent"
)

assert(
!((Test / fullClasspath).value exists (f =>
f.data.name.contains("maxwell")
)),
"maxwell test agent is available on the test run class path"
)

// Check that the agent is included in dist

assert(
(Universal / mappings).value exists { case (file, path) =>
path == "maxwell/maxwell.jar"
},
"dist mappings do not include 'maxwell/maxwell.jar'"
)

import scala.sys.process._
val output =
((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!!

assert(
output contains "Agent 86",
"output does not include 'Agent 86'"
)

}
7 changes: 7 additions & 0 deletions src/sbt-test/agent/test_no_dist/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
lazy val agentTest = project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging)

javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props(
"project.version"
) % "test"

libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
2 changes: 2 additions & 0 deletions src/sbt-test/agent/test_no_dist/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version"))
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version"))
1 change: 1 addition & 0 deletions src/sbt-test/agent/test_no_dist/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object Main extends App
3 changes: 3 additions & 0 deletions src/sbt-test/agent/test_no_dist/src/test/scala/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Test {
@org.junit.Test def test(): Unit = ()
}
2 changes: 2 additions & 0 deletions src/sbt-test/agent/test_no_dist/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> stage
> check
35 changes: 35 additions & 0 deletions src/sbt-test/agent/test_no_dist/test.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
TaskKey[Unit]("check") := {

assert(
(Test / javaOptions).value exists (s =>
s.contains("-javaagent:") && s.contains("maxwell")
),
"Test / javaOptions do not contain 'maxwell' agent"
)

assert(
!((Test / fullClasspath).value exists (f =>
f.data.name.contains("maxwell")
)),
"maxwell test agent is available on the test run class path"
)

// Check that the agent is not included in dist

assert(
(Universal / mappings).value forall { case (file, path) =>
path != "maxwell/maxwell.jar"
},
"dist mappings include 'maxwell/maxwell.jar'"
)

import scala.sys.process._
val output =
((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!!

assert(
!(output contains "Agent 86"),
"output include 'Agent 86'"
)

}