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

Enable colored output for Scala 2 #2950

Merged
merged 2 commits into from
Jan 6, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package example

trait Show[A]

object Show {
implicit def option[A](implicit s: Show[A]): Show[Option[A]] = ???
}

object Example {
def main(args: Array[String]): Unit =
println(implicitly[Show[Option[String]]])
}
33 changes: 30 additions & 3 deletions scalalib/test/src/mill/scalalib/HelloWorldTests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mill.scalalib

import java.io.ByteArrayOutputStream
import java.io.{ByteArrayOutputStream, PrintStream}
import java.util.jar.JarFile
import scala.jdk.CollectionConverters._
import scala.util.{Properties, Using}
Expand Down Expand Up @@ -283,6 +283,16 @@ object HelloWorldTests extends TestSuite {
}
}

object HelloWorldColorOutput extends HelloBase {
object core extends ScalaModule {
def scalaVersion = scala213Version

override def scalacOptions = super.scalacOptions() ++ Seq(
"-Vimplicits"
)
}
}

object HelloScalacheck extends HelloBase {
object foo extends ScalaModule {
def scalaVersion = scala212Version
Expand Down Expand Up @@ -420,9 +430,10 @@ object HelloWorldTests extends TestSuite {
m: TestUtil.BaseModule,
resourcePath: os.Path = resourcePath,
env: Map[String, String] = Evaluator.defaultEnv,
debug: Boolean = false
debug: Boolean = false,
errStream: PrintStream = System.err
)(t: TestEvaluator => T)(implicit tp: TestPath): T = {
val eval = new TestEvaluator(m, env = env, debugEnabled = debug)
val eval = new TestEvaluator(m, env = env, debugEnabled = debug, errStream = errStream)
os.remove.all(m.millSourcePath)
os.remove.all(eval.outPath)
os.makeDir.all(m.millSourcePath / os.up)
Expand Down Expand Up @@ -1189,6 +1200,22 @@ object HelloWorldTests extends TestSuite {
assert(evalCount > 0)
}
}
"color-output" - {
val errStream = new ByteArrayOutputStream()
workspaceTest(
HelloWorldColorOutput,
os.pwd / "scalalib" / "test" / "resources" / "hello-world-color-output",
errStream = new PrintStream(errStream, true)
) { eval =>
val Left(Result.Failure("Compilation failed", _)) =
eval.apply(HelloWorldColorOutput.core.compile)
val output = errStream.toString
assert(output.contains(s"${Console.RED}!${Console.RESET}${Console.BLUE}I"))
assert(output.contains(
s"${Console.GREEN}example.Show[scala.Option[java.lang.String]]${Console.RESET}"
))
}
}

"scalacheck" - workspaceTest(
HelloScalacheck,
Expand Down
4 changes: 4 additions & 0 deletions scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ class ZincWorkerImpl(
stampReader = Stamps.timeWrapBinaryStamps(converter)
)

val scalaColorProp = "scala.color"
val previousScalaColor = sys.props.get(scalaColorProp).getOrElse("auto")
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

This means, we will later "reset" it to a new value "auto" instead of the original value, in case it was not defined. I'd rather reset to the original, which means we need to unset when it was unset before.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree with you.

try {
sys.props(scalaColorProp) = if (ctx.log.colored) "true" else "false"
val newResult = ic.compile(
in = inputs,
logger = logger
Expand All @@ -557,6 +560,7 @@ class ZincWorkerImpl(
} finally {
reporter.foreach(r => sources.foreach(r.fileVisited(_)))
reporter.foreach(_.finish())
sys.props(scalaColorProp) = previousScalaColor
}
}

Expand Down