Skip to content

Commit 155e5b5

Browse files
committed
uninstall: try to determine initialization file
uninstall: TIL that `fgrep` is deprecated...
1 parent d8784d4 commit 155e5b5

File tree

2 files changed

+79
-32
lines changed

2 files changed

+79
-32
lines changed

test/install/uninstall.bats

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,84 @@ function local_setup() {
77
}
88

99
function local_setup_file() {
10-
# Determine which config file to use based on OS.
11-
case $OSTYPE in
12-
darwin*)
13-
export BASH_IT_CONFIG_FILE=.bash_profile
14-
;;
15-
*)
16-
export BASH_IT_CONFIG_FILE=.bashrc
17-
;;
18-
esac
19-
# don't load any libraries as the tests here test the *whole* kit
10+
: # don't load any libraries as the tests here test the *whole* kit
2011
}
2112

2213
@test "uninstall: verify that the uninstall script exists" {
2314
assert_file_exist "$BASH_IT/uninstall.sh"
2415
}
2516

26-
@test "uninstall: run the uninstall script with an existing backup file" {
27-
cd "$BASH_IT"
17+
@test "uninstall: run the uninstall script with existing backup 'bashrc'" {
18+
BASH_IT_CONFIG_FILE=.bashrc
2819

29-
echo "test file content for backup" > "$HOME/$BASH_IT_CONFIG_FILE.bak"
30-
echo "test file content for original file" > "$HOME/$BASH_IT_CONFIG_FILE"
31-
local md5_bak=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')
20+
echo "test file content for backup" > "${HOME?}/$BASH_IT_CONFIG_FILE.bak"
21+
echo "test file content for original file" > "${HOME?}/$BASH_IT_CONFIG_FILE"
22+
local md5_bak=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')
3223

33-
run ./uninstall.sh
24+
run "${BASH_IT?}/uninstall.sh"
3425
assert_success
3526

36-
assert_file_not_exist "$HOME/$BASH_IT_CONFIG_FILE.uninstall"
37-
assert_file_not_exist "$HOME/$BASH_IT_CONFIG_FILE.bak"
38-
assert_file_exist "$HOME/$BASH_IT_CONFIG_FILE"
27+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE.uninstall"
28+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE.bak"
29+
assert_file_exist "${HOME?}/$BASH_IT_CONFIG_FILE"
3930

40-
local md5_conf=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
31+
local md5_conf=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
4132

4233
assert_equal "$md5_bak" "$md5_conf"
4334
}
4435

45-
@test "uninstall: run the uninstall script without an existing backup file" {
46-
cd "$BASH_IT"
36+
@test "uninstall: run the uninstall script with existing backup 'bash_profile'" {
37+
BASH_IT_CONFIG_FILE=.bash_profile
4738

48-
echo "test file content for original file" > "$HOME/$BASH_IT_CONFIG_FILE"
49-
local md5_orig=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
39+
echo "test file content for backup file" > "${HOME?}/$BASH_IT_CONFIG_FILE.bak"
40+
echo "test file content for original file" > "${HOME?}/$BASH_IT_CONFIG_FILE"
41+
local md5_bak=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE.bak" | awk '{print $1}')
5042

51-
run ./uninstall.sh
43+
run "${BASH_IT?}/uninstall.sh"
5244
assert_success
5345

54-
assert_file_exist "$HOME/$BASH_IT_CONFIG_FILE.uninstall"
55-
assert_file_not_exist "$HOME/$BASH_IT_CONFIG_FILE.bak"
56-
assert_file_not_exist "$HOME/$BASH_IT_CONFIG_FILE"
46+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE.uninstall"
47+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE.bak"
48+
assert_file_exist "${HOME?}/$BASH_IT_CONFIG_FILE"
5749

58-
local md5_uninstall=$(md5sum "$HOME/$BASH_IT_CONFIG_FILE.uninstall" | awk '{print $1}')
50+
local md5_conf=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
51+
52+
assert_equal "$md5_bak" "$md5_conf"
53+
}
54+
55+
@test "uninstall: run the uninstall script without existing backup 'bashrc" {
56+
BASH_IT_CONFIG_FILE=.bashrc
57+
58+
echo "test file content for original file" > "${HOME?}/$BASH_IT_CONFIG_FILE"
59+
local md5_orig=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
60+
61+
run "${BASH_IT?}/uninstall.sh"
62+
assert_success
63+
64+
assert_file_exist "${HOME?}/$BASH_IT_CONFIG_FILE.uninstall"
65+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE.bak"
66+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE"
67+
68+
local md5_uninstall=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE.uninstall" | awk '{print $1}')
69+
70+
assert_equal "$md5_orig" "$md5_uninstall"
71+
}
72+
73+
@test "uninstall: run the uninstall script without existing backup 'bash_profile" {
74+
BASH_IT_CONFIG_FILE=.bash_profile
75+
76+
echo "test file content for original file" > "${HOME?}/$BASH_IT_CONFIG_FILE"
77+
local md5_orig=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE" | awk '{print $1}')
78+
79+
run "${BASH_IT?}/uninstall.sh"
80+
81+
assert_success
82+
83+
assert_file_exist "${HOME?}/$BASH_IT_CONFIG_FILE.uninstall"
84+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE.bak"
85+
assert_file_not_exist "${HOME?}/$BASH_IT_CONFIG_FILE"
86+
87+
local md5_uninstall=$(md5sum "${HOME?}/$BASH_IT_CONFIG_FILE.uninstall" | awk '{print $1}')
5988

6089
assert_equal "$md5_orig" "$md5_uninstall"
6190
}

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)