Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Visibly print trailing whitespace when static checks fail #75700

Merged
merged 1 commit into from
Apr 5, 2023

Conversation

akien-mga
Copy link
Member

@akien-mga akien-mga commented Apr 5, 2023

GitHub Actions seems to be hiding colored whitespace, and after lots of attempts I couldn't find a way to work it around.

So instead I'm using a perl expression to replace trailing spaces with · and tabs with <TAB> in the ANSI colored diff output. This ensure that they're visible, and they are properly colored as expected too.

Test run: https://github.com/akien-mga/godot/actions/runs/4620688099/jobs/8171140859

image

All these shell scripts could use some refactoring to de-duplicate stuff, if anything by moving a few common patterns to functions in a separate script that would be sourced in each. But for now I'm keeping it simple, already spent more time on this than I was expecting, and I don't plan to work further on this right now.

GitHub Actions seems to be hiding colored whitespace, and after lots of
attempts I couldn't find a way to work it around.

So instead I'm using a perl expression to replace trailing spaces with
`·` and tabs with `<TAB>` in the ANSI colored diff output. This ensure
that they're visible, and they are properly colored as expected too.
# A diff has been created, notify the user, clean up, and exit.
printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
# Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
printf "$diff\n" | perl -pe 's/(.*[^ ])( +)(\e\[m)$/my $spaces="·" x length($2); sprintf("$1$spaces$3")/ge' | perl -pe 's/(.*[^\t])(\t+)(\e\[m)$/my $tabs="<TAB>" x length($2); sprintf("$1$tabs$3")/ge'
Copy link
Member Author

@akien-mga akien-mga Apr 5, 2023

Choose a reason for hiding this comment

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

This was fun (not). If someone has a deep love for regex and sed/perl/awk/bash, feel free to try to optimize this so it's faster, simpler, more reliable, and more readable.

The gist of it is that it finds trailing whitespace (spaces and tabs) or colored lines (from git diff --color, i.e. lines removed or added), we replace them with · and <TAB> respectively. Had to use perl to be able to do "·" x length($2) for that, as I didn't want to replace spaces and tabs on the whole line if there's valid code before the trailing whitespace.

Copy link
Contributor

@YuriSizov YuriSizov left a comment

Choose a reason for hiding this comment

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

Seems like a nice improvement. Tried black and clang on Windows, and it doesn't appear to be broken, so that's good.

@akien-mga akien-mga merged commit fba9416 into godotengine:master Apr 5, 2023
@akien-mga akien-mga deleted the ci-visible-whitespace branch April 5, 2023 18:24
@YuriSizov
Copy link
Contributor

Cherry-picked for 4.0.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants