golang-github-cockroachdb-datadriven-1.0.2/0000755000175000017500000000000014304115003017311 5ustar zigozigogolang-github-cockroachdb-datadriven-1.0.2/line_scanner.go0000644000175000017500000000163014304115003022300 0ustar zigozigo// Copyright 2018 The Cockroach Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. package datadriven import ( "bufio" "io" ) type lineScanner struct { *bufio.Scanner line int } func newLineScanner(r io.Reader) *lineScanner { return &lineScanner{ Scanner: bufio.NewScanner(r), line: 0, } } func (l *lineScanner) Scan() bool { ok := l.Scanner.Scan() if ok { l.line++ } return ok } golang-github-cockroachdb-datadriven-1.0.2/go.mod0000644000175000017500000000014014304115003020412 0ustar zigozigomodule github.com/cockroachdb/datadriven go 1.17 require github.com/pmezard/go-difflib v1.0.0 golang-github-cockroachdb-datadriven-1.0.2/test_data_reader.go0000644000175000017500000000763514304115003023145 0ustar zigozigo// Copyright 2018 The Cockroach Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. package datadriven import ( "bytes" "fmt" "io" "strings" "testing" ) type testDataReader struct { sourceName string reader io.Reader scanner *lineScanner data TestData rewrite *bytes.Buffer } func newTestDataReader( t *testing.T, sourceName string, file io.Reader, record bool, ) *testDataReader { t.Helper() var rewrite *bytes.Buffer if record { rewrite = &bytes.Buffer{} } return &testDataReader{ sourceName: sourceName, reader: file, scanner: newLineScanner(file), rewrite: rewrite, } } func (r *testDataReader) Next(t *testing.T) bool { t.Helper() for r.scanner.Scan() { // Ensure to not re-initialize r.data unless a line is read // successfully. The reason is that we want to keep the last // stored value of `Pos` after encountering EOF, to produce useful // error messages. r.data = TestData{} line := r.scanner.Text() r.emit(line) // Update Pos early so that a late error message has an updated // position. pos := fmt.Sprintf("%s:%d", r.sourceName, r.scanner.line) r.data.Pos = pos line = strings.TrimSpace(line) if strings.HasPrefix(line, "#") { // Skip comment lines. continue } // Support wrapping directive lines using \, for example: // build-scalar \ // vars(int) for strings.HasSuffix(line, `\`) && r.scanner.Scan() { nextLine := r.scanner.Text() r.emit(nextLine) line = strings.TrimSuffix(line, `\`) + " " + strings.TrimSpace(nextLine) } cmd, args, err := ParseLine(line) if err != nil { t.Fatalf("%s: %v", pos, err) } if cmd == "" { // Nothing to do here. continue } r.data.Cmd = cmd r.data.CmdArgs = args if cmd == "subtest" { // Subtest directives do not have an input and expected output. return true } var buf bytes.Buffer var separator bool for r.scanner.Scan() { line := r.scanner.Text() if line == "----" { separator = true break } r.emit(line) fmt.Fprintln(&buf, line) } r.data.Input = strings.TrimSpace(buf.String()) if separator { r.readExpected(t) } r.data.Rewrite = *rewriteTestFiles return true } return false } func (r *testDataReader) readExpected(t *testing.T) { var buf bytes.Buffer var line string var allowBlankLines bool if r.scanner.Scan() { line = r.scanner.Text() if line == "----" { allowBlankLines = true } } if allowBlankLines { // Look for two successive lines of "----" before terminating. for r.scanner.Scan() { line = r.scanner.Text() if line == "----" { if r.scanner.Scan() { line2 := r.scanner.Text() if line2 == "----" { // Read the following blank line (if we don't do this, we will emit // an extra blank line when rewriting). if r.scanner.Scan() && r.scanner.Text() != "" { t.Fatal("non-blank line after end of double ---- separator section") } break } fmt.Fprintln(&buf, line) fmt.Fprintln(&buf, line2) continue } } fmt.Fprintln(&buf, line) } } else { // Terminate on first blank line. for { if strings.TrimSpace(line) == "" { break } fmt.Fprintln(&buf, line) if !r.scanner.Scan() { break } line = r.scanner.Text() } } r.data.Expected = buf.String() } func (r *testDataReader) emit(s string) { if r.rewrite != nil { r.rewrite.WriteString(s) r.rewrite.WriteString("\n") } } golang-github-cockroachdb-datadriven-1.0.2/README.md0000644000175000017500000000064114304115003020571 0ustar zigozigo# Data-Driven Tests for Go This repository implements an extension of [Table-Driven Testing]. Instead of building and iterating over a table in the test code, the input is further separated into files (or inline strings). For certain classes of tests, this can significantly reduce the friction involved in writing and reading these tests. [Table-Driven Testing]: https://github.com/golang/go/wiki/TableDrivenTests golang-github-cockroachdb-datadriven-1.0.2/datadriven_test.go0000644000175000017500000001264314304115003023026 0ustar zigozigo// Copyright 2019 The Cockroach Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. package datadriven import ( "bytes" "fmt" "io/ioutil" "os" "path/filepath" "sort" "strings" "testing" "github.com/pmezard/go-difflib/difflib" ) func TestNewLineBetweenDirectives(t *testing.T) { RunTestFromString(t, ` # Some testing of sensitivity to newlines foo ---- unknown command bar ---- unknown command bar ---- unknown command `, func(t *testing.T, d *TestData) string { if d.Input != "sentence" { return "unknown command" } return "" }) } func TestParseLine(t *testing.T) { RunTestFromString(t, ` parse xx = ---- here: cannot parse directive at column 4: xx = parse xx a=b a=c ---- "xx" [a=b a=c] parse xx a=b b=c c=(1,2,3) ---- "xx" [a=b b=c c=(1, 2, 3)] `, func(t *testing.T, d *TestData) string { cmd, args, err := ParseLine(d.Input) if err != nil { return fmt.Errorf("here: %w", err).Error() } return fmt.Sprintf("%q %+v", cmd, args) }) } func TestSkip(t *testing.T) { RunTestFromString(t, ` skip ---- # This error should never happen. error ---- `, func(t *testing.T, d *TestData) string { switch d.Cmd { case "skip": // Verify that calling t.Skip() does not fail with an API error on // testing.T. t.Skip("woo") case "error": // The skip should mask the error afterwards. t.Error("never reached") } return d.Expected }) } func TestSubTest(t *testing.T) { RunTest(t, "testdata/subtest", func(t *testing.T, d *TestData) string { switch d.Cmd { case "hello": return d.CmdArgs[0].Key + " was said" case "skip": // Verify that calling t.Skip() does not fail with an API error on // testing.T. t.Skip("woo") case "error": // The skip should mask the error afterwards. t.Error("never reached") default: t.Fatalf("unknown directive: %s", d.Cmd) } return d.Expected }) } func TestMultiLineTest(t *testing.T) { RunTest(t, "testdata/multiline", func(t *testing.T, d *TestData) string { switch d.Cmd { case "small": return `just two lines of output` case "large": return `more than five lines of output` default: t.Fatalf("unknown directive: %s", d.Cmd) } return d.Expected }) } func TestDirective(t *testing.T) { RunTest(t, "testdata/directive", func(t *testing.T, d *TestData) string { var buf bytes.Buffer fmt.Fprintf(&buf, "cmd: %s\n%d arguments\n", d.Cmd, len(d.CmdArgs)) for _, a := range d.CmdArgs { fmt.Fprintf(&buf, "key=%#v vals=%#v\n", a.Key, a.Vals) } return buf.String() }) } func TestWalk(t *testing.T) { Walk(t, "testdata/walk", func (t *testing.T, path string) { RunTest(t, path, func (t *testing.T, d *TestData) string { return fmt.Sprintf("test name: %s\n", t.Name()) }) }) } func TestRewrite(t *testing.T) { const testDir = "testdata/rewrite" files, err := ioutil.ReadDir(testDir) if err != nil { t.Fatal(err) } var tests []string for _, file := range files { if name := file.Name(); strings.HasSuffix(name, "-before") { tests = append(tests, strings.TrimSuffix(name, "-before")) } else if !strings.HasSuffix(name, "-after") { t.Fatalf("all files in %s must end in either -before or -after: %s", testDir, name) } } sort.Strings(tests) for _, test := range tests { t.Run(test, func(t *testing.T) { path := filepath.Join(testDir, fmt.Sprintf("%s-before", test)) file, err := os.OpenFile(path, os.O_RDONLY, 0644 /* irrelevant */) if err != nil { t.Fatal(err) } defer func() { _ = file.Close() }() // Implement a few simple directives. handler := func(t *testing.T, d *TestData) string { switch d.Cmd { case "noop": return d.Input case "duplicate": return fmt.Sprintf("%s\n%s", d.Input, d.Input) case "duplicate-with-blank": return fmt.Sprintf("%s\n\n%s", d.Input, d.Input) case "no-output": return "" default: t.Fatalf("unknown directive %s", d.Cmd) return "" } } rewriteData := runTestInternal(t, path, file, handler, true /* rewrite */) afterPath := filepath.Join(testDir, fmt.Sprintf("%s-after", test)) if *rewriteTestFiles { // We are rewriting the rewrite tests. Dump the output into -after files out, err := os.Create(afterPath) defer func() { _ = out.Close() }() if err != nil { t.Fatal(err) } if _, err := out.Write(rewriteData); err != nil { t.Fatal(err) } } else { after, err := os.Open(afterPath) defer func() { _ = after.Close() }() if err != nil { t.Fatal(err) } expected, err := ioutil.ReadAll(after) if err != nil { t.Fatal(err) } if string(rewriteData) != string(expected) { linesExp := difflib.SplitLines(string(expected)) linesActual := difflib.SplitLines(string(rewriteData)) diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ A: linesExp, B: linesActual, }) if err != nil { t.Fatal(err) } t.Errorf("expected didn't match actual:\n%s", diff) } } }) } } golang-github-cockroachdb-datadriven-1.0.2/datadriven.go0000644000175000017500000004570214304115003021771 0ustar zigozigo// Copyright 2018 The Cockroach Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. package datadriven import ( "flag" "fmt" "io" "io/ioutil" "os" "path/filepath" "regexp" "strconv" "strings" "testing" "github.com/pmezard/go-difflib/difflib" ) var ( rewriteTestFiles = flag.Bool( "rewrite", false, "ignore the expected results and rewrite the test files with the actual results from this "+ "run. Used to update tests when a change affects many cases; please verify the testfile "+ "diffs carefully!", ) quietLog = flag.Bool( "datadriven-quiet", false, "avoid echoing the directives and responses from test files.", ) ) // Verbose returns true iff -datadriven-quiet was not passed. func Verbose() bool { return testing.Verbose() && !*quietLog } // In CockroachDB we want to quiesce all the logs across all packages. // If we had only a flag to work with, we'd get command line parsing // errors on all packages that do not use datadriven. So // we make do by also making a command line parameter available. func init() { const quietEnvVar = "DATADRIVEN_QUIET_LOG" if str, ok := os.LookupEnv(quietEnvVar); ok { v, err := strconv.ParseBool(str) if err != nil { panic(fmt.Sprintf("error parsing %s: %s", quietEnvVar, err)) } *quietLog = v } } // RunTest invokes a data-driven test. The test cases are contained in a // separate test file and are dynamically loaded, parsed, and executed by this // testing framework. By convention, test files are typically located in a // sub-directory called "testdata". Each test file has the following format: // // [,...] [arg | arg=val | arg=(val1, val2, ...)]... // // ---- // // // The command input can contain blank lines. However, by default, the expected // results cannot contain blank lines. This alternate syntax allows the use of // blank lines: // // [,...] [arg | arg=val | arg=(val1, val2, ...)]... // // ---- // ---- // // // // ---- // ---- // // To execute data-driven tests, pass the path of the test file as well as a // function which can interpret and execute whatever commands are present in // the test file. The framework invokes the function, passing it information // about the test case in a TestData struct. // // The function must returns the actual results of the case, which // RunTest() compares with the expected results. If the two are not // equal, the test is marked to fail. // // Note that RunTest() creates a sub-instance of testing.T for each // directive in the input file. It is thus unsafe/invalid to call // e.g. Fatal() or Skip() on the parent testing.T from inside the // callback function. Use the provided testing.T instance instead. // // It is possible for a test to test for an "expected error" as follows: // - run the code to test // - if an error occurs, report the detail of the error as actual // output. // - place the expected error details in the expected results // in the input file. // // It is also possible for a test to report an _unexpected_ test // error by calling t.Error(). func RunTest(t *testing.T, path string, f func(t *testing.T, d *TestData) string) { t.Helper() mode := os.O_RDONLY if *rewriteTestFiles { // We only open read-write if rewriting, so as to enable running // tests on read-only copies of the source tree. mode = os.O_RDWR } file, err := os.OpenFile(path, mode, 0644 /* irrelevant */) if err != nil { t.Fatal(err) } defer func() { _ = file.Close() }() finfo, err := file.Stat() if err != nil { t.Fatal(err) } else if finfo.IsDir() { t.Fatalf("%s is a directory, not a file; consider using datadriven.Walk", path) } rewriteData := runTestInternal(t, path, file, f, *rewriteTestFiles) if *rewriteTestFiles { if _, err := file.WriteAt(rewriteData, 0); err != nil { t.Fatal(err) } if err := file.Truncate(int64(len(rewriteData))); err != nil { t.Fatal(err) } if err := file.Sync(); err != nil { t.Fatal(err) } } } // RunTestFromString is a version of RunTest which takes the contents of a test // directly. func RunTestFromString(t *testing.T, input string, f func(t *testing.T, d *TestData) string) { t.Helper() runTestInternal(t, "" /* sourceName */, strings.NewReader(input), f, *rewriteTestFiles) } func runTestInternal( t *testing.T, sourceName string, reader io.Reader, f func(t *testing.T, d *TestData) string, rewrite bool, ) (rewriteOutput []byte) { t.Helper() r := newTestDataReader(t, sourceName, reader, rewrite) for r.Next(t) { runDirectiveOrSubTest(t, r, "" /*mandatorySubTestPrefix*/, f) } if r.rewrite != nil { data := r.rewrite.Bytes() // Remove any trailing blank line. if l := len(data); l > 2 && data[l-1] == '\n' && data[l-2] == '\n' { data = data[:l-1] } return data } return nil } // runDirectiveOrSubTest runs either a "subtest" directive or an // actual test directive. The "mandatorySubTestPrefix" argument indicates // a mandatory prefix required from all sub-test names at this point. func runDirectiveOrSubTest( t *testing.T, r *testDataReader, mandatorySubTestPrefix string, f func(*testing.T, *TestData) string, ) { t.Helper() if subTestName, ok := isSubTestStart(t, r, mandatorySubTestPrefix); ok { runSubTest(subTestName, t, r, f) } else { runDirective(t, r, f) } if t.Failed() { // If a test has failed with .Error(), we can't expect any // subsequent test to be even able to start. Stop processing the // file in that case. t.FailNow() } } // runSubTest runs a subtest up to and including the final `subtest // end`. The opening `subtest` directive has been consumed already. // The first parameter `subTestName` is the full path to the subtest, // including the parent subtest names as prefix. This is used to // validate the nesting and thus prevent mistakes. func runSubTest( subTestName string, t *testing.T, r *testDataReader, f func(*testing.T, *TestData) string, ) { // Remember the current reader position in case we need to spell out // an error message below. subTestStartPos := r.data.Pos // seenSubTestEnd is used below to verify that a "subtest end" directive // has been detected (as opposed to EOF). seenSubTestEnd := false // seenSkip is used below to verify that "Skip" has not been used // inside a subtest. See below for details. seenSkip := false // The name passed to t.Run is the last component in the subtest // name, because all components before that are already prefixed by // t.Run from the names of the parent sub-tests. testingSubTestName := subTestName[strings.LastIndex(subTestName, "/")+1:] // Begin the sub-test. t.Run(testingSubTestName, func(t *testing.T) { defer func() { // Skips are signalled using Goexit() so we must catch it / // remember it here. if t.Skipped() { seenSkip = true } }() for r.Next(t) { if isSubTestEnd(t, r) { seenSubTestEnd = true return } runDirectiveOrSubTest(t, r, subTestName+"/" /*mandatorySubTestPrefix*/, f) } }) if seenSkip { // t.Skip() is not yet supported inside a subtest. To add // this functionality the following extra complexity is needed: // - the test reader must continue to read after the skip // until the end of the subtest, and ignore all the directives in-between. // - the rewrite logic must be careful to keep the input as-is // for the skipped sub-test, while proceeding to rewrite for // non-skipped tests. r.data.Fatalf(t, "cannot use t.Skip inside subtest\n%s: subtest started here", subTestStartPos) } if seenSubTestEnd && len(r.data.CmdArgs) == 2 && r.data.CmdArgs[1].Key != subTestName { // If a subtest name was provided after "subtest end", ensure that it matches. r.data.Fatalf(t, "mismatched subtest end directive: expected %q, got %q", r.data.CmdArgs[1].Key, subTestName) } if !seenSubTestEnd && !t.Failed() { // We only report missing "subtest end" if there was no error otherwise; // for if there was an error, the reading would have stopped. r.data.Fatalf(t, "EOF encountered without subtest end directive\n%s: subtest started here", subTestStartPos) } } func isSubTestStart(t *testing.T, r *testDataReader, mandatorySubTestPrefix string) (string, bool) { if r.data.Cmd != "subtest" { return "", false } if len(r.data.CmdArgs) != 1 { r.data.Fatalf(t, "invalid syntax for subtest") } subTestName := r.data.CmdArgs[0].Key if subTestName == "end" { r.data.Fatalf(t, "subtest end without corresponding start") } if !strings.HasPrefix(subTestName, mandatorySubTestPrefix) { r.data.Fatalf(t, "name of nested subtest must begin with %q", mandatorySubTestPrefix) } return subTestName, true } func isSubTestEnd(t *testing.T, r *testDataReader) bool { if r.data.Cmd != "subtest" { return false } if len(r.data.CmdArgs) == 0 || r.data.CmdArgs[0].Key != "end" { return false } if len(r.data.CmdArgs) > 2 { r.data.Fatalf(t, "invalid syntax for subtest end") } return true } // runDirective runs just one directive in the input. // // The stopNow and subTestSkipped booleans are modified by-reference // instead of returned because the testing module implements t.Skip // and t.Fatal using panics, and we're not guaranteed to get back to // the caller via a return in those cases. func runDirective(t *testing.T, r *testDataReader, f func(*testing.T, *TestData) string) { t.Helper() d := &r.data actual := func() string { defer func() { if r := recover(); r != nil { t.Logf("\npanic during %s:\n%s\n", d.Pos, d.Input) panic(r) } }() actual := f(t, d) if actual != "" && !strings.HasSuffix(actual, "\n") { actual += "\n" } return actual }() if t.Failed() { // If the test has failed with .Error(), then we can't hope it // will have produced a useful actual output. Trying to do // something with it here would risk corrupting the expected // output. // // Moreover, we can't expect any subsequent test to be even // able to start. Stop processing the file in that case. t.FailNow() } // The test has not failed, we can analyze the expected // output. if r.rewrite != nil { r.emit("----") if hasBlankLine(actual) { r.emit("----") r.rewrite.WriteString(actual) r.emit("----") r.emit("----") r.emit("") } else { // Here actual already ends in \n so emit adds a blank line. r.emit(actual) } } else if d.Expected != actual { expectedLines := difflib.SplitLines(d.Expected) actualLines := difflib.SplitLines(actual) if len(expectedLines) > 5 { // Print a unified diff if there is a lot of output to compare. diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ Context: 5, A: expectedLines, B: actualLines, }) if err == nil { t.Fatalf("\n%s:\n %s\noutput didn't match expected:\n%s", d.Pos, d.Input, diff) return } t.Logf("Failed to produce diff %v", err) } t.Fatalf("\n%s:\n %s\nexpected:\n%s\nfound:\n%s", d.Pos, d.Input, d.Expected, actual) } else if Verbose() { input := d.Input if input == "" { input = "" } // TODO(tbg): it's awkward to reproduce the args, but it would be helpful. t.Logf("\n%s:\n%s [%d args]\n%s\n----\n%s", d.Pos, d.Cmd, len(d.CmdArgs), input, actual) } return } // Walk goes through all the files in a subdirectory, creating subtests to match // the file hierarchy; for each "leaf" file, the given function is called. // // This can be used in conjunction with RunTest. For example: // // datadriven.Walk(t, path, func (t *testing.T, path string) { // // initialize per-test state // datadriven.RunTest(t, path, func (t *testing.T, d *datadriven.TestData) string { // // ... // } // } // // Files: // testdata/typing // testdata/logprops/scan // testdata/logprops/select // // If path is "testdata/typing", the function is called once and no subtests // are created. // // If path is "testdata/logprops", the function is called two times, in // separate subtests /scan, /select. // // If path is "testdata", the function is called three times, in subtest // hierarchy /typing, /logprops/scan, /logprops/select. // func Walk(t *testing.T, path string, f func(t *testing.T, path string)) { finfo, err := os.Stat(path) if err != nil { t.Fatal(err) } if !finfo.IsDir() { f(t, path) return } files, err := ioutil.ReadDir(path) if err != nil { t.Fatal(err) } for _, file := range files { if tempFileRe.MatchString(file.Name()) { // Temp or hidden file, don't even try processing. continue } t.Run(cutExt(file.Name()), func(t *testing.T) { Walk(t, filepath.Join(path, file.Name()), f) }) } } // cutExt returns the given file name with the extension removed, if there is // one. func cutExt(fileName string) string { extStart := len(fileName) - len(filepath.Ext(fileName)) return fileName[:extStart] } func ClearResults(path string) error { file, err := os.OpenFile(path, os.O_RDWR, 0644 /* irrelevant */) if err != nil { return err } defer func() { _ = file.Close() }() finfo, err := file.Stat() if err != nil { return err } if finfo.IsDir() { return fmt.Errorf("%s is a directory, not a file", path) } runTestInternal( &testing.T{}, path, file, func(t *testing.T, d *TestData) string { return "" }, true, /* rewrite */ ) return nil } // Ignore files named .XXXX, XXX~ or #XXX#. var tempFileRe = regexp.MustCompile(`(^\..*)|(.*~$)|(^#.*#$)`) // TestData contains information about one data-driven test case that was // parsed from the test file. type TestData struct { // Pos is a file:line prefix for the input test file, suitable for // inclusion in logs and error messages. Pos string // Cmd is the first string on the directive line (up to the first whitespace). Cmd string // CmdArgs contains the k/v arguments to the command. CmdArgs []CmdArg // Input is the text between the first directive line and the ---- separator. Input string // Expected is the value below the ---- separator. In most cases, // tests need not check this, and instead return their own actual // output. // This field is provided so that a test can perform an early return // with "return d.Expected" to signal that nothing has changed. Expected string // Rewrite is set if the test is being run with the -rewrite flag. Rewrite bool } // HasArg checks whether the CmdArgs array contains an entry for the given key. func (td *TestData) HasArg(key string) bool { for i := range td.CmdArgs { if td.CmdArgs[i].Key == key { return true } } return false } // ScanArgs looks up the first CmdArg matching the given key and scans it into // the given destinations in order. If the arg does not exist, the number of // destinations does not match that of the arguments, or a destination can not // be populated from its matching value, a fatal error results. // If the arg exists multiple times, the first occurrence is parsed. // // For example, for a TestData originating from // // cmd arg1=50 arg2=yoruba arg3=(50, 50, 50) // // the following would be valid: // // var i1, i2, i3, i4 int // var s string // td.ScanArgs(t, "arg1", &i1) // td.ScanArgs(t, "arg2", &s) // td.ScanArgs(t, "arg3", &i2, &i3, &i4) func (td *TestData) ScanArgs(t *testing.T, key string, dests ...interface{}) { t.Helper() var arg CmdArg for i := range td.CmdArgs { if td.CmdArgs[i].Key == key { arg = td.CmdArgs[i] break } } if arg.Key == "" { td.Fatalf(t, "missing argument: %s", key) } if len(dests) != len(arg.Vals) { td.Fatalf(t, "%s: got %d destinations, but %d values", arg.Key, len(dests), len(arg.Vals)) } for i := range dests { if err := arg.scanErr(i, dests[i]); err != nil { td.Fatalf(t, "%s: failed to scan argument %d: %v", arg.Key, i, err) } } } // CmdArg contains information about an argument on the directive line. An // argument is specified in one of the following forms: // - argument // - argument=value // - argument=(values, ...) type CmdArg struct { Key string Vals []string } func (arg CmdArg) String() string { switch len(arg.Vals) { case 0: return arg.Key case 1: return fmt.Sprintf("%s=%s", arg.Key, arg.Vals[0]) default: return fmt.Sprintf("%s=(%s)", arg.Key, strings.Join(arg.Vals, ", ")) } } // Scan attempts to parse the value at index i into the dest. func (arg CmdArg) Scan(t *testing.T, i int, dest interface{}) { if err := arg.scanErr(i, dest); err != nil { t.Fatal(err) } } // scanErr is like Scan but returns an error rather than taking a testing.T to fatal. func (arg CmdArg) scanErr(i int, dest interface{}) error { if i < 0 || i >= len(arg.Vals) { return fmt.Errorf("cannot scan index %d of key %s", i, arg.Key) } val := arg.Vals[i] switch dest := dest.(type) { case *string: *dest = val case *int: n, err := strconv.ParseInt(val, 10, 64) if err != nil { return err } *dest = int(n) // assume 64bit ints case *int64: n, err := strconv.ParseInt(val, 10, 64) if err != nil { return err } *dest = n case *uint64: n, err := strconv.ParseUint(val, 10, 64) if err != nil { return err } *dest = n case *bool: b, err := strconv.ParseBool(val) if err != nil { return err } *dest = b default: return fmt.Errorf("unsupported type %T for destination #%d (might be easy to add it)", dest, i+1) } return nil } // Fatalf wraps a fatal testing error with test file position information, so // that it's easy to locate the source of the error. func (td TestData) Fatalf(tb testing.TB, format string, args ...interface{}) { tb.Helper() tb.Fatalf("%s: %s", td.Pos, fmt.Sprintf(format, args...)) } // hasBlankLine returns true iff `s` contains at least one line that's // empty or contains only whitespace. func hasBlankLine(s string) bool { return blankLineRe.MatchString(s) } // blankLineRe matches lines that contain only whitespaces (or // entirely empty/blank lines). We use the "m" flag for "multiline" // mode so that "^" can match the beginning of individual lines inside // the input, not just the beginning of the input. In multiline mode, // "$" also matches the end of lines. However, note how the regexp // uses "\n" to match the end of lines instead of "$". This is // because of an oddity in the Go regexp engine: at the very end of // the input, *after the final \n in the input*, Go estimates there is // still one more line containing no characters but that matches the // "^.*$" regexp. The result of this oddity is that an input text like // "foo\n" will match as "foo\n" (no match) + "" (yes match). We don't // want that final match to be included, so we force the end-of-line // match using "\n" specifically. var blankLineRe = regexp.MustCompile(`(?m)^[\t ]*\n`) golang-github-cockroachdb-datadriven-1.0.2/LICENSE0000644000175000017500000002613514304115003020325 0ustar zigozigo Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. golang-github-cockroachdb-datadriven-1.0.2/datadriven-clear/0000755000175000017500000000000014304115003022516 5ustar zigozigogolang-github-cockroachdb-datadriven-1.0.2/datadriven-clear/main.go0000644000175000017500000000173114304115003023773 0ustar zigozigo// Copyright 2020 The Cockroach Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. package main import ( "fmt" "os" "github.com/cockroachdb/datadriven" ) func main() { if len(os.Args) == 1 { fmt.Fprintf(os.Stderr, "Usage: %s ...", os.Args[0]) os.Exit(1) } for _, path := range os.Args[1:] { err := datadriven.ClearResults(path) if err != nil { fmt.Fprintf(os.Stderr, "%v", err) os.Exit(1) } fmt.Printf("Cleared %s.\n", path) } } golang-github-cockroachdb-datadriven-1.0.2/go.sum0000644000175000017500000000026114304115003020443 0ustar zigozigogithub.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= golang-github-cockroachdb-datadriven-1.0.2/testdata/0000755000175000017500000000000014304115003021122 5ustar zigozigogolang-github-cockroachdb-datadriven-1.0.2/testdata/subtest0000644000175000017500000000024414304115003022536 0ustar zigozigohello world ---- world was said subtest hai hello universe ---- universe was said subtest hai/woo hello woo ---- woo was said subtest end hai/woo subtest end golang-github-cockroachdb-datadriven-1.0.2/testdata/walk/0000755000175000017500000000000014304115003022060 5ustar zigozigogolang-github-cockroachdb-datadriven-1.0.2/testdata/walk/ext.test0000644000175000017500000000016314304115003023561 0ustar zigozigosome-command ---- test name: TestWalk/ext subtest foo some-command ---- test name: TestWalk/ext/foo subtest end golang-github-cockroachdb-datadriven-1.0.2/testdata/walk/noext0000644000175000017500000000016714304115003023144 0ustar zigozigosome-command ---- test name: TestWalk/noext subtest foo some-command ---- test name: TestWalk/noext/foo subtest end golang-github-cockroachdb-datadriven-1.0.2/testdata/multiline0000644000175000017500000000015514304115003023050 0ustar zigozigosmall ---- ---- just two lines of output ---- ---- large ---- ---- more than five lines of output ---- ---- golang-github-cockroachdb-datadriven-1.0.2/testdata/directive0000644000175000017500000000270014304115003023022 0ustar zigozigosome-command ---- cmd: some-command 0 arguments some-command arg1 arg2=val1 arg3=(val1, val2) ---- cmd: some-command 3 arguments key="arg1" vals=[]string(nil) key="arg2" vals=[]string{"val1"} key="arg3" vals=[]string{"val1", "val2"} # Tolerate extra spaces. some-command arg1 arg2=val1 arg3=(val1, val2) ---- cmd: some-command 3 arguments key="arg1" vals=[]string(nil) key="arg2" vals=[]string{"val1"} key="arg3" vals=[]string{"val1", "val2"} # Tolerate unnecessary parens. some-command arg1=() arg2=(val) ---- cmd: some-command 2 arguments key="arg1" vals=[]string{""} key="arg2" vals=[]string{"val"} # Allow paren nesting inside values. some-command arg1=(1, (2,3), (4 * (1 + 2))) arg2=(some (nested (parens, etc)), more (nested (parens))) ---- cmd: some-command 2 arguments key="arg1" vals=[]string{"1", "(2,3)", "(4 * (1 + 2))"} key="arg2" vals=[]string{"some (nested (parens, etc))", "more (nested (parens))"} make argTuple=(1, 🍌) argInt=12 argString=greedily,impatient moreIgnore= a,b,c ---- cmd: make 5 arguments key="argTuple" vals=[]string{"1", "🍌"} key="argInt" vals=[]string{"12"} key="argString" vals=[]string{"greedily,impatient"} key="moreIgnore" vals=[]string{""} key="a,b,c" vals=[]string(nil) index-constraints vars=(a int not null, b int, c int as (a+b) stored) index=(a,) a+b = 1 ---- cmd: index-constraints 2 arguments key="vars" vals=[]string{"a int not null", "b int", "c int as (a+b) stored"} key="index" vals=[]string{"a", ""} golang-github-cockroachdb-datadriven-1.0.2/testdata/rewrite/0000755000175000017500000000000014304115003022603 5ustar zigozigogolang-github-cockroachdb-datadriven-1.0.2/testdata/rewrite/eof-1-before0000644000175000017500000000007414304115003024676 0ustar zigozigo# Case where the last directive has blank output. noop ---- golang-github-cockroachdb-datadriven-1.0.2/testdata/rewrite/basic-after0000644000175000017500000000056714304115003024716 0ustar zigozigonoop ---- noop ---- noop ---- noop some input ---- some input noop some input ---- some input duplicate some input ---- some input some input duplicate some input ---- some input some input duplicate-with-blank some input ---- ---- some input some input ---- ---- duplicate-with-blank some input ---- ---- some input some input ---- ---- no-output some input ---- golang-github-cockroachdb-datadriven-1.0.2/testdata/rewrite/eof-1-after0000644000175000017500000000007414304115003024535 0ustar zigozigo# Case where the last directive has blank output. noop ---- golang-github-cockroachdb-datadriven-1.0.2/testdata/rewrite/eof-2-before0000644000175000017500000000021414304115003024673 0ustar zigozigo# Case where the last directive has blank output (but the double-separator # syntax is used in the test file). noop ---- ---- foo ---- ---- golang-github-cockroachdb-datadriven-1.0.2/testdata/rewrite/eof-2-after0000644000175000017500000000017114304115003024534 0ustar zigozigo# Case where the last directive has blank output (but the double-separator # syntax is used in the test file). noop ---- golang-github-cockroachdb-datadriven-1.0.2/testdata/rewrite/basic-before0000644000175000017500000000047514304115003025055 0ustar zigozigonoop ---- noop ---- xxx noop ---- ---- xxx ---- ---- noop some input ---- noop some input ---- xxx duplicate some input ---- yyy duplicate some input ---- ---- yyy ---- ---- duplicate-with-blank some input ---- duplicate-with-blank some input ---- ---- some expected ---- ---- no-output some input ---- zzz golang-github-cockroachdb-datadriven-1.0.2/line_parser.go0000644000175000017500000000720714304115003022151 0ustar zigozigo// Copyright 2019 The Cockroach Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. package datadriven import ( "fmt" "strings" "unicode/utf8" ) // ParseLine parses a datadriven directive line and returns the parsed command // and CmdArgs. // // An input directive line is a command optionally followed by a list of // arguments. Arguments may or may not have values and are specified with one of // the forms: // - # No values. // - = # Single value. // - =(, , ...) # Multiple values. // // Note that in the last case, we allow the values to contain parens; the // parsing will take nesting into account. For example: // cmd exprs=(a + (b + c), d + f) // is valid and produces the expected values for the argument. // func ParseLine(line string) (cmd string, cmdArgs []CmdArg, err error) { line = strings.TrimSpace(line) if line == "" { return "", nil, nil } origLine := line defer func() { if r := recover(); r != nil { if r == (parseError{}) { column := len(origLine) - len(line) + 1 cmd = "" cmdArgs = nil err = fmt.Errorf("cannot parse directive at column %d: %s", column, origLine) // Note: to debug an unexpected parsing error, this is a good place to // add a debug.PrintStack(). } else { panic(r) } } }() // until removes the prefix up to one of the given characters from line and // returns the prefix. until := func(chars string) string { idx := strings.IndexAny(line, chars) if idx == -1 { idx = len(line) } res := line[:idx] line = line[idx:] return res } cmd = until(" ") if cmd == "" { panic(parseError{}) } line = strings.TrimSpace(line) for line != "" { var arg CmdArg arg.Key = until(" =") if arg.Key == "" { panic(parseError{}) } if line != "" && line[0] == '=' { // Skip the '='. line = line[1:] if line == "" || line[0] == ' ' { // Empty value. arg.Vals = []string{""} } else if line[0] != '(' { // Single value. val := until(" ") arg.Vals = []string{val} } else { // Skip the '('. pos := 1 nestLevel := 1 lastValStart := pos // Run through the characters for the values, being mindful of nested // parens. When we find a top-level comma, we "cut" a value and append // it to the array. for nestLevel > 0 { if pos == len(line) { // The string ended before we found the final ')'. panic(parseError{}) } r, runeSize := utf8.DecodeRuneInString(line[pos:]) pos += runeSize switch r { case ',': if nestLevel == 1 { // Found a top-level comma. arg.Vals = append(arg.Vals, line[lastValStart:pos-1]) // Skip any spaces after the comma. for pos < len(line) && line[pos] == ' ' { pos++ } lastValStart = pos } case '(': nestLevel++ case ')': nestLevel-- } } arg.Vals = append(arg.Vals, line[lastValStart:pos-1]) line = strings.TrimSpace(line[pos:]) } } cmdArgs = append(cmdArgs, arg) line = strings.TrimSpace(line) } return cmd, cmdArgs, nil } type parseError struct{}