Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow missing spaces if followed by a newline. #12

Merged
merged 6 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lint:
shellcheck ./check-readme-newlines.sh
shellcheck ./*.sh

test:
./test.sh
Expand Down
18 changes: 14 additions & 4 deletions check-readme-newlines.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "This line is missing its trailing spaces: '$MAYBE_INVALID_LINE'"
INVALID_LINE_FOUND=1
fi
MAYBE_INVALID_LINE=""

# Skip if the line does not contain a colon
if [[ "${line}" != *":"* ]]; then
continue
Expand All @@ -53,9 +63,9 @@ while IFS= read -r line; do
if [[ "${line}" =~ .*[[:space:]]{2}$ ]]; then
continue
fi
echo "This line is missing trailing spaces: ${line}"
INVALID_LINE_FOUND=1

# This may be invalid, but if followed by a blank line, will be deemed acceptable.
MAYBE_INVALID_LINE="${line}"
done < "$file"

if [[ ${INVALID_LINE_FOUND} -eq 1 ]];then
Expand Down
21 changes: 21 additions & 0 deletions fixtures/bad-single-space.md
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions fixtures/good-last-line-newlines.md
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions fixtures/good-middle-line-newlines.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tag-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
67 changes: 51 additions & 16 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
#!/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 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
}

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
function test_fixture() {
local FIXTURE_FILE="${1-}"
echo "➡️ Running checks on ${FIXTURE_FILE}..."
local TEST_FILE="${FIXTURE_FILE#fixtures/}"

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
}

main