Juggle the test to the right place, call it also while saving config to be consistent
This commit is contained in:
@@ -189,9 +189,10 @@ func (c *Config) UpdateFromJSON(j []byte) error {
|
||||
}
|
||||
|
||||
// check the command exists
|
||||
_, err := exec.LookPath(newConfig.DownloadProfiles[i].Command)
|
||||
|
||||
_, err := AbsPathToExecutable(newConfig.DownloadProfiles[i].Command)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not find %s on the path", newConfig.DownloadProfiles[i].Command)
|
||||
return fmt.Errorf("problem with command '%s': %s", newConfig.DownloadProfiles[i].Command, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,3 +338,28 @@ func (cs *ConfigService) WriteConfig() {
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
|
||||
// AbsPathToExecutable takes a command name, which may or may not be path-qualified,
|
||||
// and returns the fully qualified path to it, or an error if could not be found, or
|
||||
// if it does not appear to be a file.
|
||||
func AbsPathToExecutable(cmd string) (string, error) {
|
||||
|
||||
pathCmd, err := exec.LookPath(cmd)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not LookPath '%s': %w", cmd, err)
|
||||
}
|
||||
|
||||
execAbsolutePath, err := filepath.Abs(pathCmd)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not get absolute path to '%s': %w", cmd, err)
|
||||
}
|
||||
fi, err := os.Stat(execAbsolutePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not get stat '%s': %w", cmd, err)
|
||||
}
|
||||
if !fi.Mode().IsRegular() {
|
||||
return "", fmt.Errorf("'%s' is not a regular file: %w", cmd, err)
|
||||
}
|
||||
|
||||
return execAbsolutePath, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -172,3 +174,41 @@ func configServiceFromString(configString string) *ConfigService {
|
||||
}
|
||||
return &cs
|
||||
}
|
||||
|
||||
func TestLookForExecutable(t *testing.T) {
|
||||
cmdPath, err := exec.LookPath("sleep")
|
||||
if err != nil {
|
||||
t.Errorf("cannot run this test without knowing about sleep: %s", err)
|
||||
t.FailNow()
|
||||
}
|
||||
cmdDir := filepath.Dir(cmdPath)
|
||||
|
||||
cmd := "sleep"
|
||||
path, err := AbsPathToExecutable(cmd)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, cmdPath, path)
|
||||
}
|
||||
|
||||
cmd = cmdPath
|
||||
path, err = AbsPathToExecutable(cmd)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, cmdPath, path)
|
||||
}
|
||||
|
||||
cmd = "../../../../../../../../.." + cmdPath
|
||||
path, err = AbsPathToExecutable(cmd)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, cmdPath, path)
|
||||
}
|
||||
cmd = "./sleep"
|
||||
_, err = AbsPathToExecutable(cmd)
|
||||
assert.Error(t, err)
|
||||
|
||||
os.Chdir(cmdDir)
|
||||
cmd = "./sleep"
|
||||
path, err = AbsPathToExecutable(cmd)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, cmdPath, path)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user