Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How simulate system users for testing file system interactions in ruby?

Tags:

tdd

ruby

rspec

Im am quite happy to have found FakeFS to be able to fake a file system sandbox in which my tests can mess around. Now I want to be able to test test FileUtils.chown and chmod operations and therefor want to work with fake users in the test context and not tie my stuff to the operation systems user database to have fully portable and discrete tests.

I do my testing with rspec.

What would be the best way to do that?

like image 384
jo. Avatar asked Jul 20 '26 18:07

jo.


1 Answers

I'm not sure I get the question, but I assume you want to test the "real" results of the FileUtils calls, i.e. changed permissions and ownership of files, that's why you need some kind of users. I wouldn't do that, since "SELECT isn't broken": http://pragmatictips.com/26

Instead, assume the following: if you call FileUtils.chown and friends with the right parameters, they will do the right thing. To make sure your application passes the correct parameters to FileUtils, use a mock. Here's an example: https://gist.github.com/phillipoertel/5020102

If you don't want to couple your test so closely to the implementation (the mock knows what's going on in your class internally) you could test the changed permissions by expecting it's effects. For example: if @user wasn't allowed to access @file before, call the method which changes permissions, then call one of your methods which require the changed permissions and assert that works. But this approach will access the filesystem, which you didn't want in the first place.

like image 163
Phillip Avatar answered Jul 22 '26 08:07

Phillip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!