Skip to content

Instantly share code, notes, and snippets.

@mrLSD
Created September 30, 2024 21:55
Show Gist options
  • Select an option

  • Save mrLSD/f06c0a675800477550addacb438081e5 to your computer and use it in GitHub Desktop.

Select an option

Save mrLSD/f06c0a675800477550addacb438081e5 to your computer and use it in GitHub Desktop.
Run swift tests with pretty error printing
#!/bin/bash
swift test 2>&1 | awk '
/error:/ {
# Use regular expression to capture required parts
# Regex Breakdown:
# ^(.+\/)?([^:]+\.swift):([0-9]+): error: -\[(.*?)\] : expected to equal <([^>]+)>, got <([^>]+)>
regex = "^(.+\\/)?([^:]+\\.swift):([0-9]+): error: -\\[(.*?)\\] : expected to equal <([^>]+)>, got <([^>]+)>"
if (match($0, regex, m)) {
# m[2]: Filename (e.g., MyTests.swift)
# m[3]: Line number
# m[4]: Test name (e.g., MyTestName
# m[5]: Expected value (e.g., SomeVal)
# m[6]: Got value (e.g., SomeVal)
BOLD = "\033[1m"
GREY = "\033[90m"
RESET = "\033[0m"
# Print Filename:LineNumber in Bold
printf BOLD "%s:%s " RESET, m[2], m[3]
# Print Test Name in Grey
printf GREY "%s" RESET "\n", m[4]
# Print expected value with tab
printf GREY "expect: " RESET
print m[5]
# Print "got: ActualValue"
printf GREY "got: " RESET
print m[6]
print "_________________________________"
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment