-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): unescape runfiles entries from manifest #799
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "runfiles", | ||
srcs = ["runfiles.go"], | ||
importpath = "github.com/aspect-build/aspect-cli/pkg/bazel/runfiles", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "runfiles_test", | ||
srcs = ["runfiles_test.go"], | ||
deps = [ | ||
":runfiles", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package runfiles | ||
|
||
// Unescape the behavior of the C++ implementation | ||
// https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/main/tools/build-runfiles/build-runfiles.cc#L76 | ||
func Unescape(path string) string { | ||
var result []rune | ||
runes := []rune(path) | ||
|
||
for i := 0; i < len(runes); i++ { | ||
if runes[i] == '\\' && i+1 < len(runes) { | ||
switch runes[i+1] { | ||
case 's': | ||
result = append(result, ' ') | ||
case 'n': | ||
result = append(result, '\n') | ||
case 'b': | ||
result = append(result, '\\') | ||
default: | ||
// For escaped backslash (\\), output single backslash | ||
if runes[i+1] == '\\' { | ||
result = append(result, '\\') | ||
} else { | ||
// For any other escaped character, preserve both the backslash and the character | ||
result = append(result, '\\', runes[i+1]) | ||
} | ||
} | ||
i++ // Skip the escaped character | ||
} else { | ||
result = append(result, runes[i]) | ||
} | ||
} | ||
return string(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package runfiles_test | ||
|
||
import ( | ||
"github.com/aspect-build/aspect-cli/pkg/bazel/runfiles" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestUnescape(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"hello\\sworld", "hello world"}, | ||
{"new\\nline", "new\nline"}, | ||
{"back\\bslash", "back\\slash"}, | ||
{"double\\\\slash", "double\\slash"}, | ||
{"raw\\xsequence", "raw\\xsequence"}, | ||
{"no\\", "no\\"}, // Handles trailing backslash | ||
{"\\s\\n\\b", " \n\\"}, | ||
{"", ""}, | ||
{"normal text", "normal text"}, | ||
{"\\s\\s\\s", " "}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.input, func(t *testing.T) { | ||
got := runfiles.Unescape(tt.input) | ||
require.Equal(t, tt.expected, got) | ||
}) | ||
} | ||
} |