aboutsummaryrefslogtreecommitdiff
path: root/acid_test.go
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2024-04-16 07:38:23 +0200
committerPřemysl Eric Janouch <p@janouch.name>2024-04-16 08:30:25 +0200
commitb594ff78b22452b1260286f86fc5a40dbf3d38d9 (patch)
tree2155919c214082fe2e99728b6d0d0ac187d9fea8 /acid_test.go
parentfe81d713e1c59f2175974f0bc3eda5ff7a5f0749 (diff)
downloadacid-b594ff78b22452b1260286f86fc5a40dbf3d38d9.tar.gz
acid-b594ff78b22452b1260286f86fc5a40dbf3d38d9.tar.xz
acid-b594ff78b22452b1260286f86fc5a40dbf3d38d9.zip
Improve shell quoting
Diffstat (limited to 'acid_test.go')
-rw-r--r--acid_test.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/acid_test.go b/acid_test.go
new file mode 100644
index 0000000..f23d892
--- /dev/null
+++ b/acid_test.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+ "bytes"
+ "testing"
+ ttemplate "text/template"
+)
+
+func TestTemplateQuote(t *testing.T) {
+ // Ideally, we should back-parse it using sh syntax.
+ // This is an unnecessarily fragile test.
+ for _, test := range []struct {
+ input, output string
+ }{
+ {`!!`, `'!!'`},
+ {``, `""`},
+ {`${var}`, `"\${var}"`},
+ {"`cat`", "\"\\`cat\\`\""},
+ {`"魚\"`, `"\"魚\\\""`},
+ } {
+ var b bytes.Buffer
+ err := ttemplate.Must(ttemplate.New("test").
+ Funcs(shellFuncs).Parse("{{quote .}}")).Execute(&b, test.input)
+ if err != nil {
+ t.Errorf("template execution error: %s\n", err)
+ }
+ if b.String() != test.output {
+ t.Errorf("%q should be quoted os %q, not %q\n",
+ test.input, test.output, b.String())
+ }
+ }
+}