t / t3600-rm.shon commit format_sanitized_subject: Don't trim past initial length of strbuf (871d21d)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Carl D. Worth
   4#
   5
   6test_description='Test of the various options to git rm.'
   7
   8. ./test-lib.sh
   9
  10# Setup some files to be removed, some with funny characters
  11test_expect_success \
  12    'Initialize test directory' \
  13    "touch -- foo bar baz 'space embedded' -q &&
  14     git add -- foo bar baz 'space embedded' -q &&
  15     git commit -m 'add normal files' &&
  16     test_tabs=y &&
  17     if touch -- 'tab   embedded' 'newline
  18embedded'
  19     then
  20     git add -- 'tab    embedded' 'newline
  21embedded' &&
  22     git commit -m 'add files with tabs and newlines'
  23     else
  24         test_tabs=n
  25     fi"
  26
  27test "$test_tabs" = n && say 'Your filesystem does not allow tabs in filenames.'
  28
  29# Later we will try removing an unremovable path to make sure
  30# git rm barfs, but if the test is run as root that cannot be
  31# arranged.
  32test_expect_success \
  33    'Determine rm behavior' \
  34    ': >test-file
  35     chmod a-w .
  36     rm -f test-file
  37     test -f test-file && test_failed_remove=y
  38     chmod 775 .
  39     rm -f test-file'
  40
  41test_expect_success \
  42    'Pre-check that foo exists and is in index before git rm foo' \
  43    '[ -f foo ] && git ls-files --error-unmatch foo'
  44
  45test_expect_success \
  46    'Test that git rm foo succeeds' \
  47    'git rm --cached foo'
  48
  49test_expect_success \
  50    'Test that git rm --cached foo succeeds if the index matches the file' \
  51    'echo content > foo
  52     git add foo
  53     git rm --cached foo'
  54
  55test_expect_success \
  56    'Test that git rm --cached foo succeeds if the index matches the file' \
  57    'echo content > foo
  58     git add foo
  59     git commit -m foo
  60     echo "other content" > foo
  61     git rm --cached foo'
  62
  63test_expect_success \
  64    'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
  65     echo content > foo
  66     git add foo
  67     git commit -m foo
  68     echo "other content" > foo
  69     git add foo
  70     echo "yet another content" > foo
  71     test_must_fail git rm --cached foo
  72'
  73
  74test_expect_success \
  75    'Test that git rm --cached -f foo works in case where --cached only did not' \
  76    'echo content > foo
  77     git add foo
  78     git commit -m foo
  79     echo "other content" > foo
  80     git add foo
  81     echo "yet another content" > foo
  82     git rm --cached -f foo'
  83
  84test_expect_success \
  85    'Post-check that foo exists but is not in index after git rm foo' \
  86    '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
  87
  88test_expect_success \
  89    'Pre-check that bar exists and is in index before "git rm bar"' \
  90    '[ -f bar ] && git ls-files --error-unmatch bar'
  91
  92test_expect_success \
  93    'Test that "git rm bar" succeeds' \
  94    'git rm bar'
  95
  96test_expect_success \
  97    'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
  98    '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
  99
 100test_expect_success \
 101    'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
 102    'git rm -- -q'
 103
 104test "$test_tabs" = y && test_expect_success \
 105    "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
 106    "git rm -f 'space embedded' 'tab    embedded' 'newline
 107embedded'"
 108
 109if test "$test_failed_remove" = y; then
 110chmod a-w .
 111test_expect_success \
 112    'Test that "git rm -f" fails if its rm fails' \
 113    'test_must_fail git rm -f baz'
 114chmod 775 .
 115else
 116    say 'skipping removal failure test (perhaps running as root?)'
 117fi
 118
 119test_expect_success \
 120    'When the rm in "git rm -f" fails, it should not remove the file from the index' \
 121    'git ls-files --error-unmatch baz'
 122
 123test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
 124        git rm --ignore-unmatch nonexistent
 125'
 126
 127test_expect_success '"rm" command printed' '
 128        echo frotz > test-file &&
 129        git add test-file &&
 130        git commit -m "add file for rm test" &&
 131        git rm test-file > rm-output &&
 132        test `grep "^rm " rm-output | wc -l` = 1 &&
 133        rm -f test-file rm-output &&
 134        git commit -m "remove file from rm test"
 135'
 136
 137test_expect_success '"rm" command suppressed with --quiet' '
 138        echo frotz > test-file &&
 139        git add test-file &&
 140        git commit -m "add file for rm --quiet test" &&
 141        git rm --quiet test-file > rm-output &&
 142        test `wc -l < rm-output` = 0 &&
 143        rm -f test-file rm-output &&
 144        git commit -m "remove file from rm --quiet test"
 145'
 146
 147# Now, failure cases.
 148test_expect_success 'Re-add foo and baz' '
 149        git add foo baz &&
 150        git ls-files --error-unmatch foo baz
 151'
 152
 153test_expect_success 'Modify foo -- rm should refuse' '
 154        echo >>foo &&
 155        test_must_fail git rm foo baz &&
 156        test -f foo &&
 157        test -f baz &&
 158        git ls-files --error-unmatch foo baz
 159'
 160
 161test_expect_success 'Modified foo -- rm -f should work' '
 162        git rm -f foo baz &&
 163        test ! -f foo &&
 164        test ! -f baz &&
 165        test_must_fail git ls-files --error-unmatch foo &&
 166        test_must_fail git ls-files --error-unmatch bar
 167'
 168
 169test_expect_success 'Re-add foo and baz for HEAD tests' '
 170        echo frotz >foo &&
 171        git checkout HEAD -- baz &&
 172        git add foo baz &&
 173        git ls-files --error-unmatch foo baz
 174'
 175
 176test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
 177        test_must_fail git rm foo baz &&
 178        test -f foo &&
 179        test -f baz &&
 180        git ls-files --error-unmatch foo baz
 181'
 182
 183test_expect_success 'but with -f it should work.' '
 184        git rm -f foo baz &&
 185        test ! -f foo &&
 186        test ! -f baz &&
 187        test_must_fail git ls-files --error-unmatch foo
 188        test_must_fail git ls-files --error-unmatch baz
 189'
 190
 191test_expect_success 'refuse to remove cached empty file with modifications' '
 192        >empty &&
 193        git add empty &&
 194        echo content >empty &&
 195        test_must_fail git rm --cached empty
 196'
 197
 198test_expect_success 'remove intent-to-add file without --force' '
 199        echo content >intent-to-add &&
 200        git add -N intent-to-add
 201        git rm --cached intent-to-add
 202'
 203
 204test_expect_success 'Recursive test setup' '
 205        mkdir -p frotz &&
 206        echo qfwfq >frotz/nitfol &&
 207        git add frotz &&
 208        git commit -m "subdir test"
 209'
 210
 211test_expect_success 'Recursive without -r fails' '
 212        test_must_fail git rm frotz &&
 213        test -d frotz &&
 214        test -f frotz/nitfol
 215'
 216
 217test_expect_success 'Recursive with -r but dirty' '
 218        echo qfwfq >>frotz/nitfol
 219        test_must_fail git rm -r frotz &&
 220        test -d frotz &&
 221        test -f frotz/nitfol
 222'
 223
 224test_expect_success 'Recursive with -r -f' '
 225        git rm -f -r frotz &&
 226        ! test -f frotz/nitfol &&
 227        ! test -d frotz
 228'
 229
 230test_expect_success 'Remove nonexistent file returns nonzero exit status' '
 231        test_must_fail git rm nonexistent
 232'
 233
 234test_expect_success 'Call "rm" from outside the work tree' '
 235        mkdir repo &&
 236        (cd repo &&
 237         git init &&
 238         echo something > somefile &&
 239         git add somefile &&
 240         git commit -m "add a file" &&
 241         (cd .. &&
 242          git --git-dir=repo/.git --work-tree=repo rm somefile) &&
 243        test_must_fail git ls-files --error-unmatch somefile)
 244'
 245
 246test_expect_success 'refresh index before checking if it is up-to-date' '
 247
 248        git reset --hard &&
 249        test-chmtime -86400 frotz/nitfol &&
 250        git rm frotz/nitfol &&
 251        test ! -f frotz/nitfol
 252
 253'
 254
 255test_expect_success 'choking "git rm" should not let it die with cruft' '
 256        git reset -q --hard &&
 257        H=0000000000000000000000000000000000000000 &&
 258        i=0 &&
 259        while test $i -lt 12000
 260        do
 261            echo "100644 $H 0   some-file-$i"
 262            i=$(( $i + 1 ))
 263        done | git update-index --index-info &&
 264        git rm -n "some-file-*" | :;
 265        test -f .git/index.lock
 266        status=$?
 267        rm -f .git/index.lock
 268        git reset -q --hard
 269        test "$status" != 0
 270'
 271
 272test_done