| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | cmds "github.com/ipfs/go-ipfs-cmds" |
| 8 | ) |
| 9 | |
| 10 | func TestGetOutputPath(t *testing.T) { |
| 11 | cases := []struct { |
| 12 | args []string |
| 13 | opts cmds.OptMap |
| 14 | outPath string |
| 15 | }{ |
| 16 | { |
| 17 | args: []string{"/ipns/multiformats.io/"}, |
| 18 | opts: map[string]any{ |
| 19 | "output": "takes-precedence", |
| 20 | }, |
| 21 | outPath: "takes-precedence", |
| 22 | }, |
| 23 | { |
| 24 | args: []string{"/ipns/multiformats.io/", "some-other-arg-to-be-ignored"}, |
| 25 | opts: cmds.OptMap{ |
| 26 | "output": "takes-precedence", |
| 27 | }, |
| 28 | outPath: "takes-precedence", |
| 29 | }, |
| 30 | { |
| 31 | args: []string{"/ipns/multiformats.io/"}, |
| 32 | outPath: "multiformats.io", |
| 33 | opts: cmds.OptMap{}, |
| 34 | }, |
| 35 | { |
| 36 | args: []string{"/ipns/multiformats.io/logo.svg/"}, |
| 37 | outPath: "logo.svg", |
| 38 | opts: cmds.OptMap{}, |
| 39 | }, |
| 40 | { |
| 41 | args: []string{"/ipns/multiformats.io", "some-other-arg-to-be-ignored"}, |
| 42 | outPath: "multiformats.io", |
| 43 | opts: cmds.OptMap{}, |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | _, err := GetCmd.GetOptions([]string{}) |
| 48 | if err != nil { |
| 49 | t.Fatalf("error getting default command options: %v", err) |
| 50 | } |
| 51 | |
| 52 | for i, tc := range cases { |
| 53 | t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) { |
| 54 | ctx := t.Context() |
| 55 | |
| 56 | req, err := cmds.NewRequest(ctx, []string{}, tc.opts, tc.args, nil, GetCmd) |
| 57 | if err != nil { |
| 58 | t.Fatalf("error creating a command request: %v", err) |
| 59 | } |
| 60 | |
| 61 | if outPath := getOutPath(req); outPath != tc.outPath { |
| 62 | t.Errorf("expected outPath %s to be %s", outPath, tc.outPath) |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | } |