AI Hints
CI/CD with GitHub Actions · Lesson 62 of 138

Formatting Checks

One style for everyone.

⏱ 2 min read▶ Video at 1:10:53🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Arguing about spaces and quotes in code reviews wastes everyone's time. A formatter, like Black for Python, or Prettier for JavaScript, rewrites code into one standard style. In CI, we just check that the code is already formatted.

Formatting Checks
From the video: Formatting Checks
  • No more style arguments
  • A formatter decides
  • CI checks it

black --check

Here's some code with odd spacing and single quotes. Black dash dash check doesn't change anything. With dash Q it stays quiet, and exits with code one, which fails the step. Locally, running black without check fixes the file. Then the check passes.

Terminal
printf "def hi( name ):\n  return 'hi '+name\n" > hi.py
black --check -q hi.py; echo "exit code $?"
exit code 1
black -q hi.py && cat hi.py && black --check -q hi.py && echo "check passed"
def hi(name):
    return "hi " + name
check passed

Fix locally, check in CI

The usual setup is simple. On your machine, the formatter fixes files, maybe automatically when you save. In CI, it only checks, and fails the pull request if someone forgot. CI shouldn't change your code behind your back.

On your machine

  • black .
  • Fixes the files
  • Often on save

In CI

  • black --check .
  • Only reports
  • Fails if unformatted

Summary

Formatters give a project one style. Fix locally, and check in CI with dash dash check. Next, measuring how much code the tests cover.

Key takeaways
  • black --check
  • Fix locally
  • Prettier for JS
Next up: Test Coverage — what the tests touch