mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2026-11-25 01:43:04 +01:00
fix(encode64): support BSD base64 and quote arguments (#14269)
* encode64: support BSD base64 and quote arguments * chore: remove pr-body.md and pr-title.txt from branch --------- Co-authored-by: pei711 <pei711@users.noreply.github.com> Co-authored-by: pei711 <3922088406@qq.com>
This commit is contained in:
3 files changed
+101
-11
No files matched your search
@@ -46,10 +46,10 @@ Encode a file's contents to base64 and save output to text file.
|
||||
- From parameter
|
||||
|
||||
```console
|
||||
$ encodefile64 ohmyzsh.icn
|
||||
ohmyzsh.icn's content encoded in base64 and saved as ohmyzsh.icn.txt
|
||||
$ encodefile64 oh-my-zsh
|
||||
oh-my-zsh's content encoded in base64 and saved as oh-my-zsh.txt
|
||||
$ ef64 "oh-my-zsh"
|
||||
ohmyzsh.icn's content encoded in base64 and saved as ohmyzsh.icn.txt
|
||||
oh-my-zsh's content encoded in base64 and saved as oh-my-zsh.txt
|
||||
```
|
||||
|
||||
### Decoding
|
||||
|
||||
@@ -2,24 +2,37 @@ encode64() {
|
||||
if [[ $# -eq 0 ]]; then
|
||||
cat | base64
|
||||
else
|
||||
printf '%s' $1 | base64
|
||||
printf '%s' "$1" | base64
|
||||
fi
|
||||
}
|
||||
|
||||
encodefile64() {
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo "You must provide a filename"
|
||||
else
|
||||
base64 $1 > $1.txt
|
||||
echo "${1}'s content encoded in base64 and saved as ${1}.txt"
|
||||
if [[ $# -ne 1 || ! -f "$1" || ! -r "$1" ]]; then
|
||||
print -u2 -- 'Usage: encodefile64 <file>'
|
||||
return 1
|
||||
fi
|
||||
|
||||
local file="$1" output="${1}.txt" tmp_file
|
||||
[[ $output == /* ]] || output="./$output"
|
||||
|
||||
tmp_file=$(mktemp "${output}.XXXXXX") || return 1
|
||||
if ! base64 < "$file" > "$tmp_file"; then
|
||||
command rm -f "$tmp_file"
|
||||
return 1
|
||||
fi
|
||||
if ! command mv "$tmp_file" "$output"; then
|
||||
command rm -f "$tmp_file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
print -- "$file's content encoded in base64 and saved as ${file}.txt"
|
||||
}
|
||||
|
||||
decode64() {
|
||||
if [[ $# -eq 0 ]]; then
|
||||
cat | base64 --decode
|
||||
cat | base64 -d
|
||||
else
|
||||
printf '%s' $1 | base64 --decode
|
||||
printf '%s' "$1" | base64 -d
|
||||
fi
|
||||
}
|
||||
alias e64=encode64
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env zsh
|
||||
|
||||
emulate -LR zsh
|
||||
|
||||
source "${1:-${0:A:h:h}/encode64.plugin.zsh}"
|
||||
|
||||
test_dir=$(mktemp -d) || exit 1
|
||||
trap 'command rm -rf -- "$test_dir"' EXIT
|
||||
cd "$test_dir" || exit 1
|
||||
|
||||
typeset -i failures=0 tests=0
|
||||
typeset output expected_output
|
||||
|
||||
check_result() {
|
||||
local description="$1" expected_status="$2" expected_stdout="$3" expected_stderr="$4"
|
||||
shift 4
|
||||
|
||||
output=$("$@" 2> "$test_dir/stderr")
|
||||
local actual_status=$?
|
||||
(( tests++ ))
|
||||
|
||||
if [[ $actual_status == $expected_status && $output == "$expected_stdout" && $(<"$test_dir/stderr") == "$expected_stderr" ]]; then
|
||||
print -r -- "PASS: $description"
|
||||
else
|
||||
print -u2 -r -- "FAIL: $description"
|
||||
print -u2 -r -- " status: $actual_status (expected $expected_status)"
|
||||
print -u2 -r -- " stdout: ${(qqq)output} (expected ${(qqq)expected_stdout})"
|
||||
print -u2 -r -- " stderr: $(<"$test_dir/stderr") (expected $expected_stderr)"
|
||||
(( failures++ ))
|
||||
fi
|
||||
}
|
||||
|
||||
check_encode_arg() { encode64 'oh-my-zsh'; }
|
||||
check_encode_empty() { encode64 ''; }
|
||||
check_decode_arg() { decode64 'b2gtbXktenNo'; }
|
||||
|
||||
expected_output=$(printf '%s' 'oh-my-zsh' | base64)
|
||||
check_result 'encode argument' 0 "$expected_output" '' check_encode_arg
|
||||
check_result 'encode empty argument' 0 '' '' check_encode_empty
|
||||
check_result 'decode argument' 0 'oh-my-zsh' '' check_decode_arg
|
||||
|
||||
file="$test_dir/file with spaces.txt"
|
||||
print -r -- 'file contents' >| "$file"
|
||||
encodefile64 "$file" > "$test_dir/stdout" 2> "$test_dir/stderr"
|
||||
file_status=$?
|
||||
expected_output=$(base64 < "$file")
|
||||
(( tests++ ))
|
||||
if [[ $file_status == 0 && $(<"${file}.txt") == "$expected_output" && $(<"$test_dir/stdout") == "$file's content encoded in base64 and saved as ${file}.txt" && ! -s "$test_dir/stderr" ]]; then
|
||||
print -r -- 'PASS: encode file with spaces'
|
||||
else
|
||||
print -u2 -r -- 'FAIL: encode file with spaces'
|
||||
(( failures++ ))
|
||||
fi
|
||||
|
||||
check_invalid_file() { encodefile64 ''; }
|
||||
check_result 'empty filename' 1 '' 'Usage: encodefile64 <file>' check_invalid_file
|
||||
if [[ -e .txt ]]; then
|
||||
print -u2 -r -- 'FAIL: empty filename created .txt'
|
||||
(( failures++ ))
|
||||
command rm -f .txt
|
||||
fi
|
||||
|
||||
print -r -- 'preserve existing output' >| "${file}.txt"
|
||||
base64() { return 42; }
|
||||
encodefile64 "$file" > "$test_dir/stdout" 2> "$test_dir/stderr"
|
||||
file_status=$?
|
||||
unset -f base64
|
||||
(( tests++ ))
|
||||
if [[ $file_status != 0 && $(<"${file}.txt") == 'preserve existing output' && ! -s "$test_dir/stdout" ]]; then
|
||||
print -r -- 'PASS: failed encoding preserves existing output'
|
||||
else
|
||||
print -u2 -r -- 'FAIL: failed encoding preserves existing output'
|
||||
(( failures++ ))
|
||||
fi
|
||||
|
||||
print -r -- "$tests tests, $failures failures"
|
||||
(( failures == 0 ))
|
||||
Reference in new issue
Block a user