From 2300da910ae691837afb9dd82ede08be012ca800 Mon Sep 17 00:00:00 2001 From: Estrella-Explore <151703168+Estrella-Explore@users.noreply.github.com> Date: Sat, 25 Jan 2025 14:06:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20code=20lint=20=E2=80=9Cshellcheck?= =?UTF-8?q?=E2=80=9D=20to=20github=20actions=20and=20configured=20codacy?= =?UTF-8?q?=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create a complete workflow for the repository: 1. Automation A+B test. 2. `shellcheck` test. 3. Codacy code lint. --- .codacy.yml | 4 +++ .codacyignore | 4 +++ .github/workflows/greetings.yml | 2 +- .github/workflows/run_demo.yml | 52 ++++++++++++++++++++++++++++++++ .github/workflows/shellcheck.yml | 31 +++++++++++++++++++ make.sh | 32 +++++++++++++++++--- 6 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 .codacy.yml create mode 100644 .codacyignore create mode 100644 .github/workflows/run_demo.yml create mode 100644 .github/workflows/shellcheck.yml mode change 100755 => 100644 make.sh diff --git a/.codacy.yml b/.codacy.yml new file mode 100644 index 0000000..bfc2e80 --- /dev/null +++ b/.codacy.yml @@ -0,0 +1,4 @@ +--- +engines: + shellcheck: + enabled: true diff --git a/.codacyignore b/.codacyignore new file mode 100644 index 0000000..a1914a2 --- /dev/null +++ b/.codacyignore @@ -0,0 +1,4 @@ +demo/** +.github/** +README.md +README_zh-CN.md diff --git a/.github/workflows/greetings.yml b/.github/workflows/greetings.yml index f3e64a9..9588d96 100644 --- a/.github/workflows/greetings.yml +++ b/.github/workflows/greetings.yml @@ -1,4 +1,4 @@ -name: Greetings +name: Greet new contributors on: [pull_request_target, issues] diff --git a/.github/workflows/run_demo.yml b/.github/workflows/run_demo.yml new file mode 100644 index 0000000..608edb1 --- /dev/null +++ b/.github/workflows/run_demo.yml @@ -0,0 +1,52 @@ +name: Check A+B Result + +on: + push: + branches: + - '*' + +jobs: + A_plus_B: + runs-on: + ubuntu-latest + + steps: + - name: + Checkout code + uses: + actions/checkout@v3 + + - name: + try calculate A+B + run: | + echo "Beep boop! Let's try A plus B!" + + echo "All '*.cpp' files:" + find . -name "*.cpp" + chmod +x ./make.sh + + (echo "Y"; echo "Y") | ./make.sh ./demo/A_Plus_B.cpp + shell: bash + + - name: + Check A+B result + id: + check_result + run: | + if [[ $(cat ./demo/A_Plus_B.ans) != "2034324" ]]; then + echo "Beep boop! We were tricked by CCF!" + cat ./demo/A_Plus_B.log + exit 1 + else + echo "Beep boop! A+B result is correct!" + fi + shell: bash + + - name: + Collect results + if: + steps.check_result.outcome == 'success' + run: + echo "Beep boop! The check passed successfully!" + echo "Beep boop! We successfully fk-ed CCF!" + shell: bash diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.yml new file mode 100644 index 0000000..c37dc8d --- /dev/null +++ b/.github/workflows/shellcheck.yml @@ -0,0 +1,31 @@ +name: Shell check + +on: + pull_request: + branches: + - '*' + +jobs: + shellcheck: + runs-on: ubuntu-latest + + steps: + - name: FKCCF everyday + run: | + echo "Beep boop! shellcheck was triggered..." + echo "I would say, FKCCF!" + + - name: Clone repo + uses: actions/checkout@v3 + + - name: Show status + run: | + echo "repo was cloned successfully!" + + - name: shellcheck + uses: ludeeus/action-shellcheck@00b27aa7cb85167568cb48a3838b75f4265f2bca + + - name: Finish + run: | + echo "Beep boop! shellcheck finished!" + echo "Beep boop! You are able to request a review now." diff --git a/make.sh b/make.sh old mode 100755 new mode 100644 index 132ed3b..839631a --- a/make.sh +++ b/make.sh @@ -35,7 +35,7 @@ tryUsingDefaultTestcase() { if [[ "$operation" != [Nn]* ]]; then # Proceed if the user input is not "N" or "n". blueOutput "[Info]:${RESET} Using ${filename}.in as the test case." - ./${filename}.out < "${filename}.in" > "${filename}.ans" # Executes the program with input redirection. + "./${filename}.out" < "${filename}.in" > "${filename}.ans" # Executes the program with input redirection. blueOutput "[Info]:${RESET} Output is shown below and saved as ${filename}.ans.\n" @@ -83,6 +83,16 @@ cleanupEmptyFiles() { initCleanup +# @brief: New a pipe named fake_tty +# `/dev/tty` is OK for most of the users. However, GitHub Actions doesn't have it. +# It will raise an error: `tee: /dev/tty: No such device or address` +# Given that, we should `tee` outputs to fake_tty, and `cat fake_tty` after +# compilation. +# Don't forget to `rm fake_tty` at last. +if [[ -e fake_tty ]]; then rm fake_tty; fi +mkfifo fake_tty +chmod +x fake_tty + # @brief: Compiles `${filename}.cpp` using `g++` with detailed warnings and debugging flags. # --std=c++14 is the require of CCF - China Cheating-money Foundation # c++14 for CSP-J/S, NOIp and NOI, etc. @@ -95,8 +105,20 @@ g++ -g -Wall -Wextra -pedantic --std=c++14 -Og \ -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC \ -fdiagnostics-color=always \ "$1" -o "${filename}.out" 2>&1 \ - | tee /dev/tty \ - | sed "s/\x1B\[[0-9;]*[a-zA-Z]//g" > "${filename}.log" + | tee fake_tty & + +# @brief: Output `fake_tty` after compilation. +# The origin compile command was `tee /dev/tty` directly, it didn't work on +# GitHub Actions. +# We introduced `fake_tty` to resolve it. +# `wait` is because `tee fake_tty &` is asynchronous. +# Now it's time to output and clean it up. +# +# By the way, I hate the computer of GitHub Actions. It doesn't have `/dev/tty`. +./fake_tty >(sed "s/\x1B\[[0-9;]*[a-zA-Z]//g" > "${filename}.log") + +wait +rm fake_tty # Check if the output file was successfully created and is executable. if [[ -x "${filename}.out" ]]; then @@ -105,9 +127,9 @@ if [[ -x "${filename}.out" ]]; then tryUsingDefaultTestcase else redOutput "[Error]: Compilation failed.\a" - blueOutput "[Info]:${RESET} Check "${filename}.log" for details." + blueOutput "[Info]:${RESET} Check '${filename}.log' for details." fi cleanupEmptyFiles -exit \ No newline at end of file +exit