Skip to content

Commit

Permalink
platforms: Format(): use path.Join() instead of joinNotEmpty()
Browse files Browse the repository at this point in the history
path.Join() also skips empty components, making it the equivalent of
this function.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Aug 2, 2021
1 parent f12040b commit f2c3122
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
16 changes: 2 additions & 14 deletions platforms/platforms.go
Expand Up @@ -107,6 +107,7 @@
package platforms

import (
"path"
"regexp"
"runtime"
"strconv"
Expand Down Expand Up @@ -246,20 +247,7 @@ func Format(platform specs.Platform) string {
return "unknown"
}

return joinNotEmpty(platform.OS, platform.Architecture, platform.Variant)
}

func joinNotEmpty(s ...string) string {
var ss []string
for _, s := range s {
if s == "" {
continue
}

ss = append(ss, s)
}

return strings.Join(ss, "/")
return path.Join(platform.OS, platform.Architecture, platform.Variant)
}

// Normalize validates and translate the platform to the canonical value.
Expand Down
19 changes: 10 additions & 9 deletions platforms/platforms_test.go
Expand Up @@ -17,6 +17,7 @@
package platforms

import (
"path"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -204,7 +205,7 @@ func TestParseSelector(t *testing.T) {
OS: defaultOS,
Architecture: "arm",
},
formatted: joinNotEmpty(defaultOS, "arm"),
formatted: path.Join(defaultOS, "arm"),
},
{
input: "armel",
Expand All @@ -213,31 +214,31 @@ func TestParseSelector(t *testing.T) {
Architecture: "arm",
Variant: "v6",
},
formatted: joinNotEmpty(defaultOS, "arm/v6"),
formatted: path.Join(defaultOS, "arm/v6"),
},
{
input: "armhf",
expected: specs.Platform{
OS: defaultOS,
Architecture: "arm",
},
formatted: joinNotEmpty(defaultOS, "arm"),
formatted: path.Join(defaultOS, "arm"),
},
{
input: "Aarch64",
expected: specs.Platform{
OS: defaultOS,
Architecture: "arm64",
},
formatted: joinNotEmpty(defaultOS, "arm64"),
formatted: path.Join(defaultOS, "arm64"),
},
{
input: "x86_64",
expected: specs.Platform{
OS: defaultOS,
Architecture: "amd64",
},
formatted: joinNotEmpty(defaultOS, "amd64"),
formatted: path.Join(defaultOS, "amd64"),
},
{
input: "Linux/x86_64",
Expand All @@ -253,7 +254,7 @@ func TestParseSelector(t *testing.T) {
OS: defaultOS,
Architecture: "386",
},
formatted: joinNotEmpty(defaultOS, "386"),
formatted: path.Join(defaultOS, "386"),
},
{
input: "linux",
Expand All @@ -262,15 +263,15 @@ func TestParseSelector(t *testing.T) {
Architecture: defaultArch,
Variant: defaultVariant,
},
formatted: joinNotEmpty("linux", defaultArch, defaultVariant),
formatted: path.Join("linux", defaultArch, defaultVariant),
},
{
input: "s390x",
expected: specs.Platform{
OS: defaultOS,
Architecture: "s390x",
},
formatted: joinNotEmpty(defaultOS, "s390x"),
formatted: path.Join(defaultOS, "s390x"),
},
{
input: "linux/s390x",
Expand All @@ -287,7 +288,7 @@ func TestParseSelector(t *testing.T) {
Architecture: defaultArch,
Variant: defaultVariant,
},
formatted: joinNotEmpty("darwin", defaultArch, defaultVariant),
formatted: path.Join("darwin", defaultArch, defaultVariant),
},
} {
t.Run(testcase.input, func(t *testing.T) {
Expand Down

0 comments on commit f2c3122

Please sign in to comment.