|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package testhelpers |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "io/fs" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/google/go-cmp/cmp" |
| 28 | +) |
| 29 | + |
| 30 | +func RunGoldenTests(t *testing.T, basedir string, fn func(t *testing.T, dir string)) { |
| 31 | + files, err := ioutil.ReadDir(basedir) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("ReadDir(%q) failed: %v", basedir, err) |
| 34 | + } |
| 35 | + for _, file := range files { |
| 36 | + name := file.Name() |
| 37 | + absPath := filepath.Join(basedir, name) |
| 38 | + t.Run(name, func(t *testing.T) { |
| 39 | + fn(t, absPath) |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func MustReadFile(t *testing.T, p string) []byte { |
| 45 | + b, err := ioutil.ReadFile(p) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("failed to read file %q: %v", p, err) |
| 48 | + } |
| 49 | + return b |
| 50 | +} |
| 51 | + |
| 52 | +func CompareGoldenFile(t *testing.T, p string, got []byte) { |
| 53 | + if os.Getenv("WRITE_GOLDEN_OUTPUT") != "" { |
| 54 | + // Short-circuit when the output is correct |
| 55 | + b, err := ioutil.ReadFile(p) |
| 56 | + if err == nil && bytes.Equal(b, got) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + if err := ioutil.WriteFile(p, got, 0644); err != nil { |
| 61 | + t.Fatalf("failed to write golden output %s: %v", p, err) |
| 62 | + } |
| 63 | + t.Errorf("wrote output to %s", p) |
| 64 | + } else { |
| 65 | + want := MustReadFile(t, p) |
| 66 | + if diff := cmp.Diff(string(want), string(got)); diff != "" { |
| 67 | + t.Errorf("unexpected diff in %s: %s", p, diff) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func CopyDir(src, dest string) error { |
| 73 | + srcFiles, err := ioutil.ReadDir(src) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("ReadDir(%q) failed: %w", src, err) |
| 76 | + } |
| 77 | + for _, srcFile := range srcFiles { |
| 78 | + srcPath := filepath.Join(src, srcFile.Name()) |
| 79 | + destPath := filepath.Join(dest, srcFile.Name()) |
| 80 | + if srcFile.IsDir() { |
| 81 | + if err := CopyDir(srcPath, destPath); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + } else { |
| 85 | + if err := CopyFile(srcPath, destPath); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func CopyFile(src, dest string) error { |
| 94 | + in, err := os.Open(src) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("OpenFile(%q) failed: %w", src, err) |
| 97 | + } |
| 98 | + defer in.Close() |
| 99 | + |
| 100 | + out, err := os.Create(dest) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("Create(%q) failed: %w", dest, err) |
| 103 | + } |
| 104 | + |
| 105 | + if _, err := io.Copy(out, in); err != nil { |
| 106 | + out.Close() |
| 107 | + return fmt.Errorf("byte copy from %s to %s failed: %w", src, dest, err) |
| 108 | + } |
| 109 | + |
| 110 | + if err := out.Close(); err != nil { |
| 111 | + return fmt.Errorf("Close(%q) failed: %w", dest, err) |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func CompareDir(t *testing.T, expectDir, actualDir string) { |
| 118 | + expectFiles, err := ioutil.ReadDir(expectDir) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("failed to read expectation directory %q: %v", expectDir, err) |
| 121 | + } |
| 122 | + expectFileMap := make(map[string]fs.FileInfo) |
| 123 | + for _, expectFile := range expectFiles { |
| 124 | + expectFileMap[expectFile.Name()] = expectFile |
| 125 | + } |
| 126 | + |
| 127 | + actualFiles, err := ioutil.ReadDir(actualDir) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("failed to read actual directory %q: %v", actualDir, err) |
| 130 | + } |
| 131 | + actualFileMap := make(map[string]fs.FileInfo) |
| 132 | + for _, actualFile := range actualFiles { |
| 133 | + actualFileMap[actualFile.Name()] = actualFile |
| 134 | + } |
| 135 | + |
| 136 | + for _, expectFile := range expectFiles { |
| 137 | + name := expectFile.Name() |
| 138 | + actualFile := actualFileMap[name] |
| 139 | + if actualFile == nil { |
| 140 | + t.Errorf("expected file %s not found", name) |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + if expectFile.IsDir() { |
| 145 | + if !actualFile.IsDir() { |
| 146 | + t.Errorf("expected file %s was not a directory", name) |
| 147 | + continue |
| 148 | + } |
| 149 | + CompareDir(t, filepath.Join(expectDir, name), filepath.Join(actualDir, name)) |
| 150 | + } else { |
| 151 | + if actualFile.IsDir() { |
| 152 | + t.Errorf("expected file %s was not a file", name) |
| 153 | + continue |
| 154 | + } |
| 155 | + CompareFile(t, expectDir, actualDir, name) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + for _, actualFile := range actualFiles { |
| 160 | + name := actualFile.Name() |
| 161 | + expectFile := expectFileMap[name] |
| 162 | + if expectFile == nil { |
| 163 | + t.Errorf("additional file %s found in output", name) |
| 164 | + continue |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func CompareFile(t *testing.T, expectDir, actualDir string, relPath string) { |
| 170 | + expectAbs := filepath.Join(expectDir, relPath) |
| 171 | + |
| 172 | + actualAbs := filepath.Join(actualDir, relPath) |
| 173 | + actualBytes, err := ioutil.ReadFile(actualAbs) |
| 174 | + if err != nil { |
| 175 | + if os.IsNotExist(err) { |
| 176 | + t.Errorf("expected file %s not found", relPath) |
| 177 | + } else { |
| 178 | + t.Fatalf("error reading file %s: %v", actualAbs, err) |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + CompareGoldenFile(t, expectAbs, actualBytes) |
| 183 | +} |
0 commit comments