-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make report be printed in a single print command, refactor printing code
- Loading branch information
Showing
2 changed files
with
100 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 60 additions & 3 deletions
63
src/test/scala/org/jmotor/sbt/out/UpdatesPrinterSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,78 @@ | ||
package org.jmotor.sbt.out | ||
|
||
import org.jmotor.sbt.out.UpdatesPrinter.wrap | ||
import sbt.* | ||
import org.jmotor.sbt.dto.{ModuleStatus, Status} | ||
import org.jmotor.sbt.out.UpdatesPrinter.* | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import java.io.{ByteArrayOutputStream, PrintStream} | ||
import java.nio.charset.StandardCharsets | ||
|
||
/** Component: Description: Date: 2018/2/26 | ||
* | ||
* @author | ||
* AI | ||
*/ | ||
class UpdatesPrinterSpec extends AnyFunSuite { | ||
class UpdatesPrinterSpec extends AnyFunSuite with Matchers{ | ||
|
||
test("print report") { | ||
val consoleOut = captureConsoleOut( | ||
printReporter( | ||
"mainModule", | ||
Seq( | ||
ModuleStatus("oraganization" % "name" % "0.0.1", Status.Success), | ||
ModuleStatus("oraganization" % "name" % "0.0.1", Status.Unreleased, "1.0.0") | ||
), | ||
Seq(ModuleStatus("oraganization" % "name" % "0.0.1", Status.Expired, "1.0.0")), | ||
Seq(ModuleStatus("oraganization" % "name" % "0.0.1", Status.Unreleased, "1.0.0")) | ||
) | ||
) | ||
|
||
consoleOut.uncolor shouldBe | ||
s"""| mainModule${" "} | ||
|[info] -------------------------------- Global Plugins -------------------------------- | ||
|[expired ] oraganization:name:0.0.1 ---> 1.0.0 | ||
|[info] ----------------------------------- Plugins ------------------------------------ | ||
|[success ] oraganization:name:0.0.1 √ | ||
|[unreleased] oraganization:name:0.0.1 ---> 1.0.0 | ||
|[info] --------------------------------- Dependencies --------------------------------- | ||
|[unreleased] oraganization:name:0.0.1 ---> 1.0.0 | ||
|""".stripMargin | ||
} | ||
|
||
test("print layout") { | ||
val width = 80 | ||
val t1 = s"[info] ${wrap("Global Plugins", "-", width)}" | ||
val t2 = s"[info] ${wrap(" Plugins", "-", width)}" | ||
val t2 = s"[info] ${wrap("Plugins", "-", width)}" | ||
val t3 = s"[info] ${wrap("Dependencies", "-", width)}" | ||
assert(t1.length == t2.length && t2.length == t3.length) | ||
} | ||
|
||
test("status line") { | ||
val t1 = statusLine(ModuleStatus("oraganization" % "name" % "0.0.1", Status.Success)) | ||
val t2 = statusLine(ModuleStatus("oraganization" % "name" % "0.0.1", Status.Expired, "1.0.0")) | ||
val t3 = statusLine(ModuleStatus("oraganization" % "name" % "0.0.1", Status.Error)) | ||
val t4 = statusLine(ModuleStatus("oraganization" % "name" % "0.0.1", Status.Unreleased, "1.0.0")) | ||
|
||
t1.uncolor shouldBe "[success ] oraganization:name:0.0.1 √" | ||
t2.uncolor shouldBe "[expired ] oraganization:name:0.0.1 ---> 1.0.0" | ||
t3.uncolor shouldBe "[error ] oraganization:name:0.0.1 updates error, please retry!" | ||
t4.uncolor shouldBe "[unreleased] oraganization:name:0.0.1 ---> 1.0.0" | ||
} | ||
|
||
def captureConsoleOut(f: => Any): String = { | ||
val outBuffer = new ByteArrayOutputStream() | ||
val interceptionStream = new PrintStream(outBuffer, false, StandardCharsets.UTF_8) | ||
|
||
scala.Console.withOut(interceptionStream)(f) | ||
|
||
val output = new String(outBuffer.toByteArray, StandardCharsets.UTF_8) | ||
output | ||
} | ||
|
||
implicit class StringImplicits(s: String) { | ||
def uncolor: String = s.replaceAll("\u001B\\[[;\\d]*m", "") | ||
} | ||
|
||
} |