Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t0007-git-var.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t0007-git-var.sh` 437 lines ยท 13392 bytes Raw Blame History 1 #!/bin/sh 2 3 test_description='basic sanity checks for git var' 4 5 . ./test-lib.sh 6 7 sane_unset_all_editors () { 8 sane_unset GIT_EDITOR && 9 sane_unset VISUAL && 10 sane_unset EDITOR 11 } 12 13 test_expect_success 'get GIT_AUTHOR_IDENT' ' 14 test_tick && 15 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect && 16 git var GIT_AUTHOR_IDENT >actual && 17 test_cmp expect actual 18 ' 19 20 test_expect_success 'get GIT_COMMITTER_IDENT' ' 21 test_tick && 22 echo "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" >expect && 23 git var GIT_COMMITTER_IDENT >actual && 24 test_cmp expect actual 25 ' 26 27 test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' ' 28 ( 29 sane_unset GIT_COMMITTER_NAME && 30 sane_unset GIT_COMMITTER_EMAIL && 31 test_must_fail git var GIT_COMMITTER_IDENT 32 ) 33 ' 34 35 test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' ' 36 ( 37 sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME && 38 git init defbranch && 39 git -C defbranch symbolic-ref --short HEAD >expect && 40 git var GIT_DEFAULT_BRANCH >actual && 41 test_cmp expect actual 42 ) 43 ' 44 45 test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' ' 46 test_config init.defaultbranch foo && 47 ( 48 sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME && 49 echo foo >expect && 50 git var GIT_DEFAULT_BRANCH >actual && 51 test_cmp expect actual 52 ) 53 ' 54 55 test_expect_success 'get GIT_EDITOR without configuration' ' 56 ( 57 sane_unset_all_editors && 58 test_expect_code 1 git var GIT_EDITOR >out && 59 test_must_be_empty out 60 ) 61 ' 62 63 test_expect_success 'get GIT_EDITOR with configuration' ' 64 test_config core.editor foo && 65 ( 66 sane_unset_all_editors && 67 echo foo >expect && 68 git var GIT_EDITOR >actual && 69 test_cmp expect actual 70 ) 71 ' 72 73 test_expect_success 'get GIT_EDITOR with environment variable GIT_EDITOR' ' 74 ( 75 sane_unset_all_editors && 76 echo bar >expect && 77 GIT_EDITOR=bar git var GIT_EDITOR >actual && 78 test_cmp expect actual 79 ) 80 ' 81 82 test_expect_success 'get GIT_EDITOR with environment variable EDITOR' ' 83 ( 84 sane_unset_all_editors && 85 echo bar >expect && 86 EDITOR=bar git var GIT_EDITOR >actual && 87 test_cmp expect actual 88 ) 89 ' 90 91 test_expect_success 'get GIT_EDITOR with configuration and environment variable GIT_EDITOR' ' 92 test_config core.editor foo && 93 ( 94 sane_unset_all_editors && 95 echo bar >expect && 96 GIT_EDITOR=bar git var GIT_EDITOR >actual && 97 test_cmp expect actual 98 ) 99 ' 100 101 test_expect_success 'get GIT_EDITOR with configuration and environment variable EDITOR' ' 102 test_config core.editor foo && 103 ( 104 sane_unset_all_editors && 105 echo foo >expect && 106 EDITOR=bar git var GIT_EDITOR >actual && 107 test_cmp expect actual 108 ) 109 ' 110 111 test_expect_success 'get GIT_SEQUENCE_EDITOR without configuration' ' 112 ( 113 sane_unset GIT_SEQUENCE_EDITOR && 114 git var GIT_EDITOR >expect && 115 git var GIT_SEQUENCE_EDITOR >actual && 116 test_cmp expect actual 117 ) 118 ' 119 120 test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration' ' 121 test_config sequence.editor foo && 122 ( 123 sane_unset GIT_SEQUENCE_EDITOR && 124 echo foo >expect && 125 git var GIT_SEQUENCE_EDITOR >actual && 126 test_cmp expect actual 127 ) 128 ' 129 130 test_expect_success 'get GIT_SEQUENCE_EDITOR with environment variable' ' 131 ( 132 sane_unset GIT_SEQUENCE_EDITOR && 133 echo bar >expect && 134 GIT_SEQUENCE_EDITOR=bar git var GIT_SEQUENCE_EDITOR >actual && 135 test_cmp expect actual 136 ) 137 ' 138 139 test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration and environment variable' ' 140 test_config sequence.editor foo && 141 ( 142 sane_unset GIT_SEQUENCE_EDITOR && 143 echo bar >expect && 144 GIT_SEQUENCE_EDITOR=bar git var GIT_SEQUENCE_EDITOR >actual && 145 test_cmp expect actual 146 ) 147 ' 148 149 test_expect_success POSIXPERM 'GIT_SHELL_PATH points to a valid executable' ' 150 shellpath=$(git var GIT_SHELL_PATH) && 151 test_path_is_executable "$shellpath" 152 ' 153 154 # We know in this environment that our shell will be one of a few fixed values 155 # that all end in "sh". 156 test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' ' 157 shellpath=$(git var GIT_SHELL_PATH) && 158 case "$shellpath" in 159 [A-Z]:/*/sh.exe) test -f "$shellpath";; 160 *) return 1;; 161 esac 162 ' 163 164 test_expect_success 'GIT_ATTR_SYSTEM produces expected output' ' 165 test_must_fail env GIT_ATTR_NOSYSTEM=1 git var GIT_ATTR_SYSTEM && 166 ( 167 sane_unset GIT_ATTR_NOSYSTEM && 168 systempath=$(git var GIT_ATTR_SYSTEM) && 169 test "$systempath" != "" 170 ) 171 ' 172 173 test_expect_success 'GIT_ATTR_GLOBAL points to the correct location' ' 174 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && 175 globalpath=$(XDG_CONFIG_HOME="$TRASHDIR/.config" git var GIT_ATTR_GLOBAL) && 176 test "$globalpath" = "$TRASHDIR/.config/git/attributes" && 177 ( 178 sane_unset XDG_CONFIG_HOME && 179 globalpath=$(HOME="$TRASHDIR" git var GIT_ATTR_GLOBAL) && 180 test "$globalpath" = "$TRASHDIR/.config/git/attributes" 181 ) 182 ' 183 184 test_expect_success 'GIT_CONFIG_SYSTEM points to the correct location' ' 185 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && 186 test_must_fail env GIT_CONFIG_NOSYSTEM=1 git var GIT_CONFIG_SYSTEM && 187 ( 188 sane_unset GIT_CONFIG_NOSYSTEM && 189 systempath=$(git var GIT_CONFIG_SYSTEM) && 190 test "$systempath" != "" && 191 systempath=$(GIT_CONFIG_SYSTEM=/dev/null git var GIT_CONFIG_SYSTEM) && 192 if test_have_prereq MINGW 193 then 194 test "$systempath" = "nul" 195 else 196 test "$systempath" = "/dev/null" 197 fi && 198 systempath=$(GIT_CONFIG_SYSTEM="$TRASHDIR/gitconfig" git var GIT_CONFIG_SYSTEM) && 199 test "$systempath" = "$TRASHDIR/gitconfig" 200 ) 201 ' 202 203 test_expect_success 'GIT_CONFIG_GLOBAL points to the correct location' ' 204 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && 205 HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var GIT_CONFIG_GLOBAL >actual && 206 echo "$TRASHDIR/foo/git/config" >expected && 207 echo "$TRASHDIR/.gitconfig" >>expected && 208 test_cmp expected actual && 209 ( 210 sane_unset XDG_CONFIG_HOME && 211 HOME="$TRASHDIR" git var GIT_CONFIG_GLOBAL >actual && 212 echo "$TRASHDIR/.config/git/config" >expected && 213 echo "$TRASHDIR/.gitconfig" >>expected && 214 test_cmp expected actual && 215 globalpath=$(GIT_CONFIG_GLOBAL=/dev/null git var GIT_CONFIG_GLOBAL) && 216 if test_have_prereq MINGW 217 then 218 test "$globalpath" = "nul" 219 else 220 test "$globalpath" = "/dev/null" 221 fi && 222 globalpath=$(GIT_CONFIG_GLOBAL="$TRASHDIR/gitconfig" git var GIT_CONFIG_GLOBAL) && 223 test "$globalpath" = "$TRASHDIR/gitconfig" 224 ) 225 ' 226 227 test_expect_success 'GIT_HOSTNAME is not empty' ' 228 hostname=$(git var GIT_HOSTNAME) && 229 test -n "$hostname" 230 ' 231 232 # For git var -l, we check only a representative variable; 233 # testing the whole output would make our test too brittle with 234 # respect to unrelated changes in the test suite's environment. 235 test_expect_success 'git var -l lists variables' ' 236 git var -l >actual && 237 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect && 238 sed -n s/GIT_AUTHOR_IDENT=//p actual.author && 239 test_cmp expect actual.author 240 ' 241 242 test_expect_success 'git var -l lists config' ' 243 git var -l >actual && 244 echo false >expect && 245 sed -n s/core\\.bare=//p actual.bare && 246 test_cmp expect actual.bare 247 ' 248 249 test_expect_success 'git var -l lists multiple global configs' ' 250 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && 251 HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var -l >actual && 252 grep "^GIT_CONFIG_GLOBAL=" actual >filtered && 253 echo "GIT_CONFIG_GLOBAL=$TRASHDIR/foo/git/config" >expected && 254 echo "GIT_CONFIG_GLOBAL=$TRASHDIR/.gitconfig" >>expected && 255 test_cmp expected filtered 256 ' 257 258 test_expect_success 'git var -l does not split multiline editors' ' 259 ( 260 GIT_EDITOR="!f() { 261 echo Hello! 262 }; f" && 263 export GIT_EDITOR && 264 echo "GIT_EDITOR=$GIT_EDITOR" >expected && 265 git var -l >var && 266 sed -n -e "/^GIT_EDITOR/,\$p" var | head -n 3 >actual && 267 test_cmp expected actual 268 ) 269 ' 270 271 test_expect_success 'listing and asking for variables are exclusive' ' 272 test_must_fail git var -l GIT_COMMITTER_IDENT 273 ' 274 275 test_expect_success '`git var -l` works even without HOME' ' 276 ( 277 XDG_CONFIG_HOME= && 278 export XDG_CONFIG_HOME && 279 unset HOME && 280 git var -l 281 ) 282 ' 283 284 test_expect_success 'get a variable with -z' ' 285 printf "%sQ" "$(git var GIT_AUTHOR_IDENT)" >expect && 286 git var -z GIT_AUTHOR_IDENT >actual.raw && 287 nul_to_q actual && 288 test_cmp expect actual 289 ' 290 291 test_expect_success 'get multi-valued variable with -z' ' 292 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && 293 HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var -z GIT_CONFIG_GLOBAL >actual.raw && 294 printf "%sQ%sQ" "$TRASHDIR/foo/git/config" "$TRASHDIR/.gitconfig" >expect && 295 nul_to_q actual && 296 test_cmp expect actual 297 ' 298 299 test_expect_success 'git var -l -z' ' 300 git var -l -z >actual && 301 tr "\0" "\n" actual.lines && 302 git var GIT_AUTHOR_IDENT >expect && 303 sed -n "/^GIT_AUTHOR_IDENT$/{n;p;}" actual.lines >actual.author && 304 test_cmp expect actual.author && 305 echo false >expect && 306 sed -n "/^core\.bare$/{n;p;}" actual.lines >actual.bare && 307 test_cmp expect actual.bare 308 ' 309 310 test_expect_success 'options must precede variable arguments' ' 311 test_must_fail git var GIT_AUTHOR_IDENT -z 312 ' 313 314 test_expect_success 'get multiple variables' ' 315 test_tick && 316 cat >expect <<-EOF && 317 GIT_AUTHOR_IDENT=$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE 318 GIT_COMMITTER_IDENT=$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE 319 EOF 320 git var GIT_AUTHOR_IDENT GIT_COMMITTER_IDENT >actual && 321 test_cmp expect actual 322 ' 323 324 test_expect_success 'get multiple variables with -z' ' 325 test_tick && 326 printf "GIT_AUTHOR_IDENT\n%sQGIT_COMMITTER_IDENT\n%sQ" \ 327 "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" \ 328 "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" >expect && 329 git var -z GIT_AUTHOR_IDENT GIT_COMMITTER_IDENT >actual.raw && 330 nul_to_q actual && 331 test_cmp expect actual 332 ' 333 334 test_expect_success 'get multiple variables including multi-valued variable' ' 335 test_tick && 336 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && 337 cat >expect <<-EOF && 338 GIT_AUTHOR_IDENT=$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE 339 GIT_CONFIG_GLOBAL=$TRASHDIR/foo/git/config 340 GIT_CONFIG_GLOBAL=$TRASHDIR/.gitconfig 341 GIT_COMMITTER_IDENT=$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE 342 EOF 343 HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" \ 344 git var GIT_AUTHOR_IDENT GIT_CONFIG_GLOBAL GIT_COMMITTER_IDENT >actual && 345 test_cmp expect actual 346 ' 347 348 test_expect_success 'get multiple variables including multi-valued variable with -z' ' 349 test_tick && 350 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && 351 printf "GIT_AUTHOR_IDENT\n%sQGIT_CONFIG_GLOBAL\n%sQGIT_CONFIG_GLOBAL\n%sQ" \ 352 "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" \ 353 "$TRASHDIR/foo/git/config" "$TRASHDIR/.gitconfig" >expect && 354 HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" \ 355 git var -z GIT_AUTHOR_IDENT GIT_CONFIG_GLOBAL >actual.raw && 356 nul_to_q actual && 357 test_cmp expect actual 358 ' 359 360 test_expect_success 'variable without a value is omitted but is not an error' ' 361 test_tick && 362 cat >expect <<-EOF && 363 GIT_AUTHOR_IDENT=$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE 364 GIT_COMMITTER_IDENT=$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE 365 EOF 366 test_env GIT_CONFIG_GLOBAL= \ 367 git var GIT_AUTHOR_IDENT GIT_CONFIG_GLOBAL GIT_COMMITTER_IDENT >actual && 368 test_cmp expect actual 369 ' 370 371 test_expect_success 'a single variable without a value still exits with 1' ' 372 test_env GIT_CONFIG_GLOBAL= test_expect_code 1 git var GIT_CONFIG_GLOBAL >out && 373 test_must_be_empty out 374 ' 375 376 test_expect_success 'unknown variable is a usage error' ' 377 test_must_fail git var GIT_AUTHOR_IDENT NO_SUCH_VARIABLE 2>err && 378 test_grep usage err 379 ' 380 381 test_expect_success 'get author identity components' ' 382 test_tick && 383 echo "$GIT_AUTHOR_NAME" >expect.name && 384 echo "$GIT_AUTHOR_EMAIL" >expect.email && 385 echo "$GIT_AUTHOR_DATE" >expect.date && 386 git var GIT_AUTHOR_NAME >actual.name && 387 git var GIT_AUTHOR_EMAIL >actual.email && 388 git var GIT_AUTHOR_DATE >actual.date && 389 test_cmp expect.name actual.name && 390 test_cmp expect.email actual.email && 391 test_cmp expect.date actual.date 392 ' 393 394 test_expect_success 'get committer identity components' ' 395 test_tick && 396 echo "$GIT_COMMITTER_NAME" >expect.name && 397 echo "$GIT_COMMITTER_EMAIL" >expect.email && 398 echo "$GIT_COMMITTER_DATE" >expect.date && 399 git var GIT_COMMITTER_NAME >actual.name && 400 git var GIT_COMMITTER_EMAIL >actual.email && 401 git var GIT_COMMITTER_DATE >actual.date && 402 test_cmp expect.name actual.name && 403 test_cmp expect.email actual.email && 404 test_cmp expect.date actual.date 405 ' 406 407 test_expect_success !FAIL_PREREQS,!AUTOIDENT 'identity components are strict' ' 408 ( 409 sane_unset GIT_COMMITTER_NAME && 410 sane_unset GIT_COMMITTER_EMAIL && 411 test_must_fail git var GIT_COMMITTER_NAME 412 ) 413 ' 414 415 test_expect_success 'get several identity components at once' ' 416 test_tick && 417 cat >expect <<-EOF && 418 GIT_AUTHOR_NAME=$GIT_AUTHOR_NAME 419 GIT_AUTHOR_EMAIL=$GIT_AUTHOR_EMAIL 420 GIT_COMMITTER_NAME=$GIT_COMMITTER_NAME 421 GIT_COMMITTER_EMAIL=$GIT_COMMITTER_EMAIL 422 EOF 423 git var GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL >actual && 424 test_cmp expect actual 425 ' 426 427 test_expect_success 'git var -l lists the identity components' ' 428 git var -l >actual && 429 test_grep "^GIT_AUTHOR_NAME=" actual && 430 test_grep "^GIT_AUTHOR_EMAIL=" actual && 431 test_grep "^GIT_AUTHOR_DATE=" actual && 432 test_grep "^GIT_COMMITTER_NAME=" actual && 433 test_grep "^GIT_COMMITTER_EMAIL=" actual && 434 test_grep "^GIT_COMMITTER_DATE=" actual 435 ' 436 437 test_done