Giter VIP home page Giter VIP logo

shellspec-ext-invocation's Introduction

ShellSpec Extension: Invocation

License Docker Image Version (latest semver)

A ShellSpec extension for capturing mock invocations.

Overview

This extension enables bash-based ShellSpec tests to capture mock invocations and to verify expectations about these invocations.

Describe 'capture_invocation in a simple mock'
  It 'captures an invocation and its arguments'
    Mock git
      capture_invocation  git "$@"
    End
    When run  git commit "-m" "Initial commit"
    The number of mocks should equal 1
    The first mock should have received arguments  git commit "-m" "Initial commit"
  End
End

Tip

Passing the initial command ("git" in the above example) as first argument to capture_invocation is not strictly required, but it helps for distinguishing between multiple mocked functions/commands.

When mocking functions or commands in ShellSpec, mocks might get invoked multiple times with different arguments.

Describe 'capture_invocation in multiple mocks'
  It 'captures multiple invocations and their arguments'
    # Function mock:
    edit_file() {
      capture_invocation  edit_file "$@"
    }

    # Command mock:
    Mock git
      capture_invocation  git "$@"
    End

    some_complex_task() {
      # Do stuff
      edit_file "$1"
      git add "--" "$1"
      git commit "-m" "$2"
      git push
      # Do more stuff
    }
    When call  some_complex_task "README.md" "Initial commit"
    The number of mocks should equal 4
    The 1st mock should have received arguments  edit_file "README.md"
    The 2nd mock should have received arguments  git add "--" "README.md"
    The 3rd mock should have received arguments  git commit "-m" "Initial commit"
    The 4th mock should have received arguments  git push
  End
End
Describe 'capture_invocation expectations have aliases'
  It 'captures a simple invocation without additional arguments'
    When call capture_invocation  foo
    
    # aliases
    The invocations count should equal 1
    The count of invocations should equal 1
    The number of invocations should equal 1
    The count of mocks should equal 1
    The number of mocks should equal 1

    # aliases
    The first invocation should have received arguments  foo
    The mock 1 should have received arguments  foo
  End
End

This extension consists of:

  • The capture_invocation function to store the command name and the supplied arguments.
  • A ShellSpec subject number of mocks (aliases are number of invocations, count of mocks, and count of invocations) that counts how many mocks have been invoked.
  • A ShellSpec subject mock (alias: invocation) to select an invocation for verification matching.
  • A ShellSpec matcher have received arguments to match the selected invocation against expected arguments.

Usage

The easiest way to run the unit tests is via the dedicated Docker image mgrafl/shellspec-ext-invocation.

Docker

docker run --rm -t -v ".:/src" mgrafl/shellspec-ext-invocation

Linux

Prefer the dedicated Docker image over local installation. Local installation instructions are only provided for the sake of completeness.

Assuming the code from this repository is located in /path/to/shellspec-ext-invocation/, run shellspec directly as:

shellspec --shell=/bin/bash --load-path=/path/to/shellspec-ext-invocation/lib/extension/invocation --require capture_invocation_helper

or:

PATH_TO_SHELLSPEC_EXT_INVOCATION="/path/to/shellspec-ext-invocation/"
PATH="${PATH_TO_SHELLSPEC_EXT_INVOCATION}:${PATH}"
chmod +x "${PATH_TO_SHELLSPEC_EXT_INVOCATION}shellspec-ext-invocation"

# Append ShellSpec CLI parameters as needed
shellspec-ext-invocation

ShellSpec CLI parameters can be appended to the command.

Limitations

This extension is implemented in Bash and is not POSIX-compliant.

Other resources

Contributing

Contributions to this project are always welcome. Please read the contributing section and raise a Pull Request.

License

This project is licensed under the MIT license.

shellspec-ext-invocation's People

Contributors

grmhskidata avatar mgrafl avatar

Watchers

 avatar

shellspec-ext-invocation's Issues

Special characters not escaped in expected or actual invocation arguments

Special characters (wrt. bash variables) are not escaped from the failure message.

If the expectation matches the actual captured invocation, special characters are properly supported. But for the error message on expectation mismatch, the string containing the arguments is eval'ed by shellspec.
This can lead to garbled output an potentially undesired behavior. In particular, everything after the first double quote inside an argument gets interpreted as bash code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.