Commit 01014c47 authored by Nick Thomas's avatar Nick Thomas

Pass the root directory into the fallback command

parent f6a7f171
......@@ -26,8 +26,9 @@ func findRootDir() (string, error) {
// rubyExec will never return. It either replaces the current process with a
// Ruby interpreter, or outputs an error and kills the process.
func execRuby(readWriter *readwriter.ReadWriter) {
cmd := &fallback.Command{}
func execRuby(rootDir string, readWriter *readwriter.ReadWriter) {
cmd := &fallback.Command{RootDir: rootDir, Args: os.Args}
if err := cmd.Execute(readWriter); err != nil {
fmt.Fprintf(readWriter.ErrOut, "Failed to exec: %v\n", err)
os.Exit(1)
......@@ -43,8 +44,8 @@ func main() {
rootDir, err := findRootDir()
if err != nil {
fmt.Fprintln(readWriter.ErrOut, "Failed to determine root directory, falling back to gitlab-shell-ruby")
execRuby(readWriter)
fmt.Fprintln(readWriter.ErrOut, "Failed to determine root directory, exiting")
os.Exit(1)
}
// Fall back to Ruby in case of problems reading the config, but issue a
......@@ -52,7 +53,7 @@ func main() {
config, err := config.NewFromDir(rootDir)
if err != nil {
fmt.Fprintln(readWriter.ErrOut, "Failed to read config, falling back to gitlab-shell-ruby")
execRuby(readWriter)
execRuby(rootDir, readWriter)
}
cmd, err := command.New(os.Args, config)
......
......@@ -24,7 +24,7 @@ func New(arguments []string, config *config.Config) (Command, error) {
return buildCommand(args, config), nil
}
return &fallback.Command{}, nil
return &fallback.Command{RootDir: config.RootDir, Args: arguments}, nil
}
func buildCommand(args *commandargs.CommandArgs, config *config.Config) Command {
......
......@@ -8,14 +8,25 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter"
)
type Command struct{}
type Command struct {
RootDir string
Args []string
}
var (
binDir = filepath.Dir(os.Args[0])
// execFunc is overridden in tests
execFunc = syscall.Exec
)
const (
RubyProgram = "gitlab-shell-ruby"
)
func (c *Command) Execute(_ *readwriter.ReadWriter) error {
rubyCmd := filepath.Join(binDir, "gitlab-shell-ruby")
execErr := syscall.Exec(rubyCmd, os.Args, os.Environ())
return execErr
func (c *Command) Execute(*readwriter.ReadWriter) error {
rubyCmd := filepath.Join(c.RootDir, "bin", RubyProgram)
// Ensure rubyArgs[0] is the full path to gitlab-shell-ruby
rubyArgs := append([]string{rubyCmd}, c.Args[1:]...)
return execFunc(rubyCmd, rubyArgs, os.Environ())
}
package fallback
import (
"errors"
"os"
"testing"
"github.com/stretchr/testify/require"
)
type fakeExec struct {
OldExec func(string, []string, []string) error
Error error
Called bool
Filename string
Args []string
Env []string
}
var (
fakeArgs = []string{"./test", "foo", "bar"}
)
func (f *fakeExec) Exec(filename string, args []string, env []string) error {
f.Called = true
f.Filename = filename
f.Args = args
f.Env = env
return f.Error
}
func (f *fakeExec) Setup() {
f.OldExec = execFunc
execFunc = f.Exec
}
func (f *fakeExec) Cleanup() {
execFunc = f.OldExec
}
func TestExecuteExecsCommandSuccesfully(t *testing.T) {
cmd := &Command{RootDir: "/tmp", Args: fakeArgs}
// Override the exec func
fake := &fakeExec{}
fake.Setup()
defer fake.Cleanup()
require.NoError(t, cmd.Execute(nil))
require.True(t, fake.Called)
require.Equal(t, fake.Filename, "/tmp/bin/gitlab-shell-ruby")
require.Equal(t, fake.Args, []string{"/tmp/bin/gitlab-shell-ruby", "foo", "bar"})
require.Equal(t, fake.Env, os.Environ())
}
func TestExecuteExecsCommandOnError(t *testing.T) {
cmd := &Command{RootDir: "/test", Args: fakeArgs}
// Override the exec func
fake := &fakeExec{Error: errors.New("Test error")}
fake.Setup()
defer fake.Cleanup()
require.Error(t, cmd.Execute(nil))
require.True(t, fake.Called)
}
func TestExecuteGivenNonexistentCommand(t *testing.T) {
cmd := &Command{RootDir: "/tmp/does/not/exist", Args: fakeArgs}
require.Error(t, cmd.Execute(nil))
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment