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

Improve shell scripts #193

Merged
merged 6 commits into from
Jul 9, 2024
Merged

Improve shell scripts #193

merged 6 commits into from
Jul 9, 2024

Conversation

lishaduck
Copy link
Contributor

@lishaduck lishaduck commented Jul 6, 2024

I'm going to run our testing scripts through OSH, see what antipatterns it spots, and fix the warnings.
OSH is a fast, simple, (almost) 100% bash-compatible and POSIX-compliant shell from the Oils project. It supports some extra shopts, which catch things bash and shellcheck don't/can't (respectively). It also supports some shopts that make breaking changes (ysh), but I don't plan on making our tests non-compatible with bash (as that would break CI, for one reason).

@lishaduck lishaduck marked this pull request as draft July 6, 2024 21:52
@lishaduck lishaduck mentioned this pull request Jul 6, 2024
22 tasks
.gitignore Show resolved Hide resolved
@@ -1,20 +1,20 @@
#!/bin/bash

set -e
set -euo pipefail
Copy link
Contributor Author

Choose a reason for hiding this comment

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

-o pipefail fails commands if an intermediate step fails.
-u is equivalent to -o nounset, which fails a script if it references unbound variables.


CWD=$(pwd)
CMD="elm-review --no-color"
TMP="$CWD/temporary"
ELM_HOME="$TMP/elm-home"
SNAPSHOTS="$CWD/run-snapshots"
SUBCOMMAND="$1"
SUBCOMMAND="${1:-}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes the subcommand default to none if there is none passed, for nounset.

@@ -34,10 +34,12 @@ function runCommandAndCompareToSnapshot {
exit 1
fi

eval "$LOCAL_COMMAND$AUTH --FOR-TESTS $ARGS" 2>&1 \
(eval "$LOCAL_COMMAND$AUTH --FOR-TESTS $ARGS" || true) 2>&1 \
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of these || trues tell bash that the command might fail. They should be here either way, it's a correctness issue, but pipefail enforces it.
The subshell makes it so that it's not the true that gets redirected to /dev/null/, but the whole thing.

Comment on lines +40 to +42
local diffed
diffed="$(diff "$TMP/$FILE" "$SNAPSHOTS/$FILE" || true)"
if [ "$diffed" != "" ]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is something OSH's strict_errexit caught. Before, if diff failed, it would be ignored. That's not good, so it has to be extracted into a variable.
However, we do want to do our own handling of it, so we then explicitly ignore the potential failures.

@@ -46,7 +48,6 @@ function runCommandAndCompareToSnapshot {
cat "$TMP/$FILE"
echo -e "\n \x1B[31mHere is the difference:\x1B[0m\n"
diff -p "$TMP/$FILE" "$SNAPSHOTS/$FILE"
exit 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

diff exits with exit code one, so this was dead code.

@@ -153,7 +156,7 @@ rm -rf "$TMP" \

mkdir -p "$TMP"

if [ "$1" == "record" ]
if [ "$SUBCOMMAND" == "record" ]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of using the potentially unbound $1, use the bound-to-be-bound $SUBCOMMAND, which also reads easier.

@@ -254,7 +260,7 @@ $createTest "$CMD" \
"Fixing all errors for an entire rule should remove the suppression file" \
"" \
"suppressed-errors-after-fixed-errors-for-rule.txt"
if [ -f review/suppressed/NoUnused.Dependencies.json ]; then
if [ -f "./review/suppressed/NoUnused.Dependencies.json" ]; then
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Always put paths in strings. Doesn't make a difference here, but doesn't hurt.

Comment on lines +7 to +9
defaults:
run:
shell: bash
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Run commands with pipefail.
See lowlighter/libs#69 (comment) for a longer explaination.

fi
if [ "$(diff -yq "$TMP/project to fix/src/Folder/Unused.elm" "$SNAPSHOTS/project to fix/src/Folder/Unused.elm")" != "" ]
declare diffed
diffed="$(diff -q "$TMP/project to fix/src/Folder/Unused.elm" "$SNAPSHOTS/project to fix/src/Folder/Unused.elm" || true)"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

-y and -q are incompatible, at least on macOS.

@jfmengels
Copy link
Owner

Thank you for the improvements!

@jfmengels jfmengels merged commit 49ab475 into jfmengels:main Jul 9, 2024
3 checks passed
@lishaduck lishaduck deleted the better-sh branch July 9, 2024 15:19
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