Dr Nic

hash bang cucumber

I don’t know if this is a good idea or not, but often I just wish I could copy and paste a cucumber feature file into the command line and have it just run the frigging scenarios without having to prefix it with “cucumber”.

running scenarios directly from command line

Perhaps I’m a bit delirious but I think it would be fun to paste features/112_users_crud.feature into the console and it would run the scenarios:

$ features/users_crud.feature

instead of having to always do the extra key strokes:

$ cucumber features/users_crud.feature

Solution?

Two steps:

  1. At the very top of each feature file add: #!/usr/bin/env cucumber
  2. Run chmod +x features/*.feature

Shazam!

Hash bang me up!

To apply this to all your feature files, jump into script/console or irb and run the following code within your project:

Dir["**/*.feature"].each do |feature|
  contents = File.read(feature)
  File.open(feature, "w") do |f|
    f << "#!/usr/bin/env cucumber\n\n"
    f << contents
  end
  `chmod +x "#{feature}"`
end

Related posts:

  1. Cucumber: building a better World (object) How to write helper libraries for your Cucumber step definitions...
  2. Testing outbound emails with Cucumber My testimonial for Cucumber still stands even in 2009....

13 Responses to “hash bang cucumber”

  1. [...] This post was mentioned on Twitter by eladmeidar. eladmeidar said: 1. hash bang cucumber 2. … 3. profit! http://bit.ly/BwvzJ (via @drnic) [...]

  2. wuputah says:

    Alternatively or additionally, you could alias cucumber to ‘c’ or ‘cu’ in your shell environment.

  3. seems dangerous… how about an alias for cucumber? I have ‘cuke.’

  4. Dr Nic says:

    Danger is one of my middle names. Another middle name is “um, that looks unnecessary”.

    Yes, the ‘c’ alias might be better.

  5. Dr Nic says:

    No no no, I love pasting and hitting return. Oh I love it so.

  6. Joseph Wilk says:

    Very nifty!

    I’ve been using another approach (as I was keen to keep the feature files pure). I created a formatter which extends the pretty formatter but in the comment line it outputs:

    Scenario: We like cukes #c features/blah.feature:20

    Where c is aliased to cucumber.

  7. sr says:

    with zsh: alias -s feature=”cucumber”

  8. I did something similar using Fish Shell. I’ve whipped up a blog post explaining it for anyone who uses Fish. Instead of making each feature file executable, it does the equivalent of Ruby’s “method_missing” — if a command given to fish isn’t runnable it’ll call a special script (which you write) which can then decide what to do.

    Here’s the blog post: http://bjeanes.com/2009/10/07/using-fish-shells-event-system-to-behave-like-method-missing

  9. Aslak Hellesøy says:

    Very cooool :-)

  10. In Windows

    >assoc .feature=CucumberFile
    .feature=CucumberFile

    >ftype CucumberFile=C:\ruby\bin\cucumber.bat %1
    CucumberFile=C:\ruby\bin\cucumber.bat %1

    >features\users_crud.feature

  11. Pedro says:

    Alias are good but you still need to type features/foo.feature

    I used this function that I called cu in zsh

    cucumber features/$1.feature:$2

    in order to run the line 6 of the foo feature I just write

    cu foo 6

  12. Andrew Vit says:

    Pasting and hitting return? Drag & drop FTW!

    Nice tip. Simple. Love it.

  13. Chris Homer says:

    Maybe I’m missing something but it seems to me the easiest is to hit Command-R from textmate (if you have the cucumber bundle). I guess this requires you to be in the feature file though…