Assertj-core: Path contains files

Created on 17 Sep 2018  路  10Comments  路  Source: assertj/assertj-core

        Path libs = project.getProjectDir().toPath().resolve( "build/libs" );
        Stream<Path> jars = Files.find(
                libs,
                1,
                ( path, basicFileAttributes ) -> path.getFileName().toFile().getName().matches( "working.*\\.jar" )
        );

        assertThat( jars ).hasSize( 1 );

this seems verbose, maybe we could write this (or something like it) instead?

```
assertThat( libs ).hasFileWithNameMatching("working.*\.jar");

All 10 comments

A Path can also be just a plain file, not a directory. How should the assertion react to this situation? Should it check the file if it matches the name?

Yes, probably, that would make sense. Probably looking for a way to test, like an "Ant Path" Matcher, just to see if the thing exists. My first path at method naming might be terrible.

@xenoterracide @valery1707 has started working on this issue (thanks!), his PR provides too many ways of checking that a Path contains some files, I would like to have a more minimalistic approach to avoid bloating the API. I need your feedback for that (and any users interested in this kind of assertions).

What we could have based on @valery1707 work:

  1. isDirectoryContaining(Predicate<Path> filter)
  2. isDirectoryContaining(String pathMatcherFilter)

The first assertions is super flexible as you decide exactly how to match the file/path to look for.
The second one is based on the PathMatcher syntax which supports the simple glob syntax but also regular expression as defined by the Pattern class.

I believe your assertion example could be written as:

assertThat(libs).isDirectoryContaining("working.*.jar");

I have renamed Valery assertion to isDirectoryContaining but I'm open to better suggestions.

Thoughts ?

nice! I wonder if it'd be better to write directoryContains seems more natural, but other than that I don't have much of an opinion. I think this'll be a much nicer way of testing path contents.

I would ideally use contains but as Path can be a file it would be ambiguous.

I still prefer isDirectoryContaining because I think it makes it clear the assertion is going to check that the given Path is a directory.

@xenoterracide Do you see a use case for checking that a directory does not contain a file? It seems to me to be a corner case.

only use case I can think of is if I was testing removal or renaming, ensuring that something that was there isn't there now. I'm not sure how common of a use case that is.

We'll leave these assertions out of the picture for now, it is still possible to check that a given path does not exist.

@joel-costigliola, negative assertions can be helpful for assert than directory does not contain many files.
Rather then write something like

Path directory;//Somehow calculated before
String version = "1.2.3" + (isSnapshot ? "-SNAPSHOT" : "");
assertThat(directory.resolve("bundle-name-" + version + ".jar")).doesNotExist();
assertThat(directory.resolve("bundle-name-" + version + ".md5")).doesNotExist();

we can write this:

Path directory;//Somehow calculated before
assertThat(directory)
    .doesNotDirectoryContains("glob:bundle-name-*.jar")
    .doesNotDirectoryContains("glob:bundle-name-*.md5");

@valery1707 fair enough but let's rename the assertions to isDirectoryNotContaining and only have one taking a String pathMatcherFilter and another with a Predicate.

@joel-costigliola good. Will fix soon

Was this page helpful?
0 / 5 - 0 ratings