github.com/dolthub/go-mysql-server@v0.18.0/enginetest/sqllogictest/main.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 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 main 16 17 import ( 18 "encoding/csv" 19 "fmt" 20 "os" 21 22 "github.com/dolthub/sqllogictest/go/logictest" 23 24 "github.com/dolthub/go-mysql-server/enginetest" 25 "github.com/dolthub/go-mysql-server/enginetest/sqllogictest/harness" 26 ) 27 28 type MemoryResultRecord struct { 29 TestFile string 30 LineNum int 31 Query string 32 Duration int64 33 Result string 34 ErrorMessage string 35 } 36 37 func main() { 38 args := os.Args[1:] 39 40 if len(args) < 1 { 41 panic("Usage: logictest (run|parse) file1 file2 ...") 42 } 43 44 if args[0] == "run" { 45 h := harness.NewMemoryHarness(enginetest.NewDefaultMemoryHarness()) 46 logictest.RunTestFiles(h, args[1:]...) 47 } else if args[0] == "parse" { 48 if len(args) < 2 { 49 panic("Usage: logictest parse (file | dir/)") 50 } 51 parseTestResults(args[1]) 52 } else { 53 panic("Unrecognized command " + args[0]) 54 } 55 } 56 57 func parseTestResults(f string) { 58 entries, err := logictest.ParseResultFile(f) 59 if err != nil { 60 panic(err) 61 } 62 63 records := make([]*MemoryResultRecord, len(entries)) 64 for i, e := range entries { 65 records[i] = newMemoryRecordResult(e) 66 } 67 68 err = writeResultsCsv(records) 69 if err != nil { 70 panic(err) 71 } 72 } 73 74 // fromResultCsvHeaders returns supported csv headers for a Result 75 func fromResultCsvHeaders() []string { 76 return []string{ 77 "test_file", 78 "line_num", 79 "query_string", 80 "duration", 81 "result", 82 "error_message", 83 } 84 } 85 86 // writeResultsCsv writes []*MemoryResultRecord to stdout in csv format 87 func writeResultsCsv(results []*MemoryResultRecord) (err error) { 88 csvWriter := csv.NewWriter(os.Stdout) 89 90 // write header 91 headers := fromResultCsvHeaders() 92 if err := csvWriter.Write(headers); err != nil { 93 return err 94 } 95 96 // write rows 97 for _, r := range results { 98 row := make([]string, 0) 99 for _, field := range headers { 100 val, err := fromHeaderColumnValue(field, r) 101 if err != nil { 102 return err 103 } 104 row = append(row, val) 105 } 106 err = csvWriter.Write(row) 107 if err != nil { 108 return err 109 } 110 } 111 112 csvWriter.Flush() 113 if err := csvWriter.Error(); err != nil { 114 return err 115 } 116 return 117 } 118 119 func newMemoryRecordResult(e *logictest.ResultLogEntry) *MemoryResultRecord { 120 var result string 121 switch e.Result { 122 case logictest.Ok: 123 result = "ok" 124 case logictest.NotOk: 125 result = "not ok" 126 case logictest.Skipped: 127 result = "skipped" 128 case logictest.Timeout: 129 result = "timeout" 130 case logictest.DidNotRun: 131 result = "did not run" 132 } 133 return &MemoryResultRecord{ 134 TestFile: e.TestFile, 135 LineNum: e.LineNum, 136 Query: e.Query, 137 Duration: e.Duration.Milliseconds(), 138 Result: result, 139 ErrorMessage: e.ErrorMessage, 140 } 141 } 142 143 // fromHeaderColumnValue returns the value from the DoltResultRecord for the given 144 // header field 145 func fromHeaderColumnValue(h string, r *MemoryResultRecord) (string, error) { 146 var val string 147 switch h { 148 case "test_file": 149 val = r.TestFile 150 case "line_num": 151 val = fmt.Sprintf("%d", r.LineNum) 152 case "query_string": 153 val = r.Query 154 case "duration": 155 val = fmt.Sprintf("%d", r.Duration) 156 case "result": 157 val = r.Result 158 case "error_message": 159 val = r.ErrorMessage 160 default: 161 return "", fmt.Errorf("unsupported header field") 162 } 163 return val, nil 164 }