scalacenter / scalafix

Refactoring and linting tool for Scala

Home Page:https://scalacenter.github.io/scalafix/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

testkit: (unclear) error when the test input contains a license header

jrudolph opened this issue · comments

We get the below error while running simple scalafix tests. This error happens during parsing an error (uhoh).

Looking into the real error reveals this:

com.typesafe.config.ConfigException$Parse: String: 2: expecting a close parentheses ')' here, not: '*' (Reserved character '*' is not allowed outside quotes)
  | => oat com.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:201)
        at com.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:197)
	at com.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseKey(ConfigDocumentParser.java:279)
	at com.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseObject(ConfigDocumentParser.java:450)
	at com.typesafe.config.impl.ConfigDocumentParser$ParseContext.parse(ConfigDocumentParser.java:648)
	at com.typesafe.config.impl.ConfigDocumentParser.parse(ConfigDocumentParser.java:14)
	at com.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:262)
	at com.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:250)
	at com.typesafe.config.impl.Parseable.parseValue(Parseable.java:180)
	at com.typesafe.config.impl.Parseable.parseValue(Parseable.java:174)
	at com.typesafe.config.impl.Parseable.parse(Parseable.java:301)
	at com.typesafe.config.ConfigFactory.parseString(ConfigFactory.java:1187)
	at com.typesafe.config.ConfigFactory.parseString(ConfigFactory.java:1197)
	at metaconfig.typesafeconfig.TypesafeConfig2Class$.$anonfun$gimmeConfFromStringFilename$1(scala.scala:17)

because the generated file contains this line:

scalacOptions=-deprecation|-encoding|UTF-8|-unchecked|-Ywarn-dead-code|-Wconf\:msg\=object JavaConverters in package collection is deprecated\:s|-Wconf\:msg\=is deprecated \\(since 2\\.13\\.\:s|-Wconf\:cat\=deprecation&msg\=since Akka 2\\.6\\.\:s|-Wconf\:cat\=deprecation&msg\=since Akka HTTP 10\\.2\\.\:s|-Wconf\:msg\=reached max recursion depth\:s|-release\:8|-Xlint|-Wconf\:cat\=unused-imports&origin\=org.apache.pekko.http.ccompat.*\:s|-Wconf\:cat\=other-match-analysis&msg\=match may not be exhaustive\:s

Solution: Quote strings properly in the generated hocon config. Fail if quoting is not possible.

Reported error:

[info] - org/apache/pekko/http/fix/MigrateToServerBuilderTest.scala *** FAILED *** (588 milliseconds)
[info]   java.util.NoSuchElementException: next on empty iterator
[info]   at scala.collection.Iterator$$anon$19.next(Iterator.scala:973)
[info]   at scala.collection.Iterator$$anon$19.next(Iterator.scala:971)
[info]   at scala.collection.StringOps$$anon$1.next(StringOps.scala:697)
[info]   at scala.collection.StringOps$$anon$1.next(StringOps.scala:695)
[info]   at metaconfig.Position.pretty(Position.scala:25)
[info]   at metaconfig.ConfError$$anon$8.<init>(ConfError.scala:131)
[info]   at metaconfig.ConfError$.parseError(ConfError.scala:131)
[info]   at metaconfig.typesafeconfig.TypesafeConfig2Class$.gimmeSafeConf(TypesafeConfig2Class.scala:68)
[info]   at metaconfig.typesafeconfig.TypesafeConfig2Class$.gimmeConfFromStringFilename(TypesafeConfig2Class.scala:17)
[info]   at metaconfig.Hocon$.fromInput(Hocon.scala:10)
[info]   at metaconfig.Input$InputImplicits.parse(Input.scala:104)
[info]   at metaconfig.Conf$.parseString(Conf.scala:70)
[info]   at scalafix.testkit.RuleTest$.$anonfun$fromPath$1(RuleTest.scala:36)
[info]   at scalafix.testkit.AbstractSemanticRuleSuite.evaluateTestBody(AbstractSemanticRuleSuite.scala:37)
[info]   at scalafix.testkit.AbstractSemanticRuleSuite.$anonfun$runOn$1(AbstractSemanticRuleSuite.scala:97)

The issue was something else: in the target input file for the test the /* rule = ... */ comment was below the license headers which led to the issue because the conf parser then tried to parse the license header...

Thanks for the report!

The issue was something else: in the target input file for the test the /* rule = ... */ comment was below the license headers which led to the issue because the conf parser then tried to parse the license header...

😅 I renamed the issue accordingly.

The heuristic to detect the semantic comment is indeed quite basic:

def findTestkitComment(tokens: Tokens): Token = {
tokens
.find { x =>
x.is[Token.Comment] && x.syntax.startsWith("/*")
}
.getOrElse {
val input = tokens.headOption.fold("the file")(_.input.syntax)
throw new IllegalArgumentException(
s"Missing /* */ comment at the top of $input"
)
}
}

I guess a reasonable fix would be to iterate on all comments until a valid HOCON syntax with a rule or rules key defined is found.

Created a PR at #1832