Skip to content

Commit

Permalink
[Issue #216] Validate if current directory has git executable in wind…
Browse files Browse the repository at this point in the history
…ows OS (#219)
  • Loading branch information
prabhu43 authored Aug 8, 2020
1 parent b94762f commit b821169
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
25 changes: 25 additions & 0 deletions talisman.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"github.com/spf13/afero"
flag "github.com/spf13/pflag"
"runtime"
"talisman/prompt"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
43 changes: 43 additions & 0 deletions talisman_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/spf13/afero"
"io/ioutil"
"os"
"testing"
Expand All @@ -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)
})

})

}

0 comments on commit b821169

Please sign in to comment.