| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package yts |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"encoding/json" |
| 12 |
"io" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"reflect" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"go.yaml.in/yaml/v4" |
| 20 |
"go.yaml.in/yaml/v4/internal/libyaml" |
| 21 |
) |
| 22 |
|
| 23 |
var knownFailingTests = loadKnownFailingTests() |
| 24 |
|
| 25 |
func loadKnownFailingTests() map[string]bool { |
| 26 |
fileContent, err := os.ReadFile("known-failing-tests") |
| 27 |
if err != nil { |
| 28 |
return make(map[string]bool) |
| 29 |
} |
| 30 |
|
| 31 |
lines := strings.Split(string(fileContent), "\n") |
| 32 |
knownTests := make(map[string]bool) |
| 33 |
for _, line := range lines { |
| 34 |
trimmedLine := strings.TrimSpace(line) |
| 35 |
if trimmedLine != "" { |
| 36 |
knownTests[trimmedLine] = true |
| 37 |
} |
| 38 |
} |
| 39 |
return knownTests |
| 40 |
} |
| 41 |
|
| 42 |
func shouldSkipTest(t *testing.T) { |
| 43 |
t.Helper() |
| 44 |
|
| 45 |
if os.Getenv("RUNALL") == "1" { |
| 46 |
return |
| 47 |
} |
| 48 |
name := t.Name() |
| 49 |
runFailing := os.Getenv("RUNFAILING") == "1" |
| 50 |
isKnownFailing := knownFailingTests[name] |
| 51 |
t.Logf("NAME::: %v, %v, %v", name, runFailing, isKnownFailing) |
| 52 |
|
| 53 |
switch { |
| 54 |
case runFailing && !isKnownFailing: |
| 55 |
t.Skipf("Skipping non-failing test: %s", name) |
| 56 |
case !runFailing && isKnownFailing: |
| 57 |
t.Skipf("Skipping known failing test: %s", name) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestYAMLSuite(t *testing.T) { |
| 62 |
testDir := "./testdata/data-2022-01-17" |
| 63 |
if _, err := os.Stat(testDir + "/229Q"); os.IsNotExist(err) { |
| •64 |
t.Skipf(`YTS tests require data files to be present at '%s'. |
| 65 |
Run 'make get-test-data' to download them first, |
| 66 |
or just run the tests with 'make test'.`, testDir) |
| 67 |
} |
| 68 |
runTestsInDir(t, testDir, "") |
| 69 |
} |
| 70 |
|
| 71 |
func runTestsInDir(t *testing.T, rootDir string, relPath string) { |
| 72 |
t.Helper() |
| 73 |
|
| 74 |
fullPath := filepath.Join(rootDir, relPath) |
| 75 |
entries, err := os.ReadDir(fullPath) |
| 76 |
if err != nil { |
| 77 |
t.Fatalf("Failed to read directory %s: %v", fullPath, err) |
| 78 |
} |
| 79 |
|
| 80 |
for _, entry := range entries { |
| 81 |
entryRelPath := filepath.Join(relPath, entry.Name()) |
| 82 |
entryFullPath := filepath.Join(rootDir, entryRelPath) |
| 83 |
|
| 84 |
if entry.IsDir() { |
| 85 |
|
| 86 |
if _, err := os.Stat(filepath.Join(entryFullPath, "in.yaml")); err == nil { |
| 87 |
t.Run(strings.ReplaceAll(entryRelPath, string(filepath.Separator), "-"), func(t *testing.T) { |
| 88 |
runTest(t, entryFullPath) |
| 89 |
}) |
| 90 |
} else { |
| 91 |
|
| 92 |
runTestsInDir(t, rootDir, entryRelPath) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func normalizeLineEndings(s string) string { |
| 99 |
return strings.NewReplacer( |
| 100 |
"\r", "", |
| 101 |
).Replace(s) |
| 102 |
} |
| 103 |
|
| 104 |
func getEvents(in []byte) (string, error) { |
| 105 |
return libyaml.ParserGetEvents(in) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
func loadFirstDocument(in []byte, out any) error { |
| 112 |
loader, err := yaml.NewLoader(bytes.NewReader(in)) |
| 113 |
if err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
err = loader.Load(out) |
| 117 |
if err == io.EOF { |
| 118 |
return nil |
| 119 |
} |
| 120 |
return err |
| 121 |
} |
| 122 |
|
| 123 |
func runTest(t *testing.T, testPath string) { |
| 124 |
t.Helper() |
| 125 |
|
| 126 |
|
| 127 |
descPath := filepath.Join(testPath, "===") |
| 128 |
desc, err := os.ReadFile(descPath) |
| 129 |
var testDescription string |
| 130 |
if err == nil { |
| 131 |
testDescription = string(desc) |
| 132 |
} else { |
| 133 |
testDescription = "No description available." |
| 134 |
} |
| 135 |
|
| 136 |
t.Logf("Running test: %s\nDescription: %s", testPath, testDescription) |
| 137 |
|
| 138 |
inYAMLPath := filepath.Join(testPath, "in.yaml") |
| 139 |
inYAML, err := os.ReadFile(inYAMLPath) |
| 140 |
if err != nil { |
| 141 |
t.Fatalf("Test: %s\nDescription: %s\nError: Failed to read in.yaml: %v", testPath, testDescription, err) |
| 142 |
} |
| 143 |
|
| 144 |
errorPath := filepath.Join(testPath, "error") |
| 145 |
_, err = os.Stat(errorPath) |
| 146 |
expectError := err == nil |
| 147 |
|
| 148 |
var unmarshaledValue any |
| 149 |
var unmarshalErr error |
| 150 |
|
| 151 |
t.Run("LoadTest", func(t *testing.T) { |
| 152 |
shouldSkipTest(t) |
| 153 |
unmarshalErr = loadFirstDocument(inYAML, &unmarshaledValue) |
| 154 |
|
| 155 |
if expectError { |
| 156 |
if unmarshalErr == nil { |
| 157 |
t.Errorf("Test: %s\nDescription: %s\nError: Expected unmarshal error but got none", testPath, testDescription) |
| 158 |
} |
| 159 |
return |
| 160 |
} |
| 161 |
if unmarshalErr != nil { |
| 162 |
t.Errorf("Test: %s\nDescription: %s\nError: Unexpected unmarshal error: %v", testPath, testDescription, unmarshalErr) |
| 163 |
} |
| 164 |
}) |
| 165 |
|
| 166 |
t.Run("EventComparisonTest", func(t *testing.T) { |
| 167 |
shouldSkipTest(t) |
| 168 |
expectedEventsPath := filepath.Join(testPath, "test.event") |
| 169 |
if _, err := os.Stat(expectedEventsPath); err != nil { |
| 170 |
return |
| 171 |
} |
| 172 |
expectedEventsBytes, err := os.ReadFile(expectedEventsPath) |
| 173 |
if err != nil { |
| 174 |
t.Errorf("Test: %s\nDescription: %s\nError: Failed to read test.event: %v", testPath, testDescription, err) |
| 175 |
return |
| 176 |
} |
| 177 |
expectedEvents := normalizeLineEndings(string(expectedEventsBytes)) |
| 178 |
expectedEvents = strings.TrimSuffix(expectedEvents, "\n") |
| 179 |
actualEvents, eventErr := getEvents(inYAML) |
| 180 |
|
| 181 |
if expectError { |
| 182 |
if eventErr == nil { |
| 183 |
t.Errorf("Test: %s\nDescription: %s\nError: Expected error on event parsing but got none", testPath, testDescription) |
| 184 |
} |
| 185 |
return |
| 186 |
} |
| 187 |
if eventErr != nil { |
| 188 |
t.Errorf("Test: %s\nDescription: %s\nError: Unexpected error on event parsing: %v", testPath, testDescription, eventErr) |
| 189 |
return |
| 190 |
} |
| 191 |
actualEventsStr := normalizeLineEndings(actualEvents) |
| 192 |
if actualEventsStr != expectedEvents { |
| 193 |
t.Errorf("Test: %s\nDescription: %s\nError: Event mismatch\nExpected:\n%q\nGot:\n%q", testPath, testDescription, expectedEvents, actualEventsStr) |
| 194 |
} |
| 195 |
}) |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
t.Run("MarshalTest", func(t *testing.T) { |
| 200 |
shouldSkipTest(t) |
| 201 |
var currentUnmarshaledValue any |
| 202 |
|
| 203 |
currentUnmarshalErr := loadFirstDocument(inYAML, ¤tUnmarshaledValue) |
| 204 |
|
| 205 |
if !(currentUnmarshalErr == nil || expectError) { |
| 206 |
return |
| 207 |
} |
| 208 |
marshaledYAML, marshalErr := yaml.Dump(currentUnmarshaledValue) |
| 209 |
if marshalErr != nil { |
| 210 |
t.Errorf("Test: %s\nDescription: %s\nError: Failed to marshal value: %v", testPath, testDescription, marshalErr) |
| 211 |
return |
| 212 |
} |
| 213 |
outYAMLPath := filepath.Join(testPath, "out.yaml") |
| 214 |
if _, err := os.Stat(outYAMLPath); err != nil { |
| 215 |
return |
| 216 |
} |
| 217 |
expectedOutYAML, err := os.ReadFile(outYAMLPath) |
| 218 |
if err != nil { |
| 219 |
t.Errorf("Test: %s\nDescription: %s\nError: Failed to read out.yaml: %v", testPath, testDescription, err) |
| 220 |
return |
| 221 |
} |
| 222 |
var expectedUnmarshaledValue any |
| 223 |
err = loadFirstDocument(expectedOutYAML, &expectedUnmarshaledValue) |
| 224 |
if err != nil { |
| 225 |
t.Errorf("Test: %s\nDescription: %s\nError: Failed to unmarshal out.yaml: %v", testPath, testDescription, err) |
| 226 |
return |
| 227 |
} |
| 228 |
var reUnmarshaledValue any |
| 229 |
err = loadFirstDocument(marshaledYAML, &reUnmarshaledValue) |
| 230 |
if err != nil { |
| 231 |
t.Errorf("Test: %s\nDescription: %s\nError: Failed to re-unmarshal marshaled YAML: %v", testPath, testDescription, err) |
| 232 |
} else if !reflect.DeepEqual(reUnmarshaledValue, expectedUnmarshaledValue) { |
| 233 |
t.Errorf("Test: %s\nDescription: %s\nError: Marshal output mismatch\nExpected: %+v\nGot : %+v", testPath, testDescription, expectedUnmarshaledValue, reUnmarshaledValue) |
| 234 |
} |
| 235 |
}) |
| 236 |
|
| 237 |
t.Run("JSONComparisonTest", func(t *testing.T) { |
| 238 |
shouldSkipTest(t) |
| 239 |
var currentUnmarshaledValue any |
| 240 |
|
| 241 |
currentUnmarshalErr := loadFirstDocument(inYAML, ¤tUnmarshaledValue) |
| 242 |
|
| 243 |
if !(currentUnmarshalErr == nil || expectError) { |
| 244 |
return |
| 245 |
} |
| 246 |
inJSONPath := filepath.Join(testPath, "in.json") |
| 247 |
if _, err := os.Stat(inJSONPath); err != nil { |
| 248 |
return |
| 249 |
} |
| 250 |
inJSON, err := os.ReadFile(inJSONPath) |
| 251 |
if err != nil { |
| 252 |
t.Errorf("Test: %s\nDescription: %s\nError: Failed to read in.json: %v", testPath, testDescription, err) |
| 253 |
return |
| 254 |
} |
| 255 |
var jsonValue any |
| 256 |
jsonErr := json.Unmarshal(inJSON, &jsonValue) |
| 257 |
if jsonErr != nil { |
| 258 |
t.Errorf("Test: %s\nDescription: %s\nError: Failed to unmarshal in.json: %v", testPath, testDescription, jsonErr) |
| 259 |
} else if !reflect.DeepEqual(currentUnmarshaledValue, jsonValue) { |
| 260 |
t.Errorf("Test: %s\nDescription: %s\nError: YAML unmarshal vs JSON unmarshal mismatch\nExpected: %+v\nGot : %+v", testPath, testDescription, jsonValue, currentUnmarshaledValue) |
| 261 |
} |
| 262 |
}) |
| 263 |
} |