Line data Source code
1 : // Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package main 6 : 7 : import ( 8 : "bytes" 9 : "io" 10 : "os" 11 : "path/filepath" 12 : "runtime" 13 : "testing" 14 : 15 : "github.com/cockroachdb/errors" 16 : "github.com/cockroachdb/errors/oserror" 17 : "github.com/pmezard/go-difflib/difflib" 18 : ) 19 : 20 : // filesEqual returns the diff between contents of a and b. 21 1 : func filesEqual(a, b string) error { 22 1 : aBytes, err := os.ReadFile(a) 23 1 : if err != nil { 24 0 : return err 25 0 : } 26 1 : bBytes, err := os.ReadFile(b) 27 1 : if err != nil { 28 0 : return err 29 0 : } 30 : 31 : // Normalize newlines. 32 1 : aBytes = bytes.Replace(aBytes, []byte{13, 10} /* \r\n */, []byte{10} /* \n */, -1) 33 1 : bBytes = bytes.Replace(bBytes, []byte{13, 10}, []byte{10}, -1) 34 1 : 35 1 : d, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ 36 1 : A: difflib.SplitLines(string(aBytes)), 37 1 : B: difflib.SplitLines(string(bBytes)), 38 1 : }) 39 1 : if d != "" { 40 1 : return errors.Errorf("a != b\ndiff = %s", d) 41 1 : } 42 : 43 1 : return nil 44 : } 45 : 46 : // copyDir recursively copies the fromPath to toPath, excluding certain paths. 47 1 : func copyDir(fromPath, toPath string) error { 48 1 : walkFn := func(path, pathRel string, info os.FileInfo) error { 49 1 : // Preserve the directory structure. 50 1 : if info.IsDir() { 51 1 : err := os.Mkdir(filepath.Join(toPath, pathRel), 0700) 52 1 : if err != nil && !oserror.IsNotExist(err) { 53 0 : return err 54 0 : } 55 1 : return nil 56 : } 57 : 58 : // Copy files. 59 1 : fIn, err := os.Open(path) 60 1 : if err != nil { 61 0 : return err 62 0 : } 63 1 : defer func() { _ = fIn.Close() }() 64 : 65 1 : fOut, err := os.OpenFile(filepath.Join(toPath, pathRel), os.O_CREATE|os.O_WRONLY, 0700) 66 1 : if err != nil { 67 0 : return err 68 0 : } 69 1 : defer func() { _ = fOut.Close() }() 70 : 71 1 : _, err = io.Copy(fOut, fIn) 72 1 : return err 73 : } 74 1 : return walkDir(fromPath, walkFn) 75 : } 76 : 77 1 : func maybeSkip(t *testing.T) { 78 1 : // The paths in the per-run summary.json files are UNIX-oriented. To avoid 79 1 : // duplicating the test fixtures just for Windows, just skip the tests on 80 1 : // Windows. 81 1 : if runtime.GOOS == "windows" { 82 0 : t.Skipf("skipped on windows") 83 0 : } 84 : }