fix windows path in aztfmigrate (#113)

This commit is contained in:
Heng Lu 2024-10-14 15:18:02 +08:00 коммит произвёл GitHub
Родитель 615fe86cca
Коммит 7290837fed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 61 добавлений и 7 удалений

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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)
}
})
}
}