diff --git a/internal/bake/hcl/documentLink_test.go b/internal/bake/hcl/documentLink_test.go index e15953d..c42cec3 100644 --- a/internal/bake/hcl/documentLink_test.go +++ b/internal/bake/hcl/documentLink_test.go @@ -2,6 +2,10 @@ package hcl import ( "context" + "fmt" + "os" + "path/filepath" + "strings" "testing" "github.com/docker/docker-language-server/internal/pkg/document" @@ -11,94 +15,86 @@ import ( ) func TestDocumentLink(t *testing.T) { + testsFolder := filepath.Join(os.TempDir(), "documentLinkTests") + userFolder := filepath.Join(testsFolder, "user") + bakeFilePath := filepath.Join(userFolder, "docker-bake.hcl") + bakeFileStringURI := fmt.Sprintf("file:///%v", strings.TrimPrefix(bakeFilePath, "/")) + testCases := []struct { - name string - content string - links []protocol.DocumentLink + name string + content string + path string + linkRange protocol.Range + links func(path string) []protocol.DocumentLink }{ { name: "dockerfile attribute in targets block", content: "target \"api\" {\n dockerfile = \"Dockerfile.api\"\n}", - links: []protocol.DocumentLink{ - { - Range: protocol.Range{ - Start: protocol.Position{Line: 1, Character: 16}, - End: protocol.Position{Line: 1, Character: 30}, - }, - Target: types.CreateStringPointer("file:///home/user/Dockerfile.api"), - Tooltip: types.CreateStringPointer("/home/user/Dockerfile.api"), - }, + path: filepath.Join(userFolder, "Dockerfile.api"), + linkRange: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 16}, + End: protocol.Position{Line: 1, Character: 30}, }, }, { name: "./dockerfile attribute in targets block", content: "target \"api\" {\n dockerfile = \"./Dockerfile.api\"\n}", - links: []protocol.DocumentLink{ - { - Range: protocol.Range{ - Start: protocol.Position{Line: 1, Character: 16}, - End: protocol.Position{Line: 1, Character: 32}, - }, - Target: types.CreateStringPointer("file:///home/user/Dockerfile.api"), - Tooltip: types.CreateStringPointer("/home/user/Dockerfile.api"), - }, + path: filepath.Join(userFolder, "Dockerfile.api"), + linkRange: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 16}, + End: protocol.Position{Line: 1, Character: 32}, }, }, { name: "../dockerfile attribute in targets block", content: "target \"api\" {\n dockerfile = \"../Dockerfile.api\"\n}", - links: []protocol.DocumentLink{ - { - Range: protocol.Range{ - Start: protocol.Position{Line: 1, Character: 16}, - End: protocol.Position{Line: 1, Character: 33}, - }, - Target: types.CreateStringPointer("file:///home/Dockerfile.api"), - Tooltip: types.CreateStringPointer("/home/Dockerfile.api"), - }, + path: filepath.Join(testsFolder, "Dockerfile.api"), + linkRange: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 16}, + End: protocol.Position{Line: 1, Character: 33}, }, }, { name: "folder/dockerfile attribute in targets block", content: "target \"api\" {\n dockerfile = \"folder/Dockerfile.api\"\n}", - links: []protocol.DocumentLink{ - { - Range: protocol.Range{ - Start: protocol.Position{Line: 1, Character: 16}, - End: protocol.Position{Line: 1, Character: 37}, - }, - Target: types.CreateStringPointer("file:///home/user/folder/Dockerfile.api"), - Tooltip: types.CreateStringPointer("/home/user/folder/Dockerfile.api"), - }, + path: filepath.Join(filepath.Join(userFolder, "folder"), "Dockerfile.api"), + linkRange: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 16}, + End: protocol.Position{Line: 1, Character: 37}, }, }, { name: "../folder/dockerfile attribute in targets block", content: "target \"api\" {\n dockerfile = \"../folder/Dockerfile.api\"\n}", - links: []protocol.DocumentLink{ - { - Range: protocol.Range{ - Start: protocol.Position{Line: 1, Character: 16}, - End: protocol.Position{Line: 1, Character: 40}, - }, - Target: types.CreateStringPointer("file:///home/folder/Dockerfile.api"), - Tooltip: types.CreateStringPointer("/home/folder/Dockerfile.api"), - }, + path: filepath.Join(filepath.Join(testsFolder, "folder"), "Dockerfile.api"), + linkRange: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 16}, + End: protocol.Position{Line: 1, Character: 40}, }, }, { - name: "dockerfile attribugte points to undefined variable", + name: "dockerfile attribute points to undefined variable", content: "target \"api\" {\n dockerfile = undefined\n}", - links: []protocol.DocumentLink{}, + path: "", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { doc := document.NewBakeHCLDocument("docker-bake.hcl", 1, []byte(tc.content)) - links, err := DocumentLink(context.Background(), "file:///home/user/docker-bake.hcl", doc) + links, err := DocumentLink(context.Background(), bakeFileStringURI, doc) require.NoError(t, err) - require.Equal(t, tc.links, links) + + if tc.path == "" { + require.Equal(t, []protocol.DocumentLink{}, links) + } else { + link := protocol.DocumentLink{ + Range: tc.linkRange, + Target: types.CreateStringPointer(fmt.Sprintf("file:///%v", strings.TrimPrefix(tc.path, "/"))), + Tooltip: types.CreateStringPointer(tc.path), + } + require.Equal(t, []protocol.DocumentLink{link}, links) + } }) } }