[PATCH] Introduce unit tests for git-rev-list --bisect
[git.git] / t / t6000-lib.sh
1 [ -d .git/refs/tags ] || mkdir -p .git/refs/tags
2
3 sed_script="";
4
5 # Answer the sha1 has associated with the tag. The tag must exist in .git or .git/refs/tags
6 tag()
7 {
8         _tag=$1
9         [ -f .git/refs/tags/$_tag ] || error "tag: \"$_tag\" does not exist"
10         cat .git/refs/tags/$_tag
11 }
12
13 # Generate a commit using the text specified to make it unique and the tree
14 # named by the tag specified.
15 unique_commit()
16 {
17         _text=$1
18         _tree=$2
19         shift 2
20         echo $_text | git-commit-tree $(tag $_tree) "$@"
21 }
22
23 # Save the output of a command into the tag specified. Prepend
24 # a substitution script for the tag onto the front of $sed_script
25 save_tag()
26 {
27         _tag=$1 
28         [ -n "$_tag" ] || error "usage: save_tag tag commit-args ..."
29         shift 1
30         "$@" >.git/refs/tags/$_tag
31         sed_script="s/$(tag $_tag)/$_tag/g${sed_script+;}$sed_script"
32 }
33
34 # Replace unhelpful sha1 hashses with their symbolic equivalents 
35 entag()
36 {
37         sed "$sed_script"
38 }
39
40 # Execute a command after first saving, then setting the GIT_AUTHOR_EMAIL
41 # tag to a specified value. Restore the original value on return.
42 as_author()
43 {
44         _author=$1
45         shift 1
46         _save=$GIT_AUTHOR_EMAIL
47
48         export GIT_AUTHOR_EMAIL="$_author"
49         "$@"
50         export GIT_AUTHOR_EMAIL="$_save"
51 }
52
53 commit_date()
54 {
55         _commit=$1
56         git-cat-file commit $_commit | sed -n "s/^committer .*> \([0-9]*\) .*/\1/p" 
57 }
58
59 on_committer_date()
60 {
61     _date=$1
62     shift 1
63     GIT_COMMITTER_DATE=$_date "$@"
64 }
65
66 # Execute a command and suppress any error output.
67 hide_error()
68 {
69         "$@" 2>/dev/null
70 }
71
72 check_output()
73 {
74         _name=$1
75         shift 1
76         if eval "$*" | entag > $_name.actual
77         then
78                 diff $_name.expected $_name.actual
79         else
80                 return 1;
81         fi
82 }
83
84 # Turn a reasonable test description into a reasonable test name.
85 # All alphanums translated into -'s which are then compressed and stripped
86 # from front and back.
87 name_from_description()
88 {
89         tr "'" '-' | tr '~`!@#$%^&*()_+={}[]|\;:"<>,/? ' '-' | tr -s '-' | tr '[A-Z]' '[a-z]' | sed "s/^-*//;s/-*\$//"
90 }
91
92
93 # Execute the test described by the first argument, by eval'ing
94 # command line specified in the 2nd argument. Check the status code
95 # is zero and that the output matches the stream read from 
96 # stdin.
97 test_output_expect_success()
98 {       
99         _description=$1
100         _test=$2
101         [ $# -eq 2 ] || error "usage: test_output_expect_success description test <<EOF ... EOF"
102         _name=$(echo $_description | name_from_description)
103         cat > $_name.expected
104         test_expect_success "$_description" "check_output $_name \"$_test\"" 
105 }