Skip to content

Commit

Permalink
Fix revert journal entries that where created with sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
usommerl committed Dec 25, 2020
1 parent 47fd369 commit c19d4b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lnkr_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ __revert_recorded_actions() {
}

__revert_action() {
local user="$(__extract_field "$1" "1")"
local user="$(__extract_field "$1" "2")"
local action="$(__extract_field "$1" "3")"
local args="$(__extract_field "$1" "4-")"
[ "$user" == "root" ] && local sudo_revert="sudo "
[ "$user" == "root" ] && local sudo_revert="$SUDO_CMD "
case "$action" in
"$ACTION_LINK")
__remove_link "${sudo_revert:-}" "$args"
Expand Down
32 changes: 23 additions & 9 deletions test/lib.bats
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,22 @@ teardown() {
}

@test 'sudo function should enable sudo mode for link function' {
stub sudo 'Stub sudo command'
SUDO_CMD="$BATS_TEST_DIRNAME/stub/sudo"
stub_sudo
local linkname='link'
touch "$TESTSPACE/$linkname"
run sudo link "$TESTSPACE/file" "$TESTSPACE/$linkname"
[ "$status" -eq 0 ]
[ $(echo "${lines[1]}" | grep "Stub sudo" | wc -l) -eq 1 ]
[ $(echo "${lines[1]}" | grep "sudo mv .*link .*link.backup-.*" | wc -l) -eq 1 ]
[ $(echo "${lines[2]}" | grep "Create backup" | wc -l) -eq 1 ]
[ $(echo "${lines[3]}" | grep "Stub sudo" | wc -l) -eq 1 ]
[ $(echo "${lines[4]}" | grep "Stub sudo" | wc -l) -eq 1 ]
[ $(echo "${lines[3]}" | grep "sudo mkdir.*" | wc -l) -eq 1 ]
[ $(echo "${lines[4]}" | grep "sudo ln .*file .*link" | wc -l) -eq 1 ]
[ $(echo "${lines[5]}" | grep "Create link" | wc -l) -eq 1 ]
}

@test 'sudo function should use sudo command for every argument other than link' {
stub sudo 'Stub sudo command'
SUDO_CMD="$BATS_TEST_DIRNAME/stub/sudo"
run sudo random_command
[ $(echo "${lines[@]}" | grep "Stub sudo" | wc -l) -eq 1 ]
stub_sudo
run sudo date
[ $(echo "${lines[@]}" | grep "sudo date" | wc -l) -eq 1 ]
[ "$status" -eq 0 ]
}

Expand Down Expand Up @@ -266,6 +264,22 @@ teardown() {
[ ! -e "$TEST_JOURNAL" ]
}

@test 'remove operation should revert journal entries that where created with sudo' {
stub_sudo
local linkname='link'
printf 'file.orig\n' > "$TESTSPACE/$linkname"
printf 'file.linked\n' > "$TESTSPACE/file"
run sudo link "$TESTSPACE/file" "$linkname"
[ $(cat "$TEST_JOURNAL" | wc -l) -eq 2 ]
[ $(echo "${lines[3]}" | grep "sudo ln.*file link" | wc -l) -eq 1 ]
run __main remove
[ "$status" -eq 0 ]
[ $(grep 'file.orig' "$TESTSPACE/$linkname" | wc -l) -eq 1 ]
[ $(echo "${lines[1]}" | grep "sudo rm link" | wc -l) -eq 1 ]
[ $(echo "${lines[3]}" | grep "sudo mv -n link.backup-.* link" | wc -l) -eq 1 ]
[ ! -e "$TEST_JOURNAL" ]
}

@test 'remove operation should revert journal entries with names that contain spaces' {
local linkname='link - with - spaces'
printf 'file.orig\n' > "$TESTSPACE/$linkname"
Expand Down
6 changes: 6 additions & 0 deletions test/stub.bash
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ stub_curl_and_wget() {
stub curl "bash: curl: command not found" 127 2
stub wget "bash: wget: command not found" 127 2
}

stub_sudo() {
stub sudo ""
echo 'echo "sudo $*";eval $*' > "$BATS_TEST_DIRNAME/stub/sudo"
SUDO_CMD="$BATS_TEST_DIRNAME/stub/sudo"
}

0 comments on commit c19d4b5

Please sign in to comment.