Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn user that installation will fail if she doesn't have necessary write permissions #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion share/ruby-install/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ source "$ruby_install_dir/checksums.sh"
#
function pre_install()
{

local status
local exisiting_part_dir="${install_dir%/*}"

mkdir -p "$src_dir" || return $?
mkdir -p "${install_dir%/*}" || return $?

while [ ! -d "$exisiting_part_dir" ]; do
exisiting_part_dir="${exisiting_part_dir%/*}"
done

if [ ! -w "$exisiting_part_dir" ]; then
fail "Installation directory is not writable by the user : ${exisiting_part_dir}"
Copy link
Owner

@postmodern postmodern Sep 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fail calls exit -1, so status=$? has no effect. Maybe you want to use error and then return 1?

status=$?
fi

mkdir -p "${install_dir%/*}" || return $status
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're going to hate me for constantly suggesting alternative approaches, but I think recursively checking each parent directory is unnecessary:

local parent_dir="${install_dir%/*}"

if [[ ! -d "$parent_dir" ]]; then
  mkdir -p "$parent_dir" || fail "cannot create installation directory"
elif [[ ! -w "$parent_dir" ]]; then
  fail "installation directory is not writable"
fi
  • If the parent directory does not exist, try creating it with mkdir -p. If mkdir-p fails, this means that one of the parent directories is not writable, and hence all sub-directories would also not be writable. This basically offloads the recursive checking logic to mkdir -p.
  • If the parent directory does exist, then test if it is writable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think the above while loop is a bit too complex and think checking mkdir -p would have the same effect.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be return $??

}

#
Expand Down
29 changes: 29 additions & 0 deletions test/cli-tests/warn_if_no_write_permissions_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

. ./test/helper.sh

test_install_dir="$test_fixtures_dir/no_write_permissions_test"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe to make this test more explicit, you could add another variable called non_writable_parent_dir="$test_fixtures_dir/..." and set test_install_dir="$non_writable_parent_dir/ruby".


function setUp()
{
mkdir -p "$test_install_dir/bin"
chmod -w "$test_install_dir/bin"
}

function test_no_write_permissions()
{
local output status
output="$(ruby-install --install-dir "$test_install_dir/bin/rubies" ruby 2>&1)"
status=$?

assertEquals "did not return 0" 255 $status
assertTrue "did not print a message to STDOUT" \
'[[ "$output" == *"Installation directory is not writable by the user"* ]]'
}

function tearDown()
{
rm -rf "$test_install_dir"
}

SHUNIT_PARENT=$0 . $SHUNIT2