Skip to content

Commit 3752378

Browse files
authored
minor refactoring: use path.Dir when splitting a file path into parts in fake_workspace.go (#2621)
## Why <!-- Why are these changes needed? Provide the context that the reviewer might be missing. For example, were there any decisions behind the change that are not reflected in the code itself? --> Addresses a comment from #2465 ## Tests <!-- How have you tested the changes? --> Existing tests <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent 1300073 commit 3752378

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

libs/testserver/fake_workspace.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"path"
78
"sort"
89
"strconv"
910
"strings"
@@ -82,23 +83,15 @@ func (s *FakeWorkspace) WorkspaceDelete(path string, recursive bool) {
8283
}
8384
}
8485

85-
func (s *FakeWorkspace) WorkspaceFilesImportFile(path string, body []byte) {
86-
if !strings.HasPrefix(path, "/") {
87-
path = "/" + path
86+
func (s *FakeWorkspace) WorkspaceFilesImportFile(filePath string, body []byte) {
87+
if !strings.HasPrefix(filePath, "/") {
88+
filePath = "/" + filePath
8889
}
89-
s.files[path] = body
90+
s.files[filePath] = body
9091

9192
// Add all directories in the path to the directories map
92-
parts := strings.Split(path, "/")
93-
currentPath := ""
94-
for i, part := range parts {
95-
// Skip empty parts and the last part (which is the file itself)
96-
if part == "" || i == len(parts)-1 {
97-
continue
98-
}
99-
100-
currentPath = currentPath + "/" + part
101-
s.directories[currentPath] = true
93+
for dir := path.Dir(filePath); dir != "/"; dir = path.Dir(dir) {
94+
s.directories[dir] = true
10295
}
10396
}
10497

0 commit comments

Comments
 (0)