1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

TestParseInitVersion: add some additional tests

Also slightly harden parseInitVersion

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-10-16 03:33:48 +02:00
parent f5bb374a0c
commit 2030daf2ee
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 26 additions and 10 deletions

View file

@ -205,14 +205,15 @@ func getBackingFs(v *types.Info) string {
// //
// tini version 0.18.0 - git.fec3683 // tini version 0.18.0 - git.fec3683
func parseInitVersion(v string) (version string, commit string, err error) { func parseInitVersion(v string) (version string, commit string, err error) {
parts := strings.Split(strings.TrimSpace(v), " - ") parts := strings.Split(v, " - ")
if len(parts) >= 2 { if len(parts) >= 2 {
gitParts := strings.Split(parts[1], ".") gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
if len(gitParts) == 2 && gitParts[0] == "git" { if len(gitParts) == 2 && gitParts[0] == "git" {
commit = gitParts[1] commit = gitParts[1]
} }
} }
parts[0] = strings.TrimSpace(parts[0])
if strings.HasPrefix(parts[0], "tini version ") { if strings.HasPrefix(parts[0], "tini version ") {
version = strings.TrimPrefix(parts[0], "tini version ") version = strings.TrimPrefix(parts[0], "tini version ")
} }

View file

@ -26,12 +26,24 @@ func TestParseInitVersion(t *testing.T) {
}, { }, {
output: "tini version 0.13.2", output: "tini version 0.13.2",
version: "0.13.2", version: "0.13.2",
}, {
output: "tini version 0.13.2 - ",
version: "0.13.2",
}, {
output: " - git.949e6fa",
commit: "949e6fa",
}, { }, {
output: "tini version0.13.2", output: "tini version0.13.2",
invalid: true, invalid: true,
}, {
output: "version 0.13.0",
invalid: true,
}, { }, {
output: "", output: "",
invalid: true, invalid: true,
}, {
output: " - ",
invalid: true,
}, { }, {
output: "hello world", output: "hello world",
invalid: true, invalid: true,
@ -39,14 +51,17 @@ func TestParseInitVersion(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
version, commit, err := parseInitVersion(test.output) test := test
if test.invalid { t.Run(test.output, func(t *testing.T) {
assert.Check(t, is.ErrorContains(err, "")) version, commit, err := parseInitVersion(test.output)
} else { if test.invalid {
assert.Check(t, err) assert.Check(t, is.ErrorContains(err, ""))
} } else {
assert.Equal(t, test.version, version) assert.Check(t, err)
assert.Equal(t, test.commit, commit) }
assert.Equal(t, test.version, version)
assert.Equal(t, test.commit, commit)
})
} }
} }