cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/read_test.go
282lines · modecode
| 1 | package parser_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/require" |
| 11 | |
| 12 | "github.com/cloudflare/pint/internal/parser" |
| 13 | ) |
| 14 | |
| 15 | func TestReadContent(t *testing.T) { |
| 16 | type testCaseT struct { |
| 17 | input []byte |
| 18 | output []byte |
| 19 | ignored bool |
| 20 | shouldError bool |
| 21 | } |
| 22 | |
| 23 | testCases := []testCaseT{ |
| 24 | { |
| 25 | input: []byte(""), |
| 26 | output: []byte(""), |
| 27 | }, |
| 28 | { |
| 29 | input: []byte("\n"), |
| 30 | output: []byte("\n"), |
| 31 | }, |
| 32 | { |
| 33 | input: []byte("\n \n"), |
| 34 | output: []byte("\n \n"), |
| 35 | }, |
| 36 | { |
| 37 | input: []byte("foo bar"), |
| 38 | output: []byte("foo bar"), |
| 39 | }, |
| 40 | { |
| 41 | input: []byte("foo bar\n"), |
| 42 | output: []byte("foo bar\n"), |
| 43 | }, |
| 44 | { |
| 45 | input: []byte("line1\nline2"), |
| 46 | output: []byte("line1\nline2"), |
| 47 | }, |
| 48 | { |
| 49 | input: []byte("line1\nline2\n"), |
| 50 | output: []byte("line1\nline2\n"), |
| 51 | }, |
| 52 | { |
| 53 | input: []byte("line1\n\nline2\n\n"), |
| 54 | output: []byte("line1\n\nline2\n\n"), |
| 55 | }, |
| 56 | { |
| 57 | input: []byte("# pint ignore/next-line\nfoo\n"), |
| 58 | output: []byte("# pint ignore/next-line\n \n"), |
| 59 | }, |
| 60 | { |
| 61 | input: []byte("# pint ignore/next-line\nfoo"), |
| 62 | output: []byte("# pint ignore/next-line\n "), |
| 63 | }, |
| 64 | { |
| 65 | input: []byte("# pint ignore/next-line\nfoo\n\n"), |
| 66 | output: []byte("# pint ignore/next-line\n \n\n"), |
| 67 | }, |
| 68 | { |
| 69 | input: []byte("# pint ignore/next-line\nfoo\nbar\n"), |
| 70 | output: []byte("# pint ignore/next-line\n \nbar\n"), |
| 71 | }, |
| 72 | { |
| 73 | input: []byte("# pint ignore/next-line \nfoo\n"), |
| 74 | output: []byte("# pint ignore/next-line \n \n"), |
| 75 | }, |
| 76 | { |
| 77 | input: []byte("# pint ignore/next-line \nfoo\n"), |
| 78 | output: []byte("# pint ignore/next-line \n \n"), |
| 79 | }, |
| 80 | { |
| 81 | input: []byte("#pint ignore/next-line \nfoo\n"), |
| 82 | output: []byte("#pint ignore/next-line \n \n"), |
| 83 | }, |
| 84 | { |
| 85 | input: []byte("#pintignore/next-line\nfoo\n"), |
| 86 | output: []byte("#pintignore/next-line\nfoo\n"), |
| 87 | }, |
| 88 | { |
| 89 | input: []byte("# pint ignore/next-linex\nfoo\n"), |
| 90 | output: []byte("# pint ignore/next-linex\nfoo\n"), |
| 91 | }, |
| 92 | { |
| 93 | input: []byte("# pint ignore/begin\nfoo\nbar\n"), |
| 94 | output: []byte("# pint ignore/begin\n \n \n"), |
| 95 | }, |
| 96 | { |
| 97 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin"), |
| 98 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin"), |
| 99 | }, |
| 100 | { |
| 101 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin\nfoo\n"), |
| 102 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin\n \n"), |
| 103 | }, |
| 104 | { |
| 105 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/end\nfoo\n"), |
| 106 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/end\nfoo\n"), |
| 107 | }, |
| 108 | { |
| 109 | input: []byte("# pint ignore/begin\nfoo # pint ignore/line\nbar\n# pint ignore/begin"), |
| 110 | output: []byte("# pint ignore/begin\n # pint ignore/line\n \n# pint ignore/begin"), |
| 111 | }, |
| 112 | { |
| 113 | input: []byte("line1\nline2 # pint ignore/line\n"), |
| 114 | output: []byte("line1\n # pint ignore/line\n"), |
| 115 | }, |
| 116 | { |
| 117 | input: []byte("line1\nline2 # pint ignore/line\nline3\n"), |
| 118 | output: []byte("line1\n # pint ignore/line\nline3\n"), |
| 119 | }, |
| 120 | { |
| 121 | input: []byte("{#- comment #} # pint ignore/line\n"), |
| 122 | output: []byte(" #- comment #} # pint ignore/line\n"), |
| 123 | }, |
| 124 | { |
| 125 | input: []byte("# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 126 | output: []byte("# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 127 | ignored: true, |
| 128 | }, |
| 129 | { |
| 130 | input: []byte("foo\n# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 131 | output: []byte("foo\n# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 132 | ignored: true, |
| 133 | }, |
| 134 | { |
| 135 | input: []byte(" {% raw %} # pint ignore/line\n"), |
| 136 | output: []byte(" # pint ignore/line\n"), |
| 137 | }, |
| 138 | } |
| 139 | |
| 140 | for i, tc := range testCases { |
| 141 | t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 142 | r := bytes.NewReader(tc.input) |
| 143 | output, err := parser.ReadContent(r) |
| 144 | |
| 145 | hadError := err != nil |
| 146 | if hadError != tc.shouldError { |
| 147 | t.Errorf("ReadContent() returned err=%v, expected=%v", err, tc.shouldError) |
| 148 | return |
| 149 | } |
| 150 | |
| 151 | outputLines := strings.Count(string(output.Body), "\n") |
| 152 | inputLines := strings.Count(string(tc.input), "\n") |
| 153 | if outputLines != inputLines { |
| 154 | t.Errorf("ReadContent() returned %d line(s) while input had %d", outputLines, inputLines) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | require.Equal(t, string(tc.output), string(output.Body), "ReadContent() returned wrong output") |
| 159 | require.Equal(t, tc.ignored, output.Ignored, "ReadContent() returned wrong Ignored value") |
| 160 | }) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestGetLastComment(t *testing.T) { |
| 165 | type testCaseT struct { |
| 166 | input string |
| 167 | comment []string |
| 168 | output parser.Comment |
| 169 | ok bool |
| 170 | } |
| 171 | |
| 172 | testCases := []testCaseT{ |
| 173 | { |
| 174 | input: "", |
| 175 | comment: []string{"rule/owner"}, |
| 176 | }, |
| 177 | { |
| 178 | input: "\n", |
| 179 | comment: []string{"rule/owner"}, |
| 180 | }, |
| 181 | { |
| 182 | input: "\n \n", |
| 183 | comment: []string{"rule/owner"}, |
| 184 | }, |
| 185 | { |
| 186 | input: "foo bar", |
| 187 | comment: []string{"rule/owner"}, |
| 188 | }, |
| 189 | { |
| 190 | input: "foo bar\n", |
| 191 | comment: []string{"rule/owner"}, |
| 192 | }, |
| 193 | { |
| 194 | input: "line1\nline2", |
| 195 | comment: []string{"rule/owner"}, |
| 196 | }, |
| 197 | { |
| 198 | input: "line1\nline2\n", |
| 199 | comment: []string{"rule/owner"}, |
| 200 | }, |
| 201 | { |
| 202 | input: "line1\n\nline2\n\n", |
| 203 | comment: []string{"rule/owner"}, |
| 204 | }, |
| 205 | { |
| 206 | input: "# pint rule/owner", |
| 207 | comment: []string{"rule/owner"}, |
| 208 | ok: true, |
| 209 | output: parser.Comment{Key: "rule/owner"}, |
| 210 | }, |
| 211 | { |
| 212 | input: "# pint rule/owner foo", |
| 213 | comment: []string{"rule/owner"}, |
| 214 | ok: true, |
| 215 | output: parser.Comment{Key: "rule/owner", Value: "foo"}, |
| 216 | }, |
| 217 | { |
| 218 | input: "# pint rule/owner foo bar bob/alice", |
| 219 | comment: []string{"rule/owner"}, |
| 220 | ok: true, |
| 221 | output: parser.Comment{Key: "rule/owner", Value: "foo bar bob/alice"}, |
| 222 | }, |
| 223 | { |
| 224 | input: "line1\n # pint rule/owner foo bar bob/alice\n line2\n\n", |
| 225 | comment: []string{"rule/owner"}, |
| 226 | ok: true, |
| 227 | output: parser.Comment{Key: "rule/owner", Value: "foo bar bob/alice"}, |
| 228 | }, |
| 229 | { |
| 230 | input: "line1\n #### pint rule/owner foo bar bob/alice\n line2\n\n", |
| 231 | comment: []string{"rule/owner"}, |
| 232 | ok: true, |
| 233 | output: parser.Comment{Key: "rule/owner", Value: "foo bar bob/alice"}, |
| 234 | }, |
| 235 | { |
| 236 | input: "# pint set promql/series min-age 1w", |
| 237 | comment: []string{"set promql/series min-age"}, |
| 238 | }, |
| 239 | { |
| 240 | input: "# pint set promql/series min-age 1w", |
| 241 | comment: []string{"set", "promql/series", "min-age"}, |
| 242 | ok: true, |
| 243 | output: parser.Comment{Key: "set promql/series min-age", Value: "1w"}, |
| 244 | }, |
| 245 | { |
| 246 | input: "# pint set promql/series min-age 1d\n# pint set promql/series min-age 1w\n", |
| 247 | comment: []string{"set", "promql/series", "min-age"}, |
| 248 | ok: true, |
| 249 | output: parser.Comment{Key: "set promql/series min-age", Value: "1w"}, |
| 250 | }, |
| 251 | { |
| 252 | input: "# pint set promql/series min-age 2d\n# pint set promql/series min-age 1w\n# pint set promql/series min-age 1s\n", |
| 253 | comment: []string{"set", "promql/series", "min-age"}, |
| 254 | ok: true, |
| 255 | output: parser.Comment{Key: "set promql/series min-age", Value: "1s"}, |
| 256 | }, |
| 257 | { |
| 258 | input: "# pint set promql/series min-age 1w ", |
| 259 | comment: []string{"set", "promql/series", "min-age"}, |
| 260 | ok: true, |
| 261 | output: parser.Comment{Key: "set promql/series min-age", Value: "1w"}, |
| 262 | }, |
| 263 | { |
| 264 | input: "# pint set", |
| 265 | comment: []string{"set", "promql/series", "min-age"}, |
| 266 | }, |
| 267 | { |
| 268 | input: "# pint rule/set promql/series ignore/label-value error", |
| 269 | comment: []string{"rule/set", "promql/series", "ignore/label-value"}, |
| 270 | ok: true, |
| 271 | output: parser.Comment{Key: "rule/set promql/series ignore/label-value", Value: "error"}, |
| 272 | }, |
| 273 | } |
| 274 | |
| 275 | for i, tc := range testCases { |
| 276 | t.Run(fmt.Sprintf("%d/%s", i, tc.input), func(t *testing.T) { |
| 277 | output, ok := parser.GetLastComment(tc.input, tc.comment...) |
| 278 | require.Equal(t, tc.ok, ok) |
| 279 | require.Equal(t, tc.output, output) |
| 280 | }) |
| 281 | } |
| 282 | } |
| 283 | |