Skip to content

Commit 403e2dc

Browse files
committed
Keep test literals readable and exclude goconst from tests
1 parent ba2c89d commit 403e2dc

64 files changed

Lines changed: 735 additions & 978 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.golangci.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ linters:
6060
allow-unused: true
6161
exclusions:
6262
generated: lax
63+
rules:
64+
- path: '(_test|test_helper)\.go$'
65+
linters:
66+
- goconst
6367
presets:
6468
- comments
6569
- common-false-positives

‎cmd/context/context_delete_test.go‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
const (
12-
testBar = "bar"
13-
nameFlagArg = "--name"
14-
)
15-
1611
func TestDeleteCurrentContext(t *testing.T) {
1712
cli := di.NewMockDeps(t)
1813
setupConfig(t, &cli)
1914
cmd := DeleteCommand(&cli.Deps)
2015

21-
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, nameFlagArg, "foo")
16+
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--name", "foo")
2217
assert.Errorf(t, err, "cannot delete the current context (foo)")
2318
}
2419

@@ -27,7 +22,7 @@ func TestDeleteNonExistingContext(t *testing.T) {
2722
setupConfig(t, &cli)
2823
cmd := DeleteCommand(&cli.Deps)
2924

30-
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, nameFlagArg, "x")
25+
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--name", "x")
3126
assert.Errorf(t, err, "context with name 'x' not found")
3227
}
3328

@@ -36,7 +31,7 @@ func TestDeleteContext(t *testing.T) {
3631
setupConfig(t, &cli)
3732
cmd := DeleteCommand(&cli.Deps)
3833

39-
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, nameFlagArg, testBar)
34+
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--name", "bar")
4035
assert.NoError(t, err)
4136

4237
cfg, err := config.ReadConfig(cli.ConfigPath)

‎cmd/context/context_save_test.go‎

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
)
1313

14-
const (
15-
testToken = "my-token"
16-
testContextURL = "http://bar.com"
17-
urlFlagArg = "--url"
18-
testCACertContext = "cacertdata"
19-
testAPIPath = "/api"
20-
serviceTokenFlagArg = "--service-token"
21-
caCertPathFlagArg = "--ca-cert-path"
22-
caCertBase64DataFlagArg = "--ca-cert-base64-data"
23-
)
24-
2514
func setupSaveCmd(t *testing.T) (*di.MockDeps, *cobra.Command) {
2615
cli := di.NewMockDeps(t)
2716
cmd := SaveCommand(&cli.Deps)
@@ -40,41 +29,41 @@ func TestSaveContext(t *testing.T) { //nolint:funlen
4029
}{
4130
{
4231
name: "new context",
43-
args: []string{nameFlagArg, "baz", urlFlagArg, "http://baz.com", "--api-token", testToken},
32+
args: []string{"--name", "baz", "--url", "http://baz.com", "--api-token", "my-token"},
4433
expectedContext: config.NamedContext{
4534
Name: "baz",
4635
Context: &config.StsContext{
4736
URL: "http://baz.com",
48-
APIToken: testToken,
49-
APIPath: testAPIPath,
37+
APIToken: "my-token",
38+
APIPath: "/api",
5039
},
5140
},
5241
totalContextInConfig: 7,
5342
wantErr: false,
5443
},
5544
{
5645
name: "existing context",
57-
args: []string{nameFlagArg, testBar, urlFlagArg, testContextURL, serviceTokenFlagArg, testToken},
46+
args: []string{"--name", "bar", "--url", "http://bar.com", "--service-token", "my-token"},
5847
expectedContext: config.NamedContext{
59-
Name: testBar,
48+
Name: "bar",
6049
Context: &config.StsContext{
61-
URL: testContextURL,
62-
ServiceToken: testToken,
63-
APIPath: testAPIPath,
50+
URL: "http://bar.com",
51+
ServiceToken: "my-token",
52+
APIPath: "/api",
6453
},
6554
},
6655
totalContextInConfig: 6,
6756
wantErr: false,
6857
},
6958
{
7059
name: "existing context ca-cert is set with ca-cert-path",
71-
args: []string{nameFlagArg, testBar, urlFlagArg, testContextURL, serviceTokenFlagArg, testToken, caCertPathFlagArg, "testdata/selfSignedCert.crt"},
60+
args: []string{"--name", "bar", "--url", "http://bar.com", "--service-token", "my-token", "--ca-cert-path", "testdata/selfSignedCert.crt"},
7261
expectedContext: config.NamedContext{
73-
Name: testBar,
62+
Name: "bar",
7463
Context: &config.StsContext{
75-
URL: testContextURL,
76-
ServiceToken: testToken,
77-
APIPath: testAPIPath,
64+
URL: "http://bar.com",
65+
ServiceToken: "my-token",
66+
APIPath: "/api",
7867
CaCertBase64Data: selfSignedBase64Cert,
7968
},
8069
},
@@ -83,13 +72,13 @@ func TestSaveContext(t *testing.T) { //nolint:funlen
8372
},
8473
{
8574
name: "new context ca-cert is set with ca-cert-path",
86-
args: []string{nameFlagArg, testCACertContext, urlFlagArg, testContextURL, serviceTokenFlagArg, testToken, caCertBase64DataFlagArg, privateCaBase64Cert},
75+
args: []string{"--name", "cacertdata", "--url", "http://bar.com", "--service-token", "my-token", "--ca-cert-base64-data", privateCaBase64Cert},
8776
expectedContext: config.NamedContext{
88-
Name: testCACertContext,
77+
Name: "cacertdata",
8978
Context: &config.StsContext{
90-
URL: testContextURL,
91-
ServiceToken: testToken,
92-
APIPath: testAPIPath,
79+
URL: "http://bar.com",
80+
ServiceToken: "my-token",
81+
APIPath: "/api",
9382
CaCertBase64Data: privateCaBase64Cert,
9483
},
9584
},
@@ -98,13 +87,13 @@ func TestSaveContext(t *testing.T) { //nolint:funlen
9887
},
9988
{
10089
name: "ca-cert-path takes precedence over ca-cert-base64-data",
101-
args: []string{nameFlagArg, testCACertContext, urlFlagArg, testContextURL, serviceTokenFlagArg, testToken, caCertPathFlagArg, "testdata/selfSignedCert.crt", caCertBase64DataFlagArg, privateCaBase64Cert},
90+
args: []string{"--name", "cacertdata", "--url", "http://bar.com", "--service-token", "my-token", "--ca-cert-path", "testdata/selfSignedCert.crt", "--ca-cert-base64-data", privateCaBase64Cert},
10291
expectedContext: config.NamedContext{
103-
Name: testCACertContext,
92+
Name: "cacertdata",
10493
Context: &config.StsContext{
105-
URL: testContextURL,
106-
ServiceToken: testToken,
107-
APIPath: testAPIPath,
94+
URL: "http://bar.com",
95+
ServiceToken: "my-token",
96+
APIPath: "/api",
10897
CaCertBase64Data: selfSignedBase64Cert,
10998
},
11099
},
@@ -113,13 +102,13 @@ func TestSaveContext(t *testing.T) { //nolint:funlen
113102
},
114103
{
115104
name: "ca-cert ignored if skip-ssl is set",
116-
args: []string{nameFlagArg, testBar, urlFlagArg, testContextURL, serviceTokenFlagArg, testToken, "--skip-ssl", caCertPathFlagArg, "/path/to/ca.crt", caCertBase64DataFlagArg, "base64-data"},
105+
args: []string{"--name", "bar", "--url", "http://bar.com", "--service-token", "my-token", "--skip-ssl", "--ca-cert-path", "/path/to/ca.crt", "--ca-cert-base64-data", "base64-data"},
117106
expectedContext: config.NamedContext{
118-
Name: testBar,
107+
Name: "bar",
119108
Context: &config.StsContext{
120-
URL: testContextURL,
121-
ServiceToken: testToken,
122-
APIPath: testAPIPath,
109+
URL: "http://bar.com",
110+
ServiceToken: "my-token",
111+
APIPath: "/api",
123112
SkipSSL: true,
124113
CaCertBase64Data: "",
125114
CaCertPath: "",
@@ -130,14 +119,14 @@ func TestSaveContext(t *testing.T) { //nolint:funlen
130119
},
131120
{
132121
name: "no save on missing tokens",
133-
args: []string{nameFlagArg, testBar, urlFlagArg, "http://my-bar.com"},
122+
args: []string{"--name", "bar", "--url", "http://my-bar.com"},
134123
expectedContext: config.NamedContext{},
135124
wantErr: true,
136125
errorMessage: "one of the required flags {api-token | service-token} not set",
137126
},
138127
{
139128
name: "ca-cert-path is not found",
140-
args: []string{nameFlagArg, testBar, urlFlagArg, "http://my-bar.com", serviceTokenFlagArg, testToken, caCertPathFlagArg, "/path/to/ca.crt"},
129+
args: []string{"--name", "bar", "--url", "http://my-bar.com", "--service-token", "my-token", "--ca-cert-path", "/path/to/ca.crt"},
141130
expectedContext: config.NamedContext{},
142131
wantErr: true,
143132
errorMessage: "no such file or directory",

‎cmd/context/context_set_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ func TestSetContext(t *testing.T) {
1313
setupConfig(t, &cli)
1414
cmd := SetCommand(&cli.Deps)
1515

16-
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, nameFlagArg, testBar)
16+
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--name", "bar")
1717
assert.NoError(t, err)
1818

1919
cfg, err := config.ReadConfig(cli.ConfigPath)
2020
assert.NoError(t, err)
21-
assert.Equal(t, testBar, cfg.CurrentContext)
21+
assert.Equal(t, "bar", cfg.CurrentContext)
2222
}
2323

2424
func TestSetUnknownContext(t *testing.T) {
2525
cli := di.NewMockDeps(t)
2626
setupConfig(t, &cli)
2727
cmd := SetCommand(&cli.Deps)
2828

29-
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, nameFlagArg, "x")
29+
_, err := di.ExecuteCommandWithContext(&cli.Deps, cmd, "--name", "x")
3030
assert.Errorf(t, err, "context with name 'x' not found")
3131

3232
cfg, err := config.ReadConfig(cli.ConfigPath)

‎cmd/context/context_show_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestShowContextJSON(t *testing.T) {
2424
assert.Equal(t, &config.StsContext{
2525
URL: "http://foo.com",
2626
APIToken: "apiToken",
27-
APIPath: testAPIPath,
27+
APIPath: "/api",
2828
}, calls[0]["current-context"])
2929
}
3030

‎cmd/context/context_validate_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestValidationToJson(t *testing.T) {
3232
di.ExecuteCommandWithContextUnsafe(
3333
&cli.Deps,
3434
cmd,
35-
urlFlagArg,
35+
"--url",
3636
"https://test.stackstate.io/",
3737
"--api-token",
3838
"blaat",

‎cmd/dashboard/common_test.go‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
const (
10-
testDashboardURN = "urn:custom:dashboard:test"
11-
)
12-
139
func TestResolveDashboardIdOrUrn(t *testing.T) {
1410
tests := []struct {
1511
name string
@@ -28,14 +24,14 @@ func TestResolveDashboardIdOrUrn(t *testing.T) {
2824
{
2925
name: "Should resolve valid identifier",
3026
id: 0,
31-
identifier: testDashboardURN,
32-
expectedResult: testDashboardURN,
27+
identifier: "urn:custom:dashboard:test",
28+
expectedResult: "urn:custom:dashboard:test",
3329
expectedError: "",
3430
},
3531
{
3632
name: "Should prioritize ID when both are provided",
3733
id: 456,
38-
identifier: testDashboardURN,
34+
identifier: "urn:custom:dashboard:test",
3935
expectedResult: "456",
4036
expectedError: "",
4137
},

‎cmd/dashboard/dashboard_apply_test.go‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
const (
14-
testGrid = "Grid"
15-
)
16-
1713
func setDashboardApplyCmd(t *testing.T) (*di.MockDeps, *cobra.Command) {
1814
cli := di.NewMockDeps(t)
1915
cmd := DashboardApplyCommand(&cli.Deps)
@@ -31,7 +27,7 @@ func createTestApplyResult() sts.DashboardReadFullSchema {
3127
Spec: &sts.PersesDashboardSpec{
3228
Layouts: []sts.PersesLayout{
3329
{
34-
Kind: testGrid,
30+
Kind: "Grid",
3531
Spec: sts.PersesLayoutSpec{
3632
Items: []sts.PersesGridItem{},
3733
},

‎cmd/dashboard/dashboard_delete_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func TestShouldDeleteDashboard(t *testing.T) {
2828
func TestShouldDeleteDashboardWithIdentifier(t *testing.T) {
2929
cli, cmd := setDashboardDeleteCmd(t)
3030

31-
di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "--identifier", testDashboardURN)
31+
di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "--identifier", "urn:custom:dashboard:test")
3232

3333
assert.Len(t, *cli.MockClient.ApiMocks.DashboardsApi.DeleteDashboardCalls, 1)
3434
deleteCall := (*cli.MockClient.ApiMocks.DashboardsApi.DeleteDashboardCalls)[0]
35-
assert.Equal(t, testDashboardURN, deleteCall.PdashboardIdOrUrn)
35+
assert.Equal(t, "urn:custom:dashboard:test", deleteCall.PdashboardIdOrUrn)
3636
assert.Equal(t, "Dashboard deleted: urn:custom:dashboard:test", (*cli.MockPrinter.SuccessCalls)[0])
3737
}
3838

‎cmd/dashboard/dashboard_describe_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func createTestDashboard() sts.DashboardReadSchema {
2727
Spec: &sts.PersesDashboardSpec{
2828
Layouts: []sts.PersesLayout{
2929
{
30-
Kind: testGrid,
30+
Kind: "Grid",
3131
Spec: sts.PersesLayoutSpec{
3232
Items: []sts.PersesGridItem{},
3333
},

0 commit comments

Comments
 (0)