Skip to content

Commit cfa6bc4

Browse files
committed
Add tests
1 parent bcc3c2c commit cfa6bc4

File tree

4 files changed

+274
-52
lines changed

4 files changed

+274
-52
lines changed

bin/antidote.sh

Lines changed: 105 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# shellcheck disable=SC3043
33

44
# Helpers
5-
die() { ERR=$1; shift; warn "$@"; exit "$ERR"; }
6-
say() { printf '%s\n' "$@"; }
7-
warn() { say "$@" >&2; }
8-
emit() { printf "${INDENT}%s\n" "$@"; }
9-
noop() { :; }
5+
die() { ERR=$1; shift; warn "$@"; exit "$ERR"; }
6+
say() { printf '%s\n' "$@"; }
7+
warn() { say "$@" >&2; }
8+
emit() { printf "${INDENT}%s\n" "$@"; }
9+
noop() { :; }
1010
is_cmd() { command -v "$1" >/dev/null 2>&1; }
1111
is_cmd local || alias local=noop
1212

@@ -15,8 +15,6 @@ is_cmd local || alias local=noop
1515
# TAB=$'\t'
1616
# SEP=$'\x1F'
1717
INDENT=
18-
LPAREN='('
19-
RPAREN=')'
2018

2119
script_fpath() {
2220
if [ "${O_FPATH_RULE:-append}" = append ]; then
@@ -64,47 +62,53 @@ temp_dir() {
6462

6563
antidote_help() {
6664
case "$1" in
67-
home)
68-
say "$ANTIDOTE_HOME_HELP"
69-
;;
70-
init)
71-
say "$ANTIDOTE_INIT_HELP"
72-
;;
73-
list)
74-
say "$ANTIDOTE_LIST_HELP"
75-
;;
76-
*)
77-
say "$ANTIDOTE_HELP"
78-
;;
65+
bundle) say "$ANTIDOTE_BUNDLE_HELP" ;;
66+
home) say "$ANTIDOTE_HOME_HELP" ;;
67+
init) say "$ANTIDOTE_INIT_HELP" ;;
68+
list) say "$ANTIDOTE_LIST_HELP" ;;
69+
path) say "$ANTIDOTE_PATH_HELP" ;;
70+
purge) say "$ANTIDOTE_PURGE_HELP" ;;
71+
update) say "$ANTIDOTE_UPDATE_HELP" ;;
72+
*) say "$ANTIDOTE_HELP" ;;
7973
esac
8074
}
8175

8276
antidote_bundle() {
8377
antidote_script "$@"
8478
}
8579

86-
antidote_update() {
87-
:
88-
}
89-
9080
antidote_home() {
9181
say "$ANTIDOTE_HOME"
9282
}
9383

94-
antidote_purge() {
95-
:
84+
antidote_init() {
85+
say "#!/usr/bin/env zsh"
86+
say "function antidote {"
87+
say " case \"\$1\" in"
88+
say " bundle)"
89+
say " source <( \"${ANTIDOTE_SCRIPT}\" \"\$@\" ) || \"${ANTIDOTE_SCRIPT}\" \"\$@\""
90+
say " ;;"
91+
say " *)"
92+
say " \"${ANTIDOTE_SCRIPT}\" \"\$@\""
93+
say " ;;"
94+
say " esac"
95+
say "}"
9696
}
9797

9898
antidote_list() {
9999
:
100100
}
101101

102+
antidote_purge() {
103+
:
104+
}
105+
102106
antidote_path() {
103107
:
104108
}
105109

106-
antidote_init() {
107-
say "$ANTIDOTE_INIT"
110+
antidote_update() {
111+
:
108112
}
109113

110114
bundle_info() {
@@ -302,22 +306,28 @@ antidote() {
302306
say "antidote version $ANTIDOTE_VERSION"
303307
return
304308
;;
309+
--debug)
310+
ANTIDOTE_DEBUG=true
311+
;;
305312
esac
306313

307314
if is_cmd "antidote_$1"; then
308315
cmd="antidote_$1"
309316
shift
310317
"$cmd" "$@"
311-
elif [ "$1" = info ]; then
312-
shift
313-
bundle_info "$@"
318+
elif [ "$ANTIDOTE_DEBUG" = true ]; then
319+
if [ "$1" = info ]; then
320+
shift
321+
bundle_info "$@"
322+
fi
314323
else
315324
die 1 "antidote: error: expected command but got \"$1\"."
316325
fi
317326
}
318327

319328
# Set antidote variables.
320329
ANTIDOTE_VERSION=2.0.0
330+
ANTIDOTE_SCRIPT="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"
321331
# shellcheck disable=SC3028
322332
: "${ANTIDOTE_OSTYPE:=${OSTYPE:-$(uname -s | tr '[:upper:]' '[:lower:]')}}"
323333
: "${ANTIDOTE_GIT_SITE:=https://github.com}"
@@ -326,7 +336,7 @@ ANTIDOTE_VERSION=2.0.0
326336
: "${ANTIDOTE_COMPATIBILITY_MODE:=}"
327337

328338
ANTIDOTE_HELP=$(
329-
cat <<'EOF'
339+
cat <<'EOS'
330340
antidote - the cure to slow zsh plugin management
331341
332342
usage: antidote [<flags>] <command> [<args> ...]
@@ -346,33 +356,47 @@ commands:
346356
list List cloned bundles
347357
path Print the path of a cloned bundle
348358
init Initialize the shell for dynamic bundles
349-
EOF
359+
EOS
360+
)
361+
362+
ANTIDOTE_BUNDLE_HELP=$(
363+
cat <<'EOS'
364+
Usage: antidote bundle [<bundles>...]
365+
366+
Clones a bundle and prints its source line.
367+
368+
Flags:
369+
-h, --help Show context-sensitive help.
370+
371+
Args:
372+
[<bundles>] Bundle list.
373+
EOS
350374
)
351375

352376
ANTIDOTE_HOME_HELP=$(
353-
cat <<'EOF'
377+
cat <<'EOS'
354378
usage: antidote home
355379
356380
Prints where antidote is cloning bundles.
357381
358382
Flags:
359383
-h, --help Show context-sensitive help.
360-
EOF
384+
EOS
361385
)
362386

363387
ANTIDOTE_INIT_HELP=$(
364-
cat <<'EOF'
388+
cat <<'EOS'
365389
usage: antidote init
366390
367391
Initializes the shell so antidote can load bundles dynmically.
368392
369393
Flags:
370394
-h, --help Show context-sensitive help.
371-
EOF
395+
EOS
372396
)
373397

374398
ANTIDOTE_LIST_HELP=$(
375-
cat <<'EOF'
399+
cat <<'EOS'
376400
usage: antidote list [-d|--details] [-bcprsu]
377401
378402
Lists all currently installed bundles
@@ -388,23 +412,52 @@ Format flags:
388412
-r Bundle's short repo name.
389413
-s Bundle's SHA.
390414
-u Bundle's URL.
391-
EOF
415+
EOS
392416
)
393417

394-
ANTIDOTE_INIT=$(
395-
cat <<EOF
396-
#!/usr/bin/env zsh
397-
function antidote {
398-
case "\$1" in
399-
bundle${RPAREN}
400-
source <${LPAREN} antidote-main \$@ ${RPAREN} || antidote-main \$@
401-
;;
402-
*${RPAREN}
403-
antidote-main \$@
404-
;;
405-
esac
406-
}
407-
EOF
418+
ANTIDOTE_PATH_HELP=$(
419+
cat <<'EOS'
420+
usage: antidote path <bundle>
421+
422+
Prints the path of a currently cloned bundle.
423+
424+
Flags:
425+
-h, --help Show context-sensitive help.
426+
427+
Args:
428+
<bundle> The Bundle path to print.
429+
EOS
430+
)
431+
432+
ANTIDOTE_PURGE_HELP=$(
433+
cat <<'EOS'
434+
usage: antidote purge <bundle>
435+
436+
Purges a bundle from your computer.
437+
438+
Flags:
439+
-h, --help Show context-sensitive help.
440+
441+
Args:
442+
<bundle> The bundle to be purged.
443+
EOS
444+
)
445+
446+
ANTIDOTE_UPDATE_HELP=$(
447+
cat <<'EOS'
448+
usage: antidote update [-b|--bundles] [-s|--self]
449+
antidote update <bundle>
450+
451+
Updates cloned bundle(s) and antidote itself.
452+
453+
Flags:
454+
-h, --help Show context-sensitive help.
455+
-s, --self Update antidote.
456+
-b, --bundles Update bundles.
457+
458+
Args:
459+
<bundle> The bundle to be updated.
460+
EOS
408461
)
409462

410463
# Run antidote!

tests/bin/mockgit

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env zsh
2+
function mockgit {
3+
# handle these commands:
4+
# - `git -C "$dir" config remote.origin.url`
5+
# - `git -C "$dir" pull --quiet --ff --rebase --autostash`
6+
# - `git -C "$dir" rev-parse --short HEAD`
7+
# - `git clone --quiet --depth 1 --recurse-submodules --shallow-submodules --branch branch $url $dir`
8+
# - `git --version`
9+
emulate -L zsh
10+
setopt local_options extended_glob
11+
12+
local MATCH MBEGIN MEND; local -a match mbegin mend # appease 'warn_create_global'
13+
0=${(%):-%x}
14+
local prjdir="${0:A:h:h:h}"
15+
local args=("$@[@]")
16+
local o_path o_quiet o_ff o_rebase o_autostash o_short
17+
local o_depth o_recurse_submodules o_shallow_submodules o_branch
18+
local o_init o_recursive
19+
zparseopts -D -E -- \
20+
C:=o_path \
21+
-short=o_short \
22+
-quiet=o_quiet \
23+
-ff=o_ff \
24+
-rebase=o_rebase \
25+
-autostash=o_autostash \
26+
-recurse-submodules=o_recurse_submodules \
27+
-shallow-submodules=o_shallow_submodules \
28+
-depth:=o_depth \
29+
-branch:=o_branch \
30+
-init=o_init \
31+
-recursive=o_recursive ||
32+
return 1
33+
34+
bundledir="$o_path[-1]"
35+
if [[ "$*" == (-v|--version) ]]; then
36+
echo "mockgit version 0.0.0"
37+
elif [[ "$1" = "clone" ]]; then
38+
local giturl="$2"
39+
local repo="${giturl:h:t}/${${giturl:t}%.git}"
40+
local bundledir="${3:-.}"
41+
local gitsite_repodir="$prjdir/tests/fakegitsite.com/${repo}"
42+
if [[ -d "$gitsite_repodir" ]]; then
43+
[[ -d "${bundledir:A}" ]] || mkdir -p "${bundledir:A}"
44+
cp -r -- "$gitsite_repodir" "${bundledir:A}"
45+
elif ! (( $#o_quiet )); then
46+
echo "MOCKGIT: Cloning into '${repo:t}'..."
47+
echo "MOCKGIT: Repository not found."
48+
echo "MOCKGIT: repository '$giturl' not found"
49+
fi
50+
elif [[ "$@" = "config remote.origin.url" ]]; then
51+
if [[ -e $bundledir/.git/config ]]; then
52+
awk '/^[[:space:]]*url =/ {print $3}' $bundledir/.git/config
53+
else
54+
echo "https://gitsite.com/${bundledir:h:t}/${bundledir:t}"
55+
fi
56+
elif [[ "$@" = "pull" ]]; then
57+
(( $#o_quiet )) || echo "MOCKGIT: Already up to date."
58+
elif [[ "$@" = "rev-parse --abbrev-ref HEAD" ]]; then
59+
echo "main"
60+
elif [[ "$@" = "rev-parse HEAD" ]]; then
61+
if (( $#o_short )); then
62+
print "abcd123"
63+
else
64+
print "abcd1230abcd1230abcd1230abcd1230abcd1230"
65+
fi
66+
elif [[ "$@" = "log -1 --format=%cd --date=short" ]]; then
67+
echo "2006-01-02"
68+
elif [[ "$1" == (submodule|fetch) ]]; then
69+
# nothing to do
70+
else
71+
echo >&2 "mocking not implemented for git command: git $@"
72+
return 1
73+
fi
74+
}
75+
mockgit "$@"

tests/bin/subenv

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env zsh
2+
function subenv {
3+
emulate -L zsh; setopt local_options
4+
5+
local -a sedargs=()
6+
while (( $# )); do
7+
if [[ -v "$1" ]]; then
8+
sedargs+=(-e "s|${(P)1}|\$$1|g")
9+
fi
10+
shift
11+
done
12+
sedargs+=(-e "s|$PWD|\$PWD|g" -e "s|$HOME|\$HOME|g")
13+
sed "$sedargs[@]"
14+
15+
# echo "ran sed $sedargs[@]"
16+
}
17+
subenv "$@"

0 commit comments

Comments
 (0)