cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/source/features_test.go
72lines · modecode
| 1 | package source_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/require" |
| 7 | |
| 8 | "github.com/cloudflare/pint/internal/parser/source" |
| 9 | ) |
| 10 | |
| 11 | func TestParseVersion(t *testing.T) { |
| 12 | testCases := []struct { |
| 13 | description string |
| 14 | input string |
| 15 | err string |
| 16 | expected source.PrometheusVersion |
| 17 | }{ |
| 18 | // Verifies that a plain major.minor.patch version is parsed correctly. |
| 19 | { |
| 20 | description: "plain version", |
| 21 | input: "2.49.0", |
| 22 | expected: source.PrometheusVersion{Major: 2, Minor: 49, Patch: 0}, |
| 23 | }, |
| 24 | // Verifies that a leading "v" prefix is stripped before parsing. |
| 25 | { |
| 26 | description: "v prefix", |
| 27 | input: "v3.5.1", |
| 28 | expected: source.PrometheusVersion{Major: 3, Minor: 5, Patch: 1}, |
| 29 | }, |
| 30 | // Verifies that pre-release suffixes are stripped before parsing. |
| 31 | { |
| 32 | description: "pre-release suffix", |
| 33 | input: "3.5.0-rc.1", |
| 34 | expected: source.PrometheusVersion{Major: 3, Minor: 5, Patch: 0}, |
| 35 | }, |
| 36 | // Verifies that a version with only two components is rejected. |
| 37 | { |
| 38 | description: "too few parts", |
| 39 | input: "2.49", |
| 40 | err: `failed to parse Prometheus version "2.49": expected major.minor.patch format`, |
| 41 | }, |
| 42 | // Verifies that a non-numeric major component is rejected. |
| 43 | { |
| 44 | description: "bad major", |
| 45 | input: "abc.1.2", |
| 46 | err: `failed to parse Prometheus version "abc.1.2"`, |
| 47 | }, |
| 48 | // Verifies that a non-numeric minor component is rejected. |
| 49 | { |
| 50 | description: "bad minor", |
| 51 | input: "2.abc.0", |
| 52 | err: `failed to parse Prometheus version "2.abc.0"`, |
| 53 | }, |
| 54 | // Verifies that a non-numeric patch component is rejected. |
| 55 | { |
| 56 | description: "bad patch", |
| 57 | input: "2.49.abc", |
| 58 | err: `failed to parse Prometheus version "2.49.abc"`, |
| 59 | }, |
| 60 | } |
| 61 | for _, tc := range testCases { |
| 62 | t.Run(tc.description, func(t *testing.T) { |
| 63 | v, err := source.ParseVersion(tc.input) |
| 64 | if tc.err != "" { |
| 65 | require.ErrorContains(t, err, tc.err) |
| 66 | } else { |
| 67 | require.NoError(t, err) |
| 68 | require.Equal(t, tc.expected, v) |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |