testcli

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2021 License: BSD-2-Clause Imports: 7 Imported by: 0

README

testcli

Package testcli is a golang helper utility for testing command line applications (CLI) using the standard go testing framework.

Tests are created by populating a T structure and then passing it to the Run function. The T structure must contain at a minimum the command to be executed. The remaining items within the T structure are optional and have reasonable defaults in typical use cases.

For example within a "foo_test.go" file:

func TestFooVersion(t *testing.T) {
    clt := testcli.T{
        Cmd: exec.Cmd{
            Path: "./foo",
            Args: []string{"foo", "-v"},
        },
        TStdout: `^foo 1.7.2\n$`,
    }
    testcli.Run(t, clt)
}

func TestFooBadOption(t *testing.T) {
    clt := testcli.T{
        Cmd: exec.Cmd{
            Path: "./foo",
            Args: []string{"foo", "-z"},
        },
        TStderr: `(?m)^flag provided but not defined: -z$.*$`,
        TExit:   2,
    }
    testcli.Run(t, clt)
}

A full example demonstrating additional ways to use testcli is contained within the "cmd" folder of this package.

Functions

func Run(t *testing.T, clt T)
    Run executes a single command line test.

Types

type FT struct {
        Path     string // Path to file
        TContent string // Regex to check against file contents
}
    FT defines a file path and regex to check against the file contents.

type T struct {
        Cmd     exec.Cmd // The command line details to execute
        TStdout string   // Regex to check against stdout
        TStderr string   // Regex to check against stderr
        TFiles  []FT     // An optional array of output files to check
        TExit   int      // The expected exit code
}
    T defines an individual command line test case.

Running the full package tests

$ cd cmd
$ make check

Contributing

If you have a bugfix, update, issue or feature enhancement the best way to reach me is by following the instructions in the link below. Thank you!

https://blog.lenzplace.org/contact

Versioning

I follow the SemVer strategy for versioning. The latest version is listed in the releases section.

License

This project is licensed under a BSD two clause license - see the LICENSE file for details.

Documentation

Overview

Package testcli is a helper utility for testing command line applications (CLI) using the standard go testing framework.

Tests are created by populating a T structure and then passing it to the Run function. The T structure must contain at a minimum the command to be executed. The remaining items within the T structure are optional and have reasonable defaults in typical use cases.

For example within a "foo_test.go" file:

     func TestFooVersion(t *testing.T) {
             clt := testcli.T{
                     Cmd: exec.Cmd{
                             Path: "./foo",
                             Args: []string{"foo", "-v"},
                     },
                     TStdout: `^foo 1.7.2\n$`,
             }
             testcli.Run(t, clt)
     }

     func TestFooBadOption(t *testing.T) {
             clt := testcli.T{
                     Cmd: exec.Cmd{
                             Path: "./foo",
                             Args: []string{"foo", "-z"},
                     },
			TStderr: `(?m)^flag provided but not defined: -z$.*$`,
			TExit:   2,
             }
             testcli.Run(t, clt)
     }

A full example demontrating additional ways to use testcli is contained within the "cmd" folder of this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run added in v1.0.0

func Run(t *testing.T, clt T)

Run executes a single command line test.

Types

type FT added in v1.0.0

type FT struct {
	Path     string // Path to file
	TContent string // Regex to check against file contents
}

FT defines a file path and regex to check against the file contents.

type T added in v1.0.0

type T struct {
	Cmd     exec.Cmd // The command line details to execute
	TStdout string   // Regex to check against stdout
	TStderr string   // Regex to check against stderr
	TFiles  []FT     // An optional array of output files to check
	TExit   int      // The expected exit code
}

T defines an individual command line test case.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL