diff --git a/cmd/add/add_test.go b/cmd/add/add_test.go new file mode 100644 index 0000000..d5999d2 --- /dev/null +++ b/cmd/add/add_test.go @@ -0,0 +1,37 @@ +package add + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "testing" + + "github.com/candrewlee14/webman/utils" + + "github.com/matryer/is" +) + +func TestAdd(t *testing.T) { + if _, ok := os.LookupEnv("WEBMAN_INTEGRATION"); !ok { + t.Skip("skipping integration test") + } + + assert := is.New(t) + + tmp := t.TempDir() + utils.Init(tmp) + os.Args = []string{"webman", "jq", "rg"} + + err := AddCmd.Execute() + assert.NoErr(err) // Command should execute + + _, err = os.Stat(filepath.Join(utils.WebmanBinDir, "jq")) + assert.NoErr(err) // jq binary should exist + + _, err = os.Stat(filepath.Join(utils.WebmanBinDir, "rg")) + assert.NoErr(err) // rg binary should exist + + _, err = os.Stat(filepath.Join(utils.WebmanBinDir, "bat")) + assert.True(errors.Is(err, fs.ErrNotExist)) // bat binary should not exist +} diff --git a/cmd/remove/remove_test.go b/cmd/remove/remove_test.go new file mode 100644 index 0000000..c97b8f6 --- /dev/null +++ b/cmd/remove/remove_test.go @@ -0,0 +1,41 @@ +package remove + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "testing" + + "github.com/candrewlee14/webman/cmd/add" + "github.com/candrewlee14/webman/utils" + + "github.com/matryer/is" +) + +func TestRemove(t *testing.T) { + if _, ok := os.LookupEnv("WEBMAN_INTEGRATION"); !ok { + t.Skip("skipping integration test") + } + + assert := is.New(t) + + tmp := t.TempDir() + utils.Init(tmp) + os.Args = []string{"webman", "jq"} + + err := add.AddCmd.Execute() + assert.NoErr(err) // Command should execute + + _, err = os.Stat(filepath.Join(utils.WebmanBinDir, "jq")) + assert.NoErr(err) // jq binary should exist + + err = RemoveCmd.Execute() + assert.NoErr(err) // Command should execute + + _, err = os.Stat(filepath.Join(utils.WebmanBinDir, "jq")) + assert.True(errors.Is(err, fs.ErrNotExist)) // jq binary should not exist + + _, err = os.Stat(filepath.Join(utils.WebmanPkgDir, "jq")) + assert.True(errors.Is(err, fs.ErrNotExist)) // jq pkg should not exist +} diff --git a/cmd/root.go b/cmd/root.go index b4cef72..3e6a336 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -81,6 +81,10 @@ func Execute() { } func init() { - utils.Init() + homeDir, err := os.UserHomeDir() + if err != nil { + panic(err) + } + utils.Init(homeDir) rootCmd.PersistentFlags().StringVarP(&utils.RecipeDirFlag, "local-recipes", "l", "", "use given local recipe directory") } diff --git a/go.mod b/go.mod index 175fe62..2216b9a 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/fatih/color v1.13.0 github.com/ivanpirog/coloredcobra v1.0.1 github.com/ktr0731/go-fuzzyfinder v0.6.0 + github.com/matryer/is v1.4.0 github.com/mattn/go-isatty v0.0.14 github.com/mholt/archiver/v3 v3.5.1 github.com/schollz/progressbar/v3 v3.8.6 diff --git a/go.sum b/go.sum index 6b12c57..fca8d80 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,8 @@ github.com/ktr0731/go-fuzzyfinder v0.6.0 h1:lP6B3B8CjqbKGf/K5f1X5kdpxiSkCH0+9Azg github.com/ktr0731/go-fuzzyfinder v0.6.0/go.mod h1:QrbU5RFMEFBbPZnlJBqctX6028IV8qW/yCX3DCAzi1Y= github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= +github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= diff --git a/utils/utils.go b/utils/utils.go index 9f561b1..fd3d43f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -28,11 +28,7 @@ var ( UsingFileName = "using.yaml" ) -func Init() { - homeDir, err := os.UserHomeDir() - if err != nil { - panic(err) - } +func Init(homeDir string) { WebmanDir = filepath.Join(homeDir, ".webman") WebmanConfig = filepath.Join(WebmanDir, "config.yaml") WebmanPkgDir = filepath.Join(WebmanDir, "pkg") @@ -42,13 +38,13 @@ func Init() { GOOS = runtime.GOOS GOARCH = runtime.GOARCH - if err = os.MkdirAll(WebmanBinDir, os.ModePerm); err != nil { + if err := os.MkdirAll(WebmanBinDir, os.ModePerm); err != nil { panic(err) } - if err = os.MkdirAll(WebmanPkgDir, os.ModePerm); err != nil { + if err := os.MkdirAll(WebmanPkgDir, os.ModePerm); err != nil { panic(err) } - if err = os.MkdirAll(WebmanTmpDir, os.ModePerm); err != nil { + if err := os.MkdirAll(WebmanTmpDir, os.ModePerm); err != nil { panic(err) } if RecipeDirFlag != "" { @@ -60,6 +56,9 @@ func Init() { color.Magenta("Using local recipe directory: %s", color.HiBlackString(recipeDir)) WebmanRecipeDir = recipeDir } + if err := os.MkdirAll(WebmanRecipeDir, os.ModePerm); err != nil { + panic(err) + } if !isatty.IsTerminal(os.Stdout.Fd()) { multiline.ClearLine = []byte{} multiline.MoveDown = []byte{}