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
  • Loading branch information
prabhu43 committed Aug 7, 2020
1 parent b94762f commit f1ec942
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
28 changes: 28 additions & 0 deletions talisman.go
@@ -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,24 @@ 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" {
var windowsExecutables = []string{
"bat","bin","cmd","com","cpl","exe","gadget","inf1","ins","inx","isu",
"job","jse","lnk","msc","msi","msp","mst","paf","pif","ps1","reg","rgs",
"scr","sct","shb","shs","u3p","vb","vbe","vbs","vbscript","ws","wsf","wsh",
}
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
}
42 changes: 42 additions & 0 deletions talisman_internal_test.go
@@ -1,6 +1,7 @@
package main

import (
"github.com/spf13/afero"
"io/ioutil"
"os"
"testing"
Expand All @@ -22,3 +23,44 @@ 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"

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 f1ec942

Please sign in to comment.