https://github.com/justintime50/mockcmd
Mocks the "exec.Command" interface in Golang.
https://github.com/justintime50/mockcmd
cmd command exec golang interface mock
Last synced: 7 months ago
JSON representation
Mocks the "exec.Command" interface in Golang.
- Host: GitHub
- URL: https://github.com/justintime50/mockcmd
- Owner: Justintime50
- License: mit
- Created: 2021-01-17T00:01:41.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-23T18:35:25.000Z (about 1 year ago)
- Last Synced: 2025-02-26T17:23:41.386Z (8 months ago)
- Topics: cmd, command, exec, golang, interface, mock
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mockcmd
Mocks the `exec.Command` interface in Golang.
[](https://github.com/Justintime50/mockcmd/actions)
[](https://coveralls.io/github/Justintime50/mockcmd?branch=main)
[](https://github.com/Justintime50/mockcmd/releases)
[](LICENSE)
Mocking the `exec.Command` interface in Golang is an absolute pain. You run the risk of running system commands in a unit test context or are required to build out a custom solution to run your commands in an isolated and safe manner which still isn't ideal. Enter `mockcmd`, the easy and safe way to mock your `exec.Command` functions.
`mockcmd` accomplishes a safe and easy mocking of the `exec.Command` interface by creating a fake `cmd` function and passing your command and args through to it. We assert that the `Stdout` gets passed through and returned correctly for success and that an `err` gets returned in the case of failure.
## Install
```shell
go get github.com/Justintime50/mockcmd
```## Usage
### Your Command
Below is an example on how to setup your CMD command in a way it can be easily mocked:
```go
package mypackageimport (
"github.com/Justintime50/mockcmd/mockcmd"
)// Pass in `exec.Command` as the context for your real command
func main() {
out, _ := myCommandFunction(exec.Command)
fmt.Println(out.String())
}func myCommandFunction(cmdContext mockcmd.ExecContext) (*bytes.Buffer, error) {
cmd := cmdContext("echo", "Hello World")
var outb bytes.Buffer
cmd.Stdout = &outb
err := cmd.Run()
if err != nil {
fmt.Printf("Failed to run command: %s", err)
return nil, err
}return &outb, nil
}
```### Your Test
Below is an example on how to setup your test to mock your command from above:
```go
// Mock a success and failure response from exec.Command
func TestMyCommandFunctionSuccess(t *testing.T) {
stdout, err := myCommandFunction(mockcmd.MockExecSuccess)
mockcmd.Success(t, stdout, err)
}func TestMyCommandFunctionFailure(t *testing.T) {
_, err := myCommandFunction(mockcmd.MockExecFailure)
mockcmd.Fail(t, err)
}// The following functions are required in each package that will mock an exec.Command
// Do not alter the following when placed into your package_test.go file
func TestMockProcessSuccess(t *testing.T) {
if os.Getenv("GO_TEST_PROCESS") != "1" {
return
}
os.Stdout.WriteString("mocked Stdout")
os.Exit(0)
}func TestMockProcessFailure(t *testing.T) {
if os.Getenv("GO_TEST_PROCESS") != "1" {
return
}
os.Exit(1)
}
```## Development
```bash
# Get a comprehensive list of development tools
just --list
```## Attribution
- Inspired by the articles by [Jamie Thompson](https://jamiethompson.me/posts/Unit-Testing-Exec-Command-In-Golang) and [Nate Finch](https://npf.io/2015/06/testing-exec-command/)