Skip to content

Commit

Permalink
Merge pull request #10 from generalui/lint-test-yarn
Browse files Browse the repository at this point in the history
[lint-test-yarn] Added coverage option. Added more functionality!
  • Loading branch information
jonryser authored Oct 30, 2024
2 parents e20aa8f + 7622c96 commit 3f1459a
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 8 deletions.
57 changes: 52 additions & 5 deletions .github/actions/lint-test-yarn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,41 @@ It's a comprehensive solution for maintaining code quality standards and ensurin

The action accepts the following inputs:

- `branch`:
- __Description__: The branch that is being tested. If not specified, defaults to the current branch.
- __Required__: No
- __Default__: ''

- `checkout-code`:
- __Description__: Whether or not to checkout the code.
- __Required__: No
- __Default__: 'yes'

- `node-version`:
- __Description__: The version of Node.js to use. If not specified, defaults to the latest version.
- __Required__: No
- __Default__: 'latest'

- `run-before-tests`:
- __Description__: A shell command to run before tests.
- __Required__: No
- __Default__: ''

- `should-run-lint`:
- __Description__: Whether or not to lint. Anything other than 'yes' will skip linting.
- __Required__: No
- __Default__: 'yes'

- `should-run-tests`:
- __Description__: Whether or not to run tests. Anything other than 'yes' will skip tests.
- __Required__: No
- __Default__: 'yes'

- `upload-coverage`:
- __Description__: Whether or not to upload coverage as an artifact. Anything other than 'yes' will skip uploading. Assumes that the test command is `yarn test:coverage` if true.
- __Required__: No
- __Default__: 'No'

- `yarn-version`:
- __Description__: The version of Yarn to use. If not specified, defaults to the latest version.
- __Required__: No
Expand All @@ -27,15 +57,18 @@ The action accepts the following inputs:
To use the "Lint & Test" action in your workflow, include it as a step:

```yaml
- name: Lint and Test
uses: generalui/github-workflow-accelerators/.github/actions/lint-and-test@1.0.0-lint-and-test
- name: Lint & Test
uses: generalui/github-workflow-accelerators/.github/actions/lint-test-yarn@1.0.1-lint-test-yarn
with:
node-version: '14' # Optional: Specify Node.js version
yarn-version: '1.22' # Optional: Specify Yarn version
node-version: '23.0.0' # Optional: Specify Node.js version
yarn-version: '3.8.5' # Optional: Specify Yarn version
```
## Workflow Steps
1) Get Branch:
- If `branch` is not specified, defaults to the current branch.

1) Checkout Code:
- Checks out your code from the repository to ensure the latest version is used for linting and testing.

Expand All @@ -51,15 +84,29 @@ To use the "Lint & Test" action in your workflow, include it as a step:
1) Lint Project:
- Runs the linting script defined in your package.json, ensuring your code adheres to defined coding standards.

1) Run Before Tests:
- Runs the command specified in the `run-before-tests` input.

1) Run Unit Tests:
- Executes unit tests using the test script from your package.json.
The --passWithNoTests flag ensures that the workflow doesn't fail if no tests are defined.

1) Get Coverage:
- If `upload-coverage` is true, this step will create a directory with the branch name, and copy the coverage data into it.

1) Upload Coverage:
- If `upload-coverage` is true, this step will upload the coverage data as an artifact.

1) Default Job Success:
- If `should-run-tests` and `should-run-lint` are not true, this step will exit successfully.

## Notes

- The action is versatile and can be adapted to various Node.js and Yarn-based projects.
- It ensures that both the linting and testing stages must pass before a merge or deployment, enhancing code reliability.
- The action uses the --frozen-lockfile flag with yarn install to prevent accidental updates to the lockfile, ensuring consistency across installations.
- The action uses the --immutable flag with yarn install to prevent accidental updates to the lockfile, ensuring consistency across installations.
- The action assumes that the test command is `yarn test` if `upload-coverage` is false.
- The action assumes that the test command is `yarn test:coverage` if `upload-coverage` is true.

## Integration

Expand Down
84 changes: 82 additions & 2 deletions .github/actions/lint-test-yarn/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,54 @@ name: Lint & Test
description: Lint & Test

inputs:
branch:
default: ''
description: The branch that is being tested. If not specified, defaults to the current branch.
checkout-code:
default: 'yes'
description: Whether or not to checkout the code.
node-version:
default: latest
description: The version of Nodejs to use (defaults to "latest")
run-before-tests:
default: ''
description: A shell command to run before tests.
should-run-lint:
default: 'yes'
description: Whether or not to lint (defaults to "yes"). Anything other than "yes" will skip linting.
should-run-tests:
default: 'yes'
description: Whether or not to run tests (defaults to "yes"). Anything other than "yes" will skip tests.
upload-coverage:
default: 'no'
description: Whether or not to upload coverage as an artifact. Anything other than "yes" will skip uploading. Assumes that the test command is `yarn test:coverage` if true.
yarn-version:
default: latest
description: The version of Yarn to use (defaults to "latest")

runs:
using: composite
steps:
- name: Get branch
if: inputs.should-run-tests == 'yes' && inputs.upload-coverage == 'yes'
run: |
branch=""
if [[ "${{ inputs.branch }}" == "" ]]; then
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
else
branch=${{ inputs.branch }}
fi
echo "branch="${branch} >> $GITHUB_OUTPUT
echo "::group::Branch"
echo 'Branch: '${branch}
echo "::endgroup::"
shell: bash
id: branch

- name: Checkout code
if: inputs.checkout-code == 'yes'
uses: actions/checkout@v4
with:
fetch-depth: 0
Expand Down Expand Up @@ -48,9 +85,52 @@ runs:
shell: bash

- name: Lint project
if: inputs.should-run-lint == 'yes'
run: yarn lint
shell: bash

- name: Run Before Tests
if: inputs.should-run-tests == 'yes' && inputs.run-before-tests != ''
run: |
bash -c "${{ inputs.run-before-tests }}"
shell: bash

- name: Run unit tests
run: yarn test --passWithNoTests
shell: bash
if: inputs.should-run-tests == 'yes'
run: |
if [ "${{ inputs.upload-coverage }}" = "yes" ]; then
yarn test:coverage --passWithNoTests
else
yarn test --passWithNoTests
fi
shell: bash

- name: Get the coverage file.
if: inputs.should-run-tests == 'yes' && inputs.upload-coverage == 'yes'
run: |
coverage_branch=${${{ steps.branch.outputs.branch }}//+([\"\:<>|\*\?\\\/])/-}
coverage_dir=coverage-${coverage_branch}
mkdir -p ${coverage_dir} && sudo cp -r coverage ${coverage_dir}
echo "coverage_branch=${coverage_branch}" >> $GITHUB_OUTPUT
echo "coverage_dir=${coverage_dir}" >> $GITHUB_OUTPUT
echo "::group::Coverage Data"
echo 'branch: '${coverage_branch}
echo 'dir: '${coverage_dir}
echo "::endgroup::"
shell: bash
id: coverage

- name: Upload the Coverage as an artifact
if: inputs.should-run-tests == 'yes' && inputs.upload-coverage == 'yes'
uses: actions/upload-artifact@v4
with:
name: ${{ steps.coverage.outputs.coverage_branch }}-test-coverage
path: ${{ steps.coverage.outputs.coverage_dir }}
retention-days: ${{ inputs.retention-days }}

- name: Default Job Success
if: inputs.should-run-tests != 'yes' && inputs.should-run-lint != 'yes'
run: exit 0
shell: bash
2 changes: 1 addition & 1 deletion .github/actions/lint-test-yarn/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "lint-test-yarn",
"version": "1.0.0"
"version": "1.0.1"
}
17 changes: 17 additions & 0 deletions LINTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Linting and Formatting

## Requirements

- [Markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) `DavidAnson.vscode-markdownlint`

- Markdownlint must be installed as an extension for local markdown linting to work within VS Code or Cursor on save.
- Or run in directly using [markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2).

```sh
markdownlint-cli2 "**/*.md"
```

## Configuration

MarkdownLint uses [`.markdownlint.json`](./.markdownlint.json) to configure the markdown linting rules and
[`.markdownlintignore`](./.markdownlintignore) to ignore linting for specific files and paths.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ This repo hold various accelerators for Github workflows (Actions) as well as re
- [Update AWS ECS](./.github/actions/update-aws-ecs/README.md)
- [Update AWS Lambda](./.github/actions/update-aws-lambda/README.md)
- [Validate Terraform](./.github/actions/validate-terraform/README.md)

## Contributing

### Linting and Formatting

See [LINTING.md](./LINTING.md) for more information.

0 comments on commit 3f1459a

Please sign in to comment.