yeoman-assert icon indicating copy to clipboard operation
yeoman-assert copied to clipboard

Exclusive assert for files?

Open nessthehero opened this issue 9 years ago • 8 comments
trafficstars

Is there a way to do an assert.file() but check if only the files I specify are in the directory?

As in, if any files I don't mention exist, fail the test.

nessthehero avatar Mar 04 '16 20:03 nessthehero

That could be a new assertion method.

What's the use case?

SBoudrias avatar Mar 04 '16 20:03 SBoudrias

My team uses a yeoman generator that pulls in files from other repositories, as a way of managing those parts of the project separately. E.g. we can update the default sass for the generator without updating the root code of the actual generator.

Every time I make changes, I need to update the tests that make sure all the files were put in the right place. However, I am human, so sometimes I forget to add new files to the asserts. So the tests pass but I'm not actually accounting for the files.

It's weird now that I'm thinking about it, but I think it could be useful for other purposes, like detecting unwanted files or file-copy mistakes.

nessthehero avatar Mar 04 '16 20:03 nessthehero

This could be possible with the already existing api if glob was used instead of path-exists

eddiemonge avatar Mar 04 '16 21:03 eddiemonge

@eddiemonge sorry for the "from the grave" revival of this thread, but I just wanted to be sure of what I'm gathering from your response. Are you saying that my use-case is already possible using existing functions, or are you just remarking that it would be possible to implement?

nessthehero avatar Sep 22 '16 10:09 nessthehero

I was remarking on the implementation. At the time (probably currently as well), file checks were done using path-exists. If glob was swapped in instead, then you could use glob's matching and negating features

eddiemonge avatar Sep 23 '16 03:09 eddiemonge

Forget what I said about glob, you would have to know what you don't want to see, at which point you would probably use noFile.

What would you expect the API for this to look like? I am thinking it might be too complex.

assert.onlyFilesInDirectory([files], dir);

eddiemonge avatar Dec 08 '16 21:12 eddiemonge

I'd like to chime in on this and say that I have a similar use-case to @nessthehero.

I'm also pulling in files from different repositories. He mentioned adding new files, but forgetting to test for them, which I've experienced.

I'd also add that some of the repos that my generator pulls in have files that need to be included elsewhere, but not in the generator. If there are more than a few of these, using assert.noFile() can be unwieldy, especially if the names of those files change.

I think it'd be much simpler to maintain if I could stick to specifying only the positive assertions, and have the tests fail if additional, unwanted files are present.

As far as the API, I think it would make more sense to have the directory come first, and the files array come second: assert.onlyFilesInDir(dir, [files]), since that would reflect the order of the actual file paths.

Edit: The assertion name could also possibly be shortened from onlyFilesInDirectory to just only or onlyFiles.

assert.onlyFiles('path/to/dir', [
  'file1.txt',
  'file3.txt',
]);

deimyts avatar Feb 23 '17 19:02 deimyts