Skip to content

Commit

Permalink
feat: add code lint “shellcheck” to github actions and configured cod…
Browse files Browse the repository at this point in the history
…acy (#9)

Create a complete workflow for the repository:

1. Automation A+B test.
2. `shellcheck` test.
3. Codacy code lint.
  • Loading branch information
Estrella-Explore authored Jan 25, 2025
1 parent cd1dd9d commit 2300da9
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
engines:
shellcheck:
enabled: true
4 changes: 4 additions & 0 deletions .codacyignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
demo/**
.github/**
README.md
README_zh-CN.md
2 changes: 1 addition & 1 deletion .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Greetings
name: Greet new contributors

on: [pull_request_target, issues]

Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/run_demo.yml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
@@ -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."
32 changes: 27 additions & 5 deletions make.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
exit

0 comments on commit 2300da9

Please sign in to comment.