Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Issue #216 - Validate if current directory has git executable in windows #219

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})

})

}