-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[quote/lighttpdquot] New function to quote strings for pasting in a l…
…ighttpd config file
- Loading branch information
1 parent
b9376d6
commit 3695223
Showing
2 changed files
with
110 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Quotes all arguments into a single lighttpd config-style string. | ||
# | ||
# Usage: | ||
# - lighttpdquot foo bar baz | ||
# - lighttpdquot 'foo bar baz' | ||
|
||
sed -e ':a' -e '$!N' -e '$!b a' -e 's/\n/\\n/g' -e 's/"/\\"/g' -e 's/\r/\\r/g' -e 's/ /\\t/g' -e '$s/^/"/' -e '$s/$/"/' <<EOF | ||
$* | ||
EOF |
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,101 @@ | ||
Describe 'quote/lighttpdquot' | ||
EnableSandbox | ||
AllowExternalCommand sed | ||
|
||
EnableLeakDetector | ||
|
||
SetupCommandFromFile lighttpdquot lib/quote/lighttpdquot.sh | ||
|
||
It 'quotes an empty string' | ||
When run command lighttpdquot '' | ||
|
||
The status should be success | ||
The stdout should equal '""' | ||
The stderr should equal '' | ||
End | ||
|
||
It "quotes 'word'" | ||
When run command lighttpdquot 'word' | ||
|
||
The status should be success | ||
The stdout should equal '"word"' | ||
The stderr should equal '' | ||
End | ||
|
||
It "quotes 'hello world'" | ||
When run command lighttpdquot 'hello world' | ||
|
||
The status should be success | ||
The stdout should equal '"hello world"' | ||
The stderr should equal '' | ||
End | ||
|
||
It "leaves alone single quotes in a a string" | ||
When run command lighttpdquot "It's a string" | ||
|
||
The status should be success | ||
The stdout should equal "\"It's a string\"" | ||
The stderr should equal '' | ||
End | ||
|
||
It "escapes double quotes in a string" | ||
When run command lighttpdquot 'This is "a value"' | ||
|
||
The status should be success | ||
The stdout should equal '"This is \"a value\""' | ||
The stderr should equal '' | ||
End | ||
|
||
It "escapes newlines" | ||
mlstr=$(echo 'line 1'; echo 'line 2'; echo 'line 3') | ||
|
||
When run command lighttpdquot "${mlstr}" | ||
|
||
The status should be success | ||
The stdout should equal '"line 1\nline 2\nline 3"' | ||
The stderr should equal '' | ||
End | ||
|
||
It "escapes carriage returns" | ||
# use >1 CRs to check if the sed command uses /g | ||
crstr=$(printf 'before\rmid\rafter') | ||
|
||
When run command lighttpdquot "${crstr}" | ||
|
||
The status should be success | ||
The stdout should equal '"before\rmid\rafter"' | ||
The stderr should equal '' | ||
End | ||
|
||
It "escapes tabs" | ||
# use >1 tabs to check if the sed command uses /g | ||
tsstr=$(printf 'col 1\tcol 2\tcol 3') | ||
|
||
When run command lighttpdquot "${tsstr}" | ||
|
||
The status should be success | ||
The stdout should equal '"col 1\tcol 2\tcol 3"' | ||
The stderr should equal '' | ||
End | ||
|
||
Context # long strings | ||
Parameters:dynamic | ||
l=2048 | ||
|
||
while test $((l*=2)) -le 100000 | ||
do | ||
%data $l | ||
done | ||
End | ||
|
||
It "quotes a $1 bytes long string" | ||
randstr=$(random_string $(($1)) '[:alnum:]') | ||
|
||
When run command lighttpdquot "${randstr}" | ||
|
||
The status should be success | ||
The stdout should equal "\"${randstr}\"" | ||
The stderr should equal '' | ||
End | ||
End | ||
End |