From 793cce41057c77c2ff681bf065acdb374555b93c Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Thu, 13 Jul 2023 15:36:08 -0700 Subject: [PATCH 1/6] WIP Support final line without newlines --- check-readme-newlines.sh | 37 +++++++++++++++++++++++++++-- fixtures/good-last-line-newlines.md | 21 ++++++++++++++++ test.sh | 9 +++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 fixtures/good-last-line-newlines.md diff --git a/check-readme-newlines.sh b/check-readme-newlines.sh index 56ac0e1..7abe7f6 100755 --- a/check-readme-newlines.sh +++ b/check-readme-newlines.sh @@ -53,8 +53,41 @@ while IFS= read -r line; do if [[ "${line}" =~ .*[[:space:]]{2}$ ]]; then continue fi - - echo "This line is missing trailing spaces: ${line}" + + # It's OK if the line does not end in two spaces, if there is a second line break. + # Use awk to search for the pattern and check for multiple newlines + if awk -v pattern="${line}" ' + BEGIN { + found = 0 + count = 0 + } + $0 ~ pattern { + found = 1 + next + } + found && NF == 0 { + count++ + next + } + found && NF > 0 { + if (count > 1) { + exit 1 + } + count = 0 + } + END { + if (count > 1 || (count == 1 && NF > 0)) { + exit 1 + } + exit !found + }' "$file"; then + echo "Multiple newlines found after '${line}'." + continue + else + echo "No multiple newlines found after '${line}'." + fi + + INVALID_LINE_FOUND=1 done < "$file" diff --git a/fixtures/good-last-line-newlines.md b/fixtures/good-last-line-newlines.md new file mode 100644 index 0000000..161f468 --- /dev/null +++ b/fixtures/good-last-line-newlines.md @@ -0,0 +1,21 @@ +# Rossums Universal Robots +Contributors: [pwtyler](https://profiles.wordpress.org/pwtyler) +Donate link: https://example.com/ +Tags: comments, spam +Requires at least: 4.5 +Tested up to: 6.2.1 +Requires PHP: 5.6 +Stable tag: 0.0.1 +License: GPLv2 or later +License URI: https://www.gnu.org/licenses/gpl-2.0.html + +See the robots hard at work: they are good at it. + +## Usage +Install the plugin and use it. +* Step One: Install it. +* Step Two: Profit +## Changelog + +### 0.1.0 (6 June 2023) +* Initial Release \ No newline at end of file diff --git a/test.sh b/test.sh index 37bc39c..c398bfc 100755 --- a/test.sh +++ b/test.sh @@ -17,4 +17,13 @@ if ! bash check-readme-newlines.sh fixtures/bad-single.md; then else echo "🚫 Invalidated bad README unsuccessfully. Did the bad-single.md file change? 😉" exit 1 +fi + +echo "Validate a double newline is acceptable" +echo "Running checks on fixtures/good-last-line-newlines.md..." +if bash check-readme-newlines.sh fixtures/good-last-line-newlines.md; then + echo "✅ Validated good README successfully!" +else + echo "🚫 Invalidated good README unsuccessfully. Did the good-last-line-newlines.md file change? 😉" + exit 1 fi \ No newline at end of file From aff1d5438618393c10c5a4bf9304bc03d4e1f5d7 Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Thu, 13 Jul 2023 22:25:24 -0700 Subject: [PATCH 2/6] Working alternative --- check-readme-newlines.sh | 48 ++++++++++------------------------------ test.sh | 7 ++++++ 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/check-readme-newlines.sh b/check-readme-newlines.sh index 7abe7f6..3162cfa 100755 --- a/check-readme-newlines.sh +++ b/check-readme-newlines.sh @@ -14,6 +14,7 @@ README_HEADER_SECTIONS=("Contributors" "Donate link" "Tags" "Requires at least" HEADER_ALRADY_READ=0 INVALID_LINE_FOUND=0 +MAYBE_INVALID_LINE="" # Read the README line by line while IFS= read -r line; do @@ -27,11 +28,20 @@ while IFS= read -r line; do continue fi - # Check if the line is empty if [[ "${line}" == "" ]]; then + # A line followed by a blank line (i.e. the last line) does not need double spaces. + # discard the last line's "maybe". + MAYBE_INVALID_LINE="" continue fi + + if [[ "${MAYBE_INVALID_LINE}" != "" ]]; then + echo "'$MAYBE_INVALID_LINE' is invalid" + INVALID_LINE_FOUND=1 + fi + MAYBE_INVALID_LINE="" + # Skip if the line does not contain a colon if [[ "${line}" != *":"* ]]; then continue @@ -54,41 +64,7 @@ while IFS= read -r line; do continue fi - # It's OK if the line does not end in two spaces, if there is a second line break. - # Use awk to search for the pattern and check for multiple newlines - if awk -v pattern="${line}" ' - BEGIN { - found = 0 - count = 0 - } - $0 ~ pattern { - found = 1 - next - } - found && NF == 0 { - count++ - next - } - found && NF > 0 { - if (count > 1) { - exit 1 - } - count = 0 - } - END { - if (count > 1 || (count == 1 && NF > 0)) { - exit 1 - } - exit !found - }' "$file"; then - echo "Multiple newlines found after '${line}'." - continue - else - echo "No multiple newlines found after '${line}'." - fi - - - INVALID_LINE_FOUND=1 + MAYBE_INVALID_LINE="${line}" done < "$file" if [[ ${INVALID_LINE_FOUND} -eq 1 ]];then diff --git a/test.sh b/test.sh index c398bfc..a60ec26 100755 --- a/test.sh +++ b/test.sh @@ -26,4 +26,11 @@ if bash check-readme-newlines.sh fixtures/good-last-line-newlines.md; then else echo "🚫 Invalidated good README unsuccessfully. Did the good-last-line-newlines.md file change? 😉" exit 1 +fi +echo "Running checks on fixtures/good-middle-line-newlines.md..." +if bash check-readme-newlines.sh fixtures/good-middle-line-newlines.md; then + echo "✅ Validated good README successfully!" +else + echo "🚫 Invalidated good README unsuccessfully. Did the good-last-line-newlines.md file change? 😉" + exit 1 fi \ No newline at end of file From 62349fcaabbd38a8ba86e086d988283869dbbd16 Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Thu, 13 Jul 2023 22:25:34 -0700 Subject: [PATCH 3/6] fixup --- fixtures/good-middle-line-newlines.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 fixtures/good-middle-line-newlines.md diff --git a/fixtures/good-middle-line-newlines.md b/fixtures/good-middle-line-newlines.md new file mode 100644 index 0000000..ce44ce9 --- /dev/null +++ b/fixtures/good-middle-line-newlines.md @@ -0,0 +1,22 @@ +# Rossums Universal Robots +Contributors: [pwtyler](https://profiles.wordpress.org/pwtyler) +Donate link: https://example.com/ +Tags: comments, spam +Requires at least: 4.5 +Tested up to: 6.2.1 +Requires PHP: 5.6 + +Stable tag: 0.0.1 +License: GPLv2 or later +License URI: https://www.gnu.org/licenses/gpl-2.0.html + +See the robots hard at work: they are good at it. + +## Usage +Install the plugin and use it. +* Step One: Install it. +* Step Two: Profit +## Changelog + +### 0.1.0 (6 June 2023) +* Initial Release \ No newline at end of file From ee9baa3e236118234f5f6823637db1eb657dfa73 Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Thu, 13 Jul 2023 23:26:30 -0700 Subject: [PATCH 4/6] Test Refactor --- check-readme-newlines.sh | 2 +- fixtures/bad-single-space.md | 21 ++++++++++ test.sh | 77 +++++++++++++++++++++--------------- 3 files changed, 68 insertions(+), 32 deletions(-) create mode 100644 fixtures/bad-single-space.md diff --git a/check-readme-newlines.sh b/check-readme-newlines.sh index 3162cfa..b1fac2f 100755 --- a/check-readme-newlines.sh +++ b/check-readme-newlines.sh @@ -37,7 +37,7 @@ while IFS= read -r line; do if [[ "${MAYBE_INVALID_LINE}" != "" ]]; then - echo "'$MAYBE_INVALID_LINE' is invalid" + echo "This line is missing its trailing spaces: '$MAYBE_INVALID_LINE'" INVALID_LINE_FOUND=1 fi MAYBE_INVALID_LINE="" diff --git a/fixtures/bad-single-space.md b/fixtures/bad-single-space.md new file mode 100644 index 0000000..0592a24 --- /dev/null +++ b/fixtures/bad-single-space.md @@ -0,0 +1,21 @@ +# Rossums Universal Robots +Contributors: [pwtyler](https://profiles.wordpress.org/pwtyler) +Donate link: https://example.com/ +Tags: comments, spam +Requires at least: 4.5 +Tested up to: 6.2.1 +Requires PHP: 5.6 +Stable tag: 0.0.1 +License: GPLv2 or later +License URI: https://www.gnu.org/licenses/gpl-2.0.html + +See the robots hard at work: they are good at it. + +## Usage +Install the plugin and use it. +Foo: Bar + +## Changelog + +### 0.1.0 (6 June 2023) +* Initial Release \ No newline at end of file diff --git a/test.sh b/test.sh index a60ec26..7189a78 100755 --- a/test.sh +++ b/test.sh @@ -1,36 +1,51 @@ #!/bin/bash set -eou pipefail -echo "Validate all fields are bad" -echo "Running checks on fixtures/bad-all.md..." -if ! bash check-readme-newlines.sh fixtures/bad-all.md; then - echo "✅ Validated bad README successfully!" -else - echo "🚫 Invalidated bad README unsuccessfully. Did the bad-all.md file change? 😉" - exit 1 -fi +function test_fixture() { + local FIXTURE_FILE="${1-}" + echo "➡️ Running checks on ${FIXTURE_FILE}..." + local TEST_FILE="${FIXTURE_FILE#fixtures/}" -echo "Validate single line is bad" -echo "Running checks on fixtures/bad-single.md..." -if ! bash check-readme-newlines.sh fixtures/bad-single.md; then - echo "✅ Validated bad README successfully!" -else - echo "🚫 Invalidated bad README unsuccessfully. Did the bad-single.md file change? 😉" - exit 1 -fi + if [[ ${TEST_FILE} != *.md ]]; then + echo "➖ ${FIXTURE_FILE} skipped, not a markdown file" + return + fi + if [[ ${TEST_FILE} == good* ]]; then + test_acceptable "${FIXTURE_FILE}" + return + fi + if [[ ${TEST_FILE} == bad* ]]; then + test_unacceptable "${FIXTURE_FILE}" + return + fi + echo "➖ Fixture ${FIXTURE_FILE} skipped, must start with 'good' or 'bad'" +} + +function test_acceptable() { + local FIXTURE_FILE="${1-}" + if bash check-readme-newlines.sh ${FIXTURE_FILE}; then + echo "✅ Validated good README successfully!" + else + echo "🚫 Invalidated good README unsuccessfully. Did ${FIXTURE_FILE#fixtures/} file change? 😉" + return 1 + fi +} + +function test_unacceptable() { + local fixture_file="${1-}" + if ! bash check-readme-newlines.sh ${fixture_file}; then + echo "✅ Invalidated bad README successfully!" + else + echo "🚫 Validated bad README unsuccessfully. Did ${fixture_file#fixtures/} file change? 😉" + return 1 + fi +} + +for file in fixtures/*; do + if [[ -f "$file" ]]; then + TEST_CASE=$(basename "$file") + test_fixture "fixtures/$TEST_CASE" + echo + fi +done -echo "Validate a double newline is acceptable" -echo "Running checks on fixtures/good-last-line-newlines.md..." -if bash check-readme-newlines.sh fixtures/good-last-line-newlines.md; then - echo "✅ Validated good README successfully!" -else - echo "🚫 Invalidated good README unsuccessfully. Did the good-last-line-newlines.md file change? 😉" - exit 1 -fi -echo "Running checks on fixtures/good-middle-line-newlines.md..." -if bash check-readme-newlines.sh fixtures/good-middle-line-newlines.md; then - echo "✅ Validated good README successfully!" -else - echo "🚫 Invalidated good README unsuccessfully. Did the good-last-line-newlines.md file change? 😉" - exit 1 -fi \ No newline at end of file From 0c46da8c4847e3588e4dcfa70aa83248ca692f53 Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Thu, 13 Jul 2023 23:33:02 -0700 Subject: [PATCH 5/6] CONTRIBUTING.md and fixup --- CONTRIBUTING.md | 11 +++++++++++ check-readme-newlines.sh | 1 + 2 files changed, 12 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..3429ab4 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,11 @@ +# Contributing + +## Releases +New releases are tagged and released automatically on merge to main with [autotag](https://github.com/pantheon-systems/autotag) and the `gh` CLI tool. See [Autotag's README](https://github.com/pantheon-systems/autotag#scheme-autotag-default) for details of how to annotate commit messages to denote major, minor, or patch version bumps. + +### Releasing to GitHub Action Marketplace +The release automation creates the tag and release, but `gh` cannot actually publish to the marketplace. To update the version available in the GitHub Marketplace, click the edit buttion on the latest release on the ["Release" page](https://github.com/pantheon-systems/validate-readme-spacing/releases) then on the edit page, check "Publish this Action to the GitHub Marketplace" and then click _Update Release_. + +## Tests + +Any .md file in `fixtures/` that begins with `good` or `bad` is tested for successful or unsuccessful validation respectively. \ No newline at end of file diff --git a/check-readme-newlines.sh b/check-readme-newlines.sh index b1fac2f..2e78ab4 100755 --- a/check-readme-newlines.sh +++ b/check-readme-newlines.sh @@ -64,6 +64,7 @@ while IFS= read -r line; do continue fi + # This may be invalid, but if followed by a blank line, will be deemed acceptable. MAYBE_INVALID_LINE="${line}" done < "$file" From c782a551a8c3284aa5ce1a5b5669f4ce6693ae65 Mon Sep 17 00:00:00 2001 From: Phil Tyler Date: Thu, 13 Jul 2023 23:36:34 -0700 Subject: [PATCH 6/6] lint and fixup --- Makefile | 2 +- tag-release.sh | 2 +- test.sh | 34 +++++++++++++++++++--------------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index ef9e011..9fa07ba 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ lint: - shellcheck ./check-readme-newlines.sh + shellcheck ./*.sh test: ./test.sh diff --git a/tag-release.sh b/tag-release.sh index 10c7b65..ec8d7e3 100755 --- a/tag-release.sh +++ b/tag-release.sh @@ -6,7 +6,7 @@ curl -sL https://git.io/autotag-install | sh -- # fetch all tags and history: git fetch --tags --unshallow --prune -if [ $(git rev-parse --abbrev-ref HEAD) != "main" ]; then +if [ "$(git rev-parse --abbrev-ref HEAD)" != "main" ]; then git branch --track main origin/main fi diff --git a/test.sh b/test.sh index 7189a78..6b022b6 100755 --- a/test.sh +++ b/test.sh @@ -1,20 +1,31 @@ #!/bin/bash set -eou pipefail +function main() { + for file in fixtures/*; do + if [[ -f "$file" ]]; then + local TEST_CASE + TEST_CASE=$(basename "$file") + test_fixture "fixtures/$TEST_CASE" + echo + fi + done +} + function test_fixture() { local FIXTURE_FILE="${1-}" echo "➡️ Running checks on ${FIXTURE_FILE}..." local TEST_FILE="${FIXTURE_FILE#fixtures/}" - if [[ ${TEST_FILE} != *.md ]]; then + if [[ "${TEST_FILE}" != *.md ]]; then echo "➖ ${FIXTURE_FILE} skipped, not a markdown file" return fi - if [[ ${TEST_FILE} == good* ]]; then + if [[ "${TEST_FILE}" == good* ]]; then test_acceptable "${FIXTURE_FILE}" return fi - if [[ ${TEST_FILE} == bad* ]]; then + if [[ "${TEST_FILE}" == bad* ]]; then test_unacceptable "${FIXTURE_FILE}" return fi @@ -23,7 +34,7 @@ function test_fixture() { function test_acceptable() { local FIXTURE_FILE="${1-}" - if bash check-readme-newlines.sh ${FIXTURE_FILE}; then + if bash check-readme-newlines.sh "${FIXTURE_FILE}"; then echo "✅ Validated good README successfully!" else echo "🚫 Invalidated good README unsuccessfully. Did ${FIXTURE_FILE#fixtures/} file change? 😉" @@ -32,20 +43,13 @@ function test_acceptable() { } function test_unacceptable() { - local fixture_file="${1-}" - if ! bash check-readme-newlines.sh ${fixture_file}; then + local FIXTURE_FILE="${1-}" + if ! bash check-readme-newlines.sh "${FIXTURE_FILE}"; then echo "✅ Invalidated bad README successfully!" else - echo "🚫 Validated bad README unsuccessfully. Did ${fixture_file#fixtures/} file change? 😉" + echo "🚫 Validated bad README unsuccessfully. Did ${FIXTURE_FILE#fixtures/} file change? 😉" return 1 fi } -for file in fixtures/*; do - if [[ -f "$file" ]]; then - TEST_CASE=$(basename "$file") - test_fixture "fixtures/$TEST_CASE" - echo - fi -done - +main \ No newline at end of file