Skip to content

Commit

Permalink
Apply scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich authored and rolandtritsch committed Oct 1, 2024
1 parent 0ade5a7 commit f37dea6
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 64 deletions.
25 changes: 15 additions & 10 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ generateXMLFiles := {
val dir = (Test / resourceDirectory).value
val pwd = (run / baseDirectory).value

val template = if (System.getProperty("os.name").startsWith("Windows"))
".xml.windows.template"
else
".xml.template"
val template =
if (System.getProperty("os.name").startsWith("Windows"))
".xml.windows.template"
else
".xml.template"

dir.listFiles { (_, name) => name.endsWith(template) }.foreach {
templateFile =>
Expand All @@ -30,16 +31,20 @@ prepareScripted := {
val pwd = (run / baseDirectory).value

val submodules = "git submodule status" !! log
val submodulePaths = submodules.split('\n').map{x =>
val submodulePaths = submodules.split('\n').map { x =>
x.split(" ")(2)
}

submodulePaths.foreach{ subModulePath =>
submodulePaths.foreach { subModulePath =>
val path = pwd / ".git" / "modules" / subModulePath
val pathFixedForWindows = if (System.getProperty("os.name").startsWith("Windows"))
path.absolutePath.replace(File.separator, "/") // Git under Windows uses / for path separator
else
path.absolutePath
val pathFixedForWindows =
if (System.getProperty("os.name").startsWith("Windows"))
path.absolutePath.replace(
File.separator,
"/"
) // Git under Windows uses / for path separator
else
path.absolutePath
val destination = file(subModulePath) / ".git"
IO.delete(destination)
IO.write(destination, s"gitdir: $pathFixedForWindows")
Expand Down
25 changes: 15 additions & 10 deletions src/main/scala/org/scoverage/coveralls/CIService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ trait TravisBase extends CIService {
val currentBranch: Option[String] = sys.env.get("CI_BRANCH")

// If a user token exists, use it; otherwise, Travis doesn't seem to need a token at all
override def coverallsAuth(userRepoToken: Option[String]): Option[CoverallsAuth] =
override def coverallsAuth(
userRepoToken: Option[String]
): Option[CoverallsAuth] =
Some(userRepoToken.fold[CoverallsAuth](NoTokenNeeded)(CoverallsRepoToken))
}

Expand All @@ -40,15 +42,16 @@ case object GitHubActions extends CIService {

// https://github.com/coverallsapp/github-action/blob/master/src/run.ts#L31-L40
val pullRequest: Option[String] = for {
eventName <- sys.env.get("GITHUB_EVENT_NAME") if eventName.startsWith("pull_request")
eventName <- sys.env.get("GITHUB_EVENT_NAME")
if eventName.startsWith("pull_request")
payloadPath <- sys.env.get("GITHUB_EVENT_PATH")
prNumber <- getPrNumber(payloadPath)
} yield prNumber

// https://docs.github.com/en/actions/learn-github-actions/environment-variables
val currentBranch: Option[String] = pullRequest match {
case Some(_) => sys.env.get("GITHUB_HEAD_REF")
case None => sys.env.get("GITHUB_REF_NAME")
case None => sys.env.get("GITHUB_REF_NAME")
}

def getPrNumber(payloadPath: String): Option[String] = {
Expand All @@ -61,24 +64,25 @@ case object GitHubActions extends CIService {

lines match {
case Some(ls) => getFromJson(ls, "number")
case None => None
case None => None
}

}

def getFromJson(lines: String, element: String): Option[String] = {
parser.parse(lines) match {
case Right(json) => {
case Right(json) =>
json.findAllByKey(element) match {
case prNumber :: nil => Some(prNumber.toString)
case _ => None
case prNumber :: _ => Some(prNumber.toString)
case _ => None
}
}
case Left(_) => None
}
}

override def coverallsAuth(userRepoToken: Option[String]): Option[CoverallsAuth] = {
override def coverallsAuth(
userRepoToken: Option[String]
): Option[CoverallsAuth] = {
userRepoToken match {
case Some(token) if token.matches("gh._.+") =>
// The token passed in COVERALLS_REPO_TOKEN is a GitHub token (legacy behavior)
Expand All @@ -98,6 +102,7 @@ case object GitHubActions extends CIService {
object CircleCI extends CIService {
def name: String = "circleci"
def jobId: Option[String] = sys.env.get("CIRCLE_BUILD_NUM")
def pullRequest: Option[String] = sys.env.get("CIRCLE_PULL_REQUEST").map(_.split("/").last)
def pullRequest: Option[String] =
sys.env.get("CIRCLE_PULL_REQUEST").map(_.split("/").last)
def currentBranch: Option[String] = sys.env.get("CIRCLE_BRANCH")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class CoberturaMultiSourceReader(
sourceEncoding: Option[String]
)(implicit log: Logger) {
log.debug(
s"sbt-coveralls: CobertaMultiSourceReader: coberturaFile: ${coberturaFile}"
s"sbt-coveralls: CobertaMultiSourceReader: coberturaFile: $coberturaFile"
)
log.debug(
s"sbt-coveralls: CobertaMultiSourceReader: sourceDirs: ${sourceDirs}"
s"sbt-coveralls: CobertaMultiSourceReader: sourceDirs: $sourceDirs"
)

require(sourceDirs.nonEmpty, "Given empty sequence of source directories")
Expand Down Expand Up @@ -69,10 +69,10 @@ class CoberturaMultiSourceReader(

def sourceFiles: Set[File] = {
log.debug(
s"sbt-coveralls: CobertaMultiSourceReader: sourceFiles: sourceFilesRelative: ${sourceFilesRelative}"
s"sbt-coveralls: CobertaMultiSourceReader: sourceFiles: sourceFilesRelative: $sourceFilesRelative"
)
log.debug(
s"sbt-coveralls: CobertaMultiSourceReader: sourceFiles: sourceDirs: ${sourceDirs}"
s"sbt-coveralls: CobertaMultiSourceReader: sourceFiles: sourceDirs: $sourceDirs"
)
val sfs = for {
relativePath <- sourceFilesRelative
Expand All @@ -82,7 +82,7 @@ class CoberturaMultiSourceReader(
if sourceFile.exists
} yield sourceFile
log.debug(
s"sbt-coveralls: CobertaMultiSourceReader: sourceFiles: sourceFiles: ${sfs}"
s"sbt-coveralls: CobertaMultiSourceReader: sourceFiles: sourceFiles: $sfs"
)
sfs
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/scala/org/scoverage/coveralls/CoverallsAuth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ import io.circe.generic.auto._
*/
sealed trait CoverallsAuth

/** Auth strategy where a Coveralls-specific token is used. Works
* with every CI service.
/** Auth strategy where a Coveralls-specific token is used. Works with every CI
* service.
*/
case class CoverallsRepoToken(token: String) extends CoverallsAuth

/** Auth strategy where a token specific to the CI service is used,
* such as a GitHub token. Works on selected CI services supported
* by Coveralls.
/** Auth strategy where a token specific to the CI service is used, such as a
* GitHub token. Works on selected CI services supported by Coveralls.
*/
case class CIServiceToken(token: String) extends CoverallsAuth

/** Auth strategy where no token is passed. This seems to work
* for Travis.
/** Auth strategy where no token is passed. This seems to work for Travis.
*/
case object NoTokenNeeded extends CoverallsAuth
6 changes: 3 additions & 3 deletions src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ object CoverallsPlugin extends AutoPlugin {

val coverallsAuthOpt = coverallsService.value match {
case Some(ciService) => ciService.coverallsAuth(repoToken)
case None => repoToken.map(CoverallsRepoToken)
case None => repoToken.map(CoverallsRepoToken)
}

val coverallsAuth = coverallsAuthOpt.getOrElse {
Expand Down Expand Up @@ -131,13 +131,13 @@ object CoverallsPlugin extends AutoPlugin {
)

val fileReports =
reader.sourceFilenames.par.map(reader.reportForSource(_)).seq
reader.sourceFilenames.par.map(reader.reportForSource).seq

log.info(
s"sbt-coveralls: Adding file reports to the coveralls file (${coverallsFile.value.getName}) ..."
)

fileReports.foreach(writer.addSourceFile(_))
fileReports.foreach(writer.addSourceFile)

writer.end()

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/scoverage/coveralls/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.io.File
object Utils {
def mkFileFromPath(path: Seq[String]): File = {
require(path.nonEmpty, "path cannot be empty")
val (p :: ps) = path
val p :: ps = path
mkFileFromPath(new File(p), ps)
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/scala/org/scoverage/coveralls/CIServiceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class CIServiceTest extends AnyWordSpec with Matchers {
|}
|""".stripMargin

GitHubActions.getFromJson(lines, "numericField") shouldBe Some("123") }
GitHubActions.getFromJson(lines, "numericField") shouldBe Some("123")
}
}

"getPrNumber" should {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class CoberturaMultiSourceReaderTest
val sourceDirA =
Utils.mkFileFromPath(resourceDir, Seq("projectA", "src", "main", "scala"))
val sourceDirA212 =
Utils.mkFileFromPath(resourceDir, Seq("projectA", "src", "main", "scala-2.12"))
Utils.mkFileFromPath(
resourceDir,
Seq("projectA", "src", "main", "scala-2.12")
)
val sourceDirB =
Utils.mkFileFromPath(resourceDir, Seq("projectB", "src", "main", "scala"))
val sourceDirs = Seq(sourceDirA, sourceDirA212, sourceDirB)
Expand Down Expand Up @@ -180,7 +183,7 @@ class CoberturaMultiSourceReaderTest
val sourcePath = Seq("bar", "foo", "TestSourceFile.scala")
val sourceFile = Utils.mkFileFromPath(sourceDirA, sourcePath)
val (source, file) = reader.splitPath(sourceFile)
source shouldEqual sourceDirA.getCanonicalPath()
source shouldEqual sourceDirA.getCanonicalPath
file shouldEqual sourcePath.mkString(File.separator)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ class CoverallPayloadWriterTest
writer: Writer,
tokenIn: Option[String],
service: Option[CIService],
parallel: Boolean,
parallel: Boolean
): (CoverallPayloadWriter, Writer) = {
val payloadWriter = new CoverallPayloadWriter(
new File(".").getAbsoluteFile,
new File("."),
service.flatMap(_.coverallsAuth(tokenIn)).getOrElse(CoverallsRepoToken(tokenIn.get)),
service
.flatMap(_.coverallsAuth(tokenIn))
.getOrElse(CoverallsRepoToken(tokenIn.get)),
service,
parallel,
testGitClient
Expand Down Expand Up @@ -72,10 +74,10 @@ class CoverallPayloadWriterTest
false
)

payloadWriter.start
payloadWriter.start()
payloadWriter.flush()

println(writer.toString())
println(writer.toString)

writer.toString should equal(
"""{"repo_token":"testRepoToken","service_name":"my-service","service_job_id":"testServiceJob","parallel":false,""" +
Expand All @@ -86,9 +88,14 @@ class CoverallPayloadWriterTest

"generate a correct starting payload without a CI service" in {
val (payloadWriter, writer) =
coverallsWriter(new StringWriter(), Some("testRepoToken"), None, false)
coverallsWriter(
new StringWriter(),
Some("testRepoToken"),
None,
false
)

payloadWriter.start
payloadWriter.start()
payloadWriter.flush()

writer.toString should equal(
Expand Down Expand Up @@ -116,7 +123,7 @@ class CoverallPayloadWriterTest
false
)

payloadWriter.start
payloadWriter.start()
payloadWriter.flush()

writer.toString should equal(
Expand Down Expand Up @@ -147,18 +154,31 @@ class CoverallPayloadWriterTest
)
payloadWriter.addSourceFile(
SourceFileReport(
sourceFile.getPath(),
sourceFile.getPath,
List(Some(1), None, Some(2))
)
)
payloadWriter.flush()

val separator = if (System.getProperty("os.name").startsWith("Windows"))
s"""${File.separator}\\""" // Backwards slash is a special character in JSON so it needs to be escaped
else
File.separator

val name = List(".","src","test","resources","projectA","src","main","scala","bar","foo","TestSourceFile.scala")
val separator =
if (System.getProperty("os.name").startsWith("Windows"))
s"""${File.separator}\\""" // Backwards slash is a special character in JSON so it needs to be escaped
else
File.separator

val name = List(
".",
"src",
"test",
"resources",
"projectA",
"src",
"main",
"scala",
"bar",
"foo",
"TestSourceFile.scala"
)
.mkString(separator)
writer.toString should equal(
s"""{"name":"$name","source_digest":"B77361233B09D69968F8C62491A5085F","coverage":[1,null,2]}"""
Expand All @@ -173,16 +193,17 @@ class CoverallPayloadWriterTest
false
)

payloadWriter.start
payloadWriter.start()
payloadWriter.end()

writer.toString should endWith("]}")
}

"include parallel correctly" in {
val (payloadWriter, writer) = coverallsWriter(new StringWriter(), Some("testRepoToken"), None, true)
val (payloadWriter, writer) =
coverallsWriter(new StringWriter(), Some("testRepoToken"), None, true)

payloadWriter.start
payloadWriter.start()
payloadWriter.flush()

writer.toString should equal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import scalaj.http.Http
import scalaj.http.HttpOptions._

class CoverallsClientTest
extends AnyWordSpec
with BeforeAndAfterAll
with Matchers
{
extends AnyWordSpec
with BeforeAndAfterAll
with Matchers {
val projectDir = Utils.mkFileFromPath(Seq("."))
val resourceDir =
Utils.mkFileFromPath(projectDir, Seq("src", "test", "resources"))
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/org/scoverage/coveralls/GitClientTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GitClientTest extends AnyWordSpec with BeforeAndAfterAll with Matchers {
storedConfig.save()
// Add and commit a file
val readme = new File(repoDir, "README.md")
readme.createNewFile();
readme.createNewFile()
gitRepo
.add()
.addFilepattern("README.md")
Expand Down
Loading

0 comments on commit f37dea6

Please sign in to comment.