Skip to content

Commit c49ca70

Browse files
authored
Mostly finish pytest to Bats conversion (#1200)
* Mostly finish pytest to Bats conversion * Add Bats version note to testing docs * Move Bats CI check to separate job
1 parent 18ecffe commit c49ca70

File tree

15 files changed

+409
-31
lines changed

15 files changed

+409
-31
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ jobs:
6565
grep -v -e 'History\.md' -e 'AUTHORS' -e 'man/.*\.1' -e 'man/.*\.html' | \
6666
xargs poetry run codespell --ignore-words=../.github/.ignore_words
6767
68-
test:
68+
test-pytest:
69+
name: 'Test with Pytest'
6970
runs-on: ubuntu-latest
70-
7171
steps:
7272
- uses: actions/checkout@v4
7373
with:
@@ -85,13 +85,23 @@ jobs:
8585
run: |
8686
cd tests || exit
8787
poetry install --only test
88-
- name: Setup Bats
89-
id: setup-bats
90-
uses: bats-core/bats-action@3.0.0
9188
- name: Test with Pytest
9289
run: |
9390
cd tests
9491
poetry run pytest
92+
93+
test-bats:
94+
name: 'Test with Bats'
95+
runs-on: ubuntu-latest
96+
steps:
97+
- uses: actions/checkout@v4
98+
with:
99+
submodules: recursive
100+
- name: Setup Bats
101+
id: setup-bats
102+
uses: bats-core/bats-action@3.0.0
103+
with:
104+
bats-version: 'v1.8.1'
95105
- name: Test with Bats
96106
env:
97107
BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.venv/
22
__pycache__/
3+
coverage/

tests/README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
# Test for git-extras
2-
The git-extras has its own testcases now, and the more is on the way! So let's introduce it.
1+
# Testing
32

4-
We choose python to help us to reach to other shore cause **python is life saver**.
3+
Originally, the tests were written in pytest. However, tests are in the process of being converted to Bats so coverage can be calculated.
4+
5+
## Bats Testing
6+
7+
We require a somewhat recent version of Bats. Version v1.8.1 is tested in CI. Once it is installed, the tests can be executed like so:
8+
9+
```sh
10+
bats ./tests
11+
```
12+
13+
We highly recommend adding tests for new features and fixes.
14+
15+
### Code Coverage
16+
17+
Coverage can be calculated with [bashcov](https://github.com/infertux/bashcov) like so:
18+
19+
```sh
20+
bashcov -- bats ./tests
21+
```
22+
23+
By default, the report will be generated in `./coverage/index.html`.
24+
25+
## Python Testing
526

627
The test part depends on:
728

@@ -12,7 +33,8 @@ The test part depends on:
1233

1334
So the versions are higher than above is recommended.
1435

15-
# How to run the tests
36+
### How to run the tests
37+
1638
1. Install `poetry`
1739
2. Install the dependencies via `poetry install`
1840
3. Run `poetry run pytest`
@@ -27,7 +49,8 @@ It is done or go without `poetry`,
2749

2850
The second way maybe blocked the some missing dependencies at someday, so the first one is recommended.
2951

30-
# What and how to create a unit test
52+
### What and how to create a unit test
53+
3154
One command has a unit test, because one `git-*` command is just do one thing, so we can eat a piece of `git-*` command in one time.
3255

3356
For example,
@@ -39,7 +62,8 @@ For example,
3962
* `named_temp_repo` is just same as `temp_repo` except the custom directory renaming.
4063
4. Loop the third step until the 100% coverage of the function of the `git-alias`
4164

42-
# References
65+
### References
66+
4367
* [poetry](https://github.com/python-poetry/poetry)
4468
* [pytest](https://github.com/pytest-dev/pytest/)
4569
* [git python](https://github.com/gitpython-developers/GitPython)

tests/bin/open

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
printf '%s\n' "open $*"

tests/bin/powershell.exe

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
printf '%s\n' "powershell.exe $*"

tests/bin/start

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
printf '%s\n' "start $*"

tests/bin/xdg-open

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
printf '%s\n' "xdg-open $*"

tests/git-abort.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ setup_file() {
99
setup() {
1010
test_util.cd_test
1111

12-
git init
12+
test_util.git_init
1313
git commit --allow-empty -m "Initial commit"
1414
git branch A
1515
git branch B
@@ -24,7 +24,7 @@ setup() {
2424
git status
2525
}
2626

27-
@test "cherry pick" {
27+
@test "works with cherry pick" {
2828
run git cherry-pick A
2929
assert_failure
3030

@@ -41,7 +41,7 @@ setup() {
4141
assert_success
4242
}
4343

44-
@test "merge" {
44+
@test "works with merge" {
4545
run git merge A
4646
assert_failure
4747

@@ -58,7 +58,7 @@ setup() {
5858
assert_success
5959
}
6060

61-
@test "rebase" {
61+
@test "works with rebase" {
6262
run git rebase A
6363
assert_failure
6464

@@ -75,7 +75,7 @@ setup() {
7575
assert_success
7676
}
7777

78-
@test "revert" {
78+
@test "works with revert" {
7979
run git revert A
8080
assert_failure
8181

tests/git-alias.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ setup_file() {
99
setup() {
1010
test_util.cd_test
1111

12-
git init
12+
test_util.git_init
1313
git config --global alias.globalalias status
1414
git config --global alias.x status
1515
git config --local alias.localalias status

tests/git-archive-file.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ setup_file() {
99
setup() {
1010
test_util.cd_test
1111

12-
git init
12+
test_util.git_init
1313
printf '%s\n' 'data' > tmpfile
1414
git add .
1515
git commit -m 'test: add data'

0 commit comments

Comments
 (0)