2019-11-19 10:00:20 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2020-06-30 16:53:30 -06:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
//TODO: all flag parsing continueOnError will print to stderr on its own currently
|
|
|
|
|
|
|
|
func Test_help(t *testing.T) {
|
|
|
|
expected := "Usage of " + os.Args[0] + " <global flags> <mode>:\n" +
|
|
|
|
" Global flags:\n" +
|
|
|
|
" -version: Prints the version\n" +
|
|
|
|
" -h, -help: Prints this help message\n\n" +
|
|
|
|
" Modes:\n" +
|
|
|
|
" " + caSummary() + "\n" +
|
|
|
|
" " + keygenSummary() + "\n" +
|
|
|
|
" " + signSummary() + "\n" +
|
|
|
|
" " + printSummary() + "\n" +
|
|
|
|
" " + verifySummary() + "\n"
|
|
|
|
|
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
|
|
|
|
// No error test
|
|
|
|
help("", ob)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
expected,
|
|
|
|
ob.String(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error test
|
|
|
|
ob.Reset()
|
|
|
|
help("test error", ob)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"Error: test error\n\n"+expected,
|
|
|
|
ob.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_handleError(t *testing.T) {
|
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
|
|
|
|
// normal error
|
|
|
|
handleError("", errors.New("test error"), ob)
|
|
|
|
assert.Equal(t, "Error: test error\n", ob.String())
|
|
|
|
|
|
|
|
// unknown mode help error
|
|
|
|
ob.Reset()
|
|
|
|
handleError("", newHelpErrorf("test %s", "error"), ob)
|
|
|
|
assert.Equal(t, "Error: test error\n", ob.String())
|
|
|
|
|
|
|
|
// test all modes with help error
|
|
|
|
modes := map[string]func(io.Writer){"ca": caHelp, "print": printHelp, "sign": signHelp, "verify": verifyHelp}
|
|
|
|
eb := &bytes.Buffer{}
|
|
|
|
for mode, fn := range modes {
|
|
|
|
ob.Reset()
|
|
|
|
eb.Reset()
|
|
|
|
fn(eb)
|
|
|
|
|
|
|
|
handleError(mode, newHelpErrorf("test %s", "error"), ob)
|
|
|
|
assert.Equal(t, "Error: test error\n"+eb.String(), ob.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertHelpError(t *testing.T, err error, msg string) {
|
|
|
|
switch err.(type) {
|
|
|
|
case *helpError:
|
|
|
|
// good
|
|
|
|
default:
|
|
|
|
t.Fatal("err was not a helpError")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualError(t, err, msg)
|
|
|
|
}
|