main: add testcases for convertToDoubleDash & parseCliOpts

This commit is contained in:
Jakob Unterwurzacher 2021-08-10 19:42:33 +02:00
parent 463f6e8962
commit 0c16616117
3 changed files with 113 additions and 3 deletions

View File

@ -114,9 +114,15 @@ func prefixOArgs(osArgs []string) ([]string, error) {
func convertToDoubleDash(osArgs []string) (out []string) {
out = append(out, osArgs...)
for i, v := range out {
// Leave "-h" alone so the help text keeps working
if v == "-h" {
continue
}
// Don't touch anything after "--"
if v == "--" {
break
}
// Convert "-foo" to "--foo"
if len(v) >= 2 && v[0] == '-' && v[1] != '-' {
out[i] = "-" + out[i]
}
@ -125,11 +131,11 @@ func convertToDoubleDash(osArgs []string) (out []string) {
}
// parseCliOpts - parse command line options (i.e. arguments that start with "-")
func parseCliOpts() (args argContainer) {
func parseCliOpts(osArgs []string) (args argContainer) {
var err error
var opensslAuto string
osArgsPreprocessed, err := prefixOArgs(os.Args)
osArgsPreprocessed, err := prefixOArgs(osArgs)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(exitcodes.Usage)

View File

@ -3,6 +3,8 @@ package main
import (
"reflect"
"testing"
"github.com/rfjakob/gocryptfs/internal/stupidgcm"
)
// TestPrefixOArgs checks that the "-o x,y,z" parsing works correctly.
@ -75,3 +77,105 @@ func TestPrefixOArgs(t *testing.T) {
}
}
}
func TestConvertToDoubleDash(t *testing.T) {
testcases := []struct {
// i is the input
i []string
// o is the expected output
o []string
}{
{
i: nil,
o: nil,
},
{
i: []string{"gocryptfs"},
o: []string{"gocryptfs"},
},
{
i: []string{"gocryptfs", "foo"},
o: []string{"gocryptfs", "foo"},
},
{
i: []string{"gocryptfs", "-v", "-quiet"},
o: []string{"gocryptfs", "--v", "--quiet"},
},
{
i: []string{"gocryptfs", "--", "-foo"},
o: []string{"gocryptfs", "--", "-foo"},
},
}
for _, tc := range testcases {
o := convertToDoubleDash(tc.i)
if !reflect.DeepEqual(o, tc.o) {
t.Errorf("in=%q\nwant=%q\nhave=%q", tc.i, tc.o, o)
}
}
}
func TestParseCliOpts(t *testing.T) {
defaultArgs := argContainer{
longnames: true,
raw64: true,
hkdf: true,
openssl: stupidgcm.PreferOpenSSL(), // depends on CPU and build flags
scryptn: 16,
}
type testcaseContainer struct {
// i is the input
i []string
// o is the expected output
o argContainer
}
var testcases []testcaseContainer
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs"},
o: defaultArgs,
})
o := defaultArgs
o.quiet = true
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs", "-q"},
o: o,
})
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs", "--q"},
o: o,
})
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs", "-quiet"},
o: o,
})
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs", "--quiet"},
o: o,
})
o = defaultArgs
o.exclude = []string{"foo", "bar"}
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs", "-e", "foo", "-e", "bar"},
o: o,
})
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs", "--exclude", "foo", "--exclude", "bar"},
o: o,
})
/* TODO BROKEN
testcases = append(testcases, testcaseContainer{
i: []string{"gocryptfs", "--exclude", "foo", "-e", "bar"},
o: o,
})
*/
for _, tc := range testcases {
o := parseCliOpts(tc.i)
if !reflect.DeepEqual(o, tc.o) {
t.Errorf("in=%v\nwant=%v\nhave=%v", tc.i, tc.o, o)
}
}
}

View File

@ -166,7 +166,7 @@ func main() {
var err error
// Parse all command-line options (i.e. arguments starting with "-")
// into "args". Path arguments are parsed below.
args := parseCliOpts()
args := parseCliOpts(os.Args)
// Fork a child into the background if "-fg" is not set AND we are mounting
// a filesystem. The child will do all the work.
if !args.fg && flagSet.NArg() == 2 {