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

Refresh excluding packages and files #443

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $ sbt coverageAggregate
**NOTE**: You do not need to run `coverageReport` before `coverageAggregate`; it
aggregates over the sub-projects' coverage data directly, not the report xml.

### Exclude classes and packages
### Exclude classes and packages and files

You can exclude classes from being considered for coverage measurement by
providing semicolon-separated list of regular expressions.
Expand All @@ -75,6 +75,15 @@ The regular expressions are matched against the fully qualified class name, and
must match the entire string to take effect. Any matched classes will not be
instrumented or included in the coverage report.

You can also exclude files and file paths.

```scala
coverageExcludedFiles := ".*\\/two\\/GoodCoverage\.scala;.*\\/three\\/.*"
```

Note: This only works for Scala2. Right now Scala3 does not support
a way to exclude packages or files from being instrumented.

You can also mark sections of code with comments like:

```scala
Expand Down Expand Up @@ -118,7 +127,6 @@ Can also be set through the sbt set directive
set coverageDataDir := file("/tmp")
```


## Trouble-shooting failing tests

scoverage does a lot of file writing behind the scenes in order to track which
Expand Down
13 changes: 13 additions & 0 deletions src/sbt-test/scoverage/coverage-excluded-files/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version := "0.1"

scalaVersion := "2.13.6"

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test

coverageExcludedFiles := ".*\\/two\\/GoodCoverage\\.scala"

resolvers ++= {
if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT")))
Seq(Resolver.sonatypeRepo("snapshots"))
else Seq.empty
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding a test for excludedFiles.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.6.2
16 changes: 16 additions & 0 deletions src/sbt-test/scoverage/coverage-excluded-files/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
val pluginVersion = sys.props.getOrElse(
"plugin.version",
throw new RuntimeException(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
)

addSbtPlugin("org.scoverage" % "sbt-scoverage" % pluginVersion)

resolvers ++= {
if (pluginVersion.endsWith("-SNAPSHOT"))
Seq(Resolver.sonatypeRepo("snapshots"))
else
Seq.empty
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object GoodCoverage {

def sum(num1: Int, num2: Int) = {
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package two

object GoodCoverage {

def sum(num1: Int, num2: Int) = {
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import munit.FunSuite

/** Created by tbarke001c on 7/8/14.
*/
class GoodCoverageSpec extends FunSuite {

test("GoodCoverage should sum two numbers") {
assertEquals(GoodCoverage.sum(1, 2), 3)
assertEquals(GoodCoverage.sum(0, 3), 3)
assertEquals(GoodCoverage.sum(3, 0), 3)
}

test("two.GoodCoverage should sum two numbers") {
assertEquals(two.GoodCoverage.sum(1, 2), 3)
assertEquals(two.GoodCoverage.sum(0, 3), 3)
assertEquals(two.GoodCoverage.sum(3, 0), 3)
}

}
7 changes: 7 additions & 0 deletions src/sbt-test/scoverage/coverage-excluded-files/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# run scoverage using the coverage task
> clean
> coverage
> test
> coverageReport
# There should be no directory for the excluded files
-$ exists target/scala-2.13/scoverage-report/two
13 changes: 13 additions & 0 deletions src/sbt-test/scoverage/coverage-excluded-packages/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version := "0.1"

scalaVersion := "2.13.6"

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test

coverageExcludedPackages := "two.*"

resolvers ++= {
if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT")))
Seq(Resolver.sonatypeRepo("snapshots"))
else Seq.empty
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding test for excludedPackages.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.6.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
val pluginVersion = sys.props.getOrElse(
"plugin.version",
throw new RuntimeException(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
)

addSbtPlugin("org.scoverage" % "sbt-scoverage" % pluginVersion)

resolvers ++= {
if (pluginVersion.endsWith("-SNAPSHOT"))
Seq(Resolver.sonatypeRepo("snapshots"))
else
Seq.empty
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object GoodCoverage {

def sum(num1: Int, num2: Int) = {
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package two

object GoodCoverage {

def sum(num1: Int, num2: Int) = {
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import munit.FunSuite

/** Created by tbarke001c on 7/8/14.
*/
class GoodCoverageSpec extends FunSuite {

test("GoodCoverage should sum two numbers") {
assertEquals(GoodCoverage.sum(1, 2), 3)
assertEquals(GoodCoverage.sum(0, 3), 3)
assertEquals(GoodCoverage.sum(3, 0), 3)
}

test("two.GoodCoverage should sum two numbers") {
assertEquals(two.GoodCoverage.sum(1, 2), 3)
assertEquals(two.GoodCoverage.sum(0, 3), 3)
assertEquals(two.GoodCoverage.sum(3, 0), 3)
}

}
7 changes: 7 additions & 0 deletions src/sbt-test/scoverage/coverage-excluded-packages/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# run scoverage using the coverage task
> clean
> coverage
> test
> coverageReport
# There should be no directory for the excluded package
-$ exists target/scala-2.13/scoverage-report/two
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.6.2
2 changes: 1 addition & 1 deletion src/sbt-test/scoverage/coverage-off/test
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
> coverageOff
> test
# There should be no scoverage-data directory
-$ exists target/scala-2.12/scoverage-data
-$ exists target/scala-2.13/scoverage-data
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixing typo, Making test work (again).

13 changes: 13 additions & 0 deletions src/sbt-test/scoverage/scala3-coverage-excluded-files/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version := "0.1"

scalaVersion := "3.2.0-RC1"

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test

coverageExcludedFiles := ".*\\/two\\/GoodCoverage\\.scala"

resolvers ++= {
if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT")))
Seq(Resolver.sonatypeRepo("snapshots"))
else Seq.empty
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.6.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
val pluginVersion = sys.props.getOrElse(
"plugin.version",
throw new RuntimeException(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
)

addSbtPlugin("org.scoverage" % "sbt-scoverage" % pluginVersion)

resolvers ++= {
if (pluginVersion.endsWith("-SNAPSHOT"))
Seq(Resolver.sonatypeRepo("snapshots"))
else
Seq.empty
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object GoodCoverage {

def sum(num1: Int, num2: Int) = {
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package two

object GoodCoverage {

def sum(num1: Int, num2: Int) = {
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import munit.FunSuite

/** Created by tbarke001c on 7/8/14.
*/
class GoodCoverageSpec extends FunSuite {

test("GoodCoverage should sum two numbers") {
assertEquals(GoodCoverage.sum(1, 2), 3)
assertEquals(GoodCoverage.sum(0, 3), 3)
assertEquals(GoodCoverage.sum(3, 0), 3)
}

test("two.GoodCoverage should sum two numbers") {
assertEquals(two.GoodCoverage.sum(1, 2), 3)
assertEquals(two.GoodCoverage.sum(0, 3), 3)
assertEquals(two.GoodCoverage.sum(3, 0), 3)
}

}
9 changes: 9 additions & 0 deletions src/sbt-test/scoverage/scala3-coverage-excluded-files/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# run scoverage using the coverage task
> clean
> coverage
> test
> coverageReport
# There should be no directory for the excluded files
#-$ exists target/scala-3.2.0-RC1/scoverage-report/two
# But right now there is, because Scala3 does not support excluding files
$ exists target/scala-3.2.0-RC1/scoverage-report/two
Copy link
Member Author

Choose a reason for hiding this comment

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

Just to show/document that excluding files (and packages) is not working right now.

Aiming to make this work (somehow).