Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 1 | Core GIT Tests |
| 2 | ============== |
| 3 | |
| 4 | This directory holds many test scripts for core GIT tools. The |
| 5 | first part of this short document describes how to run the tests |
| 6 | and read their output. |
| 7 | |
| 8 | When fixing the tools or adding enhancements, you are strongly |
| 9 | encouraged to add tests in this directory to cover what you are |
| 10 | trying to fix or enhance. The later part of this short document |
| 11 | describes how your test scripts should be organized. |
| 12 | |
| 13 | |
| 14 | Running Tests |
| 15 | ------------- |
| 16 | |
| 17 | The easiest way to run tests is to say "make". This runs all |
| 18 | the tests. |
| 19 | |
| 20 | *** t0000-basic.sh *** |
Nicolas Pitre | 5c94f87 | 2007-01-12 16:01:46 -0500 | [diff] [blame] | 21 | * ok 1: .git/objects should be empty after git-init in an empty repo. |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 22 | * ok 2: .git/objects should have 256 subdirectories. |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 23 | * ok 3: git-update-index without --add should fail adding. |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 24 | ... |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 25 | * ok 23: no diff after checkout and git-update-index --refresh. |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 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 | |
| 32 | Or you can run each test individually from command line, like |
| 33 | this: |
| 34 | |
Junio C Hamano | 14cd1ff | 2005-05-15 14:21:13 -0700 | [diff] [blame] | 35 | $ sh ./t3001-ls-files-killed.sh |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 36 | * ok 1: git-update-index --add to add various paths. |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 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 | |
| 41 | You 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 | |
Petr Baudis | f50c9f7 | 2005-05-15 01:34:22 +0200 | [diff] [blame] | 58 | Naming Tests |
| 59 | ------------ |
| 60 | |
| 61 | The test files are named as: |
| 62 | |
| 63 | tNNNN-commandname-details.sh |
| 64 | |
| 65 | where N is a decimal digit. |
| 66 | |
| 67 | First 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) |
Junio C Hamano | 8f4a9b6 | 2006-06-28 11:45:52 -0700 | [diff] [blame] | 76 | 7 - the porcelainish commands concerning the working tree |
Jakub Narebski | 8757749 | 2006-12-29 14:39:09 +0100 | [diff] [blame] | 77 | 8 - the porcelainish commands concerning forensics |
| 78 | 9 - the git tools |
Petr Baudis | f50c9f7 | 2005-05-15 01:34:22 +0200 | [diff] [blame] | 79 | |
| 80 | Second digit tells the particular command we are testing. |
| 81 | |
| 82 | Third digit (optionally) tells the particular switch or group of switches |
| 83 | we are testing. |
| 84 | |
Junio C Hamano | 7765660 | 2005-07-07 11:39:10 -0700 | [diff] [blame] | 85 | If you create files under t/ directory (i.e. here) that is not |
| 86 | the top-level test script, never name the file to match the above |
| 87 | pattern. The Makefile here considers all such files as the |
| 88 | top-level test script and tries to run all of them. A care is |
| 89 | especially needed if you are creating a common test library |
| 90 | file, similar to test-lib.sh, because such a library file may |
| 91 | not be suitable for standalone execution. |
| 92 | |
Petr Baudis | f50c9f7 | 2005-05-15 01:34:22 +0200 | [diff] [blame] | 93 | |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 94 | Writing Tests |
| 95 | ------------- |
| 96 | |
| 97 | The test script is written as a shell script. It should start |
| 98 | with the standard "#!/bin/sh" with copyright notices, and an |
| 99 | assignment to variable 'test_description', like this: |
| 100 | |
| 101 | #!/bin/sh |
| 102 | # |
| 103 | # Copyright (c) 2005 Junio C Hamano |
| 104 | # |
| 105 | |
Junio C Hamano | 14cd1ff | 2005-05-15 14:21:13 -0700 | [diff] [blame] | 106 | test_description='xxx test (option --frotz) |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 107 | |
| 108 | This test registers the following structure in the cache |
| 109 | and tries to run git-ls-files with option --frotz.' |
| 110 | |
Petr Baudis | f50c9f7 | 2005-05-15 01:34:22 +0200 | [diff] [blame] | 111 | |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 112 | Source 'test-lib.sh' |
| 113 | -------------------- |
| 114 | |
| 115 | After assigning test_description, the test script should source |
| 116 | test-lib.sh like this: |
| 117 | |
| 118 | . ./test-lib.sh |
| 119 | |
| 120 | This test harness library does the following things: |
| 121 | |
| 122 | - If the script is invoked with command line argument --help |
| 123 | (or -h), it shows the test_description and exits. |
| 124 | |
| 125 | - Creates an empty test directory with an empty .git/objects |
| 126 | database and chdir(2) into it. This directory is 't/trash' |
| 127 | if you must know, but I do not think you care. |
| 128 | |
| 129 | - Defines standard test helper functions for your scripts to |
| 130 | use. These functions are designed to make all scripts behave |
| 131 | consistently when command line arguments --verbose (or -v), |
| 132 | --debug (or -d), and --immediate (or -i) is given. |
| 133 | |
Junio C Hamano | 14cd1ff | 2005-05-15 14:21:13 -0700 | [diff] [blame] | 134 | |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 135 | End with test_done |
| 136 | ------------------ |
| 137 | |
| 138 | Your script will be a sequence of tests, using helper functions |
| 139 | from the test harness library. At the end of the script, call |
| 140 | 'test_done'. |
| 141 | |
| 142 | |
| 143 | Test harness library |
| 144 | -------------------- |
| 145 | |
| 146 | There are a handful helper functions defined in the test harness |
| 147 | library for your script to use. |
| 148 | |
| 149 | - test_expect_success <message> <script> |
| 150 | |
| 151 | This takes two strings as parameter, and evaluates the |
| 152 | <script>. If it yields success, test is considered |
| 153 | successful. <message> should state what it is testing. |
| 154 | |
| 155 | Example: |
| 156 | |
| 157 | test_expect_success \ |
| 158 | 'git-write-tree should be able to write an empty tree.' \ |
| 159 | 'tree=$(git-write-tree)' |
| 160 | |
| 161 | - test_expect_failure <message> <script> |
| 162 | |
| 163 | This is the opposite of test_expect_success. If <script> |
| 164 | yields success, test is considered a failure. |
| 165 | |
| 166 | Example: |
| 167 | |
| 168 | test_expect_failure \ |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 169 | 'git-update-index without --add should fail adding.' \ |
| 170 | 'git-update-index should-be-empty' |
Junio C Hamano | 986aa7f | 2005-05-14 00:25:06 -0700 | [diff] [blame] | 171 | |
| 172 | - test_debug <script> |
| 173 | |
| 174 | This takes a single argument, <script>, and evaluates it only |
| 175 | when the test script is started with --debug command line |
| 176 | argument. This is primarily meant for use during the |
| 177 | development of a new test script. |
| 178 | |
| 179 | - test_done |
| 180 | |
| 181 | Your test script must have test_done at the end. Its purpose |
| 182 | is to summarize successes and failures in the test script and |
| 183 | exit with an appropriate error code. |
| 184 | |
| 185 | |
| 186 | Tips for Writing Tests |
| 187 | ---------------------- |
| 188 | |
| 189 | As with any programming projects, existing programs are the best |
| 190 | source of the information. However, do _not_ emulate |
| 191 | t0000-basic.sh when writing your tests. The test is special in |
| 192 | that it tries to validate the very core of GIT. For example, it |
| 193 | knows that there will be 256 subdirectories under .git/objects/, |
| 194 | and it knows that the object ID of an empty tree is a certain |
| 195 | 40-byte string. This is deliberately done so in t0000-basic.sh |
| 196 | because the things the very basic core test tries to achieve is |
| 197 | to serve as a basis for people who are changing the GIT internal |
| 198 | drastically. For these people, after making certain changes, |
| 199 | not seeing failures from the basic test _is_ a failure. And |
| 200 | such drastic changes to the core GIT that even changes these |
| 201 | otherwise supposedly stable object IDs should be accompanied by |
| 202 | an update to t0000-basic.sh. |
| 203 | |
| 204 | However, other tests that simply rely on basic parts of the core |
| 205 | GIT working properly should not have that level of intimate |
| 206 | knowledge of the core GIT internals. If all the test scripts |
| 207 | hardcoded the object IDs like t0000-basic.sh does, that defeats |
| 208 | the purpose of t0000-basic.sh, which is to isolate that level of |
| 209 | validation in one place. Your test also ends up needing |
| 210 | updating when such a change to the internal happens, so do _not_ |
| 211 | do it and leave the low level of validation to t0000-basic.sh. |