Skip to content

Commit

Permalink
Replace remained testify tests with gt
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mizutani committed Feb 23, 2023
1 parent 368e01a commit 56f5ed3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 79 deletions.
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@ require (
github.com/google/uuid v1.3.0
github.com/keybase/go-keychain v0.0.0-20221221221913-9be78f6c498b
github.com/m-mizutani/goerr v0.1.8
github.com/m-mizutani/gt v0.0.3
github.com/m-mizutani/gt v0.0.4-0.20230223020823-2e7042cd92a6
github.com/mattn/go-shellwords v1.0.12
github.com/rs/zerolog v1.29.0
github.com/stretchr/testify v1.8.0
github.com/urfave/cli/v2 v2.24.4
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
27 changes: 2 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,17 @@ github.com/Songmu/prompter v0.5.1/go.mod h1:CS3jEPD6h9IaLaG6afrl1orTgII9+uDWuw95
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/keybase/go-keychain v0.0.0-20221221221913-9be78f6c498b h1:k2ZvAPXrDB1Q7fGRdUane+T08K+UaL96qH47Setr/7k=
github.com/keybase/go-keychain v0.0.0-20221221221913-9be78f6c498b/go.mod h1:TXh6wFVZNh4iuqbymzy4r3obmj8hTPDlLNnoKNIbvJE=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/m-mizutani/goerr v0.1.8 h1:6UtsMmOkJsaYNtAsMNLvWIteZPl1NOxpKFYK5m65vpQ=
github.com/m-mizutani/goerr v0.1.8/go.mod h1:fQkXuu06q+oLlp4FkbiTFzI/N/+WAK/Mz1W5kPZ6yzs=
github.com/m-mizutani/gt v0.0.3 h1:kx0WAJjLuCldVNEkYyZZh1Y+n15LB/eFiBM2vRdpCGU=
github.com/m-mizutani/gt v0.0.3/go.mod h1:0MPYSfGBLmYjTduzADVmIqD58ELQ5IfBFiK/f0FmB3k=
github.com/m-mizutani/gt v0.0.4-0.20230223020823-2e7042cd92a6 h1:aB3qT7U3VGuzOyoz/6QbwE7DxbrFWQBLlbOYwjSSBss=
github.com/m-mizutani/gt v0.0.4-0.20230223020823-2e7042cd92a6/go.mod h1:0MPYSfGBLmYjTduzADVmIqD58ELQ5IfBFiK/f0FmB3k=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand All @@ -34,18 +24,11 @@ github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/urfave/cli/v2 v2.24.4 h1:0gyJJEBYtCV87zI/x2nZCPyDxD51K6xM8SkwjHFCNEU=
github.com/urfave/cli/v2 v2.24.4/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
Expand All @@ -57,9 +40,3 @@ golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
96 changes: 49 additions & 47 deletions pkg/usecase/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,62 @@ import (
"os"
"testing"

"github.com/m-mizutani/gt"
"github.com/m-mizutani/zenv/pkg/domain/model"
"github.com/m-mizutani/zenv/pkg/domain/types"
"github.com/m-mizutani/zenv/pkg/usecase"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWrite(t *testing.T) {
t.Run("load keychain variables", func(t *testing.T) {
uc, mock := usecase.NewWithMock()
mock.ExecMock = func(vars []*model.EnvVar, args types.Arguments) error {
require.Len(t, args, 2)
assert.Equal(t, types.Argument("this"), args[0])
assert.Equal(t, types.Argument("test"), args[1])

require.Len(t, vars, 1)
assert.Equal(t, types.EnvKey("COLOR"), vars[0].Key)
assert.Equal(t, types.EnvValue("blue"), vars[0].Value)
gt.Array(t, args).Equal([]types.Argument{"this", "test"})
gt.Array(t, vars).Equal([]*model.EnvVar{
{
Key: "COLOR",
Value: "blue",
Secret: true,
},
})

return nil
}

mock.PromptMock = func(msg string) string { return "blue" }
require.NoError(t, uc.WriteSecret(&model.WriteSecretInput{
gt.NoError(t, uc.WriteSecret(&model.WriteSecretInput{
Namespace: "@tower",
Key: "COLOR",
}))

require.NoError(t, uc.Exec(&model.ExecInput{
gt.NoError(t, uc.Exec(&model.ExecInput{
EnvVars: []*model.EnvVar{},
Args: types.Arguments{"@tower", "this", "test"},
}))
})

t.Run("keychain namespace not found", func(t *testing.T) {
uc, _ := usecase.NewWithMock()
require.ErrorIs(t, uc.Exec(&model.ExecInput{
gt.Error(t, uc.Exec(&model.ExecInput{
EnvVars: []*model.EnvVar{},
Args: types.Arguments{"@tower", "this", "test"},
}), types.ErrKeychainNotFound)
})).Is(types.ErrKeychainNotFound)
})
}

func TestGenerate(t *testing.T) {
t.Run("generate random secure variable", func(t *testing.T) {
uc, mock := usecase.NewWithMock()
mock.PutKeyChainValuesMock = func(envVars []*model.EnvVar, namespace types.Namespace) error {
require.Len(t, envVars, 1)
assert.Equal(t, types.Namespace("zenv.bridge"), namespace)
assert.Equal(t, types.EnvKey("SECRET"), envVars[0].Key)
assert.Len(t, envVars[0].Value, 24)
gt.V(t, namespace).Equal("zenv.bridge")
gt.A(t, envVars).Length(1).
Elem(0, func(t testing.TB, v *model.EnvVar) {
gt.Value(t, v.Key).Equal("SECRET")
gt.N(t, len(v.Value)).Equal(24)
})
return nil
}
require.NoError(t, uc.GenerateSecret(&model.GenerateSecretInput{
gt.NoError(t, uc.GenerateSecret(&model.GenerateSecretInput{
Namespace: "@bridge",
Key: "SECRET",
Length: 24,
Expand All @@ -66,36 +68,36 @@ func TestGenerate(t *testing.T) {

t.Run("fail if length <= 0", func(t *testing.T) {
uc, _ := usecase.NewWithMock()
require.ErrorIs(t, uc.GenerateSecret(&model.GenerateSecretInput{
gt.Error(t, uc.GenerateSecret(&model.GenerateSecretInput{
Namespace: "@bridge",
Key: "SECRET",
Length: 0,
}), types.ErrInvalidArgument)
})).Is(types.ErrInvalidArgument)
})

t.Run("fail if length > 2^16", func(t *testing.T) {
uc, _ := usecase.NewWithMock()
require.ErrorIs(t, uc.GenerateSecret(&model.GenerateSecretInput{
gt.Error(t, uc.GenerateSecret(&model.GenerateSecretInput{
Namespace: "@bridge",
Key: "SECRET",
Length: 65536,
}), types.ErrInvalidArgument)
})).Is(types.ErrInvalidArgument)
})

t.Run("fail if key is empty", func(t *testing.T) {
uc, _ := usecase.NewWithMock()
require.ErrorIs(t, uc.GenerateSecret(&model.GenerateSecretInput{
gt.Error(t, uc.GenerateSecret(&model.GenerateSecretInput{
Namespace: "@bridge",
Length: 24,
}), types.ErrInvalidArgument)
})).Is(types.ErrInvalidArgument)
})

t.Run("fail if namespaec is empty", func(t *testing.T) {
uc, _ := usecase.NewWithMock()
require.ErrorIs(t, uc.GenerateSecret(&model.GenerateSecretInput{
gt.Error(t, uc.GenerateSecret(&model.GenerateSecretInput{
Key: "blue",
Length: 24,
}), types.ErrInvalidArgument)
})).Is(types.ErrInvalidArgument)
})
}

Expand All @@ -104,28 +106,29 @@ func TestFileLoader(t *testing.T) {
var calledExec int
uc, mock := usecase.NewWithMock()
mock.ReadFileMock = func(filename types.FilePath) ([]byte, error) {
assert.Equal(t, types.FilePath("myfile.txt"), filename)
gt.Value(t, filename).Equal("myfile.txt")
return []byte("yummy"), nil
}
mock.ExecMock = func(vars []*model.EnvVar, args types.Arguments) error {
calledExec++
require.Len(t, vars, 1)
require.Len(t, args, 1)
assert.Equal(t, &model.EnvVar{
Key: "FILE_VAL",
Value: "yummy",
Secret: false,
}, vars[0])
assert.Equal(t, types.Argument("gogo"), args[0])
gt.Array(t, vars).Equal([]*model.EnvVar{
{
Key: "FILE_VAL",
Value: "yummy",
Secret: false,
},
})
gt.Array(t, args).Length(1)
gt.Value(t, args[0]).Equal("gogo")
return nil
}
require.NoError(t, uc.Exec(&model.ExecInput{
gt.NoError(t, uc.Exec(&model.ExecInput{
Args: types.Arguments{
"FILE_VAL=&myfile.txt",
"gogo",
},
}))
assert.Equal(t, 1, calledExec)
gt.N(t, calledExec).Equal(1)
})

t.Run("fail if not existing file specified", func(t *testing.T) {
Expand All @@ -138,13 +141,13 @@ func TestFileLoader(t *testing.T) {
calledExec++
return nil
}
require.ErrorIs(t, uc.Exec(&model.ExecInput{
gt.Error(t, uc.Exec(&model.ExecInput{
Args: types.Arguments{
"FILE_VAL=&myfile.txt",
"gogo",
},
}), os.ErrNotExist)
assert.Equal(t, 0, calledExec)
})).Is(os.ErrNotExist)
gt.N(t, calledExec).Equal(0)
})
}

Expand All @@ -156,15 +159,14 @@ func TestAssign(t *testing.T) {
return []byte("BLUE=%ORANGE"), nil
}

args, vars, err := usecase.ParseArgs(uc, types.Arguments{
args, vars := gt.R2(usecase.ParseArgs(uc, types.Arguments{
"ORANGE=red",
"hello",
})
require.NoError(t, err)
require.Len(t, vars, 2)
assert.Equal(t, model.EnvVar{
})).NoError(t)

gt.Array(t, vars).Length(2).EqualAt(0, &model.EnvVar{
Key: "BLUE",
Value: "red",
}, *vars[0])
assert.Equal(t, args, types.Arguments{"hello"})
})
gt.Array(t, args).Equal(types.Arguments{"hello"})
}

0 comments on commit 56f5ed3

Please sign in to comment.