Skip to content

Commit 118861f

Browse files
committed
uninstall: try to determine initialization file
uninstall: TIL that `fgrep` is deprecated...
1 parent 444cb8f commit 118861f

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

test/install/uninstall.bats

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,37 @@
22

33
load ../test_helper
44

5-
# Determine which config file to use based on OS.
6-
case $OSTYPE in
7-
darwin*)
8-
export BASH_IT_CONFIG_FILE=.bash_profile
9-
;;
10-
*)
11-
export BASH_IT_CONFIG_FILE=.bashrc
12-
;;
13-
esac
14-
155
function local_setup {
166
setup_test_fixture
177
}
188

19-
@test "uninstall: verify that the uninstall script exists" {
20-
assert_file_exist "$BASH_IT/uninstall.sh"
9+
@test "uninstall: run the uninstall script with existing backup 'bashrc'" {
10+
BASH_IT_CONFIG_FILE=.bashrc
11+
12+
echo "test file content for backup" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.bak"
13+
echo "test file content for original BASH_IT file" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
14+
local md5_bak=$(md5sum "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')
15+
16+
run "${BASH_IT?}/uninstall.sh"
17+
assert_success
18+
19+
assert_file_not_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.uninstall"
20+
assert_file_not_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.bak"
21+
assert_file_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
22+
23+
local md5_conf=$(md5sum "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
24+
25+
assert_equal "$md5_bak" "$md5_conf"
2126
}
2227

23-
@test "uninstall: run the uninstall script with an existing backup file" {
24-
cd "$BASH_IT"
28+
@test "uninstall: run the uninstall script with existing backup 'bash_profile'" {
29+
BASH_IT_CONFIG_FILE=.bash_profile
2530

2631
echo "test file content for backup" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.bak"
27-
echo "test file content for original file" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
32+
echo "test file content for original BASH_IT file" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
2833
local md5_bak=$(md5sum "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')
2934

30-
./uninstall.sh
31-
35+
run "${BASH_IT?}/uninstall.sh"
3236
assert_success
3337

3438
assert_file_not_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.uninstall"
@@ -40,14 +44,31 @@ function local_setup {
4044
assert_equal "$md5_bak" "$md5_conf"
4145
}
4246

43-
@test "uninstall: run the uninstall script without an existing backup file" {
44-
cd "$BASH_IT"
47+
@test "uninstall: run the uninstall script without existing backup 'bashrc" {
48+
BASH_IT_CONFIG_FILE=.bashrc
4549

46-
echo "test file content for original file" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
50+
echo "test file content for original BASH_IT file" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
4751
local md5_orig=$(md5sum "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
4852

49-
./uninstall.sh
53+
run "${BASH_IT?}/uninstall.sh"
54+
assert_success
55+
56+
assert_file_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.uninstall"
57+
assert_file_not_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.bak"
58+
assert_file_not_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
59+
60+
local md5_uninstall=$(md5sum "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.uninstall" | awk '{print $1}')
61+
62+
assert_equal "$md5_orig" "$md5_uninstall"
63+
}
64+
65+
@test "uninstall: run the uninstall script without existing backup 'bash_profile" {
66+
BASH_IT_CONFIG_FILE=.bash_profile
67+
68+
echo "test file content for original BASH_IT file" > "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE"
69+
local md5_orig=$(md5sum "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
5070

71+
run "${BASH_IT?}/uninstall.sh"
5172
assert_success
5273

5374
assert_file_exist "$BASH_IT_TEST_HOME/$BASH_IT_CONFIG_FILE.uninstall"

uninstall.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,30 @@
55

66
: "${BASH_IT:=${HOME?}/.bash_it}"
77

8-
CONFIG_FILE=".bashrc"
9-
if [[ ! -e ~/.bashrc && -e ~/.bash_profile ]]; then
10-
# legacy Mac or WSL or just no backup file
8+
if [[ ! -e ~/.bashrc && ! -e ~/.bash_profile && ! -e ~/.bashrc.bak && ! -e ~/.bash_profile.bak ]]; then
9+
echo "We can't locate your configuration files, so we can't uninstall..."
10+
return
11+
elif grep -F -q -- BASH_IT ~/.bashrc && grep -F -q -- BASH_IT ~/.bash_profile; then
12+
echo "We can't figure out if Bash-it is loaded from ~/.bashrc or ~/.bash_profile..."
13+
return
14+
elif grep -F -q -- BASH_IT ~/.bashrc || [[ -e ~/.bashrc.bak && ! -e ~/.bashrc ]]; then
15+
CONFIG_FILE=".bashrc"
16+
elif grep -F -q -- BASH_IT ~/.bash_profile || [[ -e ~/.bash_profile.bak && ! -e ~/.bash_profile ]]; then
1117
CONFIG_FILE=".bash_profile"
18+
else
19+
echo "Bash-it does not appear to be installed."
20+
return
1221
fi
1322

23+
# possible states:
24+
# - both .bash* /and/ .bash*.bak, /and/ both config reference `$BASH_IT`: no solution
25+
# - both config and bak, but only one references `$BASH_IT`: that one
26+
# - both config, only one bak, but other references `$BASH_IT`: the other one?
27+
# - both config, no bak, with `$BASH_IT` reference: that one
28+
# - one config, no bak, but no `$BASH_IT` reference: wut
29+
# - no config, with bak, with `$BASH_IT`: re-create???
30+
# - no config, no bak: nothing.
31+
1432
BACKUP_FILE=$CONFIG_FILE.bak
1533

1634
if [[ ! -e "${HOME?}/$BACKUP_FILE" ]]; then

0 commit comments

Comments
 (0)