From 7290837fedf898dd9f4ebeaa651ef26ca601f6c2 Mon Sep 17 00:00:00 2001 From: Heng Lu <79895375+ms-henglu@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:18:02 +0800 Subject: [PATCH] fix windows path in aztfmigrate (#113) --- .../handlers/command/aztfmigrate_command.go | 19 ++++--- .../command/aztfmigrate_command_test.go | 49 +++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 internal/langserver/handlers/command/aztfmigrate_command_test.go diff --git a/internal/langserver/handlers/command/aztfmigrate_command.go b/internal/langserver/handlers/command/aztfmigrate_command.go index 91059b4e..4df59d8a 100644 --- a/internal/langserver/handlers/command/aztfmigrate_command.go +++ b/internal/langserver/handlers/command/aztfmigrate_command.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "log" + "net/url" "os" "path" "path/filepath" @@ -66,13 +67,7 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess } // creating temp workspace - workingDirectory := path.Dir(strings.TrimPrefix(string(params.TextDocument.URI), "file://")) - if runtime.GOOS == "windows" { - workingDirectory = strings.ReplaceAll(workingDirectory, "%3A", ":") - workingDirectory = strings.ReplaceAll(workingDirectory, "/", "\\") - workingDirectory = strings.TrimPrefix(workingDirectory, "\\") - } - log.Printf("[INFO] working directory: %s", workingDirectory) + workingDirectory := getWorkingDirectory(string(params.TextDocument.URI), runtime.GOOS) tempDir := filepath.Join(workingDirectory, tempFolderName) if err := os.MkdirAll(tempDir, 0750); err != nil { return nil, fmt.Errorf("creating temp workspace %q: %+v", tempDir, err) @@ -416,3 +411,13 @@ func reportProgress(ctx context.Context, message string, percentage uint32) { }) } } + +func getWorkingDirectory(uri string, os string) string { + workingDirectory := path.Dir(strings.TrimPrefix(uri, "file://")) + if os == "windows" { + workingDirectory, _ = url.QueryUnescape(workingDirectory) + workingDirectory = strings.ReplaceAll(workingDirectory, "/", "\\") + workingDirectory = strings.TrimPrefix(workingDirectory, "\\") + } + return workingDirectory +} diff --git a/internal/langserver/handlers/command/aztfmigrate_command_test.go b/internal/langserver/handlers/command/aztfmigrate_command_test.go new file mode 100644 index 00000000..107f3e78 --- /dev/null +++ b/internal/langserver/handlers/command/aztfmigrate_command_test.go @@ -0,0 +1,49 @@ +package command + +import ( + "testing" +) + +func Test_GetWorkingDirectory(t *testing.T) { + tests := []struct { + name string + os string + input string + want string + }{ + { + name: "basic on darwin", + os: "darwin", + input: "file:///Users/username/go/src/github.com/Azure/azapi-lsp/main.tf", + want: "/Users/username/go/src/github.com/Azure/azapi-lsp", + }, + + { + name: "basic on windows", + os: "windows", + input: "file:///c%3A/Users/username/go/src/github.com/Azure/azapi-lsp/main.tf", + want: "c:\\Users\\username\\go\\src\\github.com\\Azure\\azapi-lsp", + }, + + { + name: "path with spaces on windows", + os: "windows", + input: "file:///c%3A/Users/username/go/src/github.com/Azure/azapi-lsp/terraform%20files/main.tf", + want: "c:\\Users\\username\\go\\src\\github.com\\Azure\\azapi-lsp\\terraform files", + }, + + { + name: "path with Chinese characters on windows", + os: "windows", + input: "file:///c%3A/Users/username/go/src/github.com/Azure/azapi-lsp/%E4%B8%AD%E6%96%87/main.tf", + want: "c:\\Users\\username\\go\\src\\github.com\\Azure\\azapi-lsp\\中文", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getWorkingDirectory(tt.input, tt.os); got != tt.want { + t.Errorf("getWorkingDirectory() = %v, want %v", got, tt.want) + } + }) + } +}