t / READMEon commit Add valgrind support in test scripts (4e1be63)
   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 in an empty repo.
  22    *   ok 2: .git/objects should have 256 subdirectories.
  23    *   ok 3: git-update-index without --add should fail adding.
  24    ...
  25    *   ok 23: no diff after checkout and git-update-index --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-index --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, or by setting GIT_TEST_OPTS
  43appropriately before running "make".
  44
  45--verbose::
  46        This makes the test more verbose.  Specifically, the
  47        command being run and their output if any are also
  48        output.
  49
  50--debug::
  51        This may help the person who is developing a new test.
  52        It causes the command defined with test_debug to run.
  53
  54--immediate::
  55        This causes the test to immediately exit upon the first
  56        failed test.
  57
  58--long-tests::
  59        This causes additional long-running tests to be run (where
  60        available), for more exhaustive testing.
  61
  62--valgrind::
  63        Execute all Git binaries with valgrind and exit with status
  64        126 on errors (just like regular tests, this will only stop
  65        the test script when running under -i).  Valgrind errors
  66        go to stderr, so you might want to pass the -v option, too.
  67
  68Skipping Tests
  69--------------
  70
  71In some environments, certain tests have no way of succeeding
  72due to platform limitation, such as lack of 'unzip' program, or
  73filesystem that do not allow arbitrary sequence of non-NUL bytes
  74as pathnames.
  75
  76You should be able to say something like
  77
  78    $ GIT_SKIP_TESTS=t9200.8 sh ./t9200-git-cvsexport-commit.sh
  79
  80and even:
  81
  82    $ GIT_SKIP_TESTS='t[0-4]??? t91?? t9200.8' make
  83
  84to omit such tests.  The value of the environment variable is a
  85SP separated list of patterns that tells which tests to skip,
  86and either can match the "t[0-9]{4}" part to skip the whole
  87test, or t[0-9]{4} followed by ".$number" to say which
  88particular test to skip.
  89
  90Note that some tests in the existing test suite rely on previous
  91test item, so you cannot arbitrarily disable one and expect the
  92remainder of test to check what the test originally was intended
  93to check.
  94
  95
  96Naming Tests
  97------------
  98
  99The test files are named as:
 100
 101        tNNNN-commandname-details.sh
 102
 103where N is a decimal digit.
 104
 105First digit tells the family:
 106
 107        0 - the absolute basics and global stuff
 108        1 - the basic commands concerning database
 109        2 - the basic commands concerning the working tree
 110        3 - the other basic commands (e.g. ls-files)
 111        4 - the diff commands
 112        5 - the pull and exporting commands
 113        6 - the revision tree commands (even e.g. merge-base)
 114        7 - the porcelainish commands concerning the working tree
 115        8 - the porcelainish commands concerning forensics
 116        9 - the git tools
 117
 118Second digit tells the particular command we are testing.
 119
 120Third digit (optionally) tells the particular switch or group of switches
 121we are testing.
 122
 123If you create files under t/ directory (i.e. here) that is not
 124the top-level test script, never name the file to match the above
 125pattern.  The Makefile here considers all such files as the
 126top-level test script and tries to run all of them.  A care is
 127especially needed if you are creating a common test library
 128file, similar to test-lib.sh, because such a library file may
 129not be suitable for standalone execution.
 130
 131
 132Writing Tests
 133-------------
 134
 135The test script is written as a shell script.  It should start
 136with the standard "#!/bin/sh" with copyright notices, and an
 137assignment to variable 'test_description', like this:
 138
 139        #!/bin/sh
 140        #
 141        # Copyright (c) 2005 Junio C Hamano
 142        #
 143
 144        test_description='xxx test (option --frotz)
 145
 146        This test registers the following structure in the cache
 147        and tries to run git-ls-files with option --frotz.'
 148
 149
 150Source 'test-lib.sh'
 151--------------------
 152
 153After assigning test_description, the test script should source
 154test-lib.sh like this:
 155
 156        . ./test-lib.sh
 157
 158This test harness library does the following things:
 159
 160 - If the script is invoked with command line argument --help
 161   (or -h), it shows the test_description and exits.
 162
 163 - Creates an empty test directory with an empty .git/objects
 164   database and chdir(2) into it.  This directory is 't/trash directory'
 165   if you must know, but I do not think you care.
 166
 167 - Defines standard test helper functions for your scripts to
 168   use.  These functions are designed to make all scripts behave
 169   consistently when command line arguments --verbose (or -v),
 170   --debug (or -d), and --immediate (or -i) is given.
 171
 172
 173End with test_done
 174------------------
 175
 176Your script will be a sequence of tests, using helper functions
 177from the test harness library.  At the end of the script, call
 178'test_done'.
 179
 180
 181Test harness library
 182--------------------
 183
 184There are a handful helper functions defined in the test harness
 185library for your script to use.
 186
 187 - test_expect_success <message> <script>
 188
 189   This takes two strings as parameter, and evaluates the
 190   <script>.  If it yields success, test is considered
 191   successful.  <message> should state what it is testing.
 192
 193   Example:
 194
 195        test_expect_success \
 196            'git-write-tree should be able to write an empty tree.' \
 197            'tree=$(git-write-tree)'
 198
 199 - test_expect_failure <message> <script>
 200
 201   This is NOT the opposite of test_expect_success, but is used
 202   to mark a test that demonstrates a known breakage.  Unlike
 203   the usual test_expect_success tests, which say "ok" on
 204   success and "FAIL" on failure, this will say "FIXED" on
 205   success and "still broken" on failure.  Failures from these
 206   tests won't cause -i (immediate) to stop.
 207
 208 - test_debug <script>
 209
 210   This takes a single argument, <script>, and evaluates it only
 211   when the test script is started with --debug command line
 212   argument.  This is primarily meant for use during the
 213   development of a new test script.
 214
 215 - test_done
 216
 217   Your test script must have test_done at the end.  Its purpose
 218   is to summarize successes and failures in the test script and
 219   exit with an appropriate error code.
 220
 221 - test_tick
 222
 223   Make commit and tag names consistent by setting the author and
 224   committer times to defined stated.  Subsequent calls will
 225   advance the times by a fixed amount.
 226
 227 - test_commit <message> [<filename> [<contents>]]
 228
 229   Creates a commit with the given message, committing the given
 230   file with the given contents (default for both is to reuse the
 231   message string), and adds a tag (again reusing the message
 232   string as name).  Calls test_tick to make the SHA-1s
 233   reproducible.
 234
 235 - test_merge <message> <commit-or-tag>
 236
 237   Merges the given rev using the given message.  Like test_commit,
 238   creates a tag and calls test_tick before committing.
 239
 240Tips for Writing Tests
 241----------------------
 242
 243As with any programming projects, existing programs are the best
 244source of the information.  However, do _not_ emulate
 245t0000-basic.sh when writing your tests.  The test is special in
 246that it tries to validate the very core of GIT.  For example, it
 247knows that there will be 256 subdirectories under .git/objects/,
 248and it knows that the object ID of an empty tree is a certain
 24940-byte string.  This is deliberately done so in t0000-basic.sh
 250because the things the very basic core test tries to achieve is
 251to serve as a basis for people who are changing the GIT internal
 252drastically.  For these people, after making certain changes,
 253not seeing failures from the basic test _is_ a failure.  And
 254such drastic changes to the core GIT that even changes these
 255otherwise supposedly stable object IDs should be accompanied by
 256an update to t0000-basic.sh.
 257
 258However, other tests that simply rely on basic parts of the core
 259GIT working properly should not have that level of intimate
 260knowledge of the core GIT internals.  If all the test scripts
 261hardcoded the object IDs like t0000-basic.sh does, that defeats
 262the purpose of t0000-basic.sh, which is to isolate that level of
 263validation in one place.  Your test also ends up needing
 264updating when such a change to the internal happens, so do _not_
 265do it and leave the low level of validation to t0000-basic.sh.