Skip to content

Commit

Permalink
golangci-lint: Disable forcetypeassert for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 23, 2024
1 parent dc39ec7 commit b866c10
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ issues:
- gocognit
- funlen
- lll
- forcetypeassert
- path: js\/modules\/k6\/http\/.*_test\.go
linters:
# k6/http module's tests are quite complex because they often have several nested levels.
Expand Down
2 changes: 1 addition & 1 deletion js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ BigInt(1231412444)`,
require.Len(t, entries, 1)
assert.Equal(t, logrus.WarnLevel, entries[0].Level)
assert.Contains(t, entries[0].Message, "There were unknown fields")
assert.Contains(t, entries[0].Data["error"].(error).Error(), "unknown field \"something\"") //nolint:forcetypeassert
assert.Contains(t, entries[0].Data["error"].(error).Error(), "unknown field \"something\"")
})
})
}
Expand Down
10 changes: 5 additions & 5 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ func TestRequest(t *testing.T) {
logEntry := ts.hook.LastEntry()
require.NotNil(t, logEntry)
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
assert.ErrorContains(t, logEntry.Data["error"].(error), expErr) //nolint:forcetypeassert
assert.ErrorContains(t, logEntry.Data["error"].(error), expErr)
assert.Equal(t, "Request Failed", logEntry.Message)
})

Expand All @@ -599,7 +599,7 @@ func TestRequest(t *testing.T) {
logEntry := ts.hook.LastEntry()
require.NotNil(t, logEntry)
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
assert.ErrorContains(t, logEntry.Data["error"].(error), expErr) //nolint:forcetypeassert
assert.ErrorContains(t, logEntry.Data["error"].(error), expErr)
assert.Equal(t, "Request Failed", logEntry.Message)
})
})
Expand Down Expand Up @@ -2084,7 +2084,7 @@ func TestRequestAndBatchTLS(t *testing.T) {
return hosts
}(),
}
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext //nolint:forcetypeassert
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext
_, err = rt.RunString(`throw JSON.stringify(http.get("https://expired.localhost/"));`)
require.Error(t, err)
assert.Contains(t, err.Error(), "x509: certificate has expired or is not yet valid")
Expand Down Expand Up @@ -2136,7 +2136,7 @@ func TestRequestAndBatchTLS(t *testing.T) {
state.Dialer = &netext.Dialer{Hosts: hosts}
state.Transport = client.Transport
state.TLSConfig = s.TLS
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext //nolint:forcetypeassert
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext
realURL := "https://" + versionTest.URL + "/"
_, err = rt.RunString(fmt.Sprintf(`
var res = http.get("%s");
Expand Down Expand Up @@ -2181,7 +2181,7 @@ func TestRequestAndBatchTLS(t *testing.T) {
state.Dialer = &netext.Dialer{Hosts: hosts}
state.Transport = client.Transport
state.TLSConfig = s.TLS
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext //nolint:forcetypeassert
client.Transport.(*http.Transport).DialContext = state.Dialer.DialContext
realURL := "https://" + cipherSuiteTest.URL + "/"
_, err = rt.RunString(fmt.Sprintf(`
var res = http.get("%s");
Expand Down
8 changes: 4 additions & 4 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,8 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
defer cancel()
initVU, err := r.NewVU(ctx, 1, 1, make(chan metrics.SampleContainer, 100))
require.NoError(t, err)
initVU.(*VU).TLSConfig.RootCAs = x509.NewCertPool() //nolint:forcetypeassert
initVU.(*VU).TLSConfig.RootCAs.AddCert(cert) //nolint:forcetypeassert
initVU.(*VU).TLSConfig.RootCAs = x509.NewCertPool()
initVU.(*VU).TLSConfig.RootCAs.AddCert(cert)

vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
Expand Down Expand Up @@ -1428,8 +1428,8 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
defer cancel()
initVU, err := r.NewVU(ctx, 1, 1, make(chan metrics.SampleContainer, 100))
require.NoError(t, err)
initVU.(*VU).TLSConfig.RootCAs = x509.NewCertPool() //nolint:forcetypeassert
initVU.(*VU).TLSConfig.RootCAs.AddCert(cert) //nolint:forcetypeassert
initVU.(*VU).TLSConfig.RootCAs = x509.NewCertPool()
initVU.(*VU).TLSConfig.RootCAs.AddCert(cert)
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
if data.errMsg != "" {
Expand Down
4 changes: 2 additions & 2 deletions lib/executor/constant_vus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestConstantVUsRun(t *testing.T) {
default:
}
currIter, _ := result.LoadOrStore(state.VUID, uint64(0))
result.Store(state.VUID, currIter.(uint64)+1) //nolint:forcetypeassert
result.Store(state.VUID, currIter.(uint64)+1)
time.Sleep(210 * time.Millisecond)
return nil
})
Expand All @@ -45,7 +45,7 @@ func TestConstantVUsRun(t *testing.T) {

var totalIters uint64
result.Range(func(_, value interface{}) bool {
vuIters := value.(uint64) //nolint:forcetypeassert
vuIters := value.(uint64)
assert.Equal(t, uint64(5), vuIters)
totalIters += vuIters
return true
Expand Down
4 changes: 2 additions & 2 deletions lib/executor/executors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ var configMapTestCases = []configMapTestCase{
`{"carrival": {"executor": "constant-arrival-rate", "rate": 10, "duration": "10m", "preAllocatedVUs": 20}}`,
exp{custom: func(t *testing.T, cm lib.ScenarioConfigs) {
assert.Empty(t, cm["carrival"].Validate())
require.EqualValues(t, 20, cm["carrival"].(*ConstantArrivalRateConfig).MaxVUs.Int64) //nolint:forcetypeassert
require.EqualValues(t, 20, cm["carrival"].(*ConstantArrivalRateConfig).MaxVUs.Int64)
}},
},
{`{"carrival": {"executor": "constant-arrival-rate", "rate": 10, "duration": "10m", "maxVUs": 30}}`, exp{validationError: true}},
Expand Down Expand Up @@ -397,7 +397,7 @@ var configMapTestCases = []configMapTestCase{
`{"varrival": {"executor": "ramping-arrival-rate", "preAllocatedVUs": 20, "stages": [{"duration": "5m", "target": 10}]}}`,
exp{custom: func(t *testing.T, cm lib.ScenarioConfigs) {
assert.Empty(t, cm["varrival"].Validate())
require.EqualValues(t, 20, cm["varrival"].(*RampingArrivalRateConfig).MaxVUs.Int64) //nolint:forcetypeassert
require.EqualValues(t, 20, cm["varrival"].(*RampingArrivalRateConfig).MaxVUs.Int64)
}},
},
{`{"varrival": {"executor": "ramping-arrival-rate", "maxVUs": 50, "stages": [{"duration": "5m", "target": 10}]}}`, exp{validationError: true}},
Expand Down
2 changes: 1 addition & 1 deletion lib/executor/externally_controlled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestExternallyControlledRun(t *testing.T) {
MaxVUs: null.IntFrom(maxVUs),
Duration: types.NullDurationFrom(2 * time.Second),
}
err := test.executor.(*ExternallyControlled).UpdateConfig(test.ctx, newConfig) //nolint:forcetypeassert
err := test.executor.(*ExternallyControlled).UpdateConfig(test.ctx, newConfig)
if errMsg != "" {
assert.EqualError(t, err, errMsg)
} else {
Expand Down
8 changes: 4 additions & 4 deletions lib/executor/per_vu_iterations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestPerVUIterationsRun(t *testing.T) {

runner := simpleRunner(func(_ context.Context, state *lib.State) error {
currIter, _ := result.LoadOrStore(state.VUID, uint64(0))
result.Store(state.VUID, currIter.(uint64)+1) //nolint:forcetypeassert
result.Store(state.VUID, currIter.(uint64)+1)
return nil
})

Expand All @@ -44,7 +44,7 @@ func TestPerVUIterationsRun(t *testing.T) {

var totalIters uint64
result.Range(func(_, value interface{}) bool {
vuIters := value.(uint64) //nolint:forcetypeassert
vuIters := value.(uint64)
assert.Equal(t, uint64(100), vuIters)
totalIters += vuIters
return true
Expand All @@ -66,7 +66,7 @@ func TestPerVUIterationsRunVariableVU(t *testing.T) {
time.Sleep(200 * time.Millisecond)
}
currIter, _ := result.LoadOrStore(state.VUID, uint64(0))
result.Store(state.VUID, currIter.(uint64)+1) //nolint:forcetypeassert
result.Store(state.VUID, currIter.(uint64)+1)
return nil
})

Expand All @@ -81,7 +81,7 @@ func TestPerVUIterationsRunVariableVU(t *testing.T) {

var totalIters uint64
result.Range(func(key, value interface{}) bool {
vuIters := value.(uint64) //nolint:forcetypeassert
vuIters := value.(uint64)
if key != slowVUID {
assert.Equal(t, uint64(100), vuIters)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/executor/shared_iterations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestSharedIterationsRunVariableVU(t *testing.T) {
time.Sleep(200 * time.Millisecond)
}
currIter, _ := result.LoadOrStore(state.VUID, uint64(0))
result.Store(state.VUID, currIter.(uint64)+1) //nolint:forcetypeassert
result.Store(state.VUID, currIter.(uint64)+1)
return nil
})

Expand All @@ -74,7 +74,7 @@ func TestSharedIterationsRunVariableVU(t *testing.T) {

var totalIters uint64
result.Range(func(_, value interface{}) bool {
totalIters += value.(uint64) //nolint:forcetypeassert
totalIters += value.(uint64)
return true
})

Expand Down
2 changes: 1 addition & 1 deletion lib/executor/vu_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestVUHandleStartStopRace(t *testing.T) {
}

returnVU := func(v lib.InitializedVU) {
require.Equal(t, atomic.LoadUint64(&vuID), v.(*minirunner.VU).ID) //nolint:forcetypeassert
require.Equal(t, atomic.LoadUint64(&vuID), v.(*minirunner.VU).ID)
close(returned)
}
var interruptedIter int64
Expand Down
4 changes: 2 additions & 2 deletions lib/netext/httpext/error_codes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestHTTP2ConnectionError(t *testing.T) {

// Pre-configure the HTTP client transport with the dialer and TLS config (incl. HTTP2 support)
tb.Mux.HandleFunc("/tsr", func(_ http.ResponseWriter, req *http.Request) {
conn := req.Context().Value(connKey).(*tls.Conn) //nolint:forcetypeassert
conn := req.Context().Value(connKey).(*tls.Conn)
f := http2.NewFramer(conn, conn)
require.NoError(t, f.WriteData(3213, false, []byte("something")))
})
Expand All @@ -317,7 +317,7 @@ func TestHTTP2GoAwayError(t *testing.T) {

tb := getHTTP2ServerWithCustomConnContext(t)
tb.Mux.HandleFunc("/tsr", func(_ http.ResponseWriter, req *http.Request) {
conn := req.Context().Value(connKey).(*tls.Conn) //nolint:forcetypeassert
conn := req.Context().Value(connKey).(*tls.Conn)
f := http2.NewFramer(conn, conn)
require.NoError(t, f.WriteGoAway(4, http2.ErrCodeInadequateSecurity, []byte("whatever")))
require.NoError(t, conn.CloseWrite())
Expand Down
2 changes: 1 addition & 1 deletion log/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestFileHookFromConfigLine(t *testing.T) {
}

require.NoError(t, err)
assert.NotNil(t, res.(*fileHook).w) //nolint:forcetypeassert
assert.NotNil(t, res.(*fileHook).w)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion metrics/engine/ingester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestIngesterOutputFlushMetrics(t *testing.T) {
require.NotNil(t, metric.Sink)
assert.Equal(t, testMetric, metric)

sink := metric.Sink.(*metrics.TrendSink) //nolint:forcetypeassert
sink := metric.Sink.(*metrics.TrendSink)
assert.Equal(t, 42.0, sink.Total())
}

Expand Down

0 comments on commit b866c10

Please sign in to comment.