t / t7411-submodule-config.shon commit Merge branch 'sg/t5318-cleanup' (5866e58)
   1#!/bin/sh
   2#
   3# Copyright (c) 2014 Heiko Voigt
   4#
   5
   6test_description='Test submodules config cache infrastructure
   7
   8This test verifies that parsing .gitmodules configurations directly
   9from the database and from the worktree works.
  10'
  11
  12TEST_NO_CREATE_REPO=1
  13. ./test-lib.sh
  14
  15test_expect_success 'submodule config cache setup' '
  16        mkdir submodule &&
  17        (cd submodule &&
  18                git init &&
  19                echo a >a &&
  20                git add . &&
  21                git commit -ma
  22        ) &&
  23        mkdir super &&
  24        (cd super &&
  25                git init &&
  26                git submodule add ../submodule &&
  27                git submodule add ../submodule a &&
  28                git commit -m "add as submodule and as a" &&
  29                git mv a b &&
  30                git commit -m "move a to b"
  31        )
  32'
  33
  34test_expect_success 'configuration parsing with error' '
  35        test_when_finished "rm -rf repo" &&
  36        test_create_repo repo &&
  37        cat >repo/.gitmodules <<-\EOF &&
  38        [submodule "s"]
  39                path
  40                ignore
  41        EOF
  42        (
  43                cd repo &&
  44                test_must_fail test-tool submodule-config "" s 2>actual &&
  45                test_i18ngrep "bad config" actual
  46        )
  47'
  48
  49cat >super/expect <<EOF
  50Submodule name: 'a' for path 'a'
  51Submodule name: 'a' for path 'b'
  52Submodule name: 'submodule' for path 'submodule'
  53Submodule name: 'submodule' for path 'submodule'
  54EOF
  55
  56test_expect_success 'test parsing and lookup of submodule config by path' '
  57        (cd super &&
  58                test-tool submodule-config \
  59                        HEAD^ a \
  60                        HEAD b \
  61                        HEAD^ submodule \
  62                        HEAD submodule \
  63                                >actual &&
  64                test_cmp expect actual
  65        )
  66'
  67
  68test_expect_success 'test parsing and lookup of submodule config by name' '
  69        (cd super &&
  70                test-tool submodule-config --name \
  71                        HEAD^ a \
  72                        HEAD a \
  73                        HEAD^ submodule \
  74                        HEAD submodule \
  75                                >actual &&
  76                test_cmp expect actual
  77        )
  78'
  79
  80cat >super/expect_error <<EOF
  81Submodule name: 'a' for path 'b'
  82Submodule name: 'submodule' for path 'submodule'
  83EOF
  84
  85test_expect_success 'error in history of one submodule config lets continue, stderr message contains blob ref' '
  86        ORIG=$(git -C super rev-parse HEAD) &&
  87        test_when_finished "git -C super reset --hard $ORIG" &&
  88        (cd super &&
  89                cp .gitmodules .gitmodules.bak &&
  90                echo "  value = \"" >>.gitmodules &&
  91                git add .gitmodules &&
  92                mv .gitmodules.bak .gitmodules &&
  93                git commit -m "add error" &&
  94                sha1=$(git rev-parse HEAD) &&
  95                test-tool submodule-config \
  96                        HEAD b \
  97                        HEAD submodule \
  98                                >actual \
  99                                2>actual_stderr &&
 100                test_cmp expect_error actual &&
 101                test_i18ngrep "submodule-blob $sha1:.gitmodules" actual_stderr >/dev/null
 102        )
 103'
 104
 105test_expect_success 'using different treeishs works' '
 106        (
 107                cd super &&
 108                git tag new_tag &&
 109                tree=$(git rev-parse HEAD^{tree}) &&
 110                commit=$(git rev-parse HEAD^{commit}) &&
 111                test-tool submodule-config $commit b >expect &&
 112                test-tool submodule-config $tree b >actual.1 &&
 113                test-tool submodule-config new_tag b >actual.2 &&
 114                test_cmp expect actual.1 &&
 115                test_cmp expect actual.2
 116        )
 117'
 118
 119test_expect_success 'error in history in fetchrecursesubmodule lets continue' '
 120        ORIG=$(git -C super rev-parse HEAD) &&
 121        test_when_finished "git -C super reset --hard $ORIG" &&
 122        (cd super &&
 123                git config -f .gitmodules \
 124                        submodule.submodule.fetchrecursesubmodules blabla &&
 125                git add .gitmodules &&
 126                git config --unset -f .gitmodules \
 127                        submodule.submodule.fetchrecursesubmodules &&
 128                git commit -m "add error in fetchrecursesubmodules" &&
 129                test-tool submodule-config \
 130                        HEAD b \
 131                        HEAD submodule \
 132                                >actual &&
 133                test_cmp expect_error actual
 134        )
 135'
 136
 137test_expect_success 'reading submodules config from the working tree with "submodule--helper config"' '
 138        (cd super &&
 139                echo "../submodule" >expect &&
 140                git submodule--helper config submodule.submodule.url >actual &&
 141                test_cmp expect actual
 142        )
 143'
 144
 145test_expect_success 'writing submodules config with "submodule--helper config"' '
 146        (cd super &&
 147                echo "new_url" >expect &&
 148                git submodule--helper config submodule.submodule.url "new_url" &&
 149                git submodule--helper config submodule.submodule.url >actual &&
 150                test_cmp expect actual
 151        )
 152'
 153
 154test_expect_success 'overwriting unstaged submodules config with "submodule--helper config"' '
 155        test_when_finished "git -C super checkout .gitmodules" &&
 156        (cd super &&
 157                echo "newer_url" >expect &&
 158                git submodule--helper config submodule.submodule.url "newer_url" &&
 159                git submodule--helper config submodule.submodule.url >actual &&
 160                test_cmp expect actual
 161        )
 162'
 163
 164test_expect_success 'writeable .gitmodules when it is in the working tree' '
 165        git -C super submodule--helper config --check-writeable
 166'
 167
 168test_expect_success 'writeable .gitmodules when it is nowhere in the repository' '
 169        ORIG=$(git -C super rev-parse HEAD) &&
 170        test_when_finished "git -C super reset --hard $ORIG" &&
 171        (cd super &&
 172                git rm .gitmodules &&
 173                git commit -m "remove .gitmodules from the current branch" &&
 174                git submodule--helper config --check-writeable
 175        )
 176'
 177
 178test_expect_success 'non-writeable .gitmodules when it is in the index but not in the working tree' '
 179        test_when_finished "git -C super checkout .gitmodules" &&
 180        (cd super &&
 181                rm -f .gitmodules &&
 182                test_must_fail git submodule--helper config --check-writeable
 183        )
 184'
 185
 186test_expect_success 'non-writeable .gitmodules when it is in the current branch but not in the index' '
 187        ORIG=$(git -C super rev-parse HEAD) &&
 188        test_when_finished "git -C super reset --hard $ORIG" &&
 189        (cd super &&
 190                git rm .gitmodules &&
 191                test_must_fail git submodule--helper config --check-writeable
 192        )
 193'
 194
 195test_expect_success 'reading submodules config from the index when .gitmodules is not in the working tree' '
 196        ORIG=$(git -C super rev-parse HEAD) &&
 197        test_when_finished "git -C super reset --hard $ORIG" &&
 198        (cd super &&
 199                git submodule--helper config submodule.submodule.url "staged_url" &&
 200                git add .gitmodules &&
 201                rm -f .gitmodules &&
 202                echo "staged_url" >expect &&
 203                git submodule--helper config submodule.submodule.url >actual &&
 204                test_cmp expect actual
 205        )
 206'
 207
 208test_expect_success 'reading submodules config from the current branch when .gitmodules is not in the index' '
 209        ORIG=$(git -C super rev-parse HEAD) &&
 210        test_when_finished "git -C super reset --hard $ORIG" &&
 211        (cd super &&
 212                git rm .gitmodules &&
 213                echo "../submodule" >expect &&
 214                git submodule--helper config submodule.submodule.url >actual &&
 215                test_cmp expect actual
 216        )
 217'
 218
 219test_expect_success 'reading nested submodules config' '
 220        (cd super &&
 221                git init submodule/nested_submodule &&
 222                echo "a" >submodule/nested_submodule/a &&
 223                git -C submodule/nested_submodule add a &&
 224                git -C submodule/nested_submodule commit -m "add a" &&
 225                git -C submodule submodule add ./nested_submodule &&
 226                git -C submodule add nested_submodule &&
 227                git -C submodule commit -m "added nested_submodule" &&
 228                git add submodule &&
 229                git commit -m "updated submodule" &&
 230                echo "./nested_submodule" >expect &&
 231                test-tool submodule-nested-repo-config \
 232                        submodule submodule.nested_submodule.url >actual &&
 233                test_cmp expect actual
 234        )
 235'
 236
 237# When this test eventually passes, before turning it into
 238# test_expect_success, remember to replace the test_i18ngrep below with
 239# a "test_must_be_empty warning" to be sure that the warning is actually
 240# removed from the code.
 241test_expect_failure 'reading nested submodules config when .gitmodules is not in the working tree' '
 242        test_when_finished "git -C super/submodule checkout .gitmodules" &&
 243        (cd super &&
 244                echo "./nested_submodule" >expect &&
 245                rm submodule/.gitmodules &&
 246                test-tool submodule-nested-repo-config \
 247                        submodule submodule.nested_submodule.url >actual 2>warning &&
 248                test_i18ngrep "nested submodules without %s in the working tree are not supported yet" warning &&
 249                test_cmp expect actual
 250        )
 251'
 252
 253test_done