From 27506acc8a39a748c85d9bfb5df6f1df394c35ba Mon Sep 17 00:00:00 2001 From: Junpei Zhang <1259636180@qq.com> Date: Wed, 25 Nov 2026 03:28:08 +0800 Subject: [PATCH] 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 Co-authored-by: pei711 <3922088406@qq.com> --- plugins/encode64/README.md | 6 +- plugins/encode64/encode64.plugin.zsh | 29 ++++++--- plugins/encode64/tests/encode64.test.zsh | 77 ++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 plugins/encode64/tests/encode64.test.zsh diff --git a/plugins/encode64/README.md b/plugins/encode64/README.md index e3e25a742..245a2c641 100644 --- a/plugins/encode64/README.md +++ b/plugins/encode64/README.md @@ -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 diff --git a/plugins/encode64/encode64.plugin.zsh b/plugins/encode64/encode64.plugin.zsh index 8e6fdb169..c8ccb73f6 100644 --- a/plugins/encode64/encode64.plugin.zsh +++ b/plugins/encode64/encode64.plugin.zsh @@ -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 ' + 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 diff --git a/plugins/encode64/tests/encode64.test.zsh b/plugins/encode64/tests/encode64.test.zsh new file mode 100644 index 000000000..8234ed8be --- /dev/null +++ b/plugins/encode64/tests/encode64.test.zsh @@ -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 ' 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 ))