runcommand

package
v2.0.0-...-4b7107c Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 4 Imported by: 0

README

RunCommand

Description

A command-execution facility in golang

Usage

The RunCommand package provides an abstracted way of executing system commands within Go code. It consists of a CommandExecutor interface and two implementing types:

  • RealCommandExecutor and
  • MockCommandExecutor.

The CommandExecutor interface defines a method Execute which accepts the name of the command to be run, along with its arguments, and returns the output of the command as a byte slice or an error.

RealCommandExecutor

The RealCommandExecutor type is a concrete implementation of the CommandExecutor interface. This executor runs the provided command with its arguments and captures its output.

Here is an example of using RealCommandExecutor:

executor := runcommand.RealCommandExecutor{}
output, err := executor.Execute("echo", "hello")
    if err != nil {
    // Handle error
}
fmt.Println(string(output)) // Prints: hello
MockCommandExecutor

The MockCommandExecutor type is another implementation of CommandExecutor but with a difference. It doesn't actually execute any command. Instead, it returns predefined output and error which you can set at the time of creation. This executor is particularly useful when writing tests for your functions that depend on external commands. It allows you to define expected command output and error, and thereby test your function's behavior in a controlled way.

Here is an example of using MockCommandExecutor in a test:

func TestYourFunction(t *testing.T) {
	mock := runcommand.MockCommandExecutor{
		Output: "expected output",
		Error:  nil,
	}
	
	// Pass the mock to your function
	result := YourFunction(mock)
	
	// Check that the function behaves as expected
}

By abstracting the execution of commands, the RunCommand package allows you to write code that is easier to test and maintain. It provides clear separation between your application logic and the external command execution. In your tests, it provides a way to simulate different command outputs and errors, so you can ensure your code behaves correctly in different situations.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandExecutor

type CommandExecutor interface {
	Execute(name string, arg ...string) ([]byte, error)
}

CommandExecutor - An interface for both real and mock command execution.

type MockCommandExecutor

type MockCommandExecutor struct {
	ProcessFunc func(m *MockCommandExecutor, name string, arg ...string) ([]byte, error)
	Outputs     map[string]any
	Errors      map[string]error
}

MockCommandExecutor - A mock implementation of the CommandExecutor

func (MockCommandExecutor) Execute

func (m MockCommandExecutor) Execute(name string, arg ...string) ([]byte, error)

Execute - Intercept a command execution and instead return predefined response content

func (MockCommandExecutor) Validate

func (m MockCommandExecutor) Validate(expectedNumberArgs int, name string, arg ...string)

type RealCommandExecutor

type RealCommandExecutor struct{}

RealCommandExecutor - A concrete implementation of CommandExecutor.

func (RealCommandExecutor) Execute

func (r RealCommandExecutor) Execute(name string, arg ...string) ([]byte, error)

Execute - Execute a command and return the command's output/error state

type SimpleMockCommandExecutor

type SimpleMockCommandExecutor struct {
	ProcessFunc func(m *SimpleMockCommandExecutor, name string, arg ...string) ([]byte, error)
	Outputs     map[string]any
	Errors      map[string]error
}

SimpleMockCommandExecutor - A mock implementation of the CommandExecutor

func (SimpleMockCommandExecutor) Execute

func (m SimpleMockCommandExecutor) Execute(name string, arg ...string) ([]byte, error)

Execute - Intercept a command execution and instead return predefined response content

func (SimpleMockCommandExecutor) Validate

func (m SimpleMockCommandExecutor) Validate(expectedNumberArgs int, name string, arg ...string)

Jump to

Keyboard shortcuts

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