Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Cucumber parameter type 'command' #941

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })