From a556264de6ddab9bcd47023b1d3daa2e2b683505 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 25 Nov 2021 21:57:57 +0300 Subject: links-update: add --mode parameter --- links-update | 13 ++++++++++--- test/test.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/links-update b/links-update index b195305..1e1c829 100755 --- a/links-update +++ b/links-update @@ -17,7 +17,7 @@ # `CYGWIN` Windows environment variable value **must** include either # `winsymlinks:native` or `winsymlinks:nativestrict`! -# usage: ./links-update [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run] +# usage: ./links-update [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-m|--mode MODE] [-n|--dry-run] set -o errexit set -o nounset @@ -32,6 +32,8 @@ readonly script_dir src_dir="$( cd -- "$script_dir/src" && pwd )" readonly src_dir +mode='' + . "$src_dir/common.sh" . "$src_dir/os.sh" . "$src_dir/path.sh" @@ -44,11 +46,12 @@ script_usage() { echo "$script_name: $msg" done - echo "usage: $script_name [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run] + echo "usage: $script_name [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-m|--mode MODE] [-n|--dry-run] -h,--help show this message and exit -d,--database set database file path -s,--shared-dir set top-level shared directory path (current working directory by default) + -m,--mode shared files mode (as in chmod) -n,--dry-run don't actually do anything intrusive" } @@ -66,7 +69,7 @@ parse_script_options() { set_dry_run continue ;; - -d|--database|-s|--shared-dir) + -d|--database|-s|--shared-dir|-m|--mode) ;; *) script_usage "unrecognized parameter: $key" >&2 @@ -89,6 +92,9 @@ parse_script_options() { -s|--shared-dir) update_shared_dir "$value" ;; + -m|--mode) + mode="$value" + ;; *) script_usage "unrecognized parameter: $key" >&2 exit 1 @@ -103,6 +109,7 @@ main() { read_database unlink_obsolete_entries link_all_entries + test -n "$mode" && chmod_entries "$mode" write_database } diff --git a/test/test.sh b/test/test.sh index efe4315..de4dd85 100755 --- a/test/test.sh +++ b/test/test.sh @@ -58,7 +58,7 @@ call_bin_script() { } call_update() { - call_bin_script "$script_dir/../links-update" + call_bin_script "$script_dir/../links-update" "$@" } call_unlink() { @@ -98,6 +98,33 @@ verify_output() { fi } +verify_mode() { + if [ "$#" -ne 2 ]; then + echo "usage: ${FUNCNAME[0]} EXPECTED_MODE FILE" >&2 + return 1 + fi + + local expected_mode="$1" + local path="$2" + + echo + echo "Checking permissions for file: $path" + echo "Expected mode: $expected_mode" + + local actual_mode + actual_mode="$( stat -c '%a' -- "$path" )" + actual_mode="0$actual_mode" + + echo "Actual mode: $actual_mode" + + if [ "$actual_mode" = "$expected_mode" ]; then + echo "... They match!" + else + echo "... They don't match." + return 1 + fi +} + test_update_works() { # Basic test to make sure update.sh actually creates the proper symlinks. @@ -331,20 +358,28 @@ $test_dest_dir/foo/2.txt->$test_src_dir/%DEST%/foo/2.txt" echo echo 'Verifying 1.txt (the shared file) permissions...' - local mode - - mode="$( stat -c '%a' -- "$test_src_dir/%DEST%/1.txt" )" - echo "Actual permissions: $mode" - test "$mode" = '644' - + verify_mode 0644 "$test_src_dir/%DEST%/1.txt" call_chmod --mode 0600 + verify_mode 0600 "$test_src_dir/%DEST%/1.txt" +} - echo - echo 'Verifying 1.txt (the shared file) permissions have changed...' +test_update_chmod() { + # Test that links-update --mode works. + new_test + local expected_mode='0622' + call_update --mode "$expected_mode" + + local expected_output="$test_dest_dir-> +$test_dest_dir/1.txt->$test_src_dir/%DEST%/1.txt +$test_dest_dir/bar-> +$test_dest_dir/bar/3.txt->$test_src_dir/%DEST%/bar/3.txt +$test_dest_dir/bar/baz-> +$test_dest_dir/bar/baz/4.txt->$test_src_dir/%DEST%/bar/baz/4.txt +$test_dest_dir/foo-> +$test_dest_dir/foo/2.txt->$test_src_dir/%DEST%/foo/2.txt" + verify_output "$expected_output" - mode="$( stat -c '%a' -- "$test_src_dir/%DEST%/1.txt" )" - echo "Actual permissions: $mode" - test "$mode" = '600' + verify_mode "$expected_mode" "$test_src_dir/%DEST%/1.txt" } main() { @@ -361,6 +396,7 @@ main() { test_symlink_unlink_works test_chmod_works + test_update_chmod } main -- cgit v1.2.3