Skip to content

Commit

Permalink
Merge branch 'check-edit-workspace'
Browse files Browse the repository at this point in the history
* Branch commit log:
  testing.sh: remove testing.sh, superseded by tests/basics.sh
  tests/basics.sh: add test-functions-fail-early
	* Rewrite test-functions-fail-early from testing.sh
  tests/basics.sh: test-edit-workspace: add `jj-fzf new` test
  Makefile: check: run tests/basics.sh
  tests/basics.sh: add test-edit-workspace
  tests/utils.sh: add mkcommit, jj_*, assert* and more test utilities

Signed-off-by: Tim Janik <[email protected]>
  • Loading branch information
tim-janik committed Dec 9, 2024
2 parents 93c71d8 + e2ec649 commit 7a7b706
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 115 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ shellcheck-warning: jj-fzf
shellcheck-error:
$Q shellcheck --version | grep -q 'script analysis' || { echo "$@: missing GNU shellcheck"; false; }
shellcheck -W 3 -S error jj-fzf
test: jj-fzf
$Q ./testing.sh
check: jj-fzf shellcheck-error test
tests-basics.sh:
$Q tests/basics.sh
check: jj-fzf shellcheck-error tests-basics.sh
112 changes: 0 additions & 112 deletions testing.sh

This file was deleted.

50 changes: 50 additions & 0 deletions tests/basics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
# This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
set -Eeuo pipefail #-x
SCRIPTNAME="${0##*/}" && SCRIPTDIR="$(readlink -f "$0")" && SCRIPTDIR="${SCRIPTDIR%/*}"

source $SCRIPTDIR/utils.sh

# == TESTS ==
test-functions-fail-early()
(
cd_new_repo
# Check `jj-fzf describe` does not continue with $EDITOR
# once an invalid change_id has been encountered.
export JJ_CONFIG='' EDITOR='echo ERRORINERROR'
OUT="$(set +x; jj-fzf describe 'zzzzaaaa' 2>&1)" && E=$? || E=$?
assert_nonzero $E
assert1error "$OUT"
! grep -Eq 'ERRORINERROR' <<<"$OUT" ||
die "${FUNCNAME[0]}: detected nested invocation, output:"$'\n'"$(echo "$OUT" | sed 's/^/> /')"
)
TESTS+=( test-functions-fail-early )

test-edit-workspace()
(
cd_new_repo
mkcommits 'Ia' 'Ib' 'Ia ->Ic' 'Ib|Ic ->Id'
assert_commit_count $((2 + 4))
git tag IMMUTABLE `get_commit_id Id` && jj_status
assert_commit_count $((2 + 5))
mkcommits A B 'A ->C' 'B|C ->D'
assert_commit_count $((2 + 5 + 4))
jj-fzf edit-workspace 'C' >$DEVERR 2>&1
assert_commit_count $((2 + 5 + 4))
assert_@ `get_commit_id C` && assert_@- `get_commit_id A`
jj-fzf edit-workspace 'Ic' >$DEVERR 2>&1
assert_commit_count $((2 + 5 + 4 + 1))
assert_@- `get_commit_id Ic`
jj-fzf new '@' >$DEVERR 2>&1
assert_commit_count $((2 + 5 + 4 + 1 + 1))
assert_commits_eq @-- `get_commit_id Ic`
)
TESTS+=( test-edit-workspace )

# == RUN ==
temp_dir
for TEST in "${TESTS[@]}" ; do
$TEST
printf ' %-7s %s\n' OK "$TEST"
done
tear_down
168 changes: 168 additions & 0 deletions tests/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# == VARIABLE Setup ==
export JJ_FZF_ERROR_DELAY=0 # instant errors for testing
TEMPD=

# == OPTIONS ==
DEVERR=/dev/null
[[ " $* " =~ -x ]] && {
PS4="+ \${BASH_SOURCE[0]##*/}:\${LINENO}: "
DEVERR=/dev/stderr
set -x
}
[[ " $* " =~ -v ]] &&
DEVERR=/dev/stderr

# == Utils ==
die()
{
local R=$'\033[31m' Z=$'\033[0m'
[ -n "$*" ] &&
echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:${FUNCNAME[1]}: $R**ERROR**:$Z ${*:-aborting}" >&2;
exit 127
}
die-() # die, using *caller* as origin
{
local R=$'\033[31m' Z=$'\033[0m'
[ -n "$*" ] &&
echo "${BASH_SOURCE[2]}:${BASH_LINENO[1]}:${FUNCNAME[2]}: $R**ERROR**:$Z ${*:-aborting}" >&2;
exit 127
}
temp_dir()
{
test -n "$TEMPD" || {
TEMPD="`mktemp --tmpdir -d jjfzf0XXXXXX`" || die "mktemp failed"
trap "rm -rf '$TEMPD'" 0 HUP INT QUIT TRAP USR1 PIPE TERM
echo "$$" > $TEMPD/jjfzf-tests.pid
}
}

# == Repository ==
tear_down()
(
REPO="${1:-repo}"
test -n "$TEMPD" &&
rm -rf $TEMPD/$REPO
)
clear_repo()
(
REPO="${1:-repo}"
test -n "$TEMPD" || die "missing TEMPD"
cd $TEMPD/
rm -rf $TEMPD/$REPO
mkdir $TEMPD/$REPO
cd $TEMPD/$REPO
git init >$DEVERR 2>&1
jj git init --colocate >$DEVERR 2>&1
echo "$PWD"
)
cd_new_repo()
{
RP=$(clear_repo "$@")
cd "$RP"
}
mkcommits()
( # Create empty test commits with bookamrks
while test $# -ne 0 ; do
P=@ && [[ "$1" =~ (.+)-\>(.+) ]] &&
P="${BASH_REMATCH[1]}" C="${BASH_REMATCH[2]}" || C="$1"
shift
jj --no-pager new -m="$C" -r all:"$P"
jj bookmark set -r @ "$C"
done >$DEVERR 2>&1 # mkcommits A B 'A|B ->C'
)
get_commit_id()
(
REF="$1"
COMMIT_ID=$(jj --ignore-working-copy log --no-graph -T commit_id -r "description(exact:\"$REF\n\")" 2>/dev/null) &&
test -n "$COMMIT_ID" ||
COMMIT_ID=$(jj --ignore-working-copy log --no-graph -T commit_id -r "$REF") || exit
echo "$COMMIT_ID"
)
get_change_id()
(
COMMIT_ID=$(get_commit_id "$@")
UNIQUECHANGE='if(self.divergent(), "", change_id)'
# only allow non-divergent: https://martinvonz.github.io/jj/latest/FAQ/#how-do-i-deal-with-divergent-changes-after-the-change-id
CHANGE_ID=$(jj --ignore-working-copy log --no-graph -T "$UNIQUECHANGE" -r " $COMMIT_ID ") || exit
echo "$CHANGE_ID"
)
commit_count()
(
R="${1:-::}"
jj --ignore-working-copy log --no-graph -T '"\n"' -r "$R" | wc -l
)
jj_log_oneline()
(
jj --ignore-working-copy log -T builtin_log_oneline -r ::
)
jj_status()
(
jj status >$DEVERR 2>&1
)

# == Assertions ==
assert_commit_count()
(
V="$1"
C="$(commit_count "${2:-::}")"
test "$C" -eq "$V" ||
die- "assert_commit_count: mismatch: $C == $V"
)
assert_@()
(
V="$1"
C="$(get_change_id '@')"
test "$C" == "$V" && return
C="$(get_commit_id '@')"
test "$C" == "$V" && return
die- "assert_@: mismatch: $C == $V"
)
assert_@-()
(
V="$1"
C="$(get_change_id '@-')"
test "$C" == "$V" && return
C="$(get_commit_id '@-')"
test "$C" == "$V" && return
die- "assert_@-: mismatch: $C == $V"
)
assert_commits_eq()
(
U="$1"
V="$2"
C="$(get_commit_id "$U")"
D="$(get_commit_id "$V")"
test "$C" == "$D" ||
die- "assert_commits_eq: mismatch: $C == $D"
)
assert_nonzero()
{
V="$1"
test 0 != "$V" ||
die- "assert_nonzero: mismatch: 0 != $V"
}
assert_zero()
{
V="$1"
test 0 == "$V" ||
die- "assert_zero: mismatch: 0 == $V"
}
assert0error()
{
! grep -Eq '\bERROR:' <<<"$*" ||
die- "assert0error: unexpected ERROR message: $*"
}
assert1error()
{
grep -Eq '\bERROR:' <<<"$*" ||
die- "assert1error: missing mandatory ERROR message: $*"
}

# == Errors ==
bash_error()
{
local code="$?" D=$'\033[2m' Z=$'\033[0m'
echo "$D${BASH_SOURCE[1]}:${BASH_LINENO[0]}:${FUNCNAME[1]}:trap: exit status: $code$Z" >&2
exit "$code"
}
trap 'bash_error' ERR

0 comments on commit 7a7b706

Please sign in to comment.