Skip to content

Commit

Permalink
Fix js lint (#3393)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored Oct 13, 2023
1 parent c949fd8 commit 7b5aaaa
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 129 deletions.
2 changes: 1 addition & 1 deletion js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (*goja.Object, e
if err != nil {
var exception *goja.Exception
if errors.As(err, &exception) {
err = &scriptException{inner: exception}
err = &scriptExceptionError{inner: exception}
}
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestNewBundle(t *testing.T) {
t.Run("Error", func(t *testing.T) {
t.Parallel()
_, err := getSimpleBundle(t, "/script.js", `throw new Error("aaaa");`)
exception := new(scriptException)
exception := new(scriptExceptionError)
require.ErrorAs(t, err, &exception)
require.EqualError(t, err, "Error: aaaa\n\tat file:///script.js:2:7(3)\n")
})
Expand Down Expand Up @@ -453,7 +453,7 @@ func TestNewBundle(t *testing.T) {
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\"")
assert.Contains(t, entries[0].Data["error"].(error).Error(), "unknown field \"something\"") //nolint:forcetypeassert
})
})
}
Expand Down Expand Up @@ -579,6 +579,7 @@ func TestNewBundleFromArchive(t *testing.T) {
}

func TestOpen(t *testing.T) {
t.Parallel()
testCases := [...]struct {
name string
openPath string
Expand Down Expand Up @@ -667,7 +668,7 @@ func TestOpen(t *testing.T) {
if isWindows {
fs = fsext.NewTrimFilePathSeparatorFs(fs)
}
return fs, prefix, func() { require.NoError(t, os.RemoveAll(prefix)) }
return fs, prefix, func() { require.NoError(t, os.RemoveAll(prefix)) } //nolint:forbidigo
},
}

Expand All @@ -691,7 +692,7 @@ func TestOpen(t *testing.T) {
openPath = filepath.Join(prefix, openPath)
}
if isWindows {
openPath = strings.Replace(openPath, `\`, `\\`, -1)
openPath = strings.ReplaceAll(openPath, `\`, `\\`)
}
pwd := tCase.pwd
if pwd == "" {
Expand Down Expand Up @@ -727,8 +728,8 @@ func TestOpen(t *testing.T) {
t.Run(tCase.name, testFunc)
if isWindows {
// windowsify the testcase
tCase.openPath = strings.Replace(tCase.openPath, `/`, `\`, -1)
tCase.pwd = strings.Replace(tCase.pwd, `/`, `\`, -1)
tCase.openPath = strings.ReplaceAll(tCase.openPath, `/`, `\`)
tCase.pwd = strings.ReplaceAll(tCase.pwd, `/`, `\`)
t.Run(tCase.name+" with windows slash", testFunc)
}
}
Expand Down Expand Up @@ -856,7 +857,7 @@ func TestBundleNotSharable(t *testing.T) {
bi, err := b.Instantiate(context.Background(), uint64(i))
require.NoError(t, err)
for j := 0; j < iters; j++ {
bi.Runtime.Set("__ITER", j)
require.NoError(t, bi.Runtime.Set("__ITER", j))
_, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
}
Expand Down
3 changes: 2 additions & 1 deletion js/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func newConsole(logger logrus.FieldLogger) *console {

// Creates a console logger with its output set to the file at the provided `filepath`.
func newFileConsole(filepath string, formatter logrus.Formatter, level logrus.Level) (*console, error) {
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644) //nolint:gosec
//nolint:gosec,forbidigo // see https://github.com/grafana/k6/issues/2565
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
return nil, err
}
Expand Down
40 changes: 20 additions & 20 deletions js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ func getSimpleRunner(tb testing.TB, filename, data string, opts ...interface{})
}

// TODO: remove the need for this function, see https://github.com/grafana/k6/issues/2968
func extractLogger(fl logrus.FieldLogger) *logrus.Logger {
//
//nolint:forbidigo
func extractLogger(vu lib.ActiveVU) *logrus.Logger {
vuSpecific, ok := vu.(*ActiveVU)
if !ok {
panic("lib.ActiveVU can't be caset to *ActiveVU")
}
fl := vuSpecific.Console.logger
switch e := fl.(type) {
case *logrus.Entry:
return e.Logger
Expand Down Expand Up @@ -217,7 +224,7 @@ func TestConsoleLog(t *testing.T) {

vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})

logger := extractLogger(vu.(*ActiveVU).Console.logger)
logger := extractLogger(vu)

logger.Out = io.Discard
logger.Level = logrus.DebugLevel
Expand Down Expand Up @@ -275,7 +282,7 @@ func TestConsoleLevels(t *testing.T) {

vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})

logger := extractLogger(vu.(*ActiveVU).Console.logger)
logger := extractLogger(vu)

logger.Out = io.Discard
logger.Level = logrus.DebugLevel
Expand Down Expand Up @@ -334,26 +341,19 @@ func TestFileConsole(t *testing.T) {
msg, deleteFile := msg, deleteFile
t.Run(msg, func(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp("", "") //nolint:forbidigo
if err != nil {
t.Fatalf("Couldn't create temporary file for testing: %s", err)
}
f, err := os.CreateTemp("", "") //nolint:forbidigo // fix with https://github.com/grafana/k6/issues/2565
require.NoError(t, err)
logFilename := f.Name()
defer os.Remove(logFilename)
defer os.Remove(logFilename) //nolint:errcheck,forbidigo // fix with https://github.com/grafana/k6/issues/2565
// close it as we will want to reopen it and maybe remove it
if deleteFile {
f.Close()
if err := os.Remove(logFilename); err != nil {
t.Fatalf("Couldn't remove tempfile: %s", err)
}
require.NoError(t, f.Close())
require.NoError(t, os.Remove(logFilename)) //nolint:forbidigo // fix with https://github.com/grafana/k6/issues/2565
} else {
// TODO: handle case where the string was no written in full ?
_, err = f.WriteString(preExistingText)
_ = f.Close()
if err != nil {
t.Fatalf("Error while writing text to preexisting logfile: %s", err)
}

assert.NoError(t, f.Close())
require.NoError(t, err)
}
r, err := getSimpleRunner(t, "/script",
fmt.Sprintf(
Expand All @@ -375,7 +375,7 @@ func TestFileConsole(t *testing.T) {
require.NoError(t, err)

vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
logger := extractLogger(vu.(*ActiveVU).Console.logger)
logger := extractLogger(vu)

logger.Level = logrus.DebugLevel
hook := logtest.NewLocal(logger)
Expand All @@ -384,7 +384,7 @@ func TestFileConsole(t *testing.T) {
require.NoError(t, err)

// Test if the file was created.
_, err = os.Stat(logFilename)
_, err = os.Stat(logFilename) //nolint:forbidigo // fix with https://github.com/grafana/k6/issues/2565
require.NoError(t, err)

entry := hook.LastEntry()
Expand All @@ -403,7 +403,7 @@ func TestFileConsole(t *testing.T) {
entryStr, err := entry.String()
require.NoError(t, err)

f, err = os.Open(logFilename) //nolint:gosec
f, err = os.Open(logFilename) //nolint:forbidigo,gosec // fix with https://github.com/grafana/k6/issues/2565
require.NoError(t, err)

fileContent, err := io.ReadAll(f)
Expand Down
8 changes: 2 additions & 6 deletions js/empty_iterations_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/require"

"go.k6.io/k6/lib"
"go.k6.io/k6/metrics"
)

func BenchmarkEmptyIteration(b *testing.B) {
Expand All @@ -16,12 +15,9 @@ func BenchmarkEmptyIteration(b *testing.B) {
r, err := getSimpleRunner(b, "/script.js", `exports.default = function() { }`)
require.NoError(b, err)

ch := make(chan metrics.SampleContainer, 100)
ch := newDevNullSampleChannel()
defer close(ch)
go func() { // read the channel so it doesn't block
for range ch {
}
}()

initVU, err := r.NewVU(context.Background(), 1, 1, ch)
require.NoError(b, err)
ctx, cancel := context.WithCancel(context.Background())
Expand Down
12 changes: 2 additions & 10 deletions js/http_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ func BenchmarkHTTPRequests(b *testing.B) {
})
require.NoError(b, err)

ch := make(chan metrics.SampleContainer, 100)
ch := newDevNullSampleChannel()
defer close(ch)
go func() { // read the channel so it doesn't block
for range ch {
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
initVU, err := r.NewVU(ctx, 1, 1, ch)
Expand Down Expand Up @@ -77,12 +73,8 @@ func BenchmarkHTTPRequestsBase(b *testing.B) {
})
require.NoError(b, err)

ch := make(chan metrics.SampleContainer, 100)
ch := newDevNullSampleChannel()
defer close(ch)
go func() { // read the channel so it doesn't block
for range ch {
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
initVU, err := r.NewVU(ctx, 1, 1, ch)
Expand Down
2 changes: 1 addition & 1 deletion js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestInitContextOpen(t *testing.T) {
{[]byte("hello world!"), "ascii", 12},
{[]byte("?((¯°·._.• ţ€$ţɨɲǥ µɲɨȼ๏ď€ΣSЫ ɨɲ Ќ6 •._.·°¯))؟•"), "utf", 47},
{[]byte{0o44, 226, 130, 172}, "utf-8", 2}, // $€
//{[]byte{00, 36, 32, 127}, "utf-16", 2}, // $€
// {[]byte{00, 36, 32, 127}, "utf-16", 2}, // $€
}
for _, tc := range testCases {
tc := tc
Expand Down
2 changes: 1 addition & 1 deletion js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
func newDevNullSampleChannel() chan metrics.SampleContainer {
ch := make(chan metrics.SampleContainer, 100)
go func() {
for range ch {
for range ch { //nolint:revive
}
}()
return ch
Expand Down
Loading

0 comments on commit 7b5aaaa

Please sign in to comment.