github.com/quay/claircore@v1.5.28/rhel/dockerfile/vars_test.go (about) 1 package dockerfile 2 3 import ( 4 "errors" 5 "testing" 6 7 "golang.org/x/text/transform" 8 ) 9 10 func TestVars(t *testing.T) { 11 tt := []varTestcase{ 12 { 13 Name: "NoTransform", 14 In: `string with no expand`, 15 Out: `string with no expand`, 16 SpanSz: 21, 17 SrcSz: 21, 18 }, 19 { 20 Name: "OddEscape", 21 In: `string with \\ expand`, 22 Out: `string with \\ expand`, 23 SpanSz: 21, 24 SrcSz: 21, 25 }, 26 { 27 Name: "Literal", 28 In: `string with \$literal`, 29 Out: `string with $literal`, 30 SpanSz: 12, 31 SpanErr: transform.ErrEndOfSpan, 32 SrcSz: 21, 33 }, 34 { 35 Name: "WholeExpand", 36 In: `$X`, 37 Out: `expand`, 38 SpanSz: 0, 39 SpanErr: transform.ErrEndOfSpan, 40 SrcSz: 2, 41 Setup: setX, 42 }, 43 { 44 Name: "WholeExpandBrace", 45 In: `${X}`, 46 Out: `expand`, 47 SpanSz: 0, 48 SpanErr: transform.ErrEndOfSpan, 49 SrcSz: 4, 50 Setup: setX, 51 }, 52 { 53 Name: "Default", 54 In: `${X:-expand}`, 55 Out: `expand`, 56 SpanSz: 0, 57 SpanErr: transform.ErrEndOfSpan, 58 SrcSz: 12, 59 }, 60 { 61 Name: "UnsedDefault", 62 In: `${X:-default}`, 63 Out: `expand`, 64 SpanSz: 0, 65 SpanErr: transform.ErrEndOfSpan, 66 SrcSz: 13, 67 Setup: setX, 68 }, 69 { 70 Name: "IfSetUnset", 71 In: `${X:+expand}`, 72 Out: ``, 73 SpanSz: 0, 74 SpanErr: transform.ErrEndOfSpan, 75 SrcSz: 12, 76 }, 77 { 78 Name: "IfSetSet", 79 In: `${X:+expand}`, 80 Out: `expand`, 81 SpanSz: 0, 82 SpanErr: transform.ErrEndOfSpan, 83 SrcSz: 12, 84 Setup: setX, 85 }, 86 { 87 Name: "Leading", 88 In: `::::::$X`, 89 Out: `::::::expand`, 90 SpanSz: 6, 91 SpanErr: transform.ErrEndOfSpan, 92 SrcSz: 8, 93 Setup: setX, 94 }, 95 { 96 Name: "Trailing", 97 In: `$X::::::`, 98 Out: `expand::::::`, 99 SpanSz: 0, 100 SpanErr: transform.ErrEndOfSpan, 101 SrcSz: 8, 102 Setup: setX, 103 }, 104 { 105 Name: "TrailingBrace", 106 In: `${X}::::::`, 107 Out: `expand::::::`, 108 SpanSz: 0, 109 SpanErr: transform.ErrEndOfSpan, 110 SrcSz: 10, 111 Setup: setX, 112 }, 113 } 114 // TODO(hank) Need to hit the various corner error cases. 115 t.Run("Span", func(t *testing.T) { 116 t.Parallel() 117 v := NewVars() 118 for _, tc := range tt { 119 t.Run(tc.Name, tc.Span(v)) 120 } 121 }) 122 t.Run("Transform", func(t *testing.T) { 123 t.Parallel() 124 v := NewVars() 125 for _, tc := range tt { 126 t.Run(tc.Name, tc.Transform(v)) 127 } 128 }) 129 } 130 131 type varTestcase struct { 132 Setup func(testing.TB, *Vars) 133 SpanErr error 134 Name string 135 In string 136 Out string 137 SpanSz int 138 SrcSz int 139 TransformErr bool 140 } 141 142 func (tc *varTestcase) Span(tf *Vars) func(*testing.T) { 143 return func(t *testing.T) { 144 if tc.Setup != nil { 145 tc.Setup(t, tf) 146 defer tf.Clear() 147 } 148 t.Logf("input: %#q", tc.In) 149 got, err := tf.Span([]byte(tc.In), true) 150 t.Logf("got: %v, want: %v", got, tc.SpanSz) 151 if want := tc.SpanSz; got != want { 152 t.Fail() 153 } 154 if !errors.Is(tc.SpanErr, err) { 155 t.Fatalf("unexpected error: %v", err) 156 } else { 157 t.Logf("expected error: %v", err) 158 } 159 } 160 } 161 162 func (tc *varTestcase) Transform(tf *Vars) func(*testing.T) { 163 return func(t *testing.T) { 164 if tc.Setup != nil { 165 tc.Setup(t, tf) 166 defer tf.Clear() 167 } 168 t.Logf("input: %#q", tc.In) 169 got, n, err := transform.String(tf, tc.In) 170 t.Logf("got: %#q, want: %#q", got, tc.Out) 171 if want := tc.Out; got != want { 172 t.Fail() 173 } 174 t.Logf("got: %v, want: %v", n, tc.SrcSz) 175 if got, want := n, tc.SrcSz; got != want { 176 t.Fail() 177 } 178 if (err == nil) == tc.TransformErr { 179 t.Fatalf("unexpected error: %v", err) 180 } else { 181 t.Logf("expected error: %v", err) 182 } 183 } 184 } 185 186 func setX(t testing.TB, v *Vars) { 187 key, val := `X`, `expand` 188 t.Logf("setting %s=%s", key, val) 189 v.Set(key, val) 190 }