From 863b6e923af68c75cd8fd8a259039aa431571935 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Wed, 28 Dec 2022 18:39:31 -0800 Subject: [PATCH] Split overrides into separate package with tests --- internal/overrides/overrides.go | 92 ++++++++++++++++++++++++++++ internal/overrides/overrides_test.go | 86 ++++++++++++++++++++++++++ internal/shutils/decoder/decoder.go | 48 +-------------- 3 files changed, 181 insertions(+), 45 deletions(-) create mode 100644 internal/overrides/overrides.go create mode 100644 internal/overrides/overrides_test.go diff --git a/internal/overrides/overrides.go b/internal/overrides/overrides.go new file mode 100644 index 0000000..6ba4232 --- /dev/null +++ b/internal/overrides/overrides.go @@ -0,0 +1,92 @@ +package overrides + +import ( + "runtime" + "strings" + + "go.arsenm.dev/lure/distro" + "go.arsenm.dev/lure/internal/cpu" +) + +type Opts struct { + Name string + Overrides bool + LikeDistros bool +} + +var DefaultOpts = &Opts{ + Overrides: true, + LikeDistros: true, +} + +// Resolve generates a slice of possible override names in the order that they should be checked +func Resolve(info *distro.OSRelease, opts *Opts) []string { + if opts == nil { + opts = DefaultOpts + } + + if !opts.Overrides { + return []string{opts.Name} + } + + architectures := []string{runtime.GOARCH} + + if runtime.GOARCH == "arm" { + // More specific goes first + architectures[0] = cpu.ARMVariant() + architectures = append(architectures, "arm") + } + + distros := []string{info.ID} + if opts.LikeDistros { + distros = append(distros, info.Like...) + } + + var out []string + for _, arch := range architectures { + for _, distro := range distros { + if opts.Name == "" { + out = append( + out, + arch+"_"+distro, + distro, + ) + } else { + out = append( + out, + opts.Name+"_"+arch+"_"+distro, + opts.Name+"_"+distro, + ) + } + } + if opts.Name == "" { + out = append(out, arch) + } else { + out = append(out, opts.Name+"_"+arch) + } + } + if opts.Name != "" { + out = append(out, opts.Name) + } + + for index, item := range out { + out[index] = strings.ReplaceAll(item, "-", "_") + } + + return out +} + +func (o *Opts) WithName(name string) *Opts { + o.Name = name + return o +} + +func (o *Opts) WithOverrides(v bool) *Opts { + o.Overrides = v + return o +} + +func (o *Opts) WithLikeDistros(v bool) *Opts { + o.LikeDistros = v + return o +} diff --git a/internal/overrides/overrides_test.go b/internal/overrides/overrides_test.go new file mode 100644 index 0000000..b9fcb4b --- /dev/null +++ b/internal/overrides/overrides_test.go @@ -0,0 +1,86 @@ +package overrides_test + +import ( + "reflect" + "testing" + + "go.arsenm.dev/lure/distro" + "go.arsenm.dev/lure/internal/overrides" +) + +var info = &distro.OSRelease{ + ID: "centos", + Like: []string{"rhel", "fedora"}, +} + +func TestResolve(t *testing.T) { + names := overrides.Resolve(info, nil) + + expected := []string{ + "amd64_centos", + "centos", + "amd64_rhel", + "rhel", + "amd64_fedora", + "fedora", + "amd64", + } + + if !reflect.DeepEqual(names, expected) { + t.Errorf("expected %v, got %v", expected, names) + } +} + +func TestResolveName(t *testing.T) { + names := overrides.Resolve(info, &overrides.Opts{ + Name: "deps", + Overrides: true, + LikeDistros: true, + }) + + expected := []string{ + "deps_amd64_centos", + "deps_centos", + "deps_amd64_rhel", + "deps_rhel", + "deps_amd64_fedora", + "deps_fedora", + "deps_amd64", + "deps", + } + + if !reflect.DeepEqual(names, expected) { + t.Errorf("expected %v, got %v", expected, names) + } +} + +func TestResolveNoLikeDistros(t *testing.T) { + names := overrides.Resolve(info, &overrides.Opts{ + Overrides: true, + LikeDistros: false, + }) + + expected := []string{ + "amd64_centos", + "centos", + "amd64", + } + + if !reflect.DeepEqual(names, expected) { + t.Errorf("expected %v, got %v", expected, names) + } +} + +func TestResolveNoOverrides(t *testing.T) { + names := overrides.Resolve(info, &overrides.Opts{ + Name: "deps", + Overrides: false, + LikeDistros: false, + }) + + expected := []string{"deps"} + + if !reflect.DeepEqual(names, expected) { + t.Errorf("expected %v, got %v", expected, names) + } +} diff --git a/internal/shutils/decoder/decoder.go b/internal/shutils/decoder/decoder.go index d0022b4..8face0c 100644 --- a/internal/shutils/decoder/decoder.go +++ b/internal/shutils/decoder/decoder.go @@ -21,14 +21,12 @@ package decoder import ( "context" "errors" - "fmt" "reflect" - "runtime" "strings" "github.com/mitchellh/mapstructure" "go.arsenm.dev/lure/distro" - "go.arsenm.dev/lure/internal/cpu" + "go.arsenm.dev/lure/internal/overrides" "golang.org/x/exp/slices" "mvdan.cc/sh/v3/expand" "mvdan.cc/sh/v3/interp" @@ -169,7 +167,7 @@ func (d *Decoder) GetFunc(name string) (ScriptFunc, bool) { } func (d *Decoder) getFunc(name string) *syntax.Stmt { - names := d.genPossibleNames(name) + names := overrides.Resolve(d.info, overrides.DefaultOpts.WithName(name)) for _, fnName := range names { fn, ok := d.runner.Funcs[fnName] if ok { @@ -182,7 +180,7 @@ func (d *Decoder) getFunc(name string) *syntax.Stmt { // getVar gets a variable based on its name, taking into account // override variables and nameref variables. func (d *Decoder) getVar(name string) *expand.Variable { - names := d.genPossibleNames(name) + names := overrides.Resolve(d.info, overrides.DefaultOpts.WithName(name)) for _, varName := range names { val, ok := d.runner.Vars[varName] if ok { @@ -200,43 +198,3 @@ func (d *Decoder) getVar(name string) *expand.Variable { } return nil } - -// genPossibleNames generates a slice of the possible names that -// could be used in the order that they should be checked -func (d *Decoder) genPossibleNames(name string) []string { - if !d.Overrides { - return []string{name} - } - - architectures := []string{runtime.GOARCH} - - if runtime.GOARCH == "arm" { - // More specific goes first - architectures[0] = cpu.ARMVariant() - architectures = append(architectures, "arm") - } - - distros := []string{d.info.ID} - if d.LikeDistros { - distros = append(distros, d.info.Like...) - } - - var out []string - for _, arch := range architectures { - for _, distro := range distros { - out = append( - out, - fmt.Sprintf("%s_%s_%s", name, arch, distro), - fmt.Sprintf("%s_%s", name, distro), - ) - } - out = append(out, fmt.Sprintf("%s_%s", name, arch)) - } - out = append(out, name) - - for index, item := range out { - out[index] = strings.ReplaceAll(item, "-", "_") - } - - return out -}