Skip to content

Commit

Permalink
fix: Properly escape backslashes
Browse files Browse the repository at this point in the history
task-json-escape was not properly escaping backslashes in
source strings. This fixes the issue, adds a test, and
corrects a bad test which masked the problem.
  • Loading branch information
seanmil committed Jul 16, 2024
1 parent 0b39a1b commit 9e8a0af
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion examples/mytask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ elif [ "$run_type" == 'output' ]; then
task-output-json "string2" '"abcd"'
task-output-json "number" 42
task-output-json "bool" true
task-output "complex-string" "This is a \"complex string\".\n\tSecond line."
task-output "complex-string" $'This is a "complex string".\n\tSecond line.'
task-output "escape-backslash" "\\ No newline"
fi

task-succeed
2 changes: 1 addition & 1 deletion files/task_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ task-json-escape() {
# 4. Escape newlines (1/2): Replace all newlines with literal tabs
# 5. Escape newlines (2/2): Replace all literal tabs with newline escape sequences
# 6. Delete any remaining non-printable lines from the stream
sed -e 's/\\/\\/g' \
sed -e 's/\\/\\\\/g' \
| { iconv -t ASCII --unicode-subst="\u%04x" 2>/dev/null || cat; } \
| sed -e 's/"/\\"/g' \
-e 's/\//\\\//g' \
Expand Down
1 change: 1 addition & 0 deletions spec/tasks/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
it { is_expected.to include('number' => 42) }
it { is_expected.to include('bool' => true) }
it { is_expected.to include('complex-string' => "This is a \"complex string\".\n\tSecond line.") }
it { is_expected.to include('escape-backslash' => '\ No newline') }
end
end
end

0 comments on commit 9e8a0af

Please sign in to comment.