| 1 |
package approvaltests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"encoding/xml" |
| 12 |
"reflect" |
| 13 |
|
| 14 |
"github.com/approvals/go-approval-tests/reporters" |
| 15 |
"github.com/approvals/go-approval-tests/utils" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
defaultReporter = reporters.NewDiffReporter() |
| 20 |
defaultFrontLoadedReporter = reporters.NewFrontLoadedReporter() |
| 21 |
) |
| 22 |
|
| 23 |
// Failable is an interface wrapper around testing.T |
| 24 |
type Failable interface { |
| 25 |
Fail() |
| 26 |
} |
| 27 |
|
| 28 |
// VerifyWithExtension Example: |
| 29 |
// VerifyWithExtension(t, strings.NewReader("Hello"), ".txt") |
| 30 |
func VerifyWithExtension(t Failable, reader io.Reader, extWithDot string) error { |
| 31 |
namer, err := getApprovalName() |
| 32 |
if err != nil { |
| 33 |
return err |
| 34 |
} |
| 35 |
|
| 36 |
reporter := getReporter() |
| 37 |
err = namer.compare(namer.getApprovalFile(extWithDot), namer.getReceivedFile(extWithDot), reader) |
| 38 |
if err != nil { |
| 39 |
reporter.Report(namer.getApprovalFile(extWithDot), namer.getReceivedFile(extWithDot)) |
| 40 |
t.Fail() |
| 41 |
} else { |
| 42 |
os.Remove(namer.getReceivedFile(extWithDot)) |
| 43 |
} |
| 44 |
|
| 45 |
return err |
| 46 |
} |
| 47 |
|
| 48 |
// Verify Example: |
| 49 |
// Verify(t, strings.NewReader("Hello")) |
| 50 |
func Verify(t Failable, reader io.Reader) error { |
| 51 |
return VerifyWithExtension(t, reader, ".txt") |
| 52 |
} |
| 53 |
|
| 54 |
// VerifyString stores the passed string into the received file and confirms |
| 55 |
// that it matches the approved local file. On failure, it will launch a reporter. |
| 56 |
func VerifyString(t Failable, s string) error { |
| 57 |
reader := strings.NewReader(s) |
| 58 |
return Verify(t, reader) |
| 59 |
} |
| 60 |
|
| 61 |
// VerifyXMLStruct Example: |
| 62 |
// VerifyXMLStruct(t, xml) |
| 63 |
func VerifyXMLStruct(t Failable, obj interface{}) error { |
| 64 |
xmlb, err := xml.MarshalIndent(obj, "", " ") |
| 65 |
if err != nil { |
| 66 |
tip := "" |
| 67 |
if reflect.TypeOf(obj).Name() == "" { |
| 68 |
tip = "when using anonymous types be sure to include\n XMLName xml.Name `xml:\"Your_Name_Here\"`\n" |
| 69 |
} |
| 70 |
message := fmt.Sprintf("error while pretty printing XML\n%verror:\n %v\nXML:\n %v\n", tip, err, obj) |
| 71 |
return VerifyWithExtension(t, strings.NewReader(message), ".xml") |
| 72 |
} |
| 73 |
|
| 74 |
return VerifyWithExtension(t, bytes.NewReader(xmlb), ".xml") |
| 75 |
} |
| 76 |
|
| 77 |
// VerifyXMLBytes Example: |
| 78 |
// VerifyXMLBytes(t, []byte("<Test/>")) |
| 79 |
func VerifyXMLBytes(t Failable, bs []byte) error { |
| 80 |
type node struct { |
| 81 |
Attr []xml.Attr |
| 82 |
XMLName xml.Name |
| 83 |
Children []node `xml:",any"` |
| 84 |
Text string `xml:",chardata"` |
| 85 |
} |
| 86 |
x := node{} |
| 87 |
|
| 88 |
err := xml.Unmarshal(bs, &x) |
| 89 |
if err != nil { |
| 90 |
message := fmt.Sprintf("error while parsing XML\nerror:\n %s\nXML:\n %s\n", err, string(bs)) |
| 91 |
return VerifyWithExtension(t, strings.NewReader(message), ".xml") |
| 92 |
} |
| 93 |
|
| 94 |
return VerifyXMLStruct(t, x) |
| 95 |
} |
| 96 |
|
| 97 |
// VerifyJSONStruct Example: |
| 98 |
// VerifyJSONStruct(t, json) |
| 99 |
func VerifyJSONStruct(t Failable, obj interface{}) error { |
| 100 |
jsonb, err := json.MarshalIndent(obj, "", " ") |
| 101 |
if err != nil { |
| 102 |
message := fmt.Sprintf("error while pretty printing JSON\nerror:\n %s\nJSON:\n %s\n", err, obj) |
| 103 |
return VerifyWithExtension(t, strings.NewReader(message), ".json") |
| 104 |
} |
| 105 |
|
| 106 |
return VerifyWithExtension(t, bytes.NewReader(jsonb), ".json") |
| 107 |
} |
| 108 |
|
| 109 |
// VerifyJSONBytes Example: |
| 110 |
// VerifyJSONBytes(t, []byte("{ \"Greeting\": \"Hello\" }")) |
| 111 |
func VerifyJSONBytes(t Failable, bs []byte) error { |
| 112 |
var obj map[string]interface{} |
| 113 |
err := json.Unmarshal(bs, &obj) |
| 114 |
if err != nil { |
| 115 |
message := fmt.Sprintf("error while parsing JSON\nerror:\n %s\nJSON:\n %s\n", err, string(bs)) |
| 116 |
return VerifyWithExtension(t, strings.NewReader(message), ".json") |
| 117 |
} |
| 118 |
|
| 119 |
return VerifyJSONStruct(t, obj) |
| 120 |
} |
| 121 |
|
| 122 |
// VerifyMap Example: |
| 123 |
// VerifyMap(t, map[string][string] { "dog": "bark" }) |
| 124 |
func VerifyMap(t Failable, m interface{}) error { |
| 125 |
outputText := utils.PrintMap(m) |
| 126 |
return VerifyString(t, outputText) |
| 127 |
} |
| 128 |
|
| 129 |
// VerifyArray Example: |
| 130 |
// VerifyArray(t, []string{"dog", "cat"}) |
| 131 |
func VerifyArray(t Failable, array interface{}) error { |
| 132 |
outputText := utils.PrintArray(array) |
| 133 |
return VerifyString(t, outputText) |
| 134 |
} |
| 135 |
|
| 136 |
// VerifyAll Example: |
| 137 |
// VerifyAll(t, "uppercase", []string("dog", "cat"}, func(x interface{}) string { return strings.ToUpper(x.(string)) }) |
| 138 |
func VerifyAll(t Failable, header string, collection interface{}, transform func(interface{}) string) error { |
| 139 |
if len(header) != 0 { |
| 140 |
header = fmt.Sprintf("%s\n\n\n", header) |
| 141 |
} |
| 142 |
|
| 143 |
outputText := header + strings.Join(utils.MapToString(collection, transform), "\n") |
| 144 |
return VerifyString(t, outputText) |
| 145 |
} |
| 146 |
|
| 147 |
type reporterCloser struct { |
| 148 |
reporter *reporters.Reporter |
| 149 |
} |
| 150 |
|
| 151 |
func (s *reporterCloser) Close() error { |
| 152 |
defaultReporter = s.reporter |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
type frontLoadedReporterCloser struct { |
| 157 |
reporter *reporters.Reporter |
| 158 |
} |
| 159 |
|
| 160 |
func (s *frontLoadedReporterCloser) Close() error { |
| 161 |
defaultFrontLoadedReporter = s.reporter |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
// UseReporter configures which reporter to use on failure. |
| 166 |
// Add at the test or method level to configure your reporter. |
| 167 |
// |
| 168 |
// The following examples shows how to use a reporter for all of your test cases |
| 169 |
// in a package directory through go's setup feature. |
| 170 |
// |
| 171 |
// |
| 172 |
// func TestMain(m *testing.M) { |
| 173 |
// r := UseReporter(reporters.NewBeyondCompareReporter()) |
| 174 |
// defer r.Close() |
| 175 |
// |
| 176 |
// os.Exit(m.Run()) |
| 177 |
// } |
| 178 |
// |
| 179 |
func UseReporter(reporter reporters.Reporter) io.Closer { |
| 180 |
closer := &reporterCloser{ |
| 181 |
reporter: defaultReporter, |
| 182 |
} |
| 183 |
|
| 184 |
defaultReporter = &reporter |
| 185 |
return closer |
| 186 |
} |
| 187 |
|
| 188 |
// UseFrontLoadedReporter configures reporters ahead of all other reporters to |
| 189 |
// handle situations like CI. These reporters usually prevent reporting in |
| 190 |
// scenarios that are headless. |
| 191 |
func UseFrontLoadedReporter(reporter reporters.Reporter) io.Closer { |
| 192 |
closer := &frontLoadedReporterCloser{ |
| 193 |
reporter: defaultFrontLoadedReporter, |
| 194 |
} |
| 195 |
|
| 196 |
defaultFrontLoadedReporter = &reporter |
| 197 |
return closer |
| 198 |
} |
| 199 |
|
| 200 |
func getReporter() reporters.Reporter { |
| 201 |
return reporters.NewFirstWorkingReporter( |
| 202 |
*defaultFrontLoadedReporter, |
| 203 |
*defaultReporter, |
| 204 |
) |
| 205 |
} |