Skip to content

Commit

Permalink
Improve file pattern arguments handling & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iangmaia committed Feb 6, 2025
1 parent 45fefcf commit bd983b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
20 changes: 5 additions & 15 deletions bin/pr_changed_files
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ while [[ "$#" -gt 0 ]]; do
fi
mode="${1#--}"
shift
# Check if there are any patterns after the flag
if [[ "$#" -eq 0 || "$1" == "--"* ]]; then
echo "Error: must specify at least one file pattern" >&2
exit 1
fi
while [[ "$#" -gt 0 && "$1" != "--"* ]]; do
patterns+=("$1")
shift
Expand Down Expand Up @@ -104,21 +109,6 @@ if [[ -z "$mode" ]]; then
exit 0
fi

if [[ ${#patterns[@]} -eq 0 ]]; then
echo "Error: must specify at least one file pattern" >&2
exit 1
fi

# No files changed
if [[ ${#changed_files[@]} -eq 0 ]]; then
if [[ "$mode" == "all-match" ]]; then
echo "true"
else
echo "false"
fi
exit 0
fi

# Returns 0 if the file matches any of the patterns, 1 otherwise
file_matches_any_pattern() {
local file="$1"
Expand Down
10 changes: 6 additions & 4 deletions tests/pr_changed_files/test_edge_cases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ if pr_changed_files --any-match 2>/dev/null; then
exit 1
fi

# [Test] Flag followed by another flag
output=$(pr_changed_files --any-match --something 2>&1 || true)
assert_output "Error: must specify at least one file pattern" "$output" "Should fail with correct error when flag is followed by another flag"

# [Test] Mutually exclusive options
if pr_changed_files --any-match "*.txt" --all-match "*.md" 2>/dev/null; then
echo "Should fail when using both --any-match and --all-match"
exit 1
fi
output=$(pr_changed_files --any-match "*.txt" --all-match "*.md" 2>&1 || true)
assert_output "Error: either specify --all-match or --any-match; cannot specify both" "$output" "Should fail with correct error when using mutually exclusive options"

# [Test] Files with spaces and special characters
mkdir -p "folder with spaces/nested\!\@\#\$folder"
Expand Down

0 comments on commit bd983b8

Please sign in to comment.