Skip to content

Commit

Permalink
ErrorMessagesSpec: fix broken tests on Windows
Browse files Browse the repository at this point in the history
In 2b84e0a, 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
kaitai-io/kaitai_struct#507 (comment))
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.
  • Loading branch information
generalmimon committed Mar 10, 2024
1 parent 490757e commit 5bc871a
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5bc871a

Please sign in to comment.