From b594ff78b22452b1260286f86fc5a40dbf3d38d9 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Tue, 16 Apr 2024 07:38:23 +0200 Subject: Improve shell quoting --- acid_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 acid_test.go (limited to 'acid_test.go') 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()) + } + } +} -- cgit v1.2.3-70-g09d2