Skip to content
Open
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
70 changes: 46 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

`git-test` is a command-line script for running automated tests against commits in a Git repository. It is especially targeted at developers who like their tests to pass on *every* commit in a branch, not just the branch tip.

The best way to use `git test` is to keep a window open in a second linked worktree of your repository, and as often as you like run
The best way to use `git test` is to keep a window open in a second linked
[worktree](#best-practice-use-git-test-in-a-linked-worktree) of your repository,
and as often as you like run

git test run master..mybranch

`git test` will test the commits in the specified range, reporting any failures. The pass/fail results of running tests are also recorded permanently in your repository as Git "notes" (see `git-notes(1)`).
`git test` will test the commits in the specified range, reporting any failures.
The pass/fail results of running tests are also recorded permanently in your
repository as Git "notes" (see [git-notes(1)](https://www.git-scm.com/docs/git-notes)).

If a commit in the range has already been tested, then by default `git test` reports the old results rather than testing it again. This means that you can run the above command over and over as you work, and `git test` won't repeat tests whose results it already knows. (Of course there are options to allow you to request explicitly that commits be retested.)

The test results are recorded by the *tree* that was tested, not the commit, so old test results remain valid even across some kinds of commit rewriting:

* If commits are rewritten to change their log messages, authorship, dates, etc., the test results remain valid.
* If consecutive commits are squashed, the results remain valid.
* If a commit is split into two, only the first (partial) commit needs to be tested.
* If some commits deep in a branch are reordered, the test results for commits built on top of the reordered commits often remain valid.
Comment on lines -15 to -18
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally it's not welcome to change the formatting of documents in an existing project. The four-space indentation has advantages that I'd be happy to tell you about if you're curious, and surely two-space indentation does too. I don't think either one is more "correct", is it?

If that's true, I'd prefer for the whole first commit to be dropped, except for the addition of angle brackets around the URL (which is a correctness issue). But if this is too much trouble, I won't insist on it.

* If commits are rewritten to change their log messages, authorship, dates, etc., the test results remain valid.
* If consecutive commits are squashed, the results remain valid.
* If a commit is split into two, only the first (partial) commit needs to be tested.
* If some commits deep in a branch are reordered, the test results for commits built on top of the reordered commits often remain valid.

Of course this means that your tests should not depend on things besides the files in the tree. For example, whether your test passes/fails should *not* depend on the current branch name or commit message.


## Usage

### Defining tests
Expand Down Expand Up @@ -65,7 +68,11 @@ You can define multiple tests in a single repository (e.g., cheap vs. expensive

If you have flaky tests that occasionally fail for bogus reasons, you might want to re-run the test against a commit even though `git test` has already recorded a result for that commit. To do so, run `git test run` with the `--force`/`-f` or `--retest` options.

If you want to forget particular old test results without retesting, run `git test run` with the `--forget` option.
If you want to forget particular old test results without retesting, run
`git test run` with the `--forget` option, e.g.

git test run [--test=<name>] --forget commit1
git test run [--test=<name>] --forget commit1..commit2

If you want to permanently forget *all* stored results for a particular test (e.g., if something in your environment has changed), run

Expand Down Expand Up @@ -112,7 +119,6 @@ The last command can be re-run any time; it only does significant work when some

Because linked worktrees share branches and the git configuration with the main repository, test definitions and test results are visible across all worktrees. So you could even run multiple tests at the same time in multiple linked worktrees.


## Installation

Requirements:
Expand All @@ -122,43 +128,43 @@ Requirements:

Just put `bin/git-test` somewhere in your `$PATH`, adjusting its first line if necessary to invoke the desired Python interpreter properly in your environment.


## Ideas for future enhancements

Some other features that would be nice:

* Be more consistent about restoring `HEAD`. `git test run` currently checks out the branch that you started on when it is finished, but only if all of the tests passed. We need some kind of `git test reset` command analogous to `git bisect reset`.

* `git test bisect`: run `git bisect run` against a range of commits, using a configured test as the command that `bisect` uses to decide whether a commit is good/bad.
* Be more consistent about restoring `HEAD`. `git test run` currently checks out the branch that you started on when it is finished, but only if all of the tests passed. We need some kind of `git test reset` command analogous to `git bisect reset`.

* `git test prune`: delete notes for obsolete trees.
* `git test bisect`: run `git bisect run` against a range of commits, using a configured test as the command that `bisect` uses to decide whether a commit is good/bad.

* Continuous testing mode, where `git test` watches the repository for changes and re-runs itself automatically whenever the commits it is watching change.
* `git test prune`: delete notes for obsolete trees.

* Dependencies between tests; for example:
* Continuous testing mode, where `git test` watches the repository for changes and re-runs itself automatically whenever the commits it is watching change.
For now you can use the [run-continously.sh](run-continously.sh) script.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another spelling error here ☝️


* Provide a way to say "if my `full` test passes, that implies that the `build` test would also pass".
* Dependencies between tests; for example:

* Provide a way to run the `build` test (and record the `build` test's results) as the first step of the `full` test.
* Provide a way to say "if my `full` test passes, that implies that the `build` test would also pass".

* Allow trees to be marked `skip`, if they shouldn't be tested (e.g., due to a known breakage). Perhaps allow the test script to emit a special return code to ask that the commit be marked `skip` (probably following the convention of `git bisect run`).
* Provide a way to run the `build` test (and record the `build` test's results) as the first step of the `full` test.

* Remember return codes and give them back out if the old result is reused.
* Allow trees to be marked `skip`, if they shouldn't be tested (e.g., due to a known breakage). Perhaps allow the test script to emit a special return code to ask that the commit be marked `skip` (probably following the convention of `git bisect run`).

* Add a `git test fix <range>`, which starts an interactive rebase, changing the command for the first broken commit from "pick" to "edit".
* Remember return codes and give them back out if the old result is reused.

* Support tests that depend on the *commit*, not the *tree*, that they are run against.
* Add a `git test fix <range>`, which starts an interactive rebase, changing the command for the first broken commit from "pick" to "edit".

* Support tests that depend on the *commit*, not the *tree*, that they are run against.

## License

`git test` is released under the GPLv2+ license. Pull requests are welcome at the project's GitHub page, https://github.com/mhagger/git-test

`git test` is released under the GPLv2+ license. Pull requests are welcome at the project's GitHub page, <https://github.com/mhagger/git-test>.

## Caveats and disclaimers

`git test` has pretty good automated tests, but it undoubtedly still has bugs and rough edges. Use it at your own risk.

### Detached head

Please note that when you tell `git test run` to test specified commits, it checks those commits out in your working directory. If the tests fail, it leaves the failing commit checked out *in a detached HEAD state*. This is intentional, so that you can examine the cause of the failure. But it means that if you had changes on your original HEAD that weren't part of any branch, they will now be unreachable.

If you don't know what a detached HEAD state is, please read up on it. Additionally, **it is recommended that you run `git test` in a separate worktree**, which is more convenient anyway (see above for instructions). Note that the `git worktree` command was added in Git release 2.5, so make sure you are using that version of Git or (preferably) newer.
Expand All @@ -172,3 +178,19 @@ and
git test run HEAD

don't change the commit that is checked out, and they won't change your working copy to a detached HEAD state.

### Test result "noise" when viewing history with `--all`

Notice that as a side effect of `git test` saving test results in git notes,
these notes will become visible "noise" when viewing history with `git log` or
`gitk` and using the version specifier `--all`. The solution is to tell git
to ignore those notes with `--exclude` (NB, must be specified *before* `--all`
in order to have effect).

Writing the full exclude phrase every time will probably be too cumbersome, so
you most likely want to write wrapper scripts like the following

#!/bin/sh
exec gitk --exclude=refs/notes/* --all "$@" &

or include `--exclude` in your git aliases.
57 changes: 57 additions & 0 deletions run-continously.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

if [[ $# -lt 1 ]]
then
echo "Usage: $0 [-t name] <commit|range>" 1>&2
exit 1
fi

get_main_worktree() {
# Example output:

# worktree /home/user/src/git-test
# HEAD e42d298cff233471c47b50f0f44bb05867109a24
# branch refs/heads/main
#
# worktree /home/user/src/worktree.test.git-test
# HEAD 1c0c32641de4a00fa6f5fbd4ddcb478bf5a6ca3f
# detached

# Main worktree is always listed first.
git worktree list --porcelain | sed -n '1s/^worktree //p'
}

wait_for_next_run() {
if type -P inotifywait >/dev/null
then
gitdir=$(git rev-parse --git-dir)
case $gitdir
in
*.git/worktrees*)
# Inside a linked worktree so use .git dir for main worktree for monitoring.
gitdir=$(cd $(get_main_worktree); git rev-parse --path-format=absolute --git-dir)
;;
*)
;;
esac
# We need delete_self to pick up changes to HEAD (since it gets renamed
# over), and "move" to pick up changes in the refs directories.
inotifywait -qq -e delete_self -e move -r "$gitdir/HEAD" "$gitdir/refs"
else
n=0
while [[ $n -lt ${GIT_TEST_CONTINOUS_INTERVAL:-30} ]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a spelling error in several places in this PR: s/continously/continuously/. It's misspelled:

  • in the name of the script itself

  • in this environment variable name:

    Suggested change
    while [[ $n -lt ${GIT_TEST_CONTINOUS_INTERVAL:-30} ]]
    while [[ $n -lt ${GIT_TEST_CONTINUOUS_INTERVAL:-30} ]]
  • in the commit message for the commit that introduces this script

  • in the link and link text from README.md

(I won't insist on the commit message being fixed, but if you're rewriting history anyway you might want to fix it, too.)

do
n=$(( n + 1 ))
echo -n .
sleep 1
done
echo
fi
}

while true
do
git test run "$@"

wait_for_next_run
done