Skip to content

Commit

Permalink
run test concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Jul 31, 2024
1 parent 5e3275d commit 073fbf7
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 149 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ build-mac:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_MAC) -v $(MAIN_PACKAGE)

test:
$(GOTEST) -v ./...
$(GOTEST) -race -v ./...

clean:
$(GOCLEAN)
Expand Down
9 changes: 7 additions & 2 deletions cmd/tlin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ func (m *MockLintEngine) IgnoreRule(rule string) {
}

func TestParseFlags(t *testing.T) {
// Save original os.Args
t.Parallel()
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

// Test case
os.Args = []string{"cmd", "-timeout", "10s", "-cyclo", "-threshold", "15", "-ignore", "rule1,rule2", "file1.go", "file2.go"}

config := parseFlags()
Expand All @@ -48,6 +47,7 @@ func TestParseFlags(t *testing.T) {
}

func TestProcessFile(t *testing.T) {
t.Parallel()
mockEngine := new(MockLintEngine)
expectedIssues := []types.Issue{
{
Expand All @@ -68,6 +68,7 @@ func TestProcessFile(t *testing.T) {
}

func TestProcessPath(t *testing.T) {
t.Parallel()
logger, _ := zap.NewProduction()
mockEngine := new(MockLintEngine)
ctx := context.Background()
Expand Down Expand Up @@ -115,6 +116,7 @@ func TestProcessPath(t *testing.T) {
}

func TestProcessFiles(t *testing.T) {
t.Parallel()
logger, _ := zap.NewProduction()
mockEngine := new(MockLintEngine)
ctx := context.Background()
Expand Down Expand Up @@ -161,13 +163,15 @@ func TestProcessFiles(t *testing.T) {
}

func TestHasDesiredExtension(t *testing.T) {
t.Parallel()
assert.True(t, hasDesiredExtension("test.go"))
assert.True(t, hasDesiredExtension("test.gno"))
assert.False(t, hasDesiredExtension("test.txt"))
assert.False(t, hasDesiredExtension("test"))
}

func TestRunWithTimeout(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

Expand All @@ -188,6 +192,7 @@ func TestRunWithTimeout(t *testing.T) {
}

func TestRunCFGAnalysis(t *testing.T) {
t.Parallel()
logger, _ := zap.NewProduction()

tempFile, err := os.CreateTemp("", "test*.go")
Expand Down
128 changes: 20 additions & 108 deletions formatter/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package formatter

import (
"go/token"
"os"
"path/filepath"
"strings"
"testing"

"github.com/gnoswap-labs/lint/internal"
tt "github.com/gnoswap-labs/lint/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFormatIssuesWithArrows(t *testing.T) {
sourceCode := &internal.SourceCode{
t.Parallel()
code := &internal.SourceCode{
Lines: []string{
"package main",
"",
Expand Down Expand Up @@ -56,7 +53,7 @@ error: empty-if
`

result := FormatIssuesWithArrows(issues, sourceCode)
result := FormatIssuesWithArrows(issues, code)

assert.Equal(t, expected, result, "Formatted output does not match expected")

Expand Down Expand Up @@ -92,7 +89,8 @@ error: empty-if
}

func TestFormatIssuesWithArrows_MultipleDigitsLineNumbers(t *testing.T) {
sourceCode := &internal.SourceCode{
t.Parallel()
code := &internal.SourceCode{
Lines: []string{
"package main",
"",
Expand Down Expand Up @@ -151,13 +149,14 @@ error: example
`

result := FormatIssuesWithArrows(issues, sourceCode)
result := FormatIssuesWithArrows(issues, code)

assert.Equal(t, expected, result, "Formatted output with multiple digit line numbers does not match expected")
}

func TestFormatIssuesWithArrows_UnnecessaryElse(t *testing.T) {
sourceCode := &internal.SourceCode{
t.Parallel()
code := &internal.SourceCode{
Lines: []string{
"package main",
"",
Expand Down Expand Up @@ -203,102 +202,12 @@ The code inside the 'else' block has been moved outside, as it will only be exec
`

result := FormatIssuesWithArrows(issues, sourceCode)
result := FormatIssuesWithArrows(issues, code)
assert.Equal(t, expected, result, "Formatted output does not match expected for unnecessary else")
}

func TestIntegratedLintEngine(t *testing.T) {
t.Skip("skipping integrated lint engine test")
tests := []struct {
name string
code string
expected []string
}{
{
name: "Detect unused issues",
code: `
package main
import (
"fmt"
)
func main() {
x := 1
fmt.Println("Hello")
}
`,
expected: []string{
"x declared and not used",
},
},
{
name: "Detect multiple issues",
code: `
package main
import (
"fmt"
"strings"
)
func main() {
x := 1
y := "unused"
fmt.Println("Hello")
}
`,
expected: []string{
"x declared and not used",
"y declared and not used",
`"strings" imported and not used`,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "lint-test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

tmpfile := filepath.Join(tmpDir, "test.go")
err = os.WriteFile(tmpfile, []byte(tt.code), 0o644)
require.NoError(t, err)

rootDir := "."
engine, err := internal.NewEngine(rootDir)
if err != nil {
t.Fatalf("unexpected error initializing lint engine: %v", err)
}

issues, err := engine.Run(tmpfile)
require.NoError(t, err)

assert.Equal(t, len(tt.expected), len(issues), "Number of issues doesn't match")

for _, exp := range tt.expected {
found := false
for _, issue := range issues {
if strings.Contains(issue.Message, exp) {
found = true
break
}
}
assert.True(t, found, "Expected issue not found: "+exp)
}

if len(issues) > 0 {
sourceCode, err := internal.ReadSourceCode(tmpfile)
require.NoError(t, err)
formattedIssues := FormatIssuesWithArrows(issues, sourceCode)
t.Logf("Found issues with arrows:\n%s", formattedIssues)
}
})
}
}

func TestUnnecessaryTypeConversionFormatter(t *testing.T) {
t.Parallel()
formatter := &UnnecessaryTypeConversionFormatter{}

issue := tt.Issue{
Expand Down Expand Up @@ -339,9 +248,10 @@ Note: Unnecessary type conversions can make the code less readable and may sligh
}

func TestEmitFormatFormatter_Format(t *testing.T) {
t.Parallel()
formatter := &EmitFormatFormatter{}

testCases := []struct {
tests := []struct {
name string
issue tt.Issue
snippet *internal.SourceCode
Expand Down Expand Up @@ -371,8 +281,7 @@ func TestEmitFormatFormatter_Format(t *testing.T) {
"}",
},
},
expected: `error: emit-format
--> test.go
expected: ` |
3 | func main() {
4 | std.Emit("OwnershipChange", "newOwner", newOwner.String(), "oldOwner", oldOwner.String())
5 | }
Expand All @@ -390,10 +299,13 @@ Suggestion:
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := formatter.Format(tc.issue, tc.snippet)
assert.Equal(t, tc.expected, result)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Format does not render lint rule and filename
result := formatter.Format(tt.issue, tt.snippet)
assert.Equal(t, tt.expected, result)
})
}
}
2 changes: 2 additions & 0 deletions formatter/format_emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func (f *EmitFormatFormatter) Format(
maxLineNumWidth := calculateMaxLineNumWidth(issue.End.Line)
padding := strings.Repeat(" ", maxLineNumWidth+1)

result.WriteString(lineStyle.Sprintf("%s|\n", padding))

startLine := issue.Start.Line
endLine := issue.End.Line
for i := startLine; i <= endLine; i++ {
Expand Down
Loading

0 comments on commit 073fbf7

Please sign in to comment.