Skip to content

Commit

Permalink
chore: migrate test to their own package (testpackage linter) (#3709)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Mar 11, 2024
1 parent 230b1ef commit 7f06641
Show file tree
Hide file tree
Showing 145 changed files with 1,503 additions and 1,305 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ linters:
- staticcheck
- stylecheck
- tenv
- testpackage
- typecheck
- unconvert
- unparam
Expand Down
6 changes: 4 additions & 2 deletions internal/alias/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package alias
package alias_test

import (
"fmt"
"testing"

"github.com/scaleway/scaleway-cli/v2/internal/alias"

"github.com/alecthomas/assert"
)

Expand Down Expand Up @@ -37,7 +39,7 @@ func TestConfig_ResolveAliases(t *testing.T) {
}
for i, tt := range tests {
t.Run(fmt.Sprintf("Resolve_TestCase%d", i), func(t *testing.T) {
config := &Config{Aliases: tt.Aliases}
config := &alias.Config{Aliases: tt.Aliases}
actual := config.ResolveAliases(tt.Command)
assert.Equal(t, tt.Expected, actual)
})
Expand Down
10 changes: 6 additions & 4 deletions internal/args/args_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package args
package args_test

import (
"fmt"
Expand All @@ -7,6 +7,8 @@ import (
"testing"
"time"

"github.com/scaleway/scaleway-cli/v2/internal/args"

"github.com/alecthomas/assert"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -144,12 +146,12 @@ type SamePrefixArgName struct {

func TestRawArgs_GetAll(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
a := RawArgs{"ssh-keys.0=foo", "ssh-keys.1=bar"}
a := args.RawArgs{"ssh-keys.0=foo", "ssh-keys.1=bar"}
assert.Equal(t, a.GetAll("ssh-keys.{index}"), []string{"foo", "bar"})
})

t.Run("Insane", func(t *testing.T) {
a := RawArgs{
a := args.RawArgs{
"countries.FR.cities.paris.street.vaugirard=pouet",
"countries.FR.cities.paris.street.besbar=tati",
"countries.FR.cities.nice.street.promenade=anglais",
Expand All @@ -169,7 +171,7 @@ func TestGetArgType(t *testing.T) {

run := func(tc *TestCase) func(*testing.T) {
return func(t *testing.T) {
res, err := GetArgType(tc.ArgType, tc.Name)
res, err := args.GetArgType(tc.ArgType, tc.Name)
if tc.expectedError == "" {
require.NoError(t, err)
assert.Equal(t, tc.ExpectedKind, res.Kind())
Expand Down
14 changes: 8 additions & 6 deletions internal/args/marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package args
package args_test

import (
"testing"
"time"

"github.com/scaleway/scaleway-cli/v2/internal/args"

"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
)
Expand All @@ -20,7 +22,7 @@ func TestMarshal(t *testing.T) {

run := func(testCase TestCase) func(t *testing.T) {
return func(t *testing.T) {
args, err := MarshalStruct(testCase.data)
args, err := args.MarshalStruct(testCase.data)

if testCase.error == "" {
assert.NoError(t, err)
Expand All @@ -31,7 +33,7 @@ func TestMarshal(t *testing.T) {
}
}

RegisterMarshalFunc((*height)(nil), marshalHeight)
args.RegisterMarshalFunc((*height)(nil), marshalHeight)

t.Run("basic", run(TestCase{
data: &Basic{
Expand Down Expand Up @@ -198,7 +200,7 @@ func TestMarshal(t *testing.T) {
}))

t.Run("data-is-raw-args", run(TestCase{
data: &RawArgs{
data: &args.RawArgs{
"pro.access_key",
"access_key",
},
Expand Down Expand Up @@ -233,7 +235,7 @@ func TestMarshalValue(t *testing.T) {

run := func(testCase TestCase) func(t *testing.T) {
return func(t *testing.T) {
value, err := MarshalValue(testCase.data)
value, err := args.MarshalValue(testCase.data)

if testCase.error == "" {
assert.NoError(t, err)
Expand All @@ -244,7 +246,7 @@ func TestMarshalValue(t *testing.T) {
}
}

RegisterMarshalFunc((*height)(nil), marshalHeight)
args.RegisterMarshalFunc((*height)(nil), marshalHeight)

t.Run("string", run(TestCase{
data: "a simple string",
Expand Down
16 changes: 9 additions & 7 deletions internal/args/unmarshal_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package args
package args_test

import (
"net"
"reflect"
"testing"
"time"

"github.com/scaleway/scaleway-cli/v2/internal/args"

"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
)

func init() {
TestForceNow = scw.TimePtr(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))
args.TestForceNow = scw.TimePtr(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))
}

func TestUnmarshalStruct(t *testing.T) {
Expand All @@ -30,7 +32,7 @@ func TestUnmarshalStruct(t *testing.T) {
if testCase.data == nil {
testCase.data = reflect.New(reflect.TypeOf(testCase.expected).Elem()).Interface()
}
err := UnmarshalStruct(testCase.args, testCase.data)
err := args.UnmarshalStruct(testCase.args, testCase.data)

if testCase.error == "" {
assert.NoError(t, err)
Expand All @@ -41,7 +43,7 @@ func TestUnmarshalStruct(t *testing.T) {
}
}

RegisterUnmarshalFunc((*height)(nil), unmarshalHeight)
args.RegisterUnmarshalFunc((*height)(nil), unmarshalHeight)

t.Run("basic", run(TestCase{
args: []string{
Expand Down Expand Up @@ -362,7 +364,7 @@ func TestUnmarshalStruct(t *testing.T) {
"pro.access_key",
"access_key",
},
expected: &RawArgs{
expected: &args.RawArgs{
"pro.access_key",
"access_key",
},
Expand Down Expand Up @@ -537,12 +539,12 @@ func TestIsUmarshalableValue(t *testing.T) {

run := func(testCase TestCase) func(t *testing.T) {
return func(t *testing.T) {
value := IsUmarshalableValue(testCase.data)
value := args.IsUmarshalableValue(testCase.data)
assert.Equal(t, testCase.expected, value)
}
}

RegisterUnmarshalFunc((*height)(nil), unmarshalHeight)
args.RegisterUnmarshalFunc((*height)(nil), unmarshalHeight)

strPtr := "This is a pointer"
heightPtr := height(42)
Expand Down
30 changes: 16 additions & 14 deletions internal/core/alias_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package core
package core_test

import (
"testing"

"github.com/scaleway/scaleway-cli/v2/internal/core"

"github.com/alecthomas/assert"
"github.com/scaleway/scaleway-cli/v2/internal/alias"
)

func TestCommandMatchAlias(t *testing.T) {
commandWithArg := &Command{
commandWithArg := &core.Command{
Namespace: "first",
Resource: "command",
ArgSpecs: ArgSpecs{
ArgSpecs: core.ArgSpecs{
{
Name: "arg",
},
},
}
commandWithoutArg := &Command{
commandWithoutArg := &core.Command{
Namespace: "second",
Resource: "command",
ArgSpecs: ArgSpecs{
ArgSpecs: core.ArgSpecs{
{
Name: "other-arg",
},
Expand All @@ -32,28 +34,28 @@ func TestCommandMatchAlias(t *testing.T) {
Command: []string{"command"},
}

assert.True(t, commandWithArg.matchAlias(testAlias))
assert.True(t, commandWithoutArg.matchAlias(testAlias))
assert.True(t, commandWithArg.MatchAlias(testAlias))
assert.True(t, commandWithoutArg.MatchAlias(testAlias))

testAliasWithArg := alias.Alias{
Name: "alias",
Command: []string{"command", "arg=value"},
}

assert.True(t, commandWithArg.matchAlias(testAliasWithArg))
assert.False(t, commandWithoutArg.matchAlias(testAliasWithArg))
assert.True(t, commandWithArg.MatchAlias(testAliasWithArg))
assert.False(t, commandWithoutArg.MatchAlias(testAliasWithArg))
}

func TestAliasChildCommand(t *testing.T) {
namespace := &Command{
namespace := &core.Command{
Namespace: "namespace",
}
resource := &Command{
resource := &core.Command{
Namespace: "namespace",
Resource: "first",
}

commands := NewCommands(
commands := core.NewCommands(
namespace,
resource,
)
Expand All @@ -63,12 +65,12 @@ func TestAliasChildCommand(t *testing.T) {
Command: []string{"namespace", "first"},
}

assert.True(t, commands.aliasIsValidCommandChild(namespace, validAlias))
assert.True(t, commands.AliasIsValidCommandChild(namespace, validAlias))

invalidAlias := alias.Alias{
Name: "alias",
Command: []string{"namespace", "random"},
}

assert.False(t, commands.aliasIsValidCommandChild(namespace, invalidAlias))
assert.False(t, commands.AliasIsValidCommandChild(namespace, invalidAlias))
}
14 changes: 8 additions & 6 deletions internal/core/arg_specs_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package core
package core_test

import (
"testing"

"github.com/scaleway/scaleway-cli/v2/internal/core"

"github.com/alecthomas/assert"
)

func TestOneOf(t *testing.T) {
a := &ArgSpec{
a := &core.ArgSpec{
Name: "Argument A",
OneOfGroup: "ab group",
}
b := &ArgSpec{
b := &core.ArgSpec{
Name: "Argument B",
OneOfGroup: "ab group",
}
c := &ArgSpec{
c := &core.ArgSpec{
Name: "Argument C",
OneOfGroup: "",
}
d := &ArgSpec{
d := &core.ArgSpec{
Name: "Argument D",
OneOfGroup: "",
}
e := &ArgSpec{
e := &core.ArgSpec{
Name: "Argument E",
}
assert.True(t, a.ConflictWith(b))
Expand Down
Loading

0 comments on commit 7f06641

Please sign in to comment.