Skip to content

Commit

Permalink
Merge pull request #941 from cucumber/command-parameter-type
Browse files Browse the repository at this point in the history
Introduce Cucumber parameter type 'command'
  • Loading branch information
mvz authored Nov 15, 2024
2 parents e069bac + e096c9f commit 6b05a86
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Feature: Running shell commands
Given I use a fixture named "cli-app"

@requires-ruby
@requires-python
Scenario: Creating and running scripts
Given a file named "features/shell.feature" with:
"""
Expand All @@ -25,15 +24,6 @@ Feature: Running shell commands
puts "Hello"
\"\"\"
Then the output should contain exactly "Hello"
Scenario: Running python script
When I run the following script:
\"\"\"bash
#!/usr/bin/env python
print("Hello")
\"\"\"
Then the output should contain exactly "Hello"
"""
When I run `cucumber`
Then the features should all pass
Expand Down
6 changes: 0 additions & 6 deletions features/step_definitions/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

require 'cucumber/platform'

Before '@requires-python' do
next unless Aruba.platform.which('python').nil?

skip_this_scenario
end

Before '@requires-zsh' do
next unless Aruba.platform.which('zsh').nil?

Expand Down
31 changes: 21 additions & 10 deletions lib/aruba/cucumber/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,46 @@

require 'aruba/generators/script_file'

When(/^I run `([^`]*)`$/) do |cmd|
When 'I run {command}' do |cmd|
cmd = sanitize_text(cmd)
run_command_and_stop(cmd, fail_on_error: false)
end

## I successfully run `echo -n "Hello"`
## I successfully run `sleep 29` for up to 30 seconds
When(/^I successfully run `(.*?)`(?: for up to ([\d.]+) seconds)?$/) do |cmd, secs|
When 'I successfully run {command}' do |cmd|
cmd = sanitize_text(cmd)
run_command_and_stop(cmd, fail_on_error: true, exit_timeout: secs && secs.to_f)
run_command_and_stop(cmd, fail_on_error: true)
end

When(/^I run the following (?:commands|script)(?: (?:with|in) `([^`]+)`)?:$/) \
do |shell, commands|
When 'I successfully run {command} for up to {float} seconds' do |cmd, secs|
cmd = sanitize_text(cmd)
run_command_and_stop(cmd, fail_on_error: true, exit_timeout: secs.to_f)
end

When 'I run the following commands:/script:' do |commands|
full_path = expand_path('bin/myscript')

Aruba.platform.mkdir(expand_path('bin'))
shell = Aruba.platform.default_shell

Aruba::ScriptFile.new(interpreter: shell, content: commands, path: full_path).call
run_command_and_stop(Shellwords.escape(full_path), fail_on_error: false)
end

When 'I run the following commands/script with/in {command}:' do |shell, commands|
full_path = expand_path('bin/myscript')

Aruba.platform.mkdir(expand_path('bin'))
shell ||= Aruba.platform.default_shell

Aruba::ScriptFile.new(interpreter: shell, content: commands, path: full_path).call
run_command_and_stop(Shellwords.escape(full_path), fail_on_error: false)
end

When(/^I run `([^`]*)` interactively$/) do |cmd|
When 'I run {command} interactively' do |cmd|
run_command(sanitize_text(cmd))
end

# Merge interactive and background after refactoring with event queue
When(/^I run `([^`]*)` in background$/) do |cmd|
When 'I run {command} in background' do |cmd|
run_command(sanitize_text(cmd))
end

Expand Down
1 change: 1 addition & 0 deletions lib/aruba/cucumber/parameter_types.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

ParameterType(name: 'channel', regexp: 'output|stderr|stdout', transformer: ->(name) { name })
ParameterType(name: 'command', regexp: '`([^`]*)`', transformer: ->(name) { name })

0 comments on commit 6b05a86

Please sign in to comment.