| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git repo-info' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | # Test whether a key-value pair is correctly returned |
| 8 | # |
| 9 | # Usage: test_repo_info <label> <init command> <repo_name> <key> <expected value> |
| 10 | # |
| 11 | # Arguments: |
| 12 | # label: the label of the test |
| 13 | # init_command: a command which creates a repository |
| 14 | # repo_name: the name of the repository that will be created in init_command |
| 15 | # key: the key of the field that is being tested |
| 16 | # expected_value: the value that the field should contain |
| 17 | test_repo_info () { |
| 18 | label=$1 |
| 19 | init_command=$2 |
| 20 | repo_name=$3 |
| 21 | key=$4 |
| 22 | expected_value=$5 |
| 23 | |
| 24 | test_expect_success "setup: $label" ' |
| 25 | eval "$init_command $repo_name" |
| 26 | ' |
| 27 | |
| 28 | test_expect_success "lines: $label" ' |
| 29 | echo "$key=$expected_value" > expect && |
| 30 | git -C "$repo_name" repo info "$key" >actual && |
| 31 | test_cmp expect actual |
| 32 | ' |
| 33 | |
| 34 | test_expect_success "nul: $label" ' |
| 35 | printf "%s\n%s\0" "$key" "$expected_value" >expect && |
| 36 | git -C "$repo_name" repo info --format=nul "$key" >actual && |
| 37 | test_cmp_bin expect actual |
| 38 | ' |
| 39 | } |
| 40 | |
| 41 | test_repo_info 'ref format files is retrieved correctly' \ |
| 42 | 'git init --ref-format=files' 'format-files' 'references.format' 'files' |
| 43 | |
| 44 | test_repo_info 'ref format reftable is retrieved correctly' \ |
| 45 | 'git init --ref-format=reftable' 'format-reftable' 'references.format' 'reftable' |
| 46 | |
| 47 | test_repo_info 'bare repository = false is retrieved correctly' \ |
| 48 | 'git init' 'nonbare' 'layout.bare' 'false' |
| 49 | |
| 50 | test_repo_info 'bare repository = true is retrieved correctly' \ |
| 51 | 'git init --bare' 'bare' 'layout.bare' 'true' |
| 52 | |
| 53 | test_repo_info 'shallow repository = false is retrieved correctly' \ |
| 54 | 'git init' 'nonshallow' 'layout.shallow' 'false' |
| 55 | |
| 56 | test_expect_success 'setup remote' ' |
| 57 | git init remote && |
| 58 | echo x >remote/x && |
| 59 | git -C remote add x && |
| 60 | git -C remote commit -m x |
| 61 | ' |
| 62 | |
| 63 | test_repo_info 'shallow repository = true is retrieved correctly' \ |
| 64 | 'git clone --depth 1 "file://$PWD/remote"' 'shallow' 'layout.shallow' 'true' |
| 65 | |
| 66 | test_repo_info 'object.format = sha1 is retrieved correctly' \ |
| 67 | 'git init --object-format=sha1' 'sha1' 'object.format' 'sha1' |
| 68 | |
| 69 | test_repo_info 'object.format = sha256 is retrieved correctly' \ |
| 70 | 'git init --object-format=sha256' 'sha256' 'object.format' 'sha256' |
| 71 | |
| 72 | test_expect_success 'values returned in order requested' ' |
| 73 | cat >expect <<-\EOF && |
| 74 | layout.bare=false |
| 75 | references.format=files |
| 76 | layout.bare=false |
| 77 | EOF |
| 78 | git init --ref-format=files ordered && |
| 79 | git -C ordered repo info layout.bare references.format layout.bare >actual && |
| 80 | test_cmp expect actual |
| 81 | ' |
| 82 | |
| 83 | test_expect_success 'git-repo-info fails if an invalid key is requested' ' |
| 84 | echo "error: key ${SQ}foo${SQ} not found" >expect && |
| 85 | test_must_fail git repo info foo 2>actual && |
| 86 | test_cmp expect actual |
| 87 | ' |
| 88 | |
| 89 | test_expect_success 'git-repo-info outputs data even if there is an invalid field' ' |
| 90 | echo "references.format=$(test_detect_ref_format)" >expect && |
| 91 | test_must_fail git repo info foo references.format bar >actual && |
| 92 | test_cmp expect actual |
| 93 | ' |
| 94 | |
| 95 | test_expect_success 'git-repo-info aborts when requesting an invalid format' ' |
| 96 | echo "fatal: invalid format ${SQ}foo${SQ}" >expect && |
| 97 | test_must_fail git repo info --format=foo 2>actual && |
| 98 | test_cmp expect actual |
| 99 | ' |
| 100 | |
| 101 | test_expect_success '-z uses nul-terminated format' ' |
| 102 | printf "layout.bare\nfalse\0layout.shallow\nfalse\0" >expected && |
| 103 | git repo info -z layout.bare layout.shallow >actual && |
| 104 | test_cmp expected actual |
| 105 | ' |
| 106 | |
| 107 | test_expect_success 'git repo info uses the last requested format' ' |
| 108 | echo "layout.bare=false" >expected && |
| 109 | git repo info --format=nul -z --format=lines layout.bare >actual && |
| 110 | test_cmp expected actual |
| 111 | ' |
| 112 | |
| 113 | test_expect_success 'git repo info --all and git repo info $(git repo info --keys) output the same data' ' |
| 114 | git repo info $(git repo info --keys) >expect && |
| 115 | git repo info --all >actual && |
| 116 | test_cmp expect actual |
| 117 | ' |
| 118 | |
| 119 | test_expect_success 'git repo info --all <key> aborts' ' |
| 120 | echo "fatal: --all and <key> cannot be used together" >expect && |
| 121 | test_must_fail git repo info --all object.format 2>actual && |
| 122 | test_cmp expect actual |
| 123 | ' |
| 124 | |
| 125 | test_expect_success 'git repo info --keys --format=nul uses nul-terminated output' ' |
| 126 | git repo info --keys --format=lines >lines && |
| 127 | lf_to_nul <lines >expect && |
| 128 | git repo info --keys --format=nul >actual && |
| 129 | test_cmp expect actual |
| 130 | ' |
| 131 | |
| 132 | test_expect_success 'git repo info --keys aborts when using --format other than lines or nul' ' |
| 133 | echo "fatal: --keys can only be used with --format=lines or --format=nul" >expect && |
| 134 | test_must_fail git repo info --keys --format=table 2>actual && |
| 135 | test_cmp expect actual |
| 136 | ' |
| 137 | |
| 138 | test_expect_success 'git repo info --keys aborts when requesting keys' ' |
| 139 | echo "fatal: --keys cannot be used with a <key> or --all" >expect && |
| 140 | test_must_fail git repo info --keys --all 2>actual_all && |
| 141 | test_must_fail git repo info --keys some.key 2>actual_key && |
| 142 | test_cmp expect actual_all && |
| 143 | test_cmp expect actual_key |
| 144 | ' |
| 145 | |
| 146 | test_expect_success 'git repo info --keys uses lines as its default output format' ' |
| 147 | git repo info --keys --format=lines >expect && |
| 148 | git repo info --keys >actual && |
| 149 | test_cmp expect actual |
| 150 | ' |
| 151 | |
| 152 | test_expect_success 'git repo info -h shows only repo info usage' ' |
| 153 | test_must_fail git repo info -h >actual && |
| 154 | test_grep "git repo info" actual && |
| 155 | test_grep ! "git repo structure" actual |
| 156 | ' |
| 157 | |
| 158 | test_done |