Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

SHell command MOCKer for integration testing

Project Description

Purpose

Tools for system administration typically call lots of programs on the command line. This makes automated testing quite tricky, since you may need to

  • run “sudo ….”, even though the build system is not allowed to use sudo
  • have tools like “uname” or “ifconfig” produce certain output for testing

shmock helps you by creating mock commands that supersede the system’s own commands due to a temporarily manipulated $PATH. Based on the command line parameters, mock commands can have different

  • output on STDOUT and STDERR
  • exit code

It is not possible to simulate slowness or commands that behave differently the second time you call them. Command line parsing is very limited, but that’s not a problem for auto-generated calls. However, these limitations make the implementation very simple.

Configuration

To configure which commands should be mocked (and how), use a dictionary like this:

commands_to_mock = {
    'saynay': 'Nay sayers say nay.'
    'jonny': {
        (): "walker",
        "foo": "bar",
        ("b", "goode"): "Go, Jonny, go!",
        ("be", "bad"): {"stderr": "yup", "returncode": 255},
        None: {
            "stdout": "You called me with some unknown parameters.",
            "stderr": "And I don't like that.",
            "returncode": 1
        }
    }
}

The first part uses the most simple way of defining a mocked command: A ‘saynay’ command is defined that always prints “Nay sayers say nay.” and exits successfully, regardless of command line options.

After that, a ‘jonny’ command is defined that illustrates the full feature set of the shmock module. The command is defined to

  • When called with no parameters, print “walker”.
  • When called with a single parameter “foo”, print “bar”.
  • When called with the two parameters “b” and “goode”, print “Go, Jonny, go!”.
  • When called with “be bad”, print “yup” to standard error and then exit with 255.
  • When called with any other parameters, print “You…” to standard out, print “And…” to standard error and then exit with 1.

Usage

The ShellCommandMock is intended to be used in “with” contexts as shown below:

import os
from shmock import ShellCommandMock
with ShellCommandMock(commands_to_mock):
    os.system("echo $PATH")

    os.system("jonny")
    os.system("jonny b goode")
    os.system("jonny be bad")
    os.system("jonny foobar")

os.system("echo $PATH")

Advanced Usage

Sometimes you want to keep the mocked shell commands for further testing/debugging. You can tell shmock to not clean up the mock environment with

from shmock import ShellCommandMock
with ShellCommandMock(commands_to_mock, keep_temp_dir=True):
    pass

shmock will print the location of the mock environment, so that you can add it to you $PATH.

When output is printed, shmock calls print(), and print() automatically appends a newline to the output. As a result, it is currently not possible to produce output that does not end in a newline. This will be fixed once it becomes a problem.

License

Copyright 2015 Immobilienscout24 GmbH

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Release History

Release History

This version
History Node

1.0.1

History Node

1.0.0.post82

History Node

1.0.0.post81

History Node

1.0.0.post80

History Node

1.0.0.post79

History Node

1.0.0.post78

History Node

1.0.0.post76

History Node

1.0.0.post75

History Node

1.0.0.post74

History Node

1.0.0.post73

History Node

1.0.0.post72

History Node

1.0.0.post71

History Node

1.0.0.post70

History Node

1.0.0.post69

History Node

1.0.0.post68

History Node

1.0.0.post67

History Node

1.0.0.post65

History Node

1.0.0.post64

History Node

1.0.0.post63

History Node

1.0.0.post62

History Node

1.0.0.post61

History Node

1.0.0.post60

History Node

1.0.0.post59

History Node

1.0.0.post58

History Node

1.0.0.post57

History Node

1.0.0.post56

History Node

1.0.0.post55

History Node

1.0.0-54

History Node

1.0.0-52

History Node

1.0.0-51

History Node

1.0.0-50

History Node

1.0.0-49

History Node

1.0.0-48

History Node

1.0.0-47

History Node

1.0.0-46

History Node

1.0.0-45

History Node

1.0.0-44

History Node

1.0.0-43

History Node

1.0.0-42

History Node

1.0.0-41

History Node

1.0.0-40

History Node

1.0.0-38

History Node

1.0.0-37

History Node

1.0.0-36

History Node

1.0.0-34

History Node

1.0.0-32

History Node

1.0.0-31

History Node

1.0.0-30

History Node

1.0.0-29

History Node

1.0.0-28

History Node

1.0.0-27

History Node

1.0.0-26

History Node

1.0.0-25

History Node

1.0.0-24

History Node

1.0.0-23

History Node

1.0.0-22

History Node

1.0.0-21

History Node

1.0.0-20

History Node

1.0.0-17

History Node

1.0.0-16

History Node

1.0.0-15

History Node

1.0.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
shmock-1.0.1.tar.gz (4.3 kB) Copy SHA256 Checksum SHA256 Source Sep 8, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting