Skip to content

Commit ed64ef0

Browse files
committed
add unit tests
1 parent 4e2f5cb commit ed64ef0

File tree

6 files changed

+218
-6
lines changed

6 files changed

+218
-6
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ tools:
77
88
99
10-
10+

cmd/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile s
228228

229229
func runDartAnalyzer(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) {
230230
dartanalyzer := config.Config.Tools()["dartanalyzer"]
231-
tools.RunDartAnalyzer(workDirectory, dartanalyzer, pathsToCheck, outputFile, outputFormat, apiToken, provider, owner, repository)
231+
tools.RunDartAnalyzer(workDirectory, dartanalyzer.InstallDir, dartanalyzer.Binaries["dart"], pathsToCheck, outputFile, outputFormat)
232232
}
233233

234234
var analyzeCmd = &cobra.Command{

tools/dartanalyzerRunner.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tools
33
import (
44
"bufio"
55
"bytes"
6-
"codacy/cli-v2/plugins"
76
"encoding/json"
87
"fmt"
98
"os"
@@ -13,13 +12,12 @@ import (
1312
"strings"
1413
)
1514

16-
const codacyToolName = "dartanalyzer"
1715
const patternPrefix = "dartanalyzer_"
1816

19-
func RunDartAnalyzer(workDirectory string, toolInfo *plugins.ToolInfo, files []string, outputFile string, outputFormat string, apiToken string, provider string, owner string, repository string) {
17+
func RunDartAnalyzer(workDirectory string, installationDirectory string, binary string, files []string, outputFile string, outputFormat string) {
2018

2119
configFiles := []string{"analysis_options.yaml", "analysis_options.yml"}
22-
dartAnalyzerPath := filepath.Join(toolInfo.InstallDir, "bin", "dart")
20+
dartAnalyzerPath := filepath.Join(installationDirectory, "bin", "dart")
2321
fmt.Println(dartAnalyzerPath)
2422

2523
args := []string{"analyze", "--format", "machine"}

tools/dartanalyzerRunner_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
11
package tools
22

33
import (
4+
"log"
5+
"os"
6+
"path/filepath"
7+
"strings"
48
"testing"
9+
10+
"github.com/stretchr/testify/assert"
511
)
612

713
func TestRunDartAnalyzerToFile(t *testing.T) {
14+
homeDirectory, err := os.UserHomeDir()
15+
if err != nil {
16+
log.Fatal(err.Error())
17+
}
18+
currentDirectory, err := os.Getwd()
19+
if err != nil {
20+
log.Fatal(err.Error())
21+
}
22+
testDirectory := "testdata/repositories/dartanalizer"
23+
tempResultFile := filepath.Join(os.TempDir(), "eslint.sarif")
24+
defer os.Remove(tempResultFile)
25+
26+
repositoryToAnalyze := filepath.Join(testDirectory, "src")
27+
expectedSarifFile := filepath.Join(testDirectory, "expected.sarif")
28+
dartInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy/runtimes/dart-sdk")
29+
dartBinary := "dart"
30+
31+
RunDartAnalyzer(repositoryToAnalyze, dartInstallationDirectory, dartBinary, nil, tempResultFile, "sarif")
32+
33+
expectedSarifBytes, err := os.ReadFile(expectedSarifFile)
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
38+
obtainedSarifBytes, err := os.ReadFile(tempResultFile)
39+
if err != nil {
40+
log.Fatal(err.Error())
41+
}
42+
obtainedSarif := string(obtainedSarifBytes)
43+
44+
filePrefix := "file://" + currentDirectory + "/"
45+
actualSarif := strings.ReplaceAll(obtainedSarif, filePrefix, "")
46+
47+
expectedSarif := strings.TrimSpace(string(expectedSarifBytes))
48+
49+
assert.Equal(t, expectedSarif, actualSarif, "output did not match expected")
850
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
3+
"runs": [
4+
{
5+
"results": [
6+
{
7+
"locations": [
8+
{
9+
"physicalLocation": {
10+
"artifactLocation": {
11+
"uri": "/Users/heliorocha/dev/codacy/codacy-cli-v2/tools/testdata/repositories/dartanalizer/src/main.dart"
12+
},
13+
"region": {
14+
"startLine": 2
15+
}
16+
}
17+
}
18+
],
19+
"message": {
20+
"text": "Unused import: 'dart:io'."
21+
},
22+
"ruleId": "UNUSED_IMPORT"
23+
},
24+
{
25+
"locations": [
26+
{
27+
"physicalLocation": {
28+
"artifactLocation": {
29+
"uri": "/Users/heliorocha/dev/codacy/codacy-cli-v2/tools/testdata/repositories/dartanalizer/src/main.dart"
30+
},
31+
"region": {
32+
"startLine": 6
33+
}
34+
}
35+
}
36+
],
37+
"message": {
38+
"text": "The value of the local variable 'x' isn't used."
39+
},
40+
"ruleId": "UNUSED_LOCAL_VARIABLE"
41+
},
42+
{
43+
"locations": [
44+
{
45+
"physicalLocation": {
46+
"artifactLocation": {
47+
"uri": "/Users/heliorocha/dev/codacy/codacy-cli-v2/tools/testdata/repositories/dartanalizer/src/main.dart"
48+
},
49+
"region": {
50+
"startLine": 23
51+
}
52+
}
53+
}
54+
],
55+
"message": {
56+
"text": "The value of the local variable 'unused' isn't used."
57+
},
58+
"ruleId": "UNUSED_LOCAL_VARIABLE"
59+
},
60+
{
61+
"locations": [
62+
{
63+
"physicalLocation": {
64+
"artifactLocation": {
65+
"uri": "/Users/heliorocha/dev/codacy/codacy-cli-v2/tools/testdata/repositories/dartanalizer/src/main.dart"
66+
},
67+
"region": {
68+
"startLine": 26
69+
}
70+
}
71+
}
72+
],
73+
"message": {
74+
"text": "The value of the local variable 'something' isn't used."
75+
},
76+
"ruleId": "UNUSED_LOCAL_VARIABLE"
77+
},
78+
{
79+
"locations": [
80+
{
81+
"physicalLocation": {
82+
"artifactLocation": {
83+
"uri": "/Users/heliorocha/dev/codacy/codacy-cli-v2/tools/testdata/repositories/dartanalizer/src/main.dart"
84+
},
85+
"region": {
86+
"startLine": 31
87+
}
88+
}
89+
}
90+
],
91+
"message": {
92+
"text": "Dead code."
93+
},
94+
"ruleId": "DEAD_CODE"
95+
},
96+
{
97+
"locations": [
98+
{
99+
"physicalLocation": {
100+
"artifactLocation": {
101+
"uri": "/Users/heliorocha/dev/codacy/codacy-cli-v2/tools/testdata/repositories/dartanalizer/src/main.dart"
102+
},
103+
"region": {
104+
"startLine": 35
105+
}
106+
}
107+
}
108+
],
109+
"message": {
110+
"text": "Dead code."
111+
},
112+
"ruleId": "DEAD_CODE"
113+
},
114+
{
115+
"locations": [
116+
{
117+
"physicalLocation": {
118+
"artifactLocation": {
119+
"uri": "/Users/heliorocha/dev/codacy/codacy-cli-v2/tools/testdata/repositories/dartanalizer/src/main.dart"
120+
},
121+
"region": {
122+
"startLine": 35
123+
}
124+
}
125+
}
126+
],
127+
"message": {
128+
"text": "The value of the local variable 'number' isn't used."
129+
},
130+
"ruleId": "UNUSED_LOCAL_VARIABLE"
131+
}
132+
]
133+
}
134+
]
135+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Unused import
2+
import 'dart:io';
3+
4+
// Function with unused parameter and missing return type
5+
function1(String unusedParam) {
6+
var x = 42;
7+
return;
8+
}
9+
10+
// Class with fields that should be final
11+
class BadClass {
12+
String mutableField = "test";
13+
var undefinedType = 123;
14+
15+
// Method with unused parameter
16+
void doSomething(int unused) {
17+
print("doing nothing");
18+
}
19+
}
20+
21+
void main() {
22+
// Unused variable
23+
var unused = 100;
24+
25+
// Variable without type annotation
26+
var something = "hello";
27+
28+
// Dead code after return
29+
if (true) {
30+
return;
31+
print("unreachable");
32+
}
33+
34+
// Using dynamic when a specific type would work
35+
dynamic number = 42;
36+
number = "not a number anymore";
37+
}

0 commit comments

Comments
 (0)