Skip to content

Commit 37be081

Browse files
create new test to check relative paths
1 parent ee92327 commit 37be081

File tree

3 files changed

+77
-47
lines changed

3 files changed

+77
-47
lines changed

cmd/upload.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ var sarifShortNameMap = map[string]string{
6060
"revive": "revive",
6161
}
6262

63-
// Helper to look up the short name
6463
func getToolShortName(fullName string) string {
6564
if shortName, ok := sarifShortNameMap[fullName]; ok {
6665
return shortName

cmd/upload_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cmd
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetRelativePath(t *testing.T) {
11+
const baseDir = "/home/user/project/src"
12+
13+
tests := []struct {
14+
name string
15+
baseDir string
16+
fullURI string
17+
expected string
18+
}{
19+
{
20+
name: "1. File URI with standard path",
21+
baseDir: baseDir,
22+
fullURI: "file:///home/user/project/src/lib/file.go",
23+
expected: "lib/file.go",
24+
},
25+
{
26+
name: "2. File URI with baseDir as the file path",
27+
baseDir: baseDir,
28+
fullURI: "file:///home/user/project/src",
29+
expected: ".",
30+
},
31+
{
32+
name: "3. Simple path (no scheme)",
33+
baseDir: baseDir,
34+
fullURI: "/home/user/project/src/main.go",
35+
expected: "main.go",
36+
},
37+
{
38+
name: "4. URI outside baseDir (should return absolute path if relative fails)",
39+
baseDir: baseDir,
40+
fullURI: "file:///etc/config/app.json",
41+
// This is outside of baseDir, so we expect the absolute path starting from the baseDir root
42+
expected: "../../../../etc/config/app.json",
43+
},
44+
{
45+
name: "5. Plain URI with different scheme (should be treated as plain path)",
46+
baseDir: baseDir,
47+
fullURI: "http://example.com/api/v1/file.go",
48+
expected: "http://example.com/api/v1/file.go",
49+
},
50+
{
51+
name: "6. Empty URI",
52+
baseDir: baseDir,
53+
fullURI: "",
54+
expected: "",
55+
},
56+
{
57+
name: "7. Windows path on a file URI (should correctly strip the leading slash from the path component)",
58+
baseDir: "C:\\Users\\dev\\repo",
59+
fullURI: "file:///C:/Users/dev/repo/app/main.go",
60+
expected: "/C:/Users/dev/repo/app/main.go",
61+
},
62+
{
63+
name: "8. URI with spaces (URL encoded)",
64+
baseDir: baseDir,
65+
fullURI: "file:///home/user/project/src/file%20with%20spaces.go",
66+
expected: "file with spaces.go",
67+
},
68+
}
69+
70+
for _, tt := range tests {
71+
t.Run(tt.name, func(t *testing.T) {
72+
actual := getRelativePath(tt.baseDir, tt.fullURI)
73+
expectedNormalized := filepath.FromSlash(tt.expected)
74+
assert.Equal(t, expectedNormalized, actual, "Relative path should match expected")
75+
})
76+
}
77+
}

codacy-client/client_test.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package codacyclient
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"net/http"
76
"net/http/httptest"
87
"testing"
@@ -108,48 +107,3 @@ func TestGetToolPatternsConfig_Empty(t *testing.T) {
108107
assert.NoError(t, err)
109108
assert.Empty(t, patterns)
110109
}
111-
112-
func TestGetToolPatternsConfig_WithNonRecommended(t *testing.T) {
113-
114-
config := []domain.PatternDefinition{
115-
{
116-
Id: "internal_id_1",
117-
Enabled: true,
118-
},
119-
{
120-
Id: "internal_id_2",
121-
Enabled: false,
122-
},
123-
}
124-
125-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126-
127-
resp := map[string]interface{}{
128-
"data": config,
129-
"pagination": map[string]interface{}{"cursor": ""},
130-
}
131-
w.Header().Set("Content-Type", "application/json")
132-
json.NewEncoder(w).Encode(resp)
133-
}))
134-
defer ts.Close()
135-
136-
expected := []domain.PatternConfiguration{
137-
{
138-
Enabled: true,
139-
PatternDefinition: domain.PatternDefinition{
140-
Id: "internal_id_1",
141-
Enabled: true,
142-
},
143-
},
144-
}
145-
146-
CodacyApiBase = ts.URL
147-
148-
initFlags := domain.InitFlags{ApiToken: "dummy"}
149-
patterns, err := GetToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, "tool-uuid", true)
150-
151-
fmt.Println(len(patterns))
152-
153-
assert.NoError(t, err)
154-
assert.Equal(t, expected, patterns)
155-
}

0 commit comments

Comments
 (0)