Skip to content

Behat extension for executing shell commands remote and locally

License

Notifications You must be signed in to change notification settings

DrSchimke/BehatShellExtension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BehatShellExtension

Build Status

Behat extension for executing shell commands within features. The shell commands can be run on remote servers using ssh or locally without network.

Installation

Using composer:

composer require postcon/behat-shell-extension dev-master

Usage

# test.feature

Feature: Running commands
  In order to run useful integration tests
  As a tester
  I want to execute shell commands and check their output

  Scenario: Run command on the default shell/server and define expected output
    When I run "pwd"
    Then It should pass
    And I see
    """
    /tmp
    """

  Scenario: Run command on the default shell/server and define expected output in inline-style
    When I run "pwd"
    Then It should pass
    And I see "/tmp"

  Scenario: Run command on the shell/server "app"
    When I run "app/console --env=prod do:mi:mi" on "app"
    Then It should pass

Configuration

To use the BehatShellExtension, it needs to be configured in the behat.yml (or behat.yml.dist). Each servr or shell, you want invoke commands on, mus be specified. Following example gives configuration for the local shell (default) and a remote server (named app).

# behat.yml
extensions:
  ShellExtension:
    default:
      type: local
      base_dir: /tmp
    app:
      type: remote
      base_dir: /var/www/
      ssh_command: /usr/bin/ssh
      ssh_options: -i ~/.ssh/id_rsa shell.example.com

The type can be local or remote. The current work directory for executing the command is defined using base_dir. The remote server address, user credentials and other relevant options for ssh are specified using ssh_options. Additionally the location of the ssh executable can be defined using ssh_command.

Defaults

Without defining any server, default is defined automatically by the extension:

...
    default:
      type: local
      base_dir: ~

Internal implementation

A command string $command is executed on a shell with type: local gets invoked in following way:

$process = new Process($command, $serverConfig['base_dir']);
$process->run();

A remote executed command string $command is executed this way:

if ($serverConfig['base_dir']) {
    $command = sprintf('cd %s ; %s', $serverConfig['base_dir'], $command);
}
$command = sprintf(
    '%s %s %s',
    $serverConfig['ssh_command'],
    $serverConfig['ssh_options'],
    escapeshellarg($command)
);

// e.g. ssh -i ~/.ssh/id_rsa [email protected] 'cd /var/www ; app/console --env=prod do:mi:mi'

$process = new Process($command);
$process->run();

License

All contents of this package are licensed under the MIT license.

About

Behat extension for executing shell commands remote and locally

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published