|
| 1 | +package mage |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestArgs(t *testing.T) { |
| 9 | + stderr := &bytes.Buffer{} |
| 10 | + stdout := &bytes.Buffer{} |
| 11 | + inv := Invocation{ |
| 12 | + Dir: "./testdata/args", |
| 13 | + Stderr: stderr, |
| 14 | + Stdout: stdout, |
| 15 | + Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false"}, |
| 16 | + } |
| 17 | + code := Invoke(inv) |
| 18 | + if code != 0 { |
| 19 | + t.Log(stderr.String()) |
| 20 | + t.Fatalf("expected 1, but got %v", code) |
| 21 | + } |
| 22 | + actual := stdout.String() |
| 23 | + expected := `status |
| 24 | +saying hi bob |
| 25 | +01234 |
| 26 | +status |
| 27 | +waiting 5ms |
| 28 | +not coughing |
| 29 | +` |
| 30 | + if actual != expected { |
| 31 | + t.Fatalf("output is not expected:\n%q", actual) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestBadIntArg(t *testing.T) { |
| 36 | + stderr := &bytes.Buffer{} |
| 37 | + stdout := &bytes.Buffer{} |
| 38 | + inv := Invocation{ |
| 39 | + Dir: "./testdata/args", |
| 40 | + Stderr: stderr, |
| 41 | + Stdout: stdout, |
| 42 | + Args: []string{"count", "abc123"}, |
| 43 | + } |
| 44 | + code := Invoke(inv) |
| 45 | + if code != 2 { |
| 46 | + t.Log("stderr:", stderr) |
| 47 | + t.Log("stdout:", stdout) |
| 48 | + t.Fatalf("expected code 2, but got %v", code) |
| 49 | + } |
| 50 | + actual := stderr.String() |
| 51 | + expected := "can't convert argument \"abc123\" to int\n" |
| 52 | + |
| 53 | + if actual != expected { |
| 54 | + t.Fatalf("output is not expected:\n%q", actual) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestBadBoolArg(t *testing.T) { |
| 59 | + stderr := &bytes.Buffer{} |
| 60 | + stdout := &bytes.Buffer{} |
| 61 | + inv := Invocation{ |
| 62 | + Dir: "./testdata/args", |
| 63 | + Stderr: stderr, |
| 64 | + Stdout: stdout, |
| 65 | + Args: []string{"cough", "abc123"}, |
| 66 | + } |
| 67 | + code := Invoke(inv) |
| 68 | + if code != 2 { |
| 69 | + t.Log("stderr:", stderr) |
| 70 | + t.Log("stdout:", stdout) |
| 71 | + t.Fatalf("expected code 2, but got %v", code) |
| 72 | + } |
| 73 | + actual := stderr.String() |
| 74 | + expected := "can't convert argument \"abc123\" to bool\n" |
| 75 | + |
| 76 | + if actual != expected { |
| 77 | + t.Fatalf("output is not expected:\n%q", actual) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestBadDurationArg(t *testing.T) { |
| 82 | + stderr := &bytes.Buffer{} |
| 83 | + stdout := &bytes.Buffer{} |
| 84 | + inv := Invocation{ |
| 85 | + Dir: "./testdata/args", |
| 86 | + Stderr: stderr, |
| 87 | + Stdout: stdout, |
| 88 | + Args: []string{"wait", "abc123"}, |
| 89 | + } |
| 90 | + code := Invoke(inv) |
| 91 | + if code != 2 { |
| 92 | + t.Log("stderr:", stderr) |
| 93 | + t.Log("stdout:", stdout) |
| 94 | + t.Fatalf("expected code 2, but got %v", code) |
| 95 | + } |
| 96 | + actual := stderr.String() |
| 97 | + expected := "can't convert argument \"abc123\" to time.Duration\n" |
| 98 | + |
| 99 | + if actual != expected { |
| 100 | + t.Fatalf("output is not expected:\n%q", actual) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestMissingArgs(t *testing.T) { |
| 105 | + stderr := &bytes.Buffer{} |
| 106 | + stdout := &bytes.Buffer{} |
| 107 | + inv := Invocation{ |
| 108 | + Dir: "./testdata/args", |
| 109 | + Stderr: stderr, |
| 110 | + Stdout: stdout, |
| 111 | + Args: []string{"say", "hi"}, |
| 112 | + } |
| 113 | + code := Invoke(inv) |
| 114 | + if code != 2 { |
| 115 | + t.Log("stderr:", stderr) |
| 116 | + t.Log("stdout:", stdout) |
| 117 | + t.Fatalf("expected code 2, but got %v", code) |
| 118 | + } |
| 119 | + actual := stderr.String() |
| 120 | + expected := "not enough arguments for target \"Say\", expected 2, got 1\n" |
| 121 | + |
| 122 | + if actual != expected { |
| 123 | + t.Fatalf("output is not expected:\n%q", actual) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestDocs(t *testing.T) { |
| 128 | + stderr := &bytes.Buffer{} |
| 129 | + stdout := &bytes.Buffer{} |
| 130 | + inv := Invocation{ |
| 131 | + Dir: "./testdata/args", |
| 132 | + Stderr: stderr, |
| 133 | + Stdout: stdout, |
| 134 | + Help: true, |
| 135 | + Args: []string{"say"}, |
| 136 | + } |
| 137 | + code := Invoke(inv) |
| 138 | + if code != 0 { |
| 139 | + t.Log("stderr:", stderr) |
| 140 | + t.Log("stdout:", stdout) |
| 141 | + t.Fatalf("expected code 0, but got %v", code) |
| 142 | + } |
| 143 | + actual := stdout.String() |
| 144 | + expected := `Say says something. It's pretty cool. I think you should try it. |
| 145 | +
|
| 146 | +Usage: |
| 147 | +
|
| 148 | + mage say <msg> <name> |
| 149 | +
|
| 150 | +Aliases: speak |
| 151 | +
|
| 152 | +` |
| 153 | + if actual != expected { |
| 154 | + t.Fatalf("output is not expected:\n%q", actual) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestMgF(t *testing.T) { |
| 159 | + stderr := &bytes.Buffer{} |
| 160 | + stdout := &bytes.Buffer{} |
| 161 | + inv := Invocation{ |
| 162 | + Dir: "./testdata/args", |
| 163 | + Stderr: stderr, |
| 164 | + Stdout: stdout, |
| 165 | + Args: []string{"HasDep"}, |
| 166 | + } |
| 167 | + code := Invoke(inv) |
| 168 | + if code != 0 { |
| 169 | + t.Log("stderr:", stderr) |
| 170 | + t.Log("stdout:", stdout) |
| 171 | + t.Fatalf("expected code 0, but got %v", code) |
| 172 | + } |
| 173 | + actual := stdout.String() |
| 174 | + expected := "saying hi Susan\n" |
| 175 | + if actual != expected { |
| 176 | + t.Fatalf("output is not expected: %q", actual) |
| 177 | + } |
| 178 | +} |
0 commit comments