get_test.go 1.5 KB
Newer Older
1 2 3
package commands

import (
4
	"context"
5
	"fmt"
6 7
	"testing"

Jakub Sztandera's avatar
Jakub Sztandera committed
8
	cmds "github.com/ipfs/go-ipfs-cmds"
9 10 11 12 13
)

func TestGetOutputPath(t *testing.T) {
	cases := []struct {
		args    []string
Steven Allen's avatar
Steven Allen committed
14
		opts    cmds.OptMap
15 16 17 18 19 20 21 22 23 24 25
		outPath string
	}{
		{
			args: []string{"/ipns/multiformats.io/"},
			opts: map[string]interface{}{
				"output": "takes-precedence",
			},
			outPath: "takes-precedence",
		},
		{
			args: []string{"/ipns/multiformats.io/", "some-other-arg-to-be-ignored"},
Steven Allen's avatar
Steven Allen committed
26
			opts: cmds.OptMap{
27 28 29 30 31 32 33
				"output": "takes-precedence",
			},
			outPath: "takes-precedence",
		},
		{
			args:    []string{"/ipns/multiformats.io/"},
			outPath: "multiformats.io",
Steven Allen's avatar
Steven Allen committed
34
			opts:    cmds.OptMap{},
35 36 37 38
		},
		{
			args:    []string{"/ipns/multiformats.io/logo.svg/"},
			outPath: "logo.svg",
Steven Allen's avatar
Steven Allen committed
39
			opts:    cmds.OptMap{},
40 41 42 43
		},
		{
			args:    []string{"/ipns/multiformats.io", "some-other-arg-to-be-ignored"},
			outPath: "multiformats.io",
Steven Allen's avatar
Steven Allen committed
44
			opts:    cmds.OptMap{},
45 46 47
		},
	}

keks's avatar
cleanup  
keks committed
48 49 50 51
	_, err := GetCmd.GetOptions([]string{})
	if err != nil {
		t.Fatalf("error getting default command options: %v", err)
	}
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66
	for i, tc := range cases {
		t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) {
			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()

			req, err := cmds.NewRequest(ctx, []string{}, tc.opts, tc.args, nil, GetCmd)
			if err != nil {
				t.Fatalf("error creating a command request: %v", err)
			}

			if outPath := getOutPath(req); outPath != tc.outPath {
				t.Errorf("expected outPath %s to be %s", outPath, tc.outPath)
			}
		})
67 68
	}
}