From 44c4d9551f2b1c6e925b1f1a723a5bc1c80711b1 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Sat, 21 Sep 2019 19:21:46 -0300 Subject: [PATCH] Validate variable names (#29) --- .circleci/config.yml | 2 +- cheats/git.cheat | 20 ++++++++++---------- src/arg.sh | 4 +++- test/core.sh | 7 +++++++ test/find_cheats | 10 ---------- test/run | 24 ++++++++++++++++++++++++ 6 files changed, 45 insertions(+), 22 deletions(-) delete mode 100755 test/find_cheats create mode 100755 test/run diff --git a/.circleci/config.yml b/.circleci/config.yml index 9e55fe2..6d7347f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -17,7 +17,7 @@ jobs: <<: *defaults steps: - checkout - - run: ./test/find_cheats + - run: ./test/run workflows: version: 2 diff --git a/cheats/git.cheat b/cheats/git.cheat index cf42242..cc79fd5 100644 --- a/cheats/git.cheat +++ b/cheats/git.cheat @@ -1,23 +1,21 @@ % git # Set global git user name -git config --global user.name "" +git config --global user.name # Set global git user email -git config --global user.email "" +git config --global user.email # Initializes a git repository git init # Adds a remote for a git repository -git remote add +git remote add # Checkout to branch # Change branch git checkout -$ branch: git branch --format='%(refname:short)' - # Displays the current status of a git repository git status @@ -31,22 +29,22 @@ git add git add . # Saves the changes to a file in a commit -git commit -m "" +git commit -m "" # Pushes committed changes to remote repository -git push -u +git push -u # Pushes changes to a remote repository overwriting another branch -git push : +git push : # Overwrites remote branch with local branch changes -git push -f +git push -f # Pulls changes to a remote repo to the local repo git pull --ff-only # Merges changes on one branch into current branch -git merge +git merge # Displays log of commits for a repo git log @@ -59,3 +57,5 @@ git clean -dxf # Sign all commits in a branch based on master git rebase master -S -f + +$ branch: git branch --format='%(refname:short)' diff --git a/src/arg.sh b/src/arg.sh index 007b821..b93d691 100644 --- a/src/arg.sh +++ b/src/arg.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +ARG_REGEX="<[0-9a-zA-Z_]+>" + arg::fn() { awk -F'---' '{print $1}' } @@ -16,7 +18,7 @@ arg::interpolate() { } arg::next() { - grep -Eo '<[0-9a-zA-Z_]+>' \ + grep -Eo "$ARG_REGEX" \ | head -n1 \ | tr -d '<' \ | tr -d '>' diff --git a/test/core.sh b/test/core.sh index 6e9dcdc..49265d2 100644 --- a/test/core.sh +++ b/test/core.sh @@ -9,4 +9,11 @@ test::success() { test::fail() { echo "Test failed..." exit 42 +} + +test::run() { + echo + echo "-> $1" + shift + eval "$*" && test::success || test::fail } \ No newline at end of file diff --git a/test/find_cheats b/test/find_cheats deleted file mode 100755 index 849d044..0000000 --- a/test/find_cheats +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -export SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)" -source "${SCRIPT_DIR}/test/core.sh" - -cheat::find \ - | grep -q "docker.cheat" \ - && test::success \ - || test::fail diff --git a/test/run b/test/run new file mode 100755 index 0000000..40e5d61 --- /dev/null +++ b/test/run @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +set -euo pipefail + +export SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)" +source "${SCRIPT_DIR}/test/core.sh" + +check_all_vars() { + local arg + IFS=$'\n' + for var in $(cat "$1" | grep -Eo "<[^>]*>"); do + if ! echo "$var" | grep -qE "$ARG_REGEX"; then + echoerr "$var isn't a valid variable name!" + exit 1 + fi + done +} + +test::run "We can find at least one known cheatsheet" \ + 'cheat::find | grep -q "docker.cheat"' + +for cheat in $(cheat::find); do + test::run "All variables in $(basename $cheat) are valid" \ + 'check_all_vars "$cheat"' +done