From 250b9d7c2191eb48083b9fba01fd8a4fe1e833e5 Mon Sep 17 00:00:00 2001 From: Lena <126529524+acuteenvy@users.noreply.github.com> Date: Thu, 26 Oct 2023 18:41:24 +0200 Subject: [PATCH] scripts: use `[[` instead of `[`, fix literal shell globs (#11290) * build.sh: move `*` outside quotes (it doesn't get expanded) * scripts: use `[[` instead of `[` in if statements --- scripts/build.sh | 8 ++++---- scripts/check-pr.sh | 14 +++++++------- scripts/deploy.sh | 2 +- scripts/test.sh | 12 ++++++------ 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/scripts/build.sh b/scripts/build.sh index 2ff6f4621..7390d3a00 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -5,11 +5,11 @@ set -ex function initialize { - if [ -z "$TLDRHOME" ]; then + if [[ -z $TLDRHOME ]]; then export TLDRHOME=${GITHUB_WORKSPACE:-$(pwd)} fi - if [ -z "$TLDR_LANG_ARCHIVES_DIRECTORY" ]; then + if [[ -z $TLDR_LANG_ARCHIVES_DIRECTORY ]]; then export TLDR_LANG_ARCHIVES_DIRECTORY="${GITHUB_WORKSPACE:-$(pwd)}/language_archives" fi @@ -32,11 +32,11 @@ function build_translation_archives { local source_directory="$TLDRHOME" local target_directory="$TLDR_LANG_ARCHIVES_DIRECTORY" mkdir -p "$target_directory" - rm -f "$target_directory/*" + rm -f "$target_directory"/* for lang_dir in "$source_directory"/pages*; do # Skip symlinks (pages.en) and things that are not directories - if [ ! -d "$lang_dir" ] || [ -h "$lang_dir" ]; then + if [[ ! -d $lang_dir || -h $lang_dir ]]; then continue fi diff --git a/scripts/check-pr.sh b/scripts/check-pr.sh index 19bb3a25a..ac7e72521 100755 --- a/scripts/check-pr.sh +++ b/scripts/check-pr.sh @@ -30,14 +30,14 @@ function check_duplicates { case "$platform" in common) # check if page already exists in other platforms for other in ${PLATFORMS/common/}; do - if [ -f "pages/$other/$file" ]; then + if [[ -f "pages/$other/$file" ]]; then printf "\x2d $MSG_EXISTS" "$page" "$other" fi done ;; *) # check if page already exists under common - if [ -f "pages/common/$file" ]; then + if [[ -f "pages/common/$file" ]]; then printf "\x2d $MSG_EXISTS" "$page" 'common' fi ;; @@ -52,7 +52,7 @@ function check_diff { git_diff=$(git diff --name-status --find-copies-harder --diff-filter=AC --relative=pages/ remotes/origin/main) - if [ -n "$git_diff" ]; then + if [[ -n $git_diff ]]; then echo -e "Check PR: git diff:\n$git_diff" >&2 else echo 'Check PR: git diff looks fine, no interesting changes detected.' >&2 @@ -85,13 +85,13 @@ function check_diff { # Recursively check the pages/ folder for anomalies. function check_structure { for platform in $PLATFORMS; do - if [ ! -d "pages/$platform" ]; then + if [[ ! -d "pages/$platform" ]]; then printf "\x2d $MSG_NOT_DIR" "pages/$platform" else for page in "pages/$platform"/*; do - if [ ! -f "$page" ]; then + if [[ ! -f $page ]]; then printf "\x2d $MSG_NOT_FILE" "$page" - elif [ "${page:(-3)}" != ".md" ]; then + elif [[ ${page:(-3)} != ".md" ]]; then printf "\x2d $MSG_NOT_MD" "$page" fi done @@ -111,7 +111,7 @@ MSG_NOT_MD='The file `%s` does not have a `.md` extension.\n' PLATFORMS=$(ls pages/) -if [ "$CI" = "true" ] && [ "$GITHUB_REPOSITORY" = "tldr-pages/tldr" ] && [ "$PULL_REQUEST_ID" != "" ]; then +if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then check_diff check_structure else diff --git a/scripts/deploy.sh b/scripts/deploy.sh index e77a69ab5..5ded42634 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -5,7 +5,7 @@ set -ex function initialize { - if [ -z "$TLDRHOME" ]; then + if [[ -z $TLDRHOME ]]; then export TLDRHOME=${GITHUB_WORKSPACE:-$(pwd)} fi diff --git a/scripts/test.sh b/scripts/test.sh index 9d43cf28b..2ab4afcbe 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -24,9 +24,9 @@ function run_black { errs=$(python3 -m black scripts --check --required-version ${target_black_version} 2>&1 || true) fi - if [ -z "${errs}" ]; then + if [[ -z $errs ]]; then # skip black check if command is not available in the system. - if [ "$CI" != "true" ] && ! exists black; then + if [[ $CI != true ]] && ! exists black; then echo "Skipping black check, command not available." return 0 fi @@ -49,7 +49,7 @@ function run_black { function run_flake8 { # skip flake8 check if command is not available in the system. - if [ "$CI" != "true" ] && ! exists flake8; then + if [[ $CI != true ]] && ! exists flake8; then echo "Skipping flake8 check, command not available." return 0 fi @@ -73,7 +73,7 @@ function run_tests { function run_tests_pr { errs=$(run_tests 2>&1) - if [ -n "$errs" ]; then + if [[ -n $errs ]]; then echo -e "Test failed!\n$errs\n" >&2 echo 'Sending errors to tldr-bot.' >&2 echo -n "$errs" | python3 scripts/send-to-bot.py report-errors @@ -86,7 +86,7 @@ function run_tests_pr { function run_checks_pr { msgs=$(bash scripts/check-pr.sh) - if [ -n "$msgs" ]; then + if [[ -n $msgs ]]; then echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2 echo 'Sending check results to tldr-bot.' >&2 echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results @@ -97,7 +97,7 @@ function run_checks_pr { # MAIN ################################### -if [ "$CI" = "true" ] && [ "$GITHUB_REPOSITORY" = "tldr-pages/tldr" ] && [ "$PULL_REQUEST_ID" != "" ]; then +if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then run_checks_pr run_tests_pr else