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

fix: lint errors from scripts/checkstyle.py #1385

Merged
merged 6 commits into from
Dec 23, 2022

Conversation

hyperupcall
Copy link
Contributor

@hyperupcall hyperupcall commented Dec 18, 2022

Summary

Sorry for the onslaught of PR's, this is going to be the last one until all the other ones are either closed or merged. This adds preliminary support for an automated way of enforcing particular styles that shellcheck doesn't catch.

Right now, it's a simple Python file (instead the originally proposed Perl for obvious readability reasons), which just checks a single "rule" - "unecessary printf backslashes".

The second commit uses this rule to format a single file like so:

$ ./scripts/checkstyle.py ./lib/functions/plugins.bash --fix
  • If --fix is not supplied, then it only prints out the errors
  • If no file paths are supplied, then it reads all of the files matching *.{bash,sh,bats}.
  • The comand sometimes needs to be run multiple times if there is more than one style violation on a single line.

I have the regular expressions and will write auto-fixers for each of the fixed things from #1349, but I'm only adding them one-at-a-time as stated.

Soon I guess it would be good to add GitHub actions and stuff, but I think should that be in a separate PR? I'm also looking for feedback on the checkstyle script, hopefully it's readable enough?

@hyperupcall hyperupcall requested a review from a team as a code owner December 18, 2022 07:10
@hyperupcall hyperupcall changed the title Add checkstyle.py script feat: Add checkstyle.py script Dec 18, 2022
@jthegedus
Copy link
Contributor

jthegedus commented Dec 20, 2022

My personal opinions of Python aside, this looks fine to me as a starting point. I prefer to use existing tooling if it exists with options to configure regex checks and fixes, but I couldn't find anything with a quick look. So, something custom now that is usable is great. We can always come back to it.

Would be good to add Python to this repository's .tool-versions file.

And adding the GitHub Action step now would be fine, no need to wait for another PR, just another step in the .github/workflows/lint.yml file.

@jthegedus jthegedus changed the title feat: Add checkstyle.py script feat: add scripts/checkstyle.py for improved style linting Dec 21, 2022
@hyperupcall
Copy link
Contributor Author

Sounds good! - In that case I'll run the script on every file so there aren't a million different lint commits

This script checks things that shellcheck fails to.
The following command was executed twice:
$ ./scripts/checkstyle.py ./lib/functions/plugins.bash --fix
@hyperupcall
Copy link
Contributor Author

I made those changed 👍

The regex was also updated to match within any case of double-quotes (not just after printf statements) as I mentioned here. This caught one extra violation at ./lib/commands/command-current.bash:47

@jthegedus
Copy link
Contributor

@Stratus3D

Building on your comment - #1349 (comment)

@hyperupcall while the PR is too much to merge all at once, it would be great if we had some automated way of enforcing these things via github actions. We currently require shfmt and shellcheck to pass for PRs, is there another tool we can use to enforce better Bash conventions? I know I would typically NOT catch all these things when reviewing a PR manually.

Is there a reason we can't capture this like we do test/banned_commands.bats?

While the automated fix is nice, we only need the automated fix once, then we can just ban with regex checks right?

@jthegedus
Copy link
Contributor

jthegedus commented Dec 21, 2022

@Stratus3D I am good with the changes here, but will leave final approval & merge to you.

I am going to merge, we can always back out these scripts which are part of CI.

@jthegedus jthegedus changed the title feat: add scripts/checkstyle.py for improved style linting fix: add scripts/checkstyle.py for improved style linting Dec 23, 2022
@jthegedus jthegedus changed the title fix: add scripts/checkstyle.py for improved style linting fix: lint errors from scripts/checkstyle.py Dec 23, 2022
@jthegedus
Copy link
Contributor

Cheers @hyperupcall

hyperupcall added a commit to fox-forks/asdf that referenced this pull request Dec 27, 2022
Co-authored-by: James Hegedus <jthegedus@hey.com>
slewsys added a commit to slewsys/asdf that referenced this pull request Dec 28, 2022
This reverts commit 3492043 due to
breaking changes in some contexts. To reproduce the error, install
`asdf` versions of `node` and `python` then use `npm` to install
`markov-pwgen`, whose installer runs a python script. Here is the
output:

```shell
npm i -g markov-pwgen
```
> npm ERR! code 126
> npm ERR! path /home/alm/.asdf/installs/nodejs/18.12.1/lib/node_modules/markov-pwgen
> npm ERR! command failed
> npm ERR! command sh -c test -f dictionary.js || ./scripts/filter-wordlist.py
> npm ERR! No preset version installed for command python3
> npm ERR! Please install a version by running one of the following:
> npm ERR!
> npm ERR! asdf install python 3.10.9
> npm ERR!
> npm ERR! or add one of the following versions in your config file at /home/alm/.asdf/.tool-versions
> npm ERR! python 3.11.1
>
> npm ERR! A complete log of this run can be found in:
> npm ERR!     /home/alm/.npm/_logs/2022-12-28T16_47_59_838Z-debug-0.log
slewsys added a commit to slewsys/asdf that referenced this pull request Dec 28, 2022
This reverts commit 3492043 due to
breaking changes in some contexts. To reproduce the error, install
`asdf` versions of `node` and `python` then use `npm` to install
`markov-pwgen`, whose installer runs a python script. Here is the
output:

```shell
npm i -g markov-pwgen
```
> npm ERR! code 126
> npm ERR! path /home/alm/.asdf/installs/nodejs/18.12.1/lib/node_modules/markov-pwgen
> npm ERR! command failed
> npm ERR! command sh -c test -f dictionary.js || ./scripts/filter-wordlist.py
> npm ERR! No preset version installed for command python3
> npm ERR! Please install a version by running one of the following:
> npm ERR!
> npm ERR! asdf install python 3.10.9
> npm ERR!
> npm ERR! or add one of the following versions in your config file at /home/alm/.asdf/.tool-versions
> npm ERR! python 3.11.1
>
> npm ERR! A complete log of this run can be found in:
> npm ERR!     /home/alm/.npm/_logs/2022-12-28T16_47_59_838Z-debug-0.log
slewsys added a commit to slewsys/asdf that referenced this pull request Dec 29, 2022
Move python from ~/.asdf/.tool-versions to ~/.asdf/scripts/.tool-versions.
Prior to this change, Python scripts under ~/.asdf attempted to open
the python version defined in ~/.asdf/.tool-versions, which generally
won't be installed.
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.

2 participants