Skip to content

Commit

Permalink
Add basic tests (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolheiser authored Sep 28, 2022
1 parent 610c1ca commit 14eae1d
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
37 changes: 37 additions & 0 deletions cmd/add/add_test.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions cmd/remove/remove_test.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
15 changes: 7 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 != "" {
Expand All @@ -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{}
Expand Down

0 comments on commit 14eae1d

Please sign in to comment.