Skip to content

Commit

Permalink
test convoluted arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed May 25, 2023
1 parent 63390fd commit 6fea52e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 11 deletions.
22 changes: 11 additions & 11 deletions cmd/slackdump/internal/diag/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ func initRecipient() error {
}

func runEncrypt(ctx context.Context, cmd *base.Command, args []string) error {
in, out, err := parseArgs(args)
in, out, arm, err := parseArgs(args)
if err != nil {
return err
}
defer in.Close()
defer out.Close()

var w io.Writer = out
if gArm {
if arm || gArm {
// arm if requested
aw, err := armor.Encode(out, "PGP MESSAGE", nil)
if err != nil {
Expand Down Expand Up @@ -170,49 +170,49 @@ func runEncrypt(ctx context.Context, cmd *base.Command, args []string) error {
// the output is a file, if it's not a "-" otherwise stdout.
// 4. if more than two arguments are given, it's an error
// 5. if output is stdout, arm the output automatically
func parseArgs(args []string) (in io.ReadCloser, out io.WriteCloser, err error) {
func parseArgs(args []string) (in io.ReadCloser, out io.WriteCloser, arm bool, err error) {

switch len(args) {
case 0:
in = os.Stdin
out = os.Stdout
gArm = true
arm = true
case 1:
if args[0] == "-" {
in = os.Stdin
} else {
in, err = os.Open(args[0])
if err != nil {
base.SetExitStatus(base.SApplicationError)
return nil, nil, err
return nil, nil, false, err
}
}
out = os.Stdout
gArm = true
arm = true
case 2:
if args[0] == "-" {
in = os.Stdin
} else {
in, err = os.Open(args[0])
if err != nil {
base.SetExitStatus(base.SApplicationError)
return nil, nil, err
return nil, nil, false, err
}
}
if args[1] == "-" {
out = os.Stdout
gArm = true
arm = true
} else {
out, err = os.Create(args[1])
if err != nil {
in.Close()
base.SetExitStatus(base.SApplicationError)
return nil, nil, err
return nil, nil, false, err
}
}
default:
base.SetExitStatus(base.SInvalidParameters)
return nil, nil, errors.New("invalid number of arguments")
return nil, nil, false, errors.New("invalid number of arguments")
}
return in, out, nil
return in, out, arm, nil
}
84 changes: 84 additions & 0 deletions cmd/slackdump/internal/diag/encrypt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package diag

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_parseArgs(t *testing.T) {
t.Run("no args", func(t *testing.T) {
in, out, arm, err := parseArgs([]string{})
require.NoError(t, err)
assert.Equal(t, os.Stdin, in)
assert.Equal(t, os.Stdout, out)
assert.True(t, arm)
})
t.Run("one arg", func(t *testing.T) {
in, out, arm, err := parseArgs([]string{"-"})
require.NoError(t, err)
assert.Equal(t, os.Stdin, in)
assert.Equal(t, os.Stdout, out)
assert.True(t, arm)
})
t.Run("two args", func(t *testing.T) {
in, out, arm, err := parseArgs([]string{"-", "-"})
require.NoError(t, err)
assert.Equal(t, os.Stdin, in)
assert.Equal(t, os.Stdout, out)
assert.True(t, arm)
})
t.Run("two args, first is file", func(t *testing.T) {
dir := t.TempDir()
f, _ := os.Create(filepath.Join(dir, "foo"))
f.Close()

in, out, arm, err := parseArgs([]string{filepath.Join(dir, "foo"), "-"})
require.NoError(t, err)
assert.Equal(t, os.Stdout, out)
assert.True(t, arm)

if inf, ok := in.(*os.File); ok {
assert.Equal(t, filepath.Join(dir, "foo"), inf.Name())
} else {
t.Errorf("input is not a file")
}
})
t.Run("two args, second is file", func(t *testing.T) {
dir := t.TempDir()
in, out, arm, err := parseArgs([]string{"-", filepath.Join(dir, "bar")})
require.NoError(t, err)
assert.Equal(t, os.Stdin, in)
assert.False(t, arm)

if outf, ok := out.(*os.File); ok {
assert.Equal(t, filepath.Join(dir, "bar"), outf.Name())
} else {
t.Errorf("output is not a file")
}
})
t.Run("two args, both are files", func(t *testing.T) {
dir := t.TempDir()
f, _ := os.Create(filepath.Join(dir, "foo"))
f.Close()

in, out, arm, err := parseArgs([]string{filepath.Join(dir, "foo"), filepath.Join(dir, "bar")})
require.NoError(t, err)
assert.False(t, arm)

if inf, ok := in.(*os.File); ok {
assert.Equal(t, filepath.Join(dir, "foo"), inf.Name())
} else {
t.Errorf("input is not a file")
}

if outf, ok := out.(*os.File); ok {
assert.Equal(t, filepath.Join(dir, "bar"), outf.Name())
} else {
t.Errorf("output is not a file")
}
})
}

0 comments on commit 6fea52e

Please sign in to comment.