From 55aefe8a0338b77febeaa97b2a521c572e708752 Mon Sep 17 00:00:00 2001 From: Prabhu Jayakumar Date: Fri, 7 Aug 2020 15:15:13 +0530 Subject: [PATCH] [Issue #216] Validate if current directory has git executable in windows OS --- talisman.go | 25 +++++++++++++++++++++++ talisman_internal_test.go | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/talisman.go b/talisman.go index 076ba5c2..f085327f 100644 --- a/talisman.go +++ b/talisman.go @@ -1,7 +1,9 @@ package main import ( + "github.com/spf13/afero" flag "github.com/spf13/pflag" + "runtime" "talisman/prompt" ) @@ -100,6 +102,11 @@ func main() { } func run(stdin io.Reader, _options options, promptContext prompt.PromptContext) (returnCode int) { + if err := validateGitExecutable(afero.NewOsFs(), runtime.GOOS); err != nil { + log.Printf("error validating git executable: %v", err) + return 1 + } + if _options.debug { log.SetLevel(log.DebugLevel) } else { @@ -145,3 +152,21 @@ func readRefAndSha(file io.Reader) (string, string, string, string) { } return refsAndShas[0], refsAndShas[1], refsAndShas[2], refsAndShas[3] } + +func validateGitExecutable(fs afero.Fs, operatingSystem string) error { + if operatingSystem == "windows" { + extensions := strings.ToLower(os.Getenv("PATHEXT")) + windowsExecutables := strings.Split(extensions, ";") + for _, executable := range windowsExecutables { + gitExecutable := fmt.Sprintf("git%s", executable) + exists, err := afero.Exists(fs, gitExecutable) + if err != nil { + return err + } + if exists { + return fmt.Errorf("not allowed to have git executable located in repository: %s", gitExecutable) + } + } + } + return nil +} diff --git a/talisman_internal_test.go b/talisman_internal_test.go index 23610c35..7277c37a 100644 --- a/talisman_internal_test.go +++ b/talisman_internal_test.go @@ -1,6 +1,7 @@ package main import ( + "github.com/spf13/afero" "io/ioutil" "os" "testing" @@ -22,3 +23,45 @@ func TestParsingShasFromStdIn(t *testing.T) { assert.Equal(t, "localSha", oldSha, "oldSha did not equal 'localSha', got: %s", oldSha) assert.Equal(t, "remoteSha", newSha, "newSha did not equal 'remoteSha', got: %s", newSha) } + +func Test_validateGitExecutable(t *testing.T) { + t.Run("given operating systems is windows", func(t *testing.T) { + + operatingSystem := "windows" + os.Setenv("PATHEXT", ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC") + + t.Run("should return error if git executable exists in current directory", func(t *testing.T) { + fs := afero.NewMemMapFs() + gitExecutable := "git.exe" + afero.WriteFile(fs, gitExecutable, []byte("git executable"), 0700) + err := validateGitExecutable(fs, operatingSystem) + assert.EqualError(t, err, "not allowed to have git executable located in repository: git.exe") + }) + + t.Run("should return nil if git executable does not exist in current directory", func(t *testing.T) { + err := validateGitExecutable(afero.NewMemMapFs(), operatingSystem) + assert.NoError(t, err) + }) + + }) + + t.Run("given operating systems is linux", func(t *testing.T) { + + operatingSystem := "linux" + + t.Run("should return nil if git executable exists in current directory", func(t *testing.T) { + fs := afero.NewMemMapFs() + gitExecutable := "git.exe" + afero.WriteFile(fs, gitExecutable, []byte("git executable"), 0700) + err := validateGitExecutable(fs, operatingSystem) + assert.NoError(t, err) + }) + + t.Run("should return nil if git executable does not exist in current directory", func(t *testing.T) { + err := validateGitExecutable(afero.NewMemMapFs(), operatingSystem) + assert.NoError(t, err) + }) + + }) + +}