npryce/hamkrest

Failing equalsTo assertion with java.nio.file.Path results in StackOverflowError

charleskorn opened this issue · 1 comments

If you have an assertion like this:

assertThat(somePath, equalTo(fileSystem.getPath("some.file")))

and the assertion fails (ie. somePath != fileSystem.getPath("some.file")), instead of failing with an assertion failed message, a StackOverflowError is thrown.

The stack trace is:

java.lang.StackOverflowError
        at sun.nio.fs.UnixPath.initOffsets(UnixPath.java:206)
        at sun.nio.fs.UnixPath.getName(UnixPath.java:319)
        at sun.nio.fs.UnixPath.getName(UnixPath.java:43)
        at sun.nio.fs.AbstractPath$1.next(AbstractPath.java:80)
        at sun.nio.fs.AbstractPath$1.next(AbstractPath.java:71)
        at com.natpryce.hamkrest.Describe.describe(describe.kt:43)
        at com.natpryce.hamkrest.Describe.describe(describe.kt:19)
        at com.natpryce.hamkrest.Describe.describe(describe.kt:19)
        at com.natpryce.hamkrest.Describe.describe(describe.kt:19)
        ...

It looks like the issue is that Path implements Iterable<Path>, and each of those returned Paths are themselves Iterable<Path>s, and so describe just keeps recursing. Path probably shouldn't be treated as an Iterable anyway, given that the standard string representation would be much easier to understand.

Fixed in master, by iterating over collections, not arbitrary iterables.