From 741642d7a97be6582df89edcab1b6ecbf5b1852d Mon Sep 17 00:00:00 2001 From: umutcagand Date: Tue, 20 Oct 2026 03:21:39 +0300 Subject: [PATCH] fix(extract): preserve .lzma and .Z source files (#14191) --- plugins/extract/extract.plugin.zsh | 4 +- plugins/extract/tests/extract.test.zsh | 65 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 plugins/extract/tests/extract.test.zsh diff --git a/plugins/extract/extract.plugin.zsh b/plugins/extract/extract.plugin.zsh index 3a576f19e..e832ee443 100755 --- a/plugins/extract/extract.plugin.zsh +++ b/plugins/extract/extract.plugin.zsh @@ -107,8 +107,8 @@ EOF (*.xz) xzcat "$full_path" > "${file:t:r}" ;; (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$full_path" ;; (*.lz4) lz4 -d "$full_path" ;; - (*.lzma) unlzma "$full_path" ;; - (*.z) uncompress "$full_path" ;; + (*.lzma) unlzma -c "$full_path" > "${file:t:r}" ;; + (*.z) uncompress -c "$full_path" > "${file:t:r}" ;; (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl|*.vsix|*.crx|*.pk3|*.pk4) unzip "$full_path" ;; (*.rar) if (( $+commands[unrar] )); then diff --git a/plugins/extract/tests/extract.test.zsh b/plugins/extract/tests/extract.test.zsh new file mode 100644 index 000000000..fe32f978a --- /dev/null +++ b/plugins/extract/tests/extract.test.zsh @@ -0,0 +1,65 @@ +#!/usr/bin/env zsh + +# Run with: zsh -df plugins/extract/tests/extract.test.zsh +emulate -L zsh +setopt err_return + +for tool in lzma unlzma uncompress; do + (( $+commands[$tool] )) || { + print -u2 -r -- "These tests require $tool." + exit 1 + } +done + +source "${0:A:h:h}/extract.plugin.zsh" +local test_dir=$(mktemp -d) original_dir=$PWD + +check_extract() { + local extension=$1 mode=$2 + local case_dir="$test_dir/$extension-$mode" + mkdir -p "$case_dir/source files" "$case_dir/work/target dir" + local archive="$case_dir/source files/sample.txt.$extension" + cp "$test_dir/fixture.$extension" "$archive" + builtin cd "$case_dir/work" + + local output="$PWD/sample.txt" + local -a args + case "$mode" in + target) + args=(-t 'target dir') + output="$PWD/target dir/sample.txt" + ;; + remove) args=(-r) ;; + esac + + # Run the plugin without the test's ERR_RETURN option. + () { + emulate -L zsh + extract "${args[@]}" "$archive" + } || return 1 + cmp "$test_dir/plain" "$output" + if [[ $mode == remove ]]; then + [[ ! -e $archive ]] + else + [[ -f $archive ]] + fi + print -r -- "PASS: $extension ($mode)" +} + +{ + print -r -- hello > "$test_dir/plain" + lzma -c "$test_dir/plain" > "$test_dir/fixture.lzma" + # "hello\n" as a .Z stream, since many systems only ship uncompress. + printf '\037\235\220\150\312\260\141\363\106\001' \ + > "$test_dir/fixture.Z" + + for extension in lzma Z; do + for mode in default target remove; do + check_extract "$extension" "$mode" + done + done + print 'All extract compressed-file tests passed.' +} always { + builtin cd "$original_dir" + rm -rf -- "$test_dir" +}