| 32 | ) |
| 33 | |
| 34 | func TestReadFile(t *testing.T) { |
| 35 | test := func(file string, expected map[string][]string) { |
| 36 | t.Helper() |
| 37 | |
| 38 | f, err := os.CreateTemp("", "maddy-tests-") |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | defer func(name string) { |
| 43 | err := os.Remove(name) |
| 44 | if err != nil { |
| 45 | t.Log(err) |
| 46 | } |
| 47 | }(f.Name()) |
| 48 | defer func(f *os.File) { |
| 49 | err := f.Close() |
| 50 | if err != nil { |
| 51 | t.Log(err) |
| 52 | } |
| 53 | }(f) |
| 54 | if _, err := f.WriteString(file); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | |
| 58 | actual := map[string][]string{} |
| 59 | err = readFile(f.Name(), actual) |
| 60 | if expected == nil { |
| 61 | if err == nil { |
| 62 | t.Errorf("expected failure, got %+v", actual) |
| 63 | } |
| 64 | return |
| 65 | } |
| 66 | if err != nil { |
| 67 | t.Errorf("unexpected failure: %v", err) |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | if !reflect.DeepEqual(actual, expected) { |
| 72 | t.Errorf("wrong results\n want %+v\n got %+v", expected, actual) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | test("a: b", map[string][]string{"a": {"b"}}) |
| 77 | test("a@example.org: b@example.com", map[string][]string{"a@example.org": {"b@example.com"}}) |
| 78 | test(`"a @ a"@example.org: b@example.com`, map[string][]string{`"a @ a"@example.org`: {"b@example.com"}}) |
| 79 | test(`a@example.org: "b @ b"@example.com`, map[string][]string{`a@example.org`: {`"b @ b"@example.com`}}) |
| 80 | test(`"a @ a": "b @ b"`, map[string][]string{`"a @ a"`: {`"b @ b"`}}) |
| 81 | test("a: b, c", map[string][]string{"a": {"b", "c"}}) |
| 82 | test("a: b\na: c", map[string][]string{"a": {"b", "c"}}) |
| 83 | test(": b", nil) |
| 84 | test(":", nil) |
| 85 | test("aaa", map[string][]string{"aaa": {""}}) |
| 86 | test(": b", nil) |
| 87 | test(" testing@example.com : arbitrary-whitespace@example.org ", |
| 88 | map[string][]string{"testing@example.com": {"arbitrary-whitespace@example.org"}}) |
| 89 | test(`# skip comments |
| 90 | a: b`, map[string][]string{"a": {"b"}}) |
| 91 | test(`# and empty lines |