Skip to content

UnknownFormatConversionException fix #12

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

Merged
merged 1 commit into from
Jun 14, 2019
Merged
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: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN addgroup -g 9000 -S code && \
adduser -S -G code app
USER app

COPY --from=builder /code/target/scala-2.12/codeclimate-scalastyle-assembly-0.1.0.jar /usr/src/app/engine-core.jar
COPY --from=builder /code/target/scala-2.12/codeclimate-scalastyle-assembly-*.jar /usr/src/app/engine-core.jar
COPY src/main/resources/docker /usr/src/app
COPY src/main/resources/docker/engine.json /

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "codeclimate-scalastyle"
organization in ThisBuild := "codeclimate"
version in ThisBuild := "0.1.0"
version in ThisBuild := "0.1.1"
scalaVersion in ThisBuild := "2.12.2"

concurrentRestrictions in Global += Tags.limit(Tags.Test, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import io.circe.syntax._
import org.scalastyle.{FileSpec, Message, MessageHelper, StyleError}

import scala.collection.JavaConverters._
import scala.util.{Failure, Success, Try}

class CodeClimateIssuePrinter(workspacePath: String, ps: PrintStream) {
class CodeClimateIssuePrinter(workspacePath: String, ps: PrintStream, errPrinter: PrintStream = Console.err) {
private val basePath = new File(workspacePath).toPath
private val printer = Printer.noSpaces.copy(dropNullKeys = true)

Expand All @@ -30,15 +31,24 @@ class CodeClimateIssuePrinter(workspacePath: String, ps: PrintStream) {
val msg: String = se.customMessage.orElse {
Some(messageHelper.message(se.key, se.args))
}.getOrElse("Error message not provided")
val issue = Issue(location = location,
description = String.format(msg, se.args.asJava),
check_name = Some(se.clazz.getName),
categories = Seq("Style"),
severity = Some("major")
)
val jsonStr = printer.pretty(issue.asJson)
ps.print(jsonStr)
ps.print("\0")
Try(String.format(msg, se.args.asJava)) match {
case Success(formattedMessage) =>
val issue = Issue(location = location,
description = formattedMessage,
check_name = Some(se.clazz.getName),
categories = Seq("Style"),
severity = Some("major")
)
val jsonStr = printer.pretty(issue.asJson)
ps.print(jsonStr)
ps.print("\0")
case Failure(exception) =>
// scalastyle:off
errPrinter.println(s"Unable to format the message. Invalid message format for key=${se.key}: '$msg';" +
s" args = '${se.args}'. Error: ${exception.printStackTrace()}"
)
// scalastyle:on
}
case _ => // ignore
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.codeclimate.scalastyle

import java.io.{BufferedOutputStream, PrintStream}

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream
import org.scalastyle.file.FileTabChecker
import org.scalastyle.{RealFileSpec, StyleError, _}
import org.scalatest.{FunSuite, Matchers}

class CodeClimateIssuePrinterTest extends FunSuite with Matchers {

private val fileSpec = new RealFileSpec("src/main/scala/org/codeclimate/scalastyle/CodeClimateIssuePrinter.scala", None)

test("testPrintIssue") {
val out = new ByteOutputStream()
val err = new ByteOutputStream()
val outPrinter = toPrintStream(out)
val errPrinter = toPrintStream(err)
val printer = new CodeClimateIssuePrinter(".", outPrinter, errPrinter)

printer.printIssue(new StyleError[RealFileSpec](fileSpec, classOf[FileTabChecker],
new FileTabChecker().errorKey, ErrorLevel, List.empty[String], Some(0)))

outPrinter.flush()
errPrinter.flush()

out.size() should not be 0
err.size() should be(0)
}

test("testPrintIssue bad format") {
val out = new ByteOutputStream()
val err = new ByteOutputStream()
val outPrinter = toPrintStream(out)
val errPrinter = toPrintStream(err)
val printer = new CodeClimateIssuePrinter(".", outPrinter, errPrinter)

val errKey = new FileTabChecker().errorKey
printer.printIssue(new StyleError[RealFileSpec](fileSpec, classOf[FileTabChecker],
errKey, ErrorLevel, List.empty[String], Some(0), customMessage = Some(
"This is badly formatted custom message %'*'"
)))

outPrinter.flush()
errPrinter.flush()

out.size() should be(0)
err.size() should not be 0
err.toString should include(s"Invalid message format for key=$errKey")
}

private def toPrintStream(out: ByteOutputStream): PrintStream = {
new PrintStream(new BufferedOutputStream(out))
}
}
2 changes: 1 addition & 1 deletion src/main/resources/docker/engine.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"email": "jsippel@acorns.com"
},
"languages" : ["Scala"],
"version": "0.1.0",
"version": "0.1.1",
"spec_version": "0.3.1"
}