New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/rendon/testcli

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/rendon/testcli

  • v1.0.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

testcli

CLI testing package for the Go language.

Developing a command line application? Wanna be able to test your app from the outside? If the answer is Yes to at least one of the questions, keep reading.

When using Ruby I use aruba for testing command line applications, in Go I still can use aruba, but it"s awkward to bring Ruby and it's artillery only to test my app.

testcli is a wrapper around os.exec to test CLI apps in Go lang, minimalistic, so you can do your tests with testing or any other testing framework.

Greetings app

main_test.go

// make sure to execute `go install` before tests
package main

import (
	"testing"

	"github.com/rendon/testcli"
)

func TestGreetings(t *testing.T) {
	// Using package functions
	testcli.Run("greetings")
	if !testcli.Success() {
		t.Fatalf("Expected to succeed, but failed: %s", testcli.Error())
	}

	if !testcli.StdoutContains("Hello?") {
		t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "Hello?")
	}
}

func TestGreetingsWithName(t *testing.T) {
	// Using the struct version, if you want to test multiple commands
	c := testcli.Command("greetings", "--name", "John")
	c.Run()
	if !c.Success() {
		t.Fatalf("Expected to succeed, but failed with error: %s", c.Error())
	}

	if !c.StdoutContains("Hello John!") {
		t.Fatalf("Expected %q to contain %q", c.Stdout(), "Hello John!")
	}
}

main.go

package main

import (
	"fmt"
	"os"

	"github.com/codegangsta/cli"
)

func main() {
	app := cli.NewApp()
	app.Name = "cli"
	app.Usage = "CLI app"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:  "name",
			Usage: "User name",
		},
	}
	app.Action = func(c *cli.Context) {
		if c.String("name") != "" {
			fmt.Printf("Hello %s!\n", c.String("name"))
		} else {
			fmt.Printf("Hello? Anyone?\n")
		}
	}

	app.Run(os.Args)
}

FAQs

Package last updated on 10 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc