Commit 60fa053ec834d38455970caa201177aed2abf57c

Authored by Shrikant Sharat
1 parent 42d76e5891

Add tests for url resolver and fix ssh urls.

All supported syntexes for a repo url are now tested. The ssh url of github was
failing the test, so that is also fixed in this commit.

Showing 3 changed files with 30 additions and 2 deletions Inline Diff

1 .PHONY: tests 1 .PHONY: tests
2 2
3 tests: 3 tests:
4 ZDOTDIR="${PWD}/tests" cram -i --shell=zsh tests/branch-bundle.t \ 4 ZDOTDIR="${PWD}/tests" cram -i --shell=zsh tests/branch-bundle.t \
5 tests/bundle.t 5 tests/bundle.t tests/url-resolver.t
6 6
1 #!/bin/zsh 1 #!/bin/zsh
2 2
3 # Each line in this string has the following entries separated by a space 3 # Each line in this string has the following entries separated by a space
4 # character. 4 # character.
5 # <repo-url>, <plugin-location>, <bundle-type> 5 # <repo-url>, <plugin-location>, <bundle-type>
6 # FIXME: Is not kept local by zsh! 6 # FIXME: Is not kept local by zsh!
7 local _ANTIGEN_BUNDLE_RECORD="" 7 local _ANTIGEN_BUNDLE_RECORD=""
8 8
9 # Syntaxes 9 # Syntaxes
10 # antigen-bundle <url> [<loc>=/] 10 # antigen-bundle <url> [<loc>=/]
11 # Keyword only arguments: 11 # Keyword only arguments:
12 # branch - The branch of the repo to use for this bundle. 12 # branch - The branch of the repo to use for this bundle.
13 antigen-bundle () { 13 antigen-bundle () {
14 14
15 # Bundle spec arguments' default values. 15 # Bundle spec arguments' default values.
16 local url="$ANTIGEN_DEFAULT_REPO_URL" 16 local url="$ANTIGEN_DEFAULT_REPO_URL"
17 local loc=/ 17 local loc=/
18 local branch=- 18 local branch=-
19 local btype=plugin 19 local btype=plugin
20 20
21 # Set spec values based on the positional arguments. 21 # Set spec values based on the positional arguments.
22 local position_args='url loc' 22 local position_args='url loc'
23 local i=1 23 local i=1
24 while ! [[ -z $1 || $1 == --*=* ]]; do 24 while ! [[ -z $1 || $1 == --*=* ]]; do
25 local arg_name="$(echo "$position_args" | cut -d\ -f$i)" 25 local arg_name="$(echo "$position_args" | cut -d\ -f$i)"
26 local arg_value="$1" 26 local arg_value="$1"
27 eval "local $arg_name='$arg_value'" 27 eval "local $arg_name='$arg_value'"
28 shift 28 shift
29 i=$(($i + 1)) 29 i=$(($i + 1))
30 done 30 done
31 31
32 # Check if url is just the plugin name. Super short syntax. 32 # Check if url is just the plugin name. Super short syntax.
33 if [[ "$url" != */* ]]; then 33 if [[ "$url" != */* ]]; then
34 loc="plugins/$url" 34 loc="plugins/$url"
35 url="$ANTIGEN_DEFAULT_REPO_URL" 35 url="$ANTIGEN_DEFAULT_REPO_URL"
36 fi 36 fi
37 37
38 # Set spec values from keyword arguments, if any. The remaining arguments 38 # Set spec values from keyword arguments, if any. The remaining arguments
39 # are all assumed to be keyword arguments. 39 # are all assumed to be keyword arguments.
40 while [[ $1 == --*=* ]]; do 40 while [[ $1 == --*=* ]]; do
41 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 41 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
42 local arg_value="$(echo "$1" | cut -d= -f2)" 42 local arg_value="$(echo "$1" | cut -d= -f2)"
43 eval "local $arg_name='$arg_value'" 43 eval "local $arg_name='$arg_value'"
44 shift 44 shift
45 done 45 done
46 46
47 # Resolve the url. 47 # Resolve the url.
48 url="$(-antigen-resolve-bundle-url "$url")" 48 url="$(-antigen-resolve-bundle-url "$url")"
49 49
50 # Add it to the record. 50 # Add it to the record.
51 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype $branch" 51 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype $branch"
52 52
53 # Ensure a clone exists for this repo. 53 # Ensure a clone exists for this repo.
54 -antigen-ensure-repo "$url" "$branch" 54 -antigen-ensure-repo "$url" "$branch"
55 55
56 # Load the plugin. 56 # Load the plugin.
57 -antigen-load "$url" "$loc" "$btype" "$branch" 57 -antigen-load "$url" "$loc" "$btype" "$branch"
58 58
59 } 59 }
60 60
61 -antigen-resolve-bundle-url () { 61 -antigen-resolve-bundle-url () {
62 # Given an acceptable short/full form of a bundle's repo url, this function 62 # Given an acceptable short/full form of a bundle's repo url, this function
63 # echoes the full form of the repo's clone url. 63 # echoes the full form of the repo's clone url.
64 64
65 local url="$1" 65 local url="$1"
66 66
67 if [[ $url != git://* && $url != https://* && $url != /* ]]; then 67 if [[ $url != git://* && \
68 $url != https://* && \
69 $url != /* && \
70 $url != git@github.com:*/*
71 ]]; then
68 url="${url%.git}" 72 url="${url%.git}"
69 url="https://github.com/$url.git" 73 url="https://github.com/$url.git"
70 fi 74 fi
71 75
72 echo "$url" 76 echo "$url"
73 } 77 }
74 78
75 antigen-bundles () { 79 antigen-bundles () {
76 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 80 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
77 # are ignored. Everything else is given to `antigen-bundle` as is, no 81 # are ignored. Everything else is given to `antigen-bundle` as is, no
78 # quoting rules applied. 82 # quoting rules applied.
79 83
80 local line 84 local line
81 85
82 grep -v '^\s*$\|^#' | while read line; do 86 grep -v '^\s*$\|^#' | while read line; do
83 # Using `eval` so that we can use the shell-style quoting in each line 87 # Using `eval` so that we can use the shell-style quoting in each line
84 # piped to `antigen-bundles`. 88 # piped to `antigen-bundles`.
85 eval "antigen-bundle $line" 89 eval "antigen-bundle $line"
86 done 90 done
87 } 91 }
88 92
89 antigen-update () { 93 antigen-update () {
90 # Update your bundles, i.e., `git pull` in all the plugin repos. 94 # Update your bundles, i.e., `git pull` in all the plugin repos.
91 -antigen-echo-record \ 95 -antigen-echo-record \
92 | awk '{print $1 "|" $4}' \ 96 | awk '{print $1 "|" $4}' \
93 | sort -u \ 97 | sort -u \
94 | while read url_line; do 98 | while read url_line; do
95 -antigen-ensure-repo --update "${url_line%|*}" "${url_line#*|}" 99 -antigen-ensure-repo --update "${url_line%|*}" "${url_line#*|}"
96 done 100 done
97 } 101 }
98 102
99 -antigen-get-clone-dir () { 103 -antigen-get-clone-dir () {
100 # Takes a repo url and gives out the path that this url needs to be cloned 104 # Takes a repo url and gives out the path that this url needs to be cloned
101 # to. Doesn't actually clone anything. 105 # to. Doesn't actually clone anything.
102 # TODO: Memoize? 106 # TODO: Memoize?
103 local url="$1" 107 local url="$1"
104 local branch="$2" 108 local branch="$2"
105 109
106 # The branched_url will be the same as the url itself, unless there is no 110 # The branched_url will be the same as the url itself, unless there is no
107 # branch specified. 111 # branch specified.
108 local branched_url="$url" 112 local branched_url="$url"
109 113
110 # If a branch is specified, i.e., branch is not `-`, append it to the url, 114 # If a branch is specified, i.e., branch is not `-`, append it to the url,
111 # separating with a pipe character. 115 # separating with a pipe character.
112 if [[ "$branch" != - ]]; then 116 if [[ "$branch" != - ]]; then
113 branched_url="$branched_url|$branch" 117 branched_url="$branched_url|$branch"
114 fi 118 fi
115 119
116 # Echo the full path to the clone directory. 120 # Echo the full path to the clone directory.
117 echo -n $ADOTDIR/repos/ 121 echo -n $ADOTDIR/repos/
118 echo "$branched_url" | sed \ 122 echo "$branched_url" | sed \
119 -e 's/\.git$//' \ 123 -e 's/\.git$//' \
120 -e 's./.-SLASH-.g' \ 124 -e 's./.-SLASH-.g' \
121 -e 's.:.-COLON-.g' \ 125 -e 's.:.-COLON-.g' \
122 -e 's.|.-PIPE-.g' 126 -e 's.|.-PIPE-.g'
123 } 127 }
124 128
125 -antigen-get-clone-url () { 129 -antigen-get-clone-url () {
126 # Takes a repo's clone dir and gives out the repo's original url that was 130 # Takes a repo's clone dir and gives out the repo's original url that was
127 # used to create the given directory path. 131 # used to create the given directory path.
128 # TODO: Memoize? 132 # TODO: Memoize?
129 echo "$1" | sed \ 133 echo "$1" | sed \
130 -e "s:^$ADOTDIR/repos/::" \ 134 -e "s:^$ADOTDIR/repos/::" \
131 -e 's/$/.git/' \ 135 -e 's/$/.git/' \
132 -e 's.-SLASH-./.g' \ 136 -e 's.-SLASH-./.g' \
133 -e 's.-COLON-.:.g' \ 137 -e 's.-COLON-.:.g' \
134 -e 's.-PIPE-.|.g' 138 -e 's.-PIPE-.|.g'
135 } 139 }
136 140
137 -antigen-ensure-repo () { 141 -antigen-ensure-repo () {
138 142
139 # Ensure that a clone exists for the given repo url and branch. If the first 143 # Ensure that a clone exists for the given repo url and branch. If the first
140 # argument is `--update` and if a clone already exists for the given repo 144 # argument is `--update` and if a clone already exists for the given repo
141 # and branch, it is pull-ed, i.e., updated. 145 # and branch, it is pull-ed, i.e., updated.
142 146
143 # Check if we have to update. 147 # Check if we have to update.
144 local update=false 148 local update=false
145 if [[ $1 == --update ]]; then 149 if [[ $1 == --update ]]; then
146 update=true 150 update=true
147 shift 151 shift
148 fi 152 fi
149 153
150 # Get the clone's directory as per the given repo url and branch. 154 # Get the clone's directory as per the given repo url and branch.
151 local url="$1" 155 local url="$1"
152 local branch="$2" 156 local branch="$2"
153 local clone_dir="$(-antigen-get-clone-dir $url $branch)" 157 local clone_dir="$(-antigen-get-clone-dir $url $branch)"
154 158
155 # Clone if it doesn't already exist. 159 # Clone if it doesn't already exist.
156 if [[ ! -d $clone_dir ]]; then 160 if [[ ! -d $clone_dir ]]; then
157 git clone "$url" "$clone_dir" 161 git clone "$url" "$clone_dir"
158 elif $update; then 162 elif $update; then
159 # Pull changes if update requested. 163 # Pull changes if update requested.
160 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull 164 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull
161 fi 165 fi
162 166
163 # If its a specific branch that we want, checkout that branch. 167 # If its a specific branch that we want, checkout that branch.
164 if [[ "$branch" != - ]]; then 168 if [[ "$branch" != - ]]; then
165 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 169 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
166 checkout "$branch" 170 checkout "$branch"
167 fi 171 fi
168 172
169 } 173 }
170 174
171 -antigen-load () { 175 -antigen-load () {
172 176
173 local url="$1" 177 local url="$1"
174 local loc="$2" 178 local loc="$2"
175 local btype="$3" 179 local btype="$3"
176 local branch="$4" 180 local branch="$4"
177 181
178 # The full location where the plugin is located. 182 # The full location where the plugin is located.
179 local location="$(-antigen-get-clone-dir "$url" "$branch")/$loc" 183 local location="$(-antigen-get-clone-dir "$url" "$branch")/$loc"
180 184
181 if [[ $btype == theme ]]; then 185 if [[ $btype == theme ]]; then
182 186
183 # Of course, if its a theme, the location would point to the script 187 # Of course, if its a theme, the location would point to the script
184 # file. 188 # file.
185 source "$location" 189 source "$location"
186 190
187 else 191 else
188 192
189 # Source the plugin script 193 # Source the plugin script
190 # FIXME: I don't know. Looks very very ugly. Needs a better 194 # FIXME: I don't know. Looks very very ugly. Needs a better
191 # implementation once tests are ready. 195 # implementation once tests are ready.
192 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" 196 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')"
193 197
194 if [[ -f $script_loc ]]; then 198 if [[ -f $script_loc ]]; then
195 # If we have a `*.plugin.zsh`, source it. 199 # If we have a `*.plugin.zsh`, source it.
196 source "$script_loc" 200 source "$script_loc"
197 201
198 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then 202 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then
199 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 203 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
200 # files. 204 # files.
201 for script ($location/*.zsh) source "$script" 205 for script ($location/*.zsh) source "$script"
202 206
203 elif [[ ! -z "$(ls "$location" | grep -m1 '.sh$')" ]]; then 207 elif [[ ! -z "$(ls "$location" | grep -m1 '.sh$')" ]]; then
204 # If there are no `*.zsh` files either, we look for and source any 208 # If there are no `*.zsh` files either, we look for and source any
205 # `*.sh` files instead. 209 # `*.sh` files instead.
206 for script ($location/*.sh) source "$script" 210 for script ($location/*.sh) source "$script"
207 211
208 fi 212 fi
209 213
210 # Add to $fpath, for completion(s). 214 # Add to $fpath, for completion(s).
211 fpath=($location $fpath) 215 fpath=($location $fpath)
212 216
213 fi 217 fi
214 218
215 } 219 }
216 220
217 antigen-cleanup () { 221 antigen-cleanup () {
218 222
219 # Cleanup unused repositories. 223 # Cleanup unused repositories.
220 224
221 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 225 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
222 echo "You don't have any bundles." 226 echo "You don't have any bundles."
223 return 0 227 return 0
224 fi 228 fi
225 229
226 # Find directores in ADOTDIR/repos, that are not in the bundles record. 230 # Find directores in ADOTDIR/repos, that are not in the bundles record.
227 local unused_clones="$(comm -13 \ 231 local unused_clones="$(comm -13 \
228 <(-antigen-echo-record | awk '{print $1 "|" $4}' | sort -u) \ 232 <(-antigen-echo-record | awk '{print $1 "|" $4}' | sort -u) \
229 <(ls "$ADOTDIR/repos" | while read line; do 233 <(ls "$ADOTDIR/repos" | while read line; do
230 -antigen-get-clone-url "$line" 234 -antigen-get-clone-url "$line"
231 done))" 235 done))"
232 236
233 if [[ -z $unused_clones ]]; then 237 if [[ -z $unused_clones ]]; then
234 echo "You don't have any unidentified bundles." 238 echo "You don't have any unidentified bundles."
235 return 0 239 return 0
236 fi 240 fi
237 241
238 echo 'You have clones for the following repos, but are not used.' 242 echo 'You have clones for the following repos, but are not used.'
239 echo "$unused_clones" \ 243 echo "$unused_clones" \
240 | sed -e 's/^/ /' -e 's/|/, branch /' 244 | sed -e 's/^/ /' -e 's/|/, branch /'
241 245
242 echo -n '\nDelete them all? [y/N] ' 246 echo -n '\nDelete them all? [y/N] '
243 if read -q; then 247 if read -q; then
244 echo 248 echo
245 echo 249 echo
246 echo "$unused_clones" | while read url; do 250 echo "$unused_clones" | while read url; do
247 echo -n "Deleting clone for $url..." 251 echo -n "Deleting clone for $url..."
248 rm -rf "$(-antigen-get-clone-dir $url)" 252 rm -rf "$(-antigen-get-clone-dir $url)"
249 echo ' done.' 253 echo ' done.'
250 done 254 done
251 else 255 else
252 echo 256 echo
253 echo Nothing deleted. 257 echo Nothing deleted.
254 fi 258 fi
255 } 259 }
256 260
257 antigen-lib () { 261 antigen-lib () {
258 antigen-bundle --loc=lib 262 antigen-bundle --loc=lib
259 } 263 }
260 264
261 antigen-theme () { 265 antigen-theme () {
262 local name="${1:-robbyrussell}" 266 local name="${1:-robbyrussell}"
263 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 267 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
264 } 268 }
265 269
266 antigen-apply () { 270 antigen-apply () {
267 # Initialize completion. 271 # Initialize completion.
268 # TODO: Only load completions if there are any changes to the bundle 272 # TODO: Only load completions if there are any changes to the bundle
269 # repositories. 273 # repositories.
270 compinit -i 274 compinit -i
271 } 275 }
272 276
273 antigen-list () { 277 antigen-list () {
274 # List all currently installed bundles 278 # List all currently installed bundles
275 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 279 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
276 echo "You don't have any bundles." >&2 280 echo "You don't have any bundles." >&2
277 return 1 281 return 1
278 else 282 else
279 -antigen-echo-record 283 -antigen-echo-record
280 fi 284 fi
281 } 285 }
282 286
283 antigen-help () { 287 antigen-help () {
284 cat <<EOF 288 cat <<EOF
285 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 289 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
286 shell scripts and utilities, put up on github. For further details and complete 290 shell scripts and utilities, put up on github. For further details and complete
287 documentation, visit the project's page at 'http://antigen.sharats.me'. 291 documentation, visit the project's page at 'http://antigen.sharats.me'.
288 EOF 292 EOF
289 } 293 }
290 294
291 # A syntax sugar to avoid the `-` when calling antigen commands. With this 295 # A syntax sugar to avoid the `-` when calling antigen commands. With this
292 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 296 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
293 antigen () { 297 antigen () {
294 local cmd="$1" 298 local cmd="$1"
295 shift 299 shift
296 "antigen-$cmd" "$@" 300 "antigen-$cmd" "$@"
297 } 301 }
298 302
299 # Echo the bundle specs as in the record. The first line is not echoed since it 303 # Echo the bundle specs as in the record. The first line is not echoed since it
300 # is a blank line. 304 # is a blank line.
301 -antigen-echo-record () { 305 -antigen-echo-record () {
302 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 306 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
303 } 307 }
304 308
305 -antigen-env-setup () { 309 -antigen-env-setup () {
306 # Pre-startup initializations 310 # Pre-startup initializations
307 -set-default ANTIGEN_DEFAULT_REPO_URL \ 311 -set-default ANTIGEN_DEFAULT_REPO_URL \
308 https://github.com/robbyrussell/oh-my-zsh.git 312 https://github.com/robbyrussell/oh-my-zsh.git
309 -set-default ADOTDIR $HOME/.antigen 313 -set-default ADOTDIR $HOME/.antigen
310 314
311 # Load the compinit module 315 # Load the compinit module
312 autoload -U compinit 316 autoload -U compinit
313 317
314 # Without the following, `compdef` function is not defined. 318 # Without the following, `compdef` function is not defined.
315 compinit -i 319 compinit -i
316 } 320 }
317 321
318 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 322 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
319 # not already set. 323 # not already set.
320 -set-default () { 324 -set-default () {
321 local arg_name="$1" 325 local arg_name="$1"
322 local arg_value="$2" 326 local arg_value="$2"
323 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 327 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
324 } 328 }
325 329
326 -antigen-env-setup 330 -antigen-env-setup
327 331
tests/url-resolver.t
File was created 1 Helper alias.
2
3 $ alias resolve=-antigen-resolve-bundle-url
4
5 Complete urls.
6
7 $ resolve https://github.com/zsh-users/antigen.git
8 https://github.com/zsh-users/antigen.git
9 $ resolve git://github.com/zsh-users/antigen.git
10 git://github.com/zsh-users/antigen.git
11 $ resolve git@github.com:zsh-users/antigen.git
12 git@github.com:zsh-users/antigen.git
13
14 Just username and repo name.
15
16 $ resolve zsh-users/antigen
17 https://github.com/zsh-users/antigen.git
18 $ resolve zsh-users/antigen.git
19 https://github.com/zsh-users/antigen.git
20
21 Local absolute file path.
22
23 $ resolve /path/to/a/local/git/repo
24 /path/to/a/local/git/repo
25