Skip to content

Commit 0253c33

Browse files
committed
Generalize the paths used in e2e tests
Replace path.Join with filepath.Join to make the core of the tests independent of the platform they are being run on. Signed-off-by: Remy Suen <[email protected]>
1 parent 3b93536 commit 0253c33

File tree

4 files changed

+48
-63
lines changed

4 files changed

+48
-63
lines changed

e2e-tests/documentLink_test.go

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"context"
66
"fmt"
77
"os"
8-
"path"
98
"path/filepath"
9+
"strings"
1010
"testing"
1111

1212
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
@@ -31,84 +31,58 @@ func TestDocumentLink(t *testing.T) {
3131

3232
homedir, err := os.UserHomeDir()
3333
require.NoError(t, err)
34-
35-
testFolder := path.Join(homedir, t.Name())
36-
parentFolder, err := filepath.Abs(path.Join(testFolder, ".."))
37-
require.NoError(t, err)
34+
testFolder := filepath.Join(homedir, t.Name())
3835

3936
testCases := []struct {
40-
name string
41-
content string
42-
links []protocol.DocumentLink
37+
name string
38+
content string
39+
linkRange protocol.Range
40+
path string
41+
links []protocol.DocumentLink
4342
}{
4443
{
4544
name: "dockerfile attribute in targets block",
4645
content: "target \"api\" {\n dockerfile = \"Dockerfile.api\"\n}",
47-
links: []protocol.DocumentLink{
48-
{
49-
Range: protocol.Range{
50-
Start: protocol.Position{Line: 1, Character: 16},
51-
End: protocol.Position{Line: 1, Character: 30},
52-
},
53-
Target: types.CreateStringPointer(fmt.Sprintf("file://%v", path.Join(homedir, "TestDocumentLink", "Dockerfile.api"))),
54-
Tooltip: types.CreateStringPointer(path.Join(homedir, "TestDocumentLink", "Dockerfile.api")),
55-
},
46+
path: filepath.Join(homedir, "TestDocumentLink", "Dockerfile.api"),
47+
linkRange: protocol.Range{
48+
Start: protocol.Position{Line: 1, Character: 16},
49+
End: protocol.Position{Line: 1, Character: 30},
5650
},
5751
},
5852
{
5953
name: "./dockerfile attribute in targets block",
6054
content: "target \"api\" {\n dockerfile = \"./Dockerfile.api\"\n}",
61-
links: []protocol.DocumentLink{
62-
{
63-
Range: protocol.Range{
64-
Start: protocol.Position{Line: 1, Character: 16},
65-
End: protocol.Position{Line: 1, Character: 32},
66-
},
67-
Target: types.CreateStringPointer(fmt.Sprintf("file://%v", path.Join(homedir, "TestDocumentLink", "Dockerfile.api"))),
68-
Tooltip: types.CreateStringPointer(path.Join(homedir, "TestDocumentLink", "Dockerfile.api")),
69-
},
55+
path: filepath.Join(homedir, "TestDocumentLink", "Dockerfile.api"),
56+
linkRange: protocol.Range{
57+
Start: protocol.Position{Line: 1, Character: 16},
58+
End: protocol.Position{Line: 1, Character: 32},
7059
},
7160
},
7261
{
7362
name: "../dockerfile attribute in targets block",
7463
content: "target \"api\" {\n dockerfile = \"../Dockerfile.api\"\n}",
75-
links: []protocol.DocumentLink{
76-
{
77-
Range: protocol.Range{
78-
Start: protocol.Position{Line: 1, Character: 16},
79-
End: protocol.Position{Line: 1, Character: 33},
80-
},
81-
Target: types.CreateStringPointer(fmt.Sprintf("file://%v", path.Join(parentFolder, "Dockerfile.api"))),
82-
Tooltip: types.CreateStringPointer(path.Join(parentFolder, "Dockerfile.api")),
83-
},
64+
path: filepath.Join(homedir, "Dockerfile.api"),
65+
linkRange: protocol.Range{
66+
Start: protocol.Position{Line: 1, Character: 16},
67+
End: protocol.Position{Line: 1, Character: 33},
8468
},
8569
},
8670
{
8771
name: "folder/dockerfile attribute in targets block",
8872
content: "target \"api\" {\n dockerfile = \"folder/Dockerfile.api\"\n}",
89-
links: []protocol.DocumentLink{
90-
{
91-
Range: protocol.Range{
92-
Start: protocol.Position{Line: 1, Character: 16},
93-
End: protocol.Position{Line: 1, Character: 37},
94-
},
95-
Target: types.CreateStringPointer(fmt.Sprintf("file://%v", path.Join(homedir, "TestDocumentLink", "folder", "Dockerfile.api"))),
96-
Tooltip: types.CreateStringPointer(path.Join(homedir, "TestDocumentLink", "folder", "Dockerfile.api")),
97-
},
73+
path: filepath.Join(homedir, "TestDocumentLink", "folder", "Dockerfile.api"),
74+
linkRange: protocol.Range{
75+
Start: protocol.Position{Line: 1, Character: 16},
76+
End: protocol.Position{Line: 1, Character: 37},
9877
},
9978
},
10079
{
10180
name: "../folder/dockerfile attribute in targets block",
10281
content: "target \"api\" {\n dockerfile = \"../folder/Dockerfile.api\"\n}",
103-
links: []protocol.DocumentLink{
104-
{
105-
Range: protocol.Range{
106-
Start: protocol.Position{Line: 1, Character: 16},
107-
End: protocol.Position{Line: 1, Character: 40},
108-
},
109-
Target: types.CreateStringPointer(fmt.Sprintf("file://%v", path.Join(parentFolder, "folder/Dockerfile.api"))),
110-
Tooltip: types.CreateStringPointer(path.Join(parentFolder, "folder/Dockerfile.api")),
111-
},
82+
path: filepath.Join(homedir, "folder/Dockerfile.api"),
83+
linkRange: protocol.Range{
84+
Start: protocol.Position{Line: 1, Character: 16},
85+
End: protocol.Position{Line: 1, Character: 40},
11286
},
11387
},
11488
}
@@ -128,7 +102,14 @@ func TestDocumentLink(t *testing.T) {
128102
TextDocument: protocol.TextDocumentIdentifier{URI: didOpen.TextDocument.URI},
129103
}, &result)
130104
require.NoError(t, err)
131-
require.Equal(t, tc.links, result)
105+
links := []protocol.DocumentLink{
106+
{
107+
Range: tc.linkRange,
108+
Target: types.CreateStringPointer(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(tc.path), "/"))),
109+
Tooltip: types.CreateStringPointer(tc.path),
110+
},
111+
}
112+
require.Equal(t, links, result)
132113
})
133114
}
134115
}

e2e-tests/initialize_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package server_test
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"os"
7-
"path"
8+
"path/filepath"
9+
"strings"
810
"testing"
911
"time"
1012

@@ -23,7 +25,7 @@ func init() {
2325
func createDidOpenTextDocumentParams(homedir, testName, text string, languageID protocol.LanguageIdentifier) protocol.DidOpenTextDocumentParams {
2426
return protocol.DidOpenTextDocumentParams{
2527
TextDocument: protocol.TextDocumentItem{
26-
URI: protocol.URI(path.Join("file://", homedir, testName)),
28+
URI: protocol.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(homedir, testName)), "/"))),
2729
Text: text,
2830
LanguageID: languageID,
2931
Version: 1,
@@ -35,7 +37,7 @@ func createDidChangeTextDocumentParams(homedir, testName, text string) protocol.
3537
return protocol.DidChangeTextDocumentParams{
3638
TextDocument: protocol.VersionedTextDocumentIdentifier{
3739
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
38-
URI: protocol.URI(path.Join("file://", homedir, testName)),
40+
URI: protocol.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(homedir, testName)), "/"))),
3941
},
4042
Version: 1,
4143
},

e2e-tests/inlayHint_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"context"
66
"fmt"
77
"os"
8-
"path"
8+
"path/filepath"
9+
"strings"
910
"testing"
1011

1112
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
@@ -53,8 +54,8 @@ func TestInlayHint(t *testing.T) {
5354
},
5455
}
5556

56-
temporaryDockerfile := fmt.Sprintf("file://%v", path.Join(os.TempDir(), "Dockerfile"))
57-
temporaryBakeFile := fmt.Sprintf("file://%v", path.Join(os.TempDir(), "docker-bake.hcl"))
57+
temporaryDockerfile := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "Dockerfile")), "/"))
58+
temporaryBakeFile := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "docker-bake.hcl")), "/"))
5859

5960
for _, tc := range testCases {
6061
t.Run(tc.name, func(t *testing.T) {

e2e-tests/inlineCompletion_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"context"
66
"fmt"
77
"os"
8-
"path"
8+
"path/filepath"
9+
"strings"
910
"testing"
1011

1112
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
@@ -49,8 +50,8 @@ func TestInlineCompletion(t *testing.T) {
4950
},
5051
}
5152

52-
temporaryDockerfile := fmt.Sprintf("file://%v", path.Join(os.TempDir(), "Dockerfile"))
53-
temporaryBakeFile := fmt.Sprintf("file://%v", path.Join(os.TempDir(), "docker-bake.hcl"))
53+
temporaryDockerfile := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "Dockerfile")), "/"))
54+
temporaryBakeFile := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "docker-bake.hcl")), "/"))
5455

5556
for _, tc := range testCases {
5657
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)