feat: refactor requirements.go
This commit is contained in:
parent
55dacbfec2
commit
54f0049c06
2 changed files with 3 additions and 38 deletions
|
|
@ -1,37 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// new type for the lookup function
|
|
||||||
type lookPathFunc func(file string) (string, error)
|
|
||||||
|
|
||||||
// modified function to accept a lookup function
|
|
||||||
func verifyGitAvailableWithLookup(lookPath lookPathFunc) error {
|
|
||||||
_, err := lookPath("git")
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("could not find git in path")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVerifyGitAvailable(t *testing.T) {
|
|
||||||
|
|
||||||
// test case 1: git is available
|
|
||||||
err := verifyGitAvailableWithLookup(func(file string) (string, error) {
|
|
||||||
return "/usr/bin/git", nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("expected no error when git is available, got: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// test case 2: git is not available
|
|
||||||
err = verifyGitAvailableWithLookup(func(file string) (string, error) {
|
|
||||||
return "", errors.New("git not found")
|
|
||||||
})
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error when git is not available, got nil")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// verify if git is available
|
||||||
func verifyGitAvailable() error {
|
func verifyGitAvailable() error {
|
||||||
_, err := exec.LookPath("git")
|
_, err := exec.LookPath("git")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("git is not installed or not in PATH: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue