@@ -401,13 +401,13 @@ This test harness library does the following things:
401401 consistently when command line arguments --verbose (or -v),
402402 --debug (or -d), and --immediate (or -i) is given.
403403
404- Do's, don'ts & things to keep in mind
405- -------------------------------------
404+ Do's & don'ts
405+ -------------
406406
407407Here are a few examples of things you probably should and shouldn't do
408408when writing tests.
409409
410- Do:
410+ Here are the "do's:"
411411
412412 - Put all code inside test_expect_success and other assertions.
413413
@@ -452,43 +452,75 @@ Do:
452452 Windows, where the shell (MSYS bash) mangles absolute path names.
453453 For details, see the commit message of 4114156ae9.
454454
455- Don't:
455+ - Remember that inside the <script> part, the standard output and
456+ standard error streams are discarded, and the test harness only
457+ reports "ok" or "not ok" to the end user running the tests. Under
458+ --verbose, they are shown to help debug the tests.
459+
460+ And here are the "don'ts:"
456461
457- - exit() within a <script> part.
462+ - Don't exit() within a <script> part.
458463
459464 The harness will catch this as a programming error of the test.
460465 Use test_done instead if you need to stop the tests early (see
461466 "Skipping tests" below).
462467
463- - use '! git cmd' when you want to make sure the git command exits
464- with failure in a controlled way by calling "die()". Instead,
468+ - Don't use '! git cmd' when you want to make sure the git command
469+ exits with failure in a controlled way by calling "die()". Instead,
465470 use 'test_must_fail git cmd'. This will signal a failure if git
466471 dies in an unexpected way (e.g. segfault).
467472
468473 On the other hand, don't use test_must_fail for running regular
469474 platform commands; just use '! cmd'. We are not in the business
470475 of verifying that the world given to us sanely works.
471476
472- - use perl without spelling it as "$PERL_PATH". This is to help our
473- friends on Windows where the platform Perl often adds CR before
477+ - Don't feed the output of a git command to a pipe, as in:
478+
479+ git -C repo ls-files |
480+ xargs -n 1 basename |
481+ grep foo
482+
483+ which will discard git's exit code and may mask a crash. In the
484+ above example, all exit codes are ignored except grep's.
485+
486+ Instead, write the output of that command to a temporary
487+ file with ">" or assign it to a variable with "x=$(git ...)" rather
488+ than pipe it.
489+
490+ - Don't use command substitution in a way that discards git's exit
491+ code. When assigning to a variable, the exit code is not discarded,
492+ e.g.:
493+
494+ x=$(git cat-file -p $sha) &&
495+ ...
496+
497+ is OK because a crash in "git cat-file" will cause the "&&" chain
498+ to fail, but:
499+
500+ test "refs/heads/foo" = "$(git symbolic-ref HEAD)"
501+
502+ is not OK and a crash in git could go undetected.
503+
504+ - Don't use perl without spelling it as "$PERL_PATH". This is to help
505+ our friends on Windows where the platform Perl often adds CR before
474506 the end of line, and they bundle Git with a version of Perl that
475507 does not do so, whose path is specified with $PERL_PATH. Note that we
476508 provide a "perl" function which uses $PERL_PATH under the hood, so
477509 you do not need to worry when simply running perl in the test scripts
478510 (but you do, for example, on a shebang line or in a sub script
479511 created via "write_script").
480512
481- - use sh without spelling it as "$SHELL_PATH", when the script can
482- be misinterpreted by broken platform shell (e.g. Solaris).
513+ - Don't use sh without spelling it as "$SHELL_PATH", when the script
514+ can be misinterpreted by broken platform shell (e.g. Solaris).
483515
484- - chdir around in tests. It is not sufficient to chdir to
516+ - Don't chdir around in tests. It is not sufficient to chdir to
485517 somewhere and then chdir back to the original location later in
486518 the test, as any intermediate step can fail and abort the test,
487519 causing the next test to start in an unexpected directory. Do so
488520 inside a subshell if necessary.
489521
490- - save and verify the standard error of compound commands, i.e. group
491- commands, subshells, and shell functions (except test helper
522+ - Don't save and verify the standard error of compound commands, i.e.
523+ group commands, subshells, and shell functions (except test helper
492524 functions like 'test_must_fail') like this:
493525
494526 ( cd dir && git cmd ) 2>error &&
@@ -503,7 +535,7 @@ Don't:
503535 ( cd dir && git cmd 2>../error ) &&
504536 test_cmp expect error
505537
506- - Break the TAP output
538+ - Don't break the TAP output
507539
508540 The raw output from your test may be interpreted by a TAP harness. TAP
509541 harnesses will ignore everything they don't know about, but don't step
@@ -523,13 +555,6 @@ Don't:
523555 but the best indication is to just run the tests with prove(1),
524556 it'll complain if anything is amiss.
525557
526- Keep in mind:
527-
528- - Inside the <script> part, the standard output and standard error
529- streams are discarded, and the test harness only reports "ok" or
530- "not ok" to the end user running the tests. Under --verbose, they
531- are shown to help debugging the tests.
532-
533558
534559Skipping tests
535560--------------
0 commit comments