-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Similar to strconv.Quote, but with shell syntax. See the added godoc for more details. Unlike strconv.Quote, it needs to return a boolean, as not all strings can be represented in shell. Fixes #328.
- Loading branch information
Showing
4 changed files
with
191 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,42 @@ | ||
//go:build dev.fuzz | ||
// +build dev.fuzz | ||
|
||
package syntax_test | ||
|
||
import ( | ||
"os/exec" | ||
"testing" | ||
|
||
"mvdan.cc/sh/v3/syntax" | ||
) | ||
|
||
func FuzzQuote(f *testing.F) { | ||
// Keep in sync with ExampleQuote. | ||
f.Add("foo") | ||
f.Add("bar $baz") | ||
f.Add(`"won't"`) | ||
f.Add(`~/home`) | ||
f.Add("#1304") | ||
f.Add("name=value") | ||
f.Add(`glob-*`) | ||
f.Add("invalid-\xe2'") | ||
f.Add("nonprint-\x0b\x1b") | ||
f.Fuzz(func(t *testing.T, s string) { | ||
quoted, ok := syntax.Quote(s) | ||
if !ok { | ||
// Contains a null byte; not interesting. | ||
return | ||
} | ||
out, err := exec.Command("bash", "-c", | ||
"printf %s "+quoted+ | ||
"", // TODO: quoted as a command (func), not an arg | ||
).CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("bash error on %q quoted as %s: %v: %s", s, quoted, err, out) | ||
} | ||
want, got := s, string(out) | ||
if want != got { | ||
t.Fatalf("output mismatch on %q quoted as %s: got %q (len=%d)", want, quoted, got, len(got)) | ||
} | ||
}) | ||
} |
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