From 5bc871a79afc693ca08bf6109416ce019a919cb1 Mon Sep 17 00:00:00 2001 From: Petr Pucil Date: Sun, 10 Mar 2024 01:00:54 +0100 Subject: [PATCH] ErrorMessagesSpec: fix broken tests on Windows In 2b84e0a1e29712304debf709c143d9505e3062ca, I reverted a previous change that was replacing all backslashes `\` to forward slashes `/` in file paths used in error messages (meaning that backslashes are kept since then if present in input paths). However, until now I didn't notice that this broke `ErrorMessagesSpec` tests on Windows. This is because compile error messages actually include the full path to the `.ksy` spec, for example: ``` ../tests/formats_err/attr_bad_size.ksy: /seq/0/size: error: invalid type: expected integer, got CalcStrType ``` However, in `formats_err` specs, only the base name is specified (see https://github.com/kaitai-io/kaitai_struct_tests/blob/73486eef/formats_err/attr_bad_size.ksy below): ``` # attr_bad_size.ksy: /seq/0/size: # error: invalid type: expected integer, got CalcStrType # meta: id: attr_bad_size seq: # ... ``` The `../tests/formats_err/` prefix in the actual message is supposed to be removed by `.replace(FORMATS_ERR_DIR + "/", "")` in `ErrorMessagesSpec` (line 53). Note that the string to be removed always uses forward slashes `/`. The problem is that on Windows, `f.toString` (which returns a file path with the backslash `\` as name separator - this is explained at https://github.com/kaitai-io/kaitai_struct/issues/507#issuecomment-1679686055) is passed to the compiler as the .ksy spec to process. Since the path normalization (i.e. `\` -> `/` conversion) was removed, error messages will start like `..\tests\formats_err\attr_bad_size.ksy: ...`. This means the `.replace(FORMATS_ERR_DIR + "/", "")` call won't do anything, because it looks for ``../tests/formats_err/`, which is not there. The fix is to pass forward slash `/` paths to the compiler even on Windows. --- jvm/src/test/scala/io/kaitai/struct/ErrorMessagesSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jvm/src/test/scala/io/kaitai/struct/ErrorMessagesSpec.scala b/jvm/src/test/scala/io/kaitai/struct/ErrorMessagesSpec.scala index aa9861bf9..14a9794b4 100644 --- a/jvm/src/test/scala/io/kaitai/struct/ErrorMessagesSpec.scala +++ b/jvm/src/test/scala/io/kaitai/struct/ErrorMessagesSpec.scala @@ -38,7 +38,7 @@ class ErrorMessagesSpec extends AnyFunSuite with SimpleMatchers { def testOne(f: File): Unit = { val fileName = f.getName val testName = fileName.stripSuffix(".ksy") - val fn = f.toString + val fn = FORMATS_ERR_DIR + "/" + fileName test(testName) { val expected = getExpected(fn) val (_, problems) = JavaKSYParser.localFileToSpecs(fn, DEFAULT_CONFIG)