t / READMEon commit [PATCH 4/4] Trivial test harness fixes. (14cd1ff)
   1Core GIT Tests
   2==============
   3
   4This directory holds many test scripts for core GIT tools.  The
   5first part of this short document describes how to run the tests
   6and read their output.
   7
   8When fixing the tools or adding enhancements, you are strongly
   9encouraged to add tests in this directory to cover what you are
  10trying to fix or enhance.  The later part of this short document
  11describes how your test scripts should be organized.
  12
  13
  14Running Tests
  15-------------
  16
  17The easiest way to run tests is to say "make".  This runs all
  18the tests.
  19
  20    *** t0000-basic.sh ***
  21    *   ok 1: .git/objects should be empty after git-init-db in an empty repo.
  22    *   ok 2: .git/objects should have 256 subdirectories.
  23    *   ok 3: git-update-cache without --add should fail adding.
  24    ...
  25    *   ok 23: no diff after checkout and git-update-cache --refresh.
  26    * passed all 23 test(s)
  27    *** t0100-environment-names.sh ***
  28    *   ok 1: using old names should issue warnings.
  29    *   ok 2: using old names but having new names should not issue warnings.
  30    ...
  31
  32Or you can run each test individually from command line, like
  33this:
  34
  35    $ sh ./t3001-ls-files-killed.sh
  36    *   ok 1: git-update-cache --add to add various paths.
  37    *   ok 2: git-ls-files -k to show killed files.
  38    *   ok 3: validate git-ls-files -k output.
  39    * passed all 3 test(s)
  40
  41You can pass --verbose (or -v), --debug (or -d), and --immediate
  42(or -i) command line argument to the test.
  43
  44--verbose::
  45        This makes the test more verbose.  Specifically, the
  46        command being run and their output if any are also
  47        output.
  48
  49--debug::
  50        This may help the person who is developing a new test.
  51        It causes the command defined with test_debug to run.
  52
  53--immediate::
  54        This causes the test to immediately exit upon the first
  55        failed test.
  56
  57
  58Naming Tests
  59------------
  60
  61The test files are named as:
  62
  63        tNNNN-commandname-details.sh
  64
  65where N is a decimal digit.
  66
  67First digit tells the family:
  68
  69        0 - the absolute basics and global stuff
  70        1 - the basic commands concerning database
  71        2 - the basic commands concerning the working tree
  72        3 - the other basic commands (e.g. ls-files)
  73        4 - the diff commands
  74        5 - the pull and exporting commands
  75        6 - the revision tree commands (even e.g. merge-base)
  76
  77Second digit tells the particular command we are testing.
  78
  79Third digit (optionally) tells the particular switch or group of switches
  80we are testing.
  81
  82
  83Writing Tests
  84-------------
  85
  86The test script is written as a shell script.  It should start
  87with the standard "#!/bin/sh" with copyright notices, and an
  88assignment to variable 'test_description', like this:
  89
  90        #!/bin/sh
  91        #
  92        # Copyright (c) 2005 Junio C Hamano
  93        #
  94
  95        test_description='xxx test (option --frotz)
  96
  97        This test registers the following structure in the cache
  98        and tries to run git-ls-files with option --frotz.'
  99
 100
 101Source 'test-lib.sh'
 102--------------------
 103
 104After assigning test_description, the test script should source
 105test-lib.sh like this:
 106
 107        . ./test-lib.sh
 108
 109This test harness library does the following things:
 110
 111 - If the script is invoked with command line argument --help
 112   (or -h), it shows the test_description and exits.
 113
 114 - Creates an empty test directory with an empty .git/objects
 115   database and chdir(2) into it.  This directory is 't/trash'
 116   if you must know, but I do not think you care.
 117
 118 - Defines standard test helper functions for your scripts to
 119   use.  These functions are designed to make all scripts behave
 120   consistently when command line arguments --verbose (or -v),
 121   --debug (or -d), and --immediate (or -i) is given.
 122
 123
 124End with test_done
 125------------------
 126
 127Your script will be a sequence of tests, using helper functions
 128from the test harness library.  At the end of the script, call
 129'test_done'.
 130
 131
 132Test harness library
 133--------------------
 134
 135There are a handful helper functions defined in the test harness
 136library for your script to use.
 137
 138 - test_expect_success <message> <script>
 139
 140   This takes two strings as parameter, and evaluates the
 141   <script>.  If it yields success, test is considered
 142   successful.  <message> should state what it is testing.
 143
 144   Example:
 145
 146        test_expect_success \
 147            'git-write-tree should be able to write an empty tree.' \
 148            'tree=$(git-write-tree)'
 149
 150 - test_expect_failure <message> <script>
 151
 152   This is the opposite of test_expect_success.  If <script>
 153   yields success, test is considered a failure.
 154
 155   Example:
 156
 157        test_expect_failure \
 158            'git-update-cache without --add should fail adding.' \
 159            'git-update-cache should-be-empty'
 160
 161 - test_debug <script>
 162
 163   This takes a single argument, <script>, and evaluates it only
 164   when the test script is started with --debug command line
 165   argument.  This is primarily meant for use during the
 166   development of a new test script.
 167
 168 - test_done
 169
 170   Your test script must have test_done at the end.  Its purpose
 171   is to summarize successes and failures in the test script and
 172   exit with an appropriate error code.
 173
 174
 175Tips for Writing Tests
 176----------------------
 177
 178As with any programming projects, existing programs are the best
 179source of the information.  However, do _not_ emulate
 180t0000-basic.sh when writing your tests.  The test is special in
 181that it tries to validate the very core of GIT.  For example, it
 182knows that there will be 256 subdirectories under .git/objects/,
 183and it knows that the object ID of an empty tree is a certain
 18440-byte string.  This is deliberately done so in t0000-basic.sh
 185because the things the very basic core test tries to achieve is
 186to serve as a basis for people who are changing the GIT internal
 187drastically.  For these people, after making certain changes,
 188not seeing failures from the basic test _is_ a failure.  And
 189such drastic changes to the core GIT that even changes these
 190otherwise supposedly stable object IDs should be accompanied by
 191an update to t0000-basic.sh.
 192
 193However, other tests that simply rely on basic parts of the core
 194GIT working properly should not have that level of intimate
 195knowledge of the core GIT internals.  If all the test scripts
 196hardcoded the object IDs like t0000-basic.sh does, that defeats
 197the purpose of t0000-basic.sh, which is to isolate that level of
 198validation in one place.  Your test also ends up needing
 199updating when such a change to the internal happens, so do _not_
 200do it and leave the low level of validation to t0000-basic.sh.