Skip to content

Commit

Permalink
✨ Use slashes only in path interpolation
Browse files Browse the repository at this point in the history
Allows to reference the dropin cache folder path using slashes only regardless the OS, which is convenient for running bash commands on windows
  • Loading branch information
g.monceyron committed Jun 21, 2022
1 parent 02222d0 commit 0f68f98
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 169 deletions.
27 changes: 18 additions & 9 deletions internal/command/default-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

Expand Down Expand Up @@ -262,7 +263,7 @@ func (cmd *DefaultCommand) interpolate(text string) string {
}

func (cmd *DefaultCommand) doInterpolate(os string, arch string, text string) string {
output := strings.ReplaceAll(text, CACHE_DIR_PATTERN, cmd.PkgDir)
output := strings.ReplaceAll(text, CACHE_DIR_PATTERN, filepath.ToSlash(cmd.PkgDir))
output = strings.ReplaceAll(output, OS_PATTERN, os)
output = strings.ReplaceAll(output, ARCH_PATTERN, arch)
output = strings.ReplaceAll(output, BINARY_PATTERN, cmd.binary(os))
Expand All @@ -275,18 +276,26 @@ func (cmd *DefaultCommand) doInterpolate(os string, arch string, text string) st

// Support golang built-in text/template engine
type TemplateContext struct {
Os string
Arch string
Cache string
Root string
Os string
Arch string
Cache string
Root string
Binary string
Script string
Extension string
ScriptExtension string
}

func (cmd *DefaultCommand) render(text string) string {
ctx := TemplateContext{
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Cache: cmd.PkgDir,
Root: cmd.PkgDir,
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Cache: filepath.ToSlash(cmd.PkgDir),
Root: filepath.ToSlash(cmd.PkgDir),
Binary: cmd.binary(runtime.GOOS),
Script: cmd.script(runtime.GOOS),
Extension: cmd.extension(runtime.GOOS),
ScriptExtension: cmd.script_ext(runtime.GOOS),
}

t, err := template.New("command-template").Parse(text)
Expand Down
240 changes: 81 additions & 159 deletions internal/command/default-command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCommandValidArgs(t *testing.T) {
cmd := DefaultCommand{
func getDefaultCommand() DefaultCommand {
return DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
Expand All @@ -20,12 +20,20 @@ func TestCommandValidArgs(t *testing.T) {
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: []string{"args1", "args2"},
CmdValidArgsCmd: []string{},
CmdRequiredFlags: []string{"moab", "moab-id"},
CmdFlagValuesCmd: []string{},
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
}

func TestCommandValidArgs(t *testing.T) {
cmd := getDefaultCommand()
cmd.CmdValidArgs = []string{"args1", "args2"}
cmd.CmdValidArgsCmd = []string{}
cmd.CmdRequiredFlags = []string{"moab", "moab-id"}
cmd.CmdFlagValuesCmd = []string{}

validArgs := cmd.ValidArgs()

Expand All @@ -41,23 +49,11 @@ func TestCommandValidArgs(t *testing.T) {
}

func TestCommandValidArgsCmd(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "ls",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: []string{"args1", "args2"},
CmdValidArgsCmd: []string{"#CACHE#/test", "arg1", "arg2"},
CmdRequiredFlags: []string{"moab", "moab-id"},
CmdFlagValuesCmd: []string{"#CACHE#/test", "arg1", "arg2"},
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()
cmd.CmdValidArgs = []string{"args1", "args2"}
cmd.CmdValidArgsCmd = []string{"#CACHE#/test", "arg1", "arg2"}
cmd.CmdRequiredFlags = []string{"moab", "moab-id"}
cmd.CmdFlagValuesCmd = []string{"#CACHE#/test", "arg1", "arg2"}

validArgsCmd := cmd.ValidArgsCmd()
assert.Equal(t, 3, len(validArgsCmd))
Expand All @@ -78,23 +74,7 @@ func TestCommandValidArgsCmd(t *testing.T) {
}

func TestNullFields(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "#CACHE#/#OS#/#ARCH#/test#EXT#",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()

assert.NotNil(t, cmd.ValidArgs())
assert.NotNil(t, cmd.RequiredFlags())
Expand All @@ -104,23 +84,8 @@ func TestNullFields(t *testing.T) {
}

func TestCloneCommand(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "#CACHE#/#OS#/#ARCH#/test#EXT#",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()
cmd.CmdExecutable = "#CACHE#/#OS#/#ARCH#/test#EXT#"

newCmd := cmd.Clone()
assert.NotNil(t, newCmd.CmdValidArgs)
Expand All @@ -136,23 +101,8 @@ func TestCloneCommand(t *testing.T) {
}

func TestLegacyVariableInterpolation(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "#CACHE#/#OS#/test#EXT#",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()
cmd.CmdExecutable = "#CACHE#/#OS#/test#EXT#"

if runtime.GOOS == "windows" {
assert.Equal(t, fmt.Sprintf("/tmp/test/root/%s/test.exe", runtime.GOOS), cmd.interpolateCmd())
Expand All @@ -162,71 +112,72 @@ func TestLegacyVariableInterpolation(t *testing.T) {
}

func TestVariableRender(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "{{.Root}}/{{.Os}}/test",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()
cmd.CmdExecutable = "{{.Root}}/{{.Os}}/test"

assert.Equal(t, fmt.Sprintf("/tmp/test/root/%s/test", runtime.GOOS), cmd.interpolateCmd())
}

func TestConditionalVariableRender(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "{{.Root}}/{{.Os}}/test{{if eq .Os \"windows\"}}.bat{{else}}.sh{{end}}",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()
cmd.CmdExecutable = "{{.Root}}/{{.Os}}/test{{if eq .Os \"windows\"}}.bat{{else}}.sh{{end}}"

if runtime.GOOS == "windows" {
cmd.PkgDir = "\\tmp\\test\\root"
assert.Equal(t, fmt.Sprintf("/tmp/test/root/%s/test.bat", runtime.GOOS), cmd.interpolateCmd())
} else {
cmd.PkgDir = "/tmp/test/root"
assert.Equal(t, fmt.Sprintf("/tmp/test/root/%s/test.sh", runtime.GOOS), cmd.interpolateCmd())
}
}

func TestMixedRender(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "#CACHE#/#OS#/test{{if eq .Os \"windows\"}}.bat{{else}}.sh{{end}}",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
func TestConditionalBinaryRender(t *testing.T) {
cmd := getDefaultCommand()
cmd.CmdExecutable = "{{.Binary}}"

if runtime.GOOS == "windows" {
assert.Equal(t, "test.exe", cmd.interpolateCmd())
} else {
assert.Equal(t, "test", cmd.interpolateCmd())
}
}

func TestConditionalScriptRender(t *testing.T) {
cmd := getDefaultCommand()
cmd.CmdExecutable = "{{.Script}}"

if runtime.GOOS == "windows" {
assert.Equal(t, "test.bat", cmd.interpolateCmd())
} else {
assert.Equal(t, "test", cmd.interpolateCmd())
}
}

func TestConditionalExtensionRender(t *testing.T) {
cmd := getDefaultCommand()
cmd.CmdExecutable = "test{{.Extension}}"

if runtime.GOOS == "windows" {
assert.Equal(t, "test.exe", cmd.interpolateCmd())
} else {
assert.Equal(t, "test", cmd.interpolateCmd())
}
}

func TestConditionalScriptExtensionRender(t *testing.T) {
cmd := getDefaultCommand()
cmd.CmdExecutable = "test{{.ScriptExtension}}"

if runtime.GOOS == "windows" {
assert.Equal(t, "test.bat", cmd.interpolateCmd())
} else {
assert.Equal(t, "test", cmd.interpolateCmd())
}
}

func TestMixedRender(t *testing.T) {
cmd := getDefaultCommand()
cmd.CmdExecutable = "#CACHE#/#OS#/test{{if eq .Os \"windows\"}}.bat{{else}}.sh{{end}}"

if runtime.GOOS == "windows" {
assert.Equal(t, fmt.Sprintf("/tmp/test/root/%s/test.bat", runtime.GOOS), cmd.interpolateCmd())
Expand All @@ -236,45 +187,16 @@ func TestMixedRender(t *testing.T) {
}

func TestVariableRenderError(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "{{.Root}}/{{.Os}}/test{{.NonExistKey}}",
CmdArguments: []string{"-l", "-a"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()
cmd.CmdExecutable = "{{.Root}}/{{.Os}}/test{{.NonExistKey}}"

assert.Equal(t, "{{.Root}}/{{.Os}}/test{{.NonExistKey}}", cmd.interpolateCmd())
}

func TestInterpolate(t *testing.T) {
cmd := DefaultCommand{
CmdName: "test",
CmdCategory: "",
CmdType: "executable",
CmdGroup: "",
CmdShortDescription: "test command",
CmdLongDescription: "test command - long description",
CmdExecutable: "#CACHE#/#OS#/#ARCH#/test#EXT#",
CmdArguments: []string{"-l", "-a", "#SCRIPT#"},
CmdDocFile: "",
CmdDocLink: "",
CmdValidArgs: nil,
CmdValidArgsCmd: nil,
CmdRequiredFlags: nil,
CmdFlagValuesCmd: nil,
PkgDir: "/tmp/test/root",
}
cmd := getDefaultCommand()
cmd.CmdExecutable = "#CACHE#/#OS#/#ARCH#/test#EXT#"
cmd.CmdArguments = []string{"-l", "-a", "#SCRIPT#"}

assert.Equal(t, ".bat", cmd.doInterpolate("windows", "x64", "#SCRIPT_EXT#"))
assert.Equal(t, "", cmd.doInterpolate("linux", "x64", "#SCRIPT_EXT#"))
Expand Down
2 changes: 1 addition & 1 deletion test/integration.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

SCRIPT_DIR=${1:-$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )}
echo "integration test directory: $SCRIPT_DIR"
Expand Down

0 comments on commit 0f68f98

Please sign in to comment.