0

I have a very similar issue as sbt test encoding hickup but since the answer does not apply and my case is in Scala code I ask here.

I have a string containing non ASCII characters in a unit test. This test is working fine on Linux, and fine on Windows when run from IntelliJ. However, when run from a Windows shell with sbt test they fail. If I print the string humanité it is displayed as humanitΘ in the failing case. The file encoding is UTF-8.

println(new java.io.InputStreamReader(System.in).getEncoding) returns UTF8 when run from IntelliJ, and Cp1252 from the shell. I tried various things to change the encoding:

  • run sbt "-Dfile.encoding=UTF-8" test
  • check that scalacOptions defined in my build.sbt contains "-encoding", "UTF8"

But the default encoding is always Cp1252 (maybe that's normal?) and the test keeps failing.

The failing code is the following:

val stringToEncrypt = "l'humanité"
println(test)

From IntelliJ I get:

l'humanité

From a windows shell running sbt:

l'humanitΘ
CanardMoussant
  • 913
  • 8
  • 22

1 Answers1

0

In order to avoid problems with default charset for OS, you can pass explicitly the desired charset when creating the InputStream:

new java.io.InputStreamReader(System.in,"UTF-8")
Edmondo
  • 19,559
  • 13
  • 62
  • 115
  • The code with InputStreamReader was just here to point out my encoding issue. The root issue is that it looks like sbt when run from the shell does not pick the right encoding when compiling my scala source code. – CanardMoussant Feb 27 '18 at 09:24
  • We found that to avoid this case, the best thing is to pass the encoding explicitly: we had this problem when running the tests with docker – Edmondo Feb 27 '18 at 11:47
  • not sure what you mean then, how do you specify the encoding of a String you create? – CanardMoussant Feb 27 '18 at 14:43
  • https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[],%20java.nio.charset.Charset) – Edmondo Feb 27 '18 at 17:11
  • I am not sure I understand what you mean, I will enhance the question to make it more clear. – CanardMoussant Feb 28 '18 at 10:38