From e096c9f92f3ee08fc8db6f0262aa7159c058e055 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 15 Nov 2024 14:02:14 +0100 Subject: [PATCH] Introduce Cucumber parameter type 'command' This allows more steps to use the Cucumber Expression syntax. --- lib/aruba/cucumber/command.rb | 31 ++++++++++++++++++--------- lib/aruba/cucumber/parameter_types.rb | 1 + 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/aruba/cucumber/command.rb b/lib/aruba/cucumber/command.rb index 619ebad1..71459d5e 100644 --- a/lib/aruba/cucumber/command.rb +++ b/lib/aruba/cucumber/command.rb @@ -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 diff --git a/lib/aruba/cucumber/parameter_types.rb b/lib/aruba/cucumber/parameter_types.rb index 5680893b..c3b4f852 100644 --- a/lib/aruba/cucumber/parameter_types.rb +++ b/lib/aruba/cucumber/parameter_types.rb @@ -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 })