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

feat: coverage pull request #70

Merged
merged 12 commits into from
Apr 7, 2024
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ lib/
dist/
node_modules/
coverage/
submodules/
6 changes: 5 additions & 1 deletion .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
- name: Checkout
id: checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Setup Node.js
uses: actions/setup-node@v4
Expand All @@ -51,7 +53,9 @@ jobs:
id: diff
run: |
if [ "$(git diff --ignore-space-at-eol --text dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
echo "Detected uncommitted changes after build."
echo "Make sure you have updated all submodules and have run 'npm run bundle'."
echo "See status below:"
git diff --ignore-space-at-eol --text dist/
exit 1
fi
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
- name: Checkout
id: checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Setup Node.js
id: setup-node
Expand Down Expand Up @@ -51,6 +53,8 @@ jobs:
- name: Checkout
id: checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Test Local Action
id: test-action
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
- name: Checkout
id: checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Initialize CodeQL
id: initialize
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "submodules/create-pull-request"]
path = submodules/create-pull-request
url = git@github.com:peter-evans/create-pull-request.git
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ play, both for your existing CI, or in a new project.
- Show off your coverage in your README or on your website, without signing up
to a third party service
- Generate badges on `push` events
- Can be configured to either push new badges to the current branch, or open a
PR with them.

## Get Started

Expand All @@ -61,10 +63,10 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
lower_threshold: 50
upper_threshold: 90
generate_badges: pr
```

> [!IMPORTANT]
> With this setup, this action will assume, that your projects have
> [!IMPORTANT] With this setup, this action will assume, that your projects have
> up-to-date coverage. This can be achieved by either running the tests like
> `flutter test --coverage` in the steps before this action, or always running
> them locally. Check the section below for more information about configuration
Expand Down Expand Up @@ -113,6 +115,7 @@ jobs:
testing_command: flutter test --coverage
lower_threshold: 50
upper_threshold: 90
generate_badges: pr
```

## Configuration
Expand All @@ -132,8 +135,11 @@ what they do.
- `enforce_forbidden_decrease` (optional, default: `'total'`): Whether the
action should fail if the coverage decreases. Can be set to "none", "single",
or "total".
- `generate_badges` (optional, default: `true`): Whether to generate badges for
the coverage on 'push' workflow triggers.
- `generate_badges` (optional, default: `none`): Whether to generate badges for
the coverage on "push" workflow triggers. Can be set to "push" (push new
badges, make sure your branch is not protected), "pr" (open a pull request
with the changes, make sure you allow Actions to open PRs on your Repo), or
"none" (no badge generation).

## Honorable mentions

Expand Down
58 changes: 58 additions & 0 deletions __tests__/git.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as exec from '@actions/exec'
import { getChanges, popStash } from '../src/git'

describe('popStash', () => {
it('should successfully pop the stash', async () => {
const mockExec = jest
.spyOn(exec, 'exec')
.mockImplementation((_, __, options) => {
return Promise.resolve(0)
})

const result = await popStash()

expect(mockExec).toHaveBeenCalledWith('git', ['stash', 'pop'])
expect(result).toBe(true)

mockExec.mockRestore()
})

it('should return false if popping the stash fails', async () => {
const mockExec = jest
.spyOn(exec, 'exec')
.mockImplementation((_, __, options) => {
return Promise.reject(new Error('Failed to pop stash'))
})

const result = await popStash()

expect(mockExec).toHaveBeenCalledWith('git', ['stash', 'pop'])
expect(result).toBe(false)

mockExec.mockRestore()
})
})

describe('getChanges', () => {
it('should return the list of changed files', async () => {
// Mock the exec function
const mockExec = jest
.spyOn(exec, 'exec')
.mockImplementation((_, __, options) => {
options?.listeners?.stdout?.(Buffer.from('file1.txt\nfile2.txt\n'))
return Promise.resolve(0)
})

const changes = await getChanges()

expect(mockExec).toHaveBeenCalledWith(
'git',
['diff', '--name-only'],
expect.any(Object)
)
expect(changes).toEqual('file1.txt\nfile2.txt\n')

// Restore the original exec function
mockExec.mockRestore()
})
})
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ inputs:
default: "none"
required: false
generate_badges:
description: "Whether to generate badges for the coverage on 'push' workflow triggers. Defaults to true."
default: "true"
description: 'Whether to generate badges for the coverage on "push" workflow triggers. Can be set to "push" (push new badges, make sure your branch is not protected), "pr" (open a pull request with the changes, make sure you allow Actions to open PRs on your Repo), or "none" (no badge generation). Defaults to none.'
default: "false"
required: false

# Define your outputs here.
Expand Down
Loading
Loading