Skip to content
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
20 changes: 15 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ jobs:
grep -v -e 'History\.md' -e 'AUTHORS' -e 'man/.*\.1' -e 'man/.*\.html' | \
xargs poetry run codespell --ignore-words=../.github/.ignore_words

test:
test-pytest:
name: 'Test with Pytest'
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
Expand All @@ -85,13 +85,23 @@ jobs:
run: |
cd tests || exit
poetry install --only test
- name: Setup Bats
id: setup-bats
uses: bats-core/bats-action@3.0.0
- name: Test with Pytest
run: |
cd tests
poetry run pytest

test-bats:
name: 'Test with Bats'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Setup Bats
id: setup-bats
uses: bats-core/bats-action@3.0.0
with:
bats-version: 'v1.8.1'
- name: Test with Bats
env:
BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.venv/
__pycache__/
coverage/
36 changes: 30 additions & 6 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
# Test for git-extras
The git-extras has its own testcases now, and the more is on the way! So let's introduce it.
# Testing

We choose python to help us to reach to other shore cause **python is life saver**.
Originally, the tests were written in pytest. However, tests are in the process of being converted to Bats so coverage can be calculated.

## Bats Testing

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:

```sh
bats ./tests
```

We highly recommend adding tests for new features and fixes.

### Code Coverage

Coverage can be calculated with [bashcov](https://github.com/infertux/bashcov) like so:

```sh
bashcov -- bats ./tests
```

By default, the report will be generated in `./coverage/index.html`.

## Python Testing

The test part depends on:

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

So the versions are higher than above is recommended.

# How to run the tests
### How to run the tests

1. Install `poetry`
2. Install the dependencies via `poetry install`
3. Run `poetry run pytest`
Expand All @@ -27,7 +49,8 @@ It is done or go without `poetry`,

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

# What and how to create a unit test
### What and how to create a unit test

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.

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

# References
### References

* [poetry](https://github.com/python-poetry/poetry)
* [pytest](https://github.com/pytest-dev/pytest/)
* [git python](https://github.com/gitpython-developers/GitPython)
2 changes: 2 additions & 0 deletions tests/bin/open
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
printf '%s\n' "open $*"
2 changes: 2 additions & 0 deletions tests/bin/powershell.exe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
printf '%s\n' "powershell.exe $*"
2 changes: 2 additions & 0 deletions tests/bin/start
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
printf '%s\n' "start $*"
2 changes: 2 additions & 0 deletions tests/bin/xdg-open
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
printf '%s\n' "xdg-open $*"
10 changes: 5 additions & 5 deletions tests/git-abort.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ setup_file() {
setup() {
test_util.cd_test

git init
test_util.git_init
git commit --allow-empty -m "Initial commit"
git branch A
git branch B
Expand All @@ -24,7 +24,7 @@ setup() {
git status
}

@test "cherry pick" {
@test "works with cherry pick" {
run git cherry-pick A
assert_failure

Expand All @@ -41,7 +41,7 @@ setup() {
assert_success
}

@test "merge" {
@test "works with merge" {
run git merge A
assert_failure

Expand All @@ -58,7 +58,7 @@ setup() {
assert_success
}

@test "rebase" {
@test "works with rebase" {
run git rebase A
assert_failure

Expand All @@ -75,7 +75,7 @@ setup() {
assert_success
}

@test "revert" {
@test "works with revert" {
run git revert A
assert_failure

Expand Down
2 changes: 1 addition & 1 deletion tests/git-alias.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ setup_file() {
setup() {
test_util.cd_test

git init
test_util.git_init
git config --global alias.globalalias status
git config --global alias.x status
git config --local alias.localalias status
Expand Down
2 changes: 1 addition & 1 deletion tests/git-archive-file.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ setup_file() {
setup() {
test_util.cd_test

git init
test_util.git_init
printf '%s\n' 'data' > tmpfile
git add .
git commit -m 'test: add data'
Expand Down
15 changes: 10 additions & 5 deletions tests/git-authors.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ setup_file() {
setup() {
test_util.cd_test

git init
GIT_CONFIG_VALUE_0='test@example.com'
GIT_CONFIG_VALUE_1='test'
test_util.git_init

git config user.name 'test'
git config user.email 'test@example.com'
printf '%s\n' 'A' > tmpfile
git add .
git commit -m 'test: add data A'
GIT_CONFIG_VALUE_0='testagain@example.com'
GIT_CONFIG_VALUE_1='testagain'

git config user.name 'testagain'
git config user.email 'testagain@example.com'
printf '%s\n' 'B' > tmpfile
git add .
git commit -m 'test: add data B'

# git config unset user.name
# git config unset user.email
}

@test "output authors has email without any parameter" {
Expand Down
117 changes: 117 additions & 0 deletions tests/git-browse-ci.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# shellcheck shell=bash

source "$BATS_TEST_DIRNAME/test_util.sh"

setup_file() {
test_util.setup_file

PATH="$BATS_TEST_DIRNAME/bin:$PATH"
}

setup() {
test_util.cd_test

test_util.git_init
}

get_ci_uri() {
local mode=$1

if [ "$mode" = 'github' ]; then
REPLY="https://github.com/tj/git-extras/actions"
elif [ "$mode" = 'gitlab' ]; then
REPLY="https://gitlab.com/tj/git-extras/-/pipelines"
elif [ "$mode" = 'bitbucket' ]; then
REPLY="https://bitbucket.org/tj/git-extras/addon/pipelines/home"
fi
}

@test "works with mac and github" {
get_ci_uri 'github'
local expected_url=$REPLY

git remote add upstream https://github.com/tj/git-extras
OSTYPE=darwin run git browse-ci upstream
assert_output "open $expected_url"
assert_success
}

@test "works with mac and gitlab" {
get_ci_uri 'gitlab'
local expected_url=$REPLY

git remote add upstream https://gitlab.com/tj/git-extras
OSTYPE=darwin run git browse-ci upstream
assert_output "open $expected_url"
assert_success
}

@test "works with mac and bitbucket" {
get_ci_uri 'bitbucket'
local expected_url=$REPLY

git remote add upstream https://bitbucket.org/tj/git-extras
OSTYPE=darwin run git browse-ci upstream
assert_output "open $expected_url"
assert_success
}

@test "works with windows and github" {
get_ci_uri 'github'
local expected_url=$REPLY

git remote add upstream https://github.com/tj/git-extras
OSTYPE=msys run git browse-ci upstream
assert_output "start $expected_url"
assert_success
}

@test "works with windows and gitlab" {
get_ci_uri 'gitlab'
local expected_url=$REPLY

git remote add upstream https://gitlab.com/tj/git-extras
OSTYPE=msys run git browse-ci upstream
assert_output "start $expected_url"
assert_success
}

@test "works with windows and bitbucket" {
get_ci_uri 'bitbucket'
local expected_url=$REPLY

git remote add upstream https://bitbucket.org/tj/git-extras
OSTYPE=msys run git browse-ci upstream
assert_output "start $expected_url"
assert_success
}

@test "works with linux and github" {
get_ci_uri 'github'
local expected_url=$REPLY

git remote add upstream https://github.com/tj/git-extras
OSTYPE=linux-gnu run git browse-ci upstream
assert_output "xdg-open $expected_url"
assert_success
}

@test "works with linux and gitlab" {
get_ci_uri 'gitlab'
local expected_url=$REPLY

git remote add upstream https://gitlab.com/tj/git-extras
OSTYPE=linux-gnu run git browse-ci upstream
assert_output "xdg-open $expected_url"
assert_success
}

@test "works with linux and bitbucket" {
get_ci_uri 'bitbucket'
local expected_url=$REPLY

git remote add upstream https://bitbucket.org/tj/git-extras
OSTYPE=linux-gnu run git browse-ci upstream
assert_output "xdg-open $expected_url"
assert_success
}
Loading
Loading