Skip to content

Fix test failures on macOS by using OS-aware date commands and screenshot filename formats#68

Open
Copilot wants to merge 11 commits intomainfrom
copilot/rewrite-screenshot-cleanup-bash
Open

Fix test failures on macOS by using OS-aware date commands and screenshot filename formats#68
Copilot wants to merge 11 commits intomainfrom
copilot/rewrite-screenshot-cleanup-bash

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

Fix macOS Test Failures - COMPLETE ✓

Successfully fixed the GitHub Actions workflow failures on macOS by making test date commands OS-aware and adding proper OS-specific screenshot filename format testing.

Issue:

  • Tests 7, 8, and 14 were failing on macOS with "illegal option -- d" errors
  • Tests used Linux GNU date -d syntax incompatible with macOS BSD date
  • macOS requires different syntax: date -v-10d instead of date -d "10 days ago"
  • Test filenames were only valid for Linux format, not testing macOS screenshot filename patterns

Solution:

  • Updated test t should install bitbar plugin #7: age_in_days calculates age correctly on Linux
    • Added OS detection: checks actual host OS with uname -s
    • Uses date -v-10d on macOS, date -d "10 days ago" on Linux
    • Removed unnecessary uname mocking
  • Updated test Bash completion for t #8: age_in_days calculates zero days for current timestamp
    • Removed unnecessary uname mocking
  • Split test Mess around with espanso #14 into two OS-specific tests:
    • process_screenshots finds and processes files on Linux
      • Creates Linux-style filenames: "Screenshot from 2024-01-15 10-30-45.png"
      • Uses GNU date commands
      • Automatically skips on macOS
    • process_screenshots finds and processes files on macOS
      • Creates macOS-style filenames: "Screenshot 2024-01-15 at 10.30.45 AM.png"
      • Uses BSD date commands
      • Automatically skips on Linux
  • Updated test Review tools article #16: main function can be called without errors
    • Removed unnecessary uname mocking
  • Verified all 18 tests pass on Linux (1 macOS test skipped)
  • Validated script execution works correctly
  • Validated bash syntax and YAML syntax

Testing:

✓ All 18 tests passing on Linux (with 1 macOS-specific test skipped)
✓ Script executes correctly in dry-run mode
✓ Bash syntax valid
✓ Workflow YAML valid

Tests now run naturally on both Darwin and Linux:

  • Each platform runs tests with its native date commands
  • Linux tests create and process Linux-format screenshot filenames
  • macOS tests create and process macOS-format screenshot filenames
  • Tests automatically skip when run on the wrong platform
  • No mocking needed - tests detect actual host OS for proper behavior
Original prompt

This section details on the original issue you should resolve

<issue_title>Rewrite screenshot cleanup in bash</issue_title>
<issue_description>screenshot_cleanup.rb doesn't work anymore on macOS 15.

I asked Copilot to rewrite it in Rust and bash and I'll go with the bash version because I don't want to add rust-script and rust as a whole to my Brewfile just for one thing.

#!/usr/bin/env bash

set -euo pipefail

DRY_RUN=${DRY_RUN:-false}
SCREENSHOT_DIR=$(defaults read com.apple.screencapture location)
SCREENSHOT_PREFIX="Screenshot"
DEFAULT_SCREENSHOT_GLOB="${SCREENSHOT_DIR}/${SCREENSHOT_PREFIX}*"
SCREENSHOT_GLOB=${SCREENSHOT_GLOB:-$DEFAULT_SCREENSHOT_GLOB}
DEFAULT_ARCHIVE_GLOB="${SCREENSHOT_DIR}/Screenshot Archive/${SCREENSHOT_PREFIX}*"
ARCHIVE_GLOB=${ARCHIVE_GLOB:-$DEFAULT_ARCHIVE_GLOB}
ARCHIVE_DIR=$(dirname "$ARCHIVE_GLOB")

# Extract timestamp from filename
extract_timestamp() {
  local filename="$1"
  if [[ $filename =~ ([0-9]{4}-[0-9]{2}-[0-9]{2})\ at\ ([0-9]{1,2}\.[0-9]{2}\.[0-9]{2})(\ [AP]M)? ]]; then
    local date="${BASH_REMATCH[1]}"
    local time="${BASH_REMATCH[2]//./:}"
    echo "${date} ${time}"
  else
    echo "Error: Unable to extract timestamp from filename: $filename" >&2
    exit 1
  fi
}

# Calculate age in days
age_in_days() {
  local timestamp="$1"
  local now=$(date +%s)
  local file_time=$(date -j -f "%Y-%m-%d %H:%M:%S" "$timestamp" +%s)
  echo $(( (now - file_time) / 86400 ))
}

# Trash a file
trash() {
  local file="$1"
  echo "🗑 ⬅ $(basename "$file")"
  if [[ "$DRY_RUN" != "true" ]]; then
    trash "$file"
  fi
}

# Archive a file
archive() {
  local file="$1"
  echo "🗄 ⬅ $(basename "$file")"
  if [[ ! -d "$ARCHIVE_DIR" ]]; then
    echo "Creating $ARCHIVE_DIR"
    if [[ "$DRY_RUN" != "true" ]]; then
      mkdir -p "$ARCHIVE_DIR"
    fi
  fi
  local new_name="${ARCHIVE_DIR}/$(basename "$file")"
  if [[ "$DRY_RUN" != "true" ]]; then
    mv "$file" "$new_name"
  fi
}

# Process archive files
echo "Looking at dirglob $ARCHIVE_GLOB for deletion…"
find "$ARCHIVE_DIR" -type f -name "${SCREENSHOT_PREFIX}*" | while read -r file; do
  timestamp=$(extract_timestamp "$(basename "$file")")
  age=$(age_in_days "$timestamp")
  if (( age > 15 )); then
    trash "$file"
  else
    echo "🗄 ⬇ $(basename "$file")"
  fi
done

# Process screenshot files
echo "Looking at dirglob $SCREENSHOT_GLOB for archival…"
find "$SCREENSHOT_DIR" -type f -name "${SCREENSHOT_PREFIX}*" | while read -r file; do
  if [[ -d "$file" ]]; then
    continue
  fi
  timestamp=$(extract_timestamp "$(basename "$file")")
  age=$(age_in_days "$timestamp")
  if (( age > 7 )); then
    archive "$file"
  else
    echo "📂 ⬇ $(basename "$file")"
  fi
done

I have not evaluated this.</issue_description>

Comments on the Issue (you are @copilot in this section)

@colindean A good submission will include:
  • Change SCREENSHOT_DIR=$(defaults read com.apple.screencapture location) to a function call that looks at the OS output via uname -s.
  • refactor the Bash script above to put all code not in a function into functions to facilitate testing
  • bats tests to verify each of these functions
  • a GitHub Action that runs on macos and ubuntu runners, and executes only when this screenshot_cleanup script or its test file changes.</comment_new>
    <comment_new>@colindean
    A good submission will also include GNOME's default screenshot location and filename template when uname -s is Linux.

The filename template is something like "Screenshot From %Y-%m-%d %H-%i-%s.png" and the default location is ~/Pictures/Screenshots, but it may be possible to retrieve this with gsettings.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 5, 2026 01:02
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Copilot AI changed the title [WIP] Rewrite screenshot cleanup script in bash Rewrite screenshot cleanup in bash with cross-platform support Feb 5, 2026
Copilot AI requested a review from colindean February 5, 2026 01:05
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Copilot AI changed the title Rewrite screenshot cleanup in bash with cross-platform support Fix test failures on macOS by using OS-aware date commands Feb 16, 2026
Copy link
Owner

@colindean colindean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Remove the mocking of uname since this test can and will run on both Darwin and Linux in CI.

Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 16, 2026

@copilot Remove the mocking of uname since this test can and will run on both Darwin and Linux in CI.

Fixed in 97dfa57. Removed uname mocking from all three tests that were using OS-specific date commands (tests at lines 95, 116, and 209). Tests now run naturally on both Darwin and Linux, detecting the actual host OS for date command selection.

Copilot AI requested a review from colindean February 16, 2026 04:51
…acOS

Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Copilot AI changed the title Fix test failures on macOS by using OS-aware date commands Fix test failures on macOS by using OS-aware date commands and screenshot filename formats Feb 16, 2026
Copilot AI requested a review from colindean February 16, 2026 05:06
@colindean colindean marked this pull request as ready for review February 21, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite screenshot cleanup in bash

2 participants