main_test.go (3367B) - Raw
1 package main 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 "os" 8 "path/filepath" 9 "regexp" 10 "testing" 11 ) 12 13 func TestExecute(t *testing.T) { 14 var _foo = []byte("foo foo") 15 16 tests := []struct { 17 name string 18 fixture func(*testing.T, string) 19 flattener func(io.ReadSeeker, io.Writer) error 20 infile string 21 outfile string 22 wantErr string 23 assertion func(*testing.T, string) 24 }{ 25 { 26 name: "ok passthrough via stdout", 27 infile: "t10-in.txt", 28 fixture: func(t *testing.T, dir string) { 29 fname := filepath.Join(dir, "t10-in.txt") 30 if err := os.WriteFile(fname, _foo, 0644); err != nil { 31 t.Fatalf("unexpected error: %v", err) 32 } 33 }, 34 outfile: "-", 35 }, 36 { 37 name: "ok passthrough via file", 38 infile: "t20-in.txt", 39 fixture: func(t *testing.T, dir string) { 40 fname := filepath.Join(dir, "t20-in.txt") 41 if err := os.WriteFile(fname, _foo, 0644); err != nil { 42 t.Fatalf("unexpected error: %v", err) 43 } 44 }, 45 outfile: "t20-out.txt", 46 }, 47 { 48 name: "bad flattener should remove the file", 49 infile: "t30-in.txt", 50 fixture: func(t *testing.T, dir string) { 51 fname := filepath.Join(dir, "t30-in.txt") 52 if err := os.WriteFile(fname, _foo, 0644); err != nil { 53 t.Fatalf("unexpected error: %v", err) 54 } 55 }, 56 flattener: flattenBad, 57 outfile: "t30-out.txt", 58 wantErr: "some error", 59 assertion: func(t *testing.T, dir string) { 60 d, err := os.ReadDir(dir) 61 if err != nil { 62 t.Fatalf("unexpected error: %v", err) 63 } 64 if len(d) != 1 { 65 t.Fatalf("expected 1 entry, got %d", len(d)) 66 } 67 if d[0].Name() != "t30-in.txt" { 68 t.Fatalf("expected to find only t30-in.txt, got %s", d[0].Name()) 69 } 70 }, 71 }, 72 { 73 name: "infile does not exist", 74 infile: "t3-does-not-exist.txt", 75 wantErr: "^open .*not-exist.txt: no such file or directory$", 76 }, 77 { 78 name: "outpath dir not writable", 79 outfile: filepath.Join("t4", "does", "not", "exist"), 80 wantErr: "^create: open .*not/exist: no such file or directory", 81 }, 82 } 83 84 for _, tt := range tests { 85 t.Run(tt.name, func(t *testing.T) { 86 dir := t.TempDir() 87 var stdout bytes.Buffer 88 if tt.flattener == nil { 89 tt.flattener = flattenPassthrough 90 } 91 92 if tt.fixture != nil { 93 tt.fixture(t, dir) 94 } 95 if tt.outfile != "-" { 96 tt.outfile = filepath.Join(dir, tt.outfile) 97 } 98 inf := filepath.Join(dir, tt.infile) 99 100 c := &command{Stdout: &stdout, flattener: tt.flattener} 101 err := c.execute(inf, tt.outfile) 102 103 if tt.assertion != nil { 104 tt.assertion(t, dir) 105 } 106 107 if tt.wantErr != "" { 108 if err == nil { 109 t.Fatal("expected error, got nil") 110 } 111 r := regexp.MustCompile(tt.wantErr) 112 if r.FindStringIndex(err.Error()) == nil { 113 t.Errorf("%s not found in %s", tt.wantErr, err.Error()) 114 } 115 return 116 } 117 if err != nil { 118 t.Fatalf("unexpected error: %v", err) 119 } 120 var out []byte 121 if tt.outfile == "-" { 122 out = stdout.Bytes() 123 } else { 124 out, err = os.ReadFile(tt.outfile) 125 if err != nil { 126 t.Fatalf("unexpected error: %v", err) 127 } 128 } 129 if !bytes.Equal([]byte("foo foo"), out) { 130 t.Errorf("out != foo foo: %q", string(out)) 131 } 132 133 }) 134 } 135 } 136 137 func flattenPassthrough(r io.ReadSeeker, w io.Writer) error { 138 _, err := io.Copy(w, r) 139 return err 140 } 141 142 func flattenBad(_ io.ReadSeeker, _ io.Writer) error { 143 return errors.New("some error") 144 }