| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"path/filepath" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestNewDefaultDumper(t *testing.T) { |
| 11 |
if newDefaultDumper() == nil { |
| 12 |
t.Fatal("Dumper instance should not be nil") |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
func TestNotEnoughArgs(t *testing.T) { |
| 17 |
b := &bytes.Buffer{} |
| 18 |
d := &dumper{ |
| 19 |
errOut: b, |
| 20 |
} |
| 21 |
exitCode := d.dump(nil) |
| 22 |
if exitCode != 1 { |
| 23 |
t.Fatal("Should have returned an exit code of 1, failed") |
| 24 |
} |
| 25 |
if b.String() != "Missing filename argument\n" { |
| 26 |
t.Fatal("Should be missing filename argument, failed") |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestFailedToOpenFile(t *testing.T) { |
| 31 |
b := &bytes.Buffer{} |
| 32 |
d := &dumper{ |
| 33 |
errOut: b, |
| 34 |
} |
| 35 |
exitCode := d.dump([]string{"", ""}) |
| 36 |
if exitCode != 1 { |
| 37 |
t.Fatal("Should have returned an exit code of 1, failed") |
| 38 |
} |
| 39 |
if !strings.HasPrefix(b.String(), "Failed to open file") { |
| 40 |
t.Fatal("Should have failed to open file, failed") |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestFailedToParseFile(t *testing.T) { |
| 45 |
b := &bytes.Buffer{} |
| 46 |
d := &dumper{ |
| 47 |
errOut: b, |
| 48 |
} |
| 49 |
exitCode := d.dump([]string{"", filepath.Join("..", "..", "testdata", "mail", "erroneous.raw")}) |
| 50 |
if exitCode != 1 { |
| 51 |
t.Fatal("Should have returned an exit code of 1, failed") |
| 52 |
} |
| 53 |
if !strings.HasPrefix(b.String(), "Failed to read envelope") { |
| 54 |
t.Fatal("Should have failed to parse file, but couldn't find error message") |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestSuccess(t *testing.T) { |
| 59 |
b := &bytes.Buffer{} |
| 60 |
s := &bytes.Buffer{} |
| 61 |
d := &dumper{ |
| 62 |
errOut: b, |
| 63 |
stdOut: s, |
| 64 |
} |
| 65 |
exitCode := d.dump([]string{"", filepath.Join("..", "..", "testdata", "mail", "attachment.raw")}) |
| 66 |
if exitCode != 0 { |
| 67 |
t.Fatal("Should have returned an exit code of 0, failed") |
| 68 |
} |
| 69 |
if b.Len() > 0 { |
| 70 |
t.Fatal("Should not have produced any errors, failed") |
| 71 |
} |
| 72 |
if !(s.Len() > 0) { |
| 73 |
t.Fatal("Should have printed markdown document, failed") |
| 74 |
} |
| 75 |
} |