mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 18:34:46 +00:00
Test case update
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
setup_file() {
|
||||
PROJECT_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)"
|
||||
export PROJECT_ROOT
|
||||
|
||||
ORIGINAL_HOME="${HOME:-}"
|
||||
export ORIGINAL_HOME
|
||||
|
||||
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-analyze-home.XXXXXX")"
|
||||
export HOME
|
||||
}
|
||||
|
||||
teardown_file() {
|
||||
rm -rf "$HOME"
|
||||
if [[ -n "${ORIGINAL_HOME:-}" ]]; then
|
||||
export HOME="$ORIGINAL_HOME"
|
||||
fi
|
||||
}
|
||||
|
||||
setup() {
|
||||
export TERM="dumb"
|
||||
rm -rf "${HOME:?}"/*
|
||||
mkdir -p "$HOME"
|
||||
}
|
||||
|
||||
@test "scan_directories lists largest folders first" {
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/bin/analyze.sh"
|
||||
|
||||
root="$HOME/analyze-root"
|
||||
mkdir -p "$root/Small" "$root/Large"
|
||||
printf 'tiny' > "$root/Small/file.txt"
|
||||
dd if=/dev/zero of="$root/Large/big.dat" bs=1024 count=200 >/dev/null 2>&1
|
||||
|
||||
output_file="$HOME/directories.txt"
|
||||
scan_directories "$root" "$output_file" 1
|
||||
|
||||
head -n1 "$output_file"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"Large"* ]]
|
||||
}
|
||||
|
||||
@test "aggregate_by_directory sums child sizes per parent" {
|
||||
run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF'
|
||||
set -euo pipefail
|
||||
source "$PROJECT_ROOT/bin/analyze.sh"
|
||||
|
||||
root="$HOME/group"
|
||||
mkdir -p "$root/a" "$root/b"
|
||||
|
||||
input_file="$HOME/files.txt"
|
||||
cat > "$input_file" <<LIST
|
||||
1024|$root/a/file1
|
||||
2048|$root/a/file2
|
||||
512|$root/b/data.bin
|
||||
LIST
|
||||
|
||||
output_file="$HOME/aggregated.txt"
|
||||
aggregate_by_directory "$input_file" "$output_file"
|
||||
|
||||
cat "$output_file"
|
||||
EOF
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"3072|$HOME/group/a/"* ]]
|
||||
[[ "$output" == *"512|$HOME/group/b/"* ]]
|
||||
}
|
||||
@@ -95,8 +95,19 @@ teardown() {
|
||||
}
|
||||
|
||||
@test "drain_pending_input clears stdin buffer" {
|
||||
# Test that drain_pending_input doesn't hang
|
||||
result=$(echo -e "test\ninput" | HOME="$HOME" timeout 1 bash --noprofile --norc -c "source '$PROJECT_ROOT/lib/common.sh'; drain_pending_input; echo done")
|
||||
# Test that drain_pending_input doesn't hang (using background job with timeout)
|
||||
result=$(
|
||||
(echo -e "test\ninput" | HOME="$HOME" bash --noprofile --norc -c "source '$PROJECT_ROOT/lib/common.sh'; drain_pending_input; echo done") &
|
||||
pid=$!
|
||||
sleep 2
|
||||
if kill -0 "$pid" 2> /dev/null; then
|
||||
kill "$pid" 2> /dev/null || true
|
||||
wait "$pid" 2> /dev/null || true
|
||||
echo "timeout"
|
||||
else
|
||||
wait "$pid" 2> /dev/null || true
|
||||
fi
|
||||
)
|
||||
[[ "$result" == "done" ]]
|
||||
}
|
||||
|
||||
|
||||
@@ -97,49 +97,3 @@ setup() {
|
||||
fi
|
||||
[ "$status" -ne 0 ]
|
||||
}
|
||||
|
||||
@test "whitelist rejects paths with spaces" {
|
||||
# Paths with spaces should be rejected by the new stricter validation
|
||||
mkdir -p "$HOME/.config/mole"
|
||||
echo "$HOME/.cache/invalid path" > "$WHITELIST_PATH"
|
||||
|
||||
# Load whitelist - path with space should be rejected
|
||||
warnings=$(HOME="$HOME" bash --noprofile --norc -c "
|
||||
source '$PROJECT_ROOT/bin/clean.sh'
|
||||
load_whitelist_file 2>&1 | grep -c 'Invalid path format' || echo 0
|
||||
")
|
||||
[ "$warnings" -ge 1 ]
|
||||
}
|
||||
|
||||
@test "whitelist rejects paths with consecutive slashes" {
|
||||
mkdir -p "$HOME/.config/mole"
|
||||
echo "$HOME/.cache//invalid" > "$WHITELIST_PATH"
|
||||
|
||||
warnings=$(HOME="$HOME" bash --noprofile --norc -c "
|
||||
source '$PROJECT_ROOT/bin/clean.sh'
|
||||
load_whitelist_file 2>&1 | grep -c 'Consecutive slashes' || echo 0
|
||||
")
|
||||
[ "$warnings" -ge 1 ]
|
||||
}
|
||||
|
||||
@test "whitelist rejects system protected paths" {
|
||||
mkdir -p "$HOME/.config/mole"
|
||||
echo "/System/Library/test" > "$WHITELIST_PATH"
|
||||
|
||||
warnings=$(HOME="$HOME" bash --noprofile --norc -c "
|
||||
source '$PROJECT_ROOT/bin/clean.sh'
|
||||
load_whitelist_file 2>&1 | grep -c 'Protected system path' || echo 0
|
||||
")
|
||||
[ "$warnings" -ge 1 ]
|
||||
}
|
||||
|
||||
@test "whitelist accepts valid paths with wildcards at end" {
|
||||
mkdir -p "$HOME/.config/mole"
|
||||
echo "$HOME/.cache/test/*" > "$WHITELIST_PATH"
|
||||
|
||||
# Should be accepted without warnings
|
||||
HOME="$HOME" bash --noprofile --norc -c "
|
||||
source '$PROJECT_ROOT/bin/clean.sh'
|
||||
load_whitelist_file
|
||||
" 2>&1 | grep -q "Invalid path format" && exit 1 || exit 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user