| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package kusttest_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"sigs.k8s.io/kustomize/api/resmap" |
| 12 |
) |
| 13 |
|
| 14 |
type hasGetT interface { |
| 15 |
GetT() *testing.T |
| 16 |
} |
| 17 |
|
| 18 |
func assertActualEqualsExpectedWithTweak( |
| 19 |
ht hasGetT, |
| 20 |
m resmap.ResMap, |
| 21 |
tweaker func([]byte) []byte, expected string) { |
| 22 |
AssertActualEqualsExpectedWithTweak(ht.GetT(), m, tweaker, expected) |
| 23 |
} |
| 24 |
|
| 25 |
func AssertActualEqualsExpectedWithTweak( |
| 26 |
t *testing.T, |
| 27 |
m resmap.ResMap, |
| 28 |
tweaker func([]byte) []byte, expected string) { |
| 29 |
t.Helper() |
| 30 |
if m == nil { |
| 31 |
t.Fatalf("Map should not be nil.") |
| 32 |
} |
| 33 |
// Ignore leading linefeed in expected value |
| 34 |
// to ease readability of tests. |
| 35 |
if len(expected) > 0 && expected[0] == 10 { |
| 36 |
expected = expected[1:] |
| 37 |
} |
| 38 |
actual, err := m.AsYaml() |
| 39 |
if err != nil { |
| 40 |
t.Fatalf("Unexpected err: %v", err) |
| 41 |
} |
| 42 |
if tweaker != nil { |
| 43 |
actual = tweaker(actual) |
| 44 |
} |
| 45 |
if string(actual) != expected { |
| 46 |
reportDiffAndFail(t, actual, expected) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Pretty printing of file differences. |
| 51 |
func reportDiffAndFail( |
| 52 |
t *testing.T, actual []byte, expected string) { |
| 53 |
t.Helper() |
| 54 |
sE, maxLen := convertToArray(expected) |
| 55 |
sA, _ := convertToArray(string(actual)) |
| 56 |
fmt.Println("===== ACTUAL BEGIN ========================================") |
| 57 |
fmt.Print(string(actual)) |
| 58 |
fmt.Println("===== ACTUAL END ==========================================") |
| 59 |
format := fmt.Sprintf("%%s %%-%ds %%s\n", maxLen+4) |
| 60 |
var limit int |
| 61 |
if len(sE) < len(sA) { |
| 62 |
limit = len(sE) |
| 63 |
} else { |
| 64 |
limit = len(sA) |
| 65 |
} |
| 66 |
fmt.Printf(format, " ", "EXPECTED", "ACTUAL") |
| 67 |
fmt.Printf(format, " ", "--------", "------") |
| 68 |
for i := 0; i < limit; i++ { |
| 69 |
fmt.Printf(format, hint(sE[i], sA[i]), sE[i], sA[i]) |
| 70 |
} |
| 71 |
if len(sE) < len(sA) { |
| 72 |
for i := len(sE); i < len(sA); i++ { |
| 73 |
fmt.Printf(format, "X", "", sA[i]) |
| 74 |
} |
| 75 |
} else { |
| 76 |
for i := len(sA); i < len(sE); i++ { |
| 77 |
fmt.Printf(format, "X", sE[i], "") |
| 78 |
} |
| 79 |
} |
| 80 |
t.Fatalf("Expected not equal to actual") |
| 81 |
} |
| 82 |
|
| 83 |
func hint(a, b string) string { |
| 84 |
if a == b { |
| 85 |
return " " |
| 86 |
} |
| 87 |
return "X" |
| 88 |
} |
| 89 |
|
| 90 |
func convertToArray(x string) ([]string, int) { |
| 91 |
a := strings.Split(strings.TrimSuffix(x, "\n"), "\n") |
| 92 |
maxLen := 0 |
| 93 |
for i, v := range a { |
| 94 |
z := tabToSpace(v) |
| 95 |
if len(z) > maxLen { |
| 96 |
maxLen = len(z) |
| 97 |
} |
| 98 |
a[i] = z |
| 99 |
} |
| 100 |
return a, maxLen |
| 101 |
} |
| 102 |
|
| 103 |
func tabToSpace(input string) string { |
| 104 |
var result []string |
| 105 |
for _, i := range input { |
| 106 |
if i == 9 { |
| 107 |
result = append(result, " ") |
| 108 |
} else { |
| 109 |
result = append(result, string(i)) |
| 110 |
} |
| 111 |
} |
| 112 |
return strings.Join(result, "") |
| 113 |
} |