Mocking and specing command line (cli) execution, file operations etc.?

What are the options for creating specs for file operations, executing
commands in the CLI etc.?

If I build a generator or something which runs a lot of things in the
command line, how do I check the results of these operations, fx
mocking file system updates and/or mocking the STDOUT/STDIN from the
terminal?

There must be some add-on libraries or options for this out there to
make it easy to accomplish.

  • Kristian

On Jun 6, 2010, at 7:42 PM, Kristian M. wrote:

What are the options for creating specs for file operations, executing
commands in the CLI etc.?

If I build a generator or something which runs a lot of things in the
command line, how do I check the results of these operations, fx
mocking file system updates and/or mocking the STDOUT/STDIN from the
terminal?

There must be some add-on libraries or options for this out there to
make it easy to accomplish.

Check out Fakefs (GitHub - fakefs/fakefs: A fake filesystem. Use it in your tests.) for file operations.

There’s a cucumber library called aruba
(GitHub - aslakhellesoy/aruba: CLI Steps for Cucumber) that was extracted from spec
suites for rspec and cucumber specifically to deal with command line
applications and their output. It’s got cucumber step definitions, but
there is a separate api file that has methods you should be able to use
to help with this stuff if you’re using rspec. Or, you could just use
cucumber for that stuff as well.

HTH,
David

Kristian M. wrote:

In addition to aruba if you want to just use RSpec with StringIO objects
you might find this example group handy:

The example group assumes you follow the “runner” pattern where your
Runner class has a ::run method that takes args, out_stream,
error_stream. Many libraries use this pattern including Cucumber and
RSpec. The RSpec Book CLI app is setup this way as well if you want to
see an in-depth example.

In general, for simple CLIs aruba is probably the way to go though.
This example group is probably best if you want/need to use mocking more
to avoid expensive operations.

HTH,
Ben