diff options
| author | Egor Tensin <egor@tensin.name> | 2026-09-23 09:29:38 +0500 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-09-23 09:29:38 +0500 |
| commit | 4b8c7cc0b10ca9551d8e8baf7b1a94153d672c45 (patch) | |
| tree | 6fb2b3024613690b2f6161c23c135f2412326077 | |
| parent | Merge pull request #4 from egor-tensin/dependabot/github_actions/debian/actio... (diff) | |
| parent | test/local: clean up lib/test.sh routines a little bit (diff) | |
| download | config-links-4b8c7cc0b10ca9551d8e8baf7b1a94153d672c45.tar.gz config-links-4b8c7cc0b10ca9551d8e8baf7b1a94153d672c45.zip | |
Merge tag 'v2.1.2' into debian
32 files changed, 586 insertions, 460 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b86aa9..bf3671a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,6 +70,8 @@ jobs: - name: Add builder user run: | useradd -m -s /bin/bash builder + passwd -d builder + echo 'builder ALL=(ALL) ALL' >> /etc/sudoers chown -R builder:builder -- "$GITHUB_WORKSPACE" - name: Configure SSH @@ -14,7 +14,7 @@ test/all: test/local test/docker .PHONY: test/local test/local: - ./test/local/test.sh + ./test/local/main.sh # This is deliberately excluded from test/all, because it's quite destructive. .PHONY: test/linux-home diff --git a/bin/links-chmod b/bin/links-chmod index 931a5b5..e46d42f 100755 --- a/bin/links-chmod +++ b/bin/links-chmod @@ -2,7 +2,7 @@ # Copyright (c) 2021 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. # usage: ./links-chmod [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run] MODE diff --git a/bin/links-remove b/bin/links-remove index a149e52..7e5938b 100755 --- a/bin/links-remove +++ b/bin/links-remove @@ -2,7 +2,7 @@ # Copyright (c) 2019 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. # usage: ./links-remove [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run] diff --git a/bin/links-update b/bin/links-update index dac40ff..9cc7454 100755 --- a/bin/links-update +++ b/bin/links-update @@ -2,7 +2,7 @@ # Copyright (c) 2016 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. # usage: ./links-update [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-m|--mode MODE] [-n|--dry-run] diff --git a/lib/common.sh b/lib/common.sh index 51c9827..c93ff8d 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -1,6 +1,6 @@ # Copyright (c) 2016 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. dump() { @@ -1,6 +1,6 @@ # Copyright (c) 2016 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. # Shared directory settings @@ -197,7 +197,7 @@ link_all_entries() { find "$shared_root_dir" \ -mindepth 1 -maxdepth 1 \ - -\( -type d -o -type l -\) \ + \( -type d -o -type l \) \ -regextype posix-basic \ -regex ".*/$var_name_regex\$" \ -printf '%P\0' | @@ -209,7 +209,7 @@ link_all_entries() { dump "shared directory: $shared_root_dir$shared_var_dir" local shared_path - find "$shared_root_dir$shared_var_dir/" -\( -type f -o -type l -\) -print0 | + find "$shared_root_dir$shared_var_dir/" \( -type f -o -type l \) -print0 | while IFS= read -d '' -r shared_path; do dump " shared file path: $shared_path" local entry="${shared_path:${#shared_root_dir}}" @@ -1,6 +1,6 @@ # Copyright (c) 2016 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. # Mostly Cygwin-related stuff diff --git a/lib/path.sh b/lib/path.sh index 4fcdb94..ad3e21b 100644 --- a/lib/path.sh +++ b/lib/path.sh @@ -1,6 +1,6 @@ # Copyright (c) 2016 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. # Making sure paths point to files/directories diff --git a/lib/vars.sh b/lib/vars.sh index 97b8da9..2204850 100644 --- a/lib/vars.sh +++ b/lib/vars.sh @@ -1,6 +1,6 @@ # Copyright (c) 2016 Egor Tensin <egor@tensin.name> # This file is part of the "Config file sharing" project. -# For details, see https://github.com/egor-tensin/config-links. +# For details, see https://github.com/egor-tensin/config-links # Distributed under the MIT License. # Variable resolution diff --git a/test/local/dirs/dest/1.txt b/test/local/data/dest/1.txt index 3a2e3f4..3a2e3f4 100644 --- a/test/local/dirs/dest/1.txt +++ b/test/local/data/dest/1.txt diff --git a/test/local/dirs/dest/bar/3.txt b/test/local/data/dest/bar/3.txt index a83d1d5..a83d1d5 100644 --- a/test/local/dirs/dest/bar/3.txt +++ b/test/local/data/dest/bar/3.txt diff --git a/test/local/dirs/src/%DEST%/1.txt b/test/local/data/src/%DEST%/1.txt index d00491f..d00491f 100644 --- a/test/local/dirs/src/%DEST%/1.txt +++ b/test/local/data/src/%DEST%/1.txt diff --git a/test/local/dirs/src/%DEST%/bar/3.txt b/test/local/data/src/%DEST%/bar/3.txt index 00750ed..00750ed 100644 --- a/test/local/dirs/src/%DEST%/bar/3.txt +++ b/test/local/data/src/%DEST%/bar/3.txt diff --git a/test/local/dirs/src/%DEST%/bar/baz/4.txt b/test/local/data/src/%DEST%/bar/baz/4.txt index b8626c4..b8626c4 100644 --- a/test/local/dirs/src/%DEST%/bar/baz/4.txt +++ b/test/local/data/src/%DEST%/bar/baz/4.txt diff --git a/test/local/dirs/src/%DEST%/foo/2.txt b/test/local/data/src/%DEST%/foo/2.txt index 0cfbf08..0cfbf08 100644 --- a/test/local/dirs/src/%DEST%/foo/2.txt +++ b/test/local/data/src/%DEST%/foo/2.txt diff --git a/test/local/lib/log.sh b/test/local/lib/log.sh new file mode 100644 index 0000000..b26e16a --- /dev/null +++ b/test/local/lib/log.sh @@ -0,0 +1,128 @@ +# Copyright (c) 2026 Egor Tensin <egor@tensin.name> +# This file is part of the "config-links" project. +# For details, see https://github.com/egor-tensin/config-links +# Distributed under the MIT License. + +log() { + local msg + for msg; do + echo -e "$test_file | $msg" >&2 + done +} + +fail() { + local msg + for msg; do + log "FAIL: $msg" + done +} + +fail_details() { + local msg + for msg; do + fail " $msg" + done +} + +log_numof_tests_ok=0 +log_numof_tests_failed=0 +declare -a log_tests_failed=() +declare -A log_tests_should_fail=() + +log_test_start() { + if [ "$#" -ne 1 ]; then + echo "usage: ${FUNCNAME[0]} TEST_FILE" >&2 + return 1 + fi + + local test_file="$1" + + if [ -n "${test_should_fail:-}" ]; then + log_tests_should_fail["$test_file"]=1 + fi + + cat <<EOF >&2 + +====================================================================== +TEST: $test_file +====================================================================== +EOF + + log "Should fail: ${test_should_fail:-No}" +} + +log_test_ok() { + if [ "$#" -ne 1 ]; then + echo "usage: ${FUNCNAME[0]} TEST_FILE" >&2 + return 1 + fi + + log_numof_tests_ok=$((log_numof_tests_ok + 1)) + + cat <<EOF >&2 +---------------------------------------------------------------------- +OK: $1 +---------------------------------------------------------------------- +EOF +} + +log_test_fail() { + if [ "$#" -ne 1 ]; then + echo "usage: ${FUNCNAME[0]} TEST_FILE" >&2 + return 1 + fi + + log_numof_tests_failed=$((log_numof_tests_failed + 1)) + log_tests_failed+=("$1") + + cat <<EOF >&2 +---------------------------------------------------------------------- +FAIL: $1 +---------------------------------------------------------------------- +EOF +} + +log_test_finish() { + if [ "$#" -ne 2 ]; then + echo "usage: ${FUNCNAME[0]} TEST_FILE EXIT_CODE" >&2 + return 1 + fi + + local test_file="$1" + local ret="$2" + local test_should_fail + test_should_fail="${log_tests_should_fail["$test_file"]:+x}" + + if [ "$ret" -eq 0 ] || [ -n "$test_should_fail" ]; then + log_test_ok "$test_file" + else + log_test_fail "$test_file" + fi +} + +log_tests_summary() { + cat <<EOF >&2 + +====================================================================== +SUMMARY +====================================================================== +OK: $log_numof_tests_ok +FAIL: $log_numof_tests_failed +EOF + + local test_file + for test_file in ${log_tests_failed[@]+"${log_tests_failed[@]}"}; do + cat <<EOF >&2 +FAIL: $test_file +EOF + done + + cat <<EOF >&2 +---------------------------------------------------------------------- +EOF + + local ret=0 + [ "$log_numof_tests_failed" -gt 0 ] && ret=1 + + return "$ret" +} diff --git a/test/local/lib/test.sh b/test/local/lib/test.sh new file mode 100644 index 0000000..43cf55b --- /dev/null +++ b/test/local/lib/test.sh @@ -0,0 +1,123 @@ +# Copyright (c) 2026 Egor Tensin <egor@tensin.name> +# This file is part of the "config-links" project. +# For details, see https://github.com/egor-tensin/config-links +# Distributed under the MIT License. + +test_should_fail= + +src_dir_name='src' +dest_dir_name='dest' +alt_dest_dir_name='alt_dest' + +test_root_dir= +test_src_dir= +test_dest_dir= +test_alt_dest_dir= + +test_setup() { + test_root_dir="$( mktemp -d )" + # mktemp returns /var/..., which is actually in /private/var/... on macOS. + test_root_dir="$( readlink -e -- "$test_root_dir" )" + + test_src_dir="$test_root_dir/$src_dir_name" + test_dest_dir="$test_root_dir/$dest_dir_name" + test_alt_dest_dir="$test_root_dir/$alt_dest_dir_name" + + log "Root directory: $test_root_dir" + log "Shared directory: $test_src_dir" + log "%DEST% directory: $test_dest_dir" + log "%ALT_DEST% directory: $test_alt_dest_dir" + + cp -r -- "$script_dir/data/$src_dir_name" "$test_src_dir" + cp -r -- "$script_dir/data/$dest_dir_name" "$test_dest_dir" + cp -r -- "$script_dir/data/$dest_dir_name" "$test_alt_dest_dir" +} + +test_setup_symlink() { + test_setup + + # Create a stupid symlink. + ln -s -- 'bar/3.txt' "$test_src_dir/%DEST%/3_copy.txt" +} + +test_setup_dir_symlink() { + test_setup + + # Files will get symlinks in the directory pointed to by $DEST, as well as + # by $ALT_DEST. + ln -s -- '%DEST%' "$test_src_dir/%ALT_DEST%" +} + +test_cleanup_default() { + if [ -n "$test_root_dir" ]; then + log "Removing test's root directory: $test_root_dir" + rm -rf -- "$test_root_dir" + fi +} + +test_run_script() { + local msg='Executing script:' + msg="$msg$( printf -- ' %q' "$@" --shared-dir "$test_src_dir" --database "$test_root_dir/links.bin" )" + log "$msg" + + DEST="$test_dest_dir" ALT_DEST="$test_alt_dest_dir" "$@" \ + --shared-dir "$test_src_dir" \ + --database "$test_root_dir/links.bin" +} + +test_run_update() { + test_run_script "$script_dir/../../bin/links-update" "$@" +} + +test_run_remove() { + test_run_script "$script_dir/../../bin/links-remove" +} + +test_run_chmod() { + test_run_script "$script_dir/../../bin/links-chmod" "$@" +} + +test_verify_output() { + if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then + echo "usage: ${FUNCNAME[0]} DEST_DIR EXPECTED_OUTPUT" >&2 + return 1 + fi + + local dest_dir="$1" + local expected_output="$2" + + log "Verifying directory structure in $dest_dir..." + + local actual_output + actual_output="$( find "$dest_dir" -printf '%h/%f->%l\n' | sort )" + + if [ "$actual_output" != "$expected_output" ]; then + fail "The actual files do not match the expected directory structure!" + fail_details "Expected:\n$expected_output" + fail_details "Actual:\n$actual_output" + return 1 + fi +} + +test_verify_mode() { + if [ "$#" -ne 2 ]; then + echo "usage: ${FUNCNAME[0]} PATH EXPECTED_MODE" >&2 + return 1 + fi + + local path="$1" + local expected_mode="$2" + + log "Checking permissions for file: $path" + + local actual_mode + actual_mode="$( stat -c '%a' -- "$path" )" + actual_mode="0$actual_mode" + + if [ "$actual_mode" != "$expected_mode" ]; then + fail "They don't match" + fail_details "Expected: $expected_mode" + fail_details "Actual: $actual_mode" + return 1 + fi +} diff --git a/test/local/main.sh b/test/local/main.sh new file mode 100755 index 0000000..5713366 --- /dev/null +++ b/test/local/main.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +# Copyright (c) 2026 Egor Tensin <egor@tensin.name> +# This file is part of the "config-links" project. +# For details, see https://github.com/egor-tensin/config-links +# Distributed under the MIT License. + +set -o errexit -o nounset -o pipefail +shopt -s inherit_errexit 2> /dev/null || true +shopt -s lastpipe + +script_dir="$( dirname -- "${BASH_SOURCE[0]}" )" +script_dir="$( cd -- "$script_dir" && pwd )" +readonly script_dir + +source "$script_dir/lib/log.sh" + +run_test_file() { + local test_file + for test_file; do + source "$script_dir/lib/test.sh" + source "$script_dir/$test_file" + + log_test_start "$test_file" + + set +e + ( + set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit 2> /dev/null || true + shopt -s lastpipe + + if [ "$( type -t test_cleanup )" == function ]; then + trap test_cleanup EXIT + else + trap test_cleanup_default EXIT + fi + + test_run + ) + local ret="$?" + set -e + + log_test_finish "$test_file" "$ret" + done +} + +main() { + local -a test_files=() + local test_file + + find "$script_dir" \ + -mindepth 1 \ + -maxdepth 1 \ + -type f \ + -regex '.*/tst-.*\.sh$' \ + -regextype posix-basic \ + -printf '%P\0' \ + | sort -z \ + | \ + while IFS= read -d '' -r test_file; do + test_files+=("$test_file") + done + + run_test_file ${test_files[@]+"${test_files[@]}"} + log_tests_summary +} + +main diff --git a/test/local/test.sh b/test/local/test.sh deleted file mode 100755 index 1e8567f..0000000 --- a/test/local/test.sh +++ /dev/null @@ -1,449 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -o nounset -o pipefail -shopt -s inherit_errexit 2> /dev/null || true -shopt -s lastpipe - -script_dir="$( dirname -- "${BASH_SOURCE[0]}" )" -script_dir="$( cd -- "$script_dir" && pwd )" -readonly script_dir -script_name="$( basename -- "${BASH_SOURCE[0]}" )" -readonly script_name - -root_dir="$( git -C "$script_dir" rev-parse --show-toplevel )" -readonly root_dir - -readonly src_dir_name='src' -readonly dest_dir_name='dest' -readonly alt_dest_dir_name='alt_dest' - -src_dir_path="$script_dir/dirs/$src_dir_name" -readonly src_dir_path -dest_dir_path="$script_dir/dirs/$dest_dir_name" -readonly dest_dir_path - -test_root_dir= -test_src_dir= -test_dest_dir= -test_alt_dest_dir= - -new_test() { - local test_name= - [ "${#FUNCNAME[@]}" -gt 1 ] && test_name="${FUNCNAME[1]}" - [ "$#" -gt 0 ] && test_name="$1" - - echo - echo ====================================================================== - echo "New test: $test_name" - - test_root_dir="$( mktemp -d )" - # mktemp returns /var/..., which is actually in /private/var/... on macOS. - test_root_dir="$( readlink -e -- "$test_root_dir" )" - test_src_dir="$test_root_dir/$src_dir_name" - test_dest_dir="$test_root_dir/$dest_dir_name" - test_alt_dest_dir="$test_root_dir/$alt_dest_dir_name" - - echo "Root directory: $test_root_dir" - echo "Shared directory: $test_src_dir" - echo "%DEST% directory: $test_dest_dir" - echo "%ALT_DEST% directory: $test_alt_dest_dir" - echo ====================================================================== - - cp -r -- "$src_dir_path" "$test_src_dir" - cp -r -- "$dest_dir_path" "$test_dest_dir" - cp -r -- "$dest_dir_path" "$test_alt_dest_dir" -} - -call_bin_script() { - echo - echo -n 'Executing script:' - - printf -- ' %q' "$@" --shared-dir "$test_src_dir" --database "$test_root_dir/links.bin" - printf -- '\n' - - echo - DEST="$test_dest_dir" ALT_DEST="$test_alt_dest_dir" "$@" --shared-dir "$test_src_dir" --database "$test_root_dir/links.bin" -} - -call_update() { - call_bin_script "$root_dir/bin/links-update" "$@" -} - -call_remove() { - call_bin_script "$root_dir/bin/links-remove" -} - -call_chmod() { - call_bin_script "$root_dir/bin/links-chmod" "$@" -} - -verify_output() { - if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then - echo "usage: ${FUNCNAME[0]} EXPECTED_OUTPUT [DEST_DIR]" >&2 - return 1 - fi - - local expected_output="$1" - echo - echo 'Expected directory structure:' - echo "$expected_output" - - local dest_dir="$test_dest_dir" - [ "$#" -ge 2 ] && dest_dir="$2" - - local actual_output - actual_output="$( find "$dest_dir" -printf '%h/%f->%l\n' | sort )" - echo - echo 'Actual directory structure:' - echo "$actual_output" - echo - - if [ "$actual_output" = "$expected_output" ]; then - echo "... They match!" - else - echo "... The actual directory structure does not match the expected directory structure!" >&2 - return 1 - 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 that links-update actually creates the proper - # symlinks. - - new_test - call_update - - 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" -} - -test_remove_works() { - # Basic test to make sure links-remove actually removes the created - # symlinks. - - new_test - call_update - call_remove - - local expected_output="$test_dest_dir->" - verify_output "$expected_output" -} - -test_remove_does_not_overwrite_files() { - # Check that if a user overwrites a symlink with his own file, links-remove - # keeps it. - - new_test - call_update - - # Simulate a user overwriting one of the symlinks with his own file. - rm -- "$test_dest_dir/bar/3.txt" - echo 'User content' > "$test_dest_dir/bar/3.txt" - - call_remove - - # 3.txt must be kept: - local expected_output="$test_dest_dir-> -$test_dest_dir/bar-> -$test_dest_dir/bar/3.txt->" - - verify_output "$expected_output" -} - -test_remove_does_not_delete_used_dirs() { - # Check that if a user adds a file to a destination directory, it's not - # deleted by links-remove. - - new_test - call_update - - # User adds his own file to the directory: - echo 'User content' > "$test_dest_dir/bar/my-file" - - call_remove - - # bar/ and bar/my-file must be kept: - local expected_output="$test_dest_dir-> -$test_dest_dir/bar-> -$test_dest_dir/bar/my-file->" - - verify_output "$expected_output" -} - -new_test_dir_symlink() { - new_test "${FUNCNAME[1]}" - - # Files will get symlinks in the directory pointed to by $DEST, as well as - # by $ALT_DEST. - ln -s -- '%DEST%' "$test_src_dir/%ALT_DEST%" -} - -test_dir_symlink_update_works() { - # We can symlink files to multiple directories by creating symlinks inside - # --shared-dir. - - new_test_dir_symlink - call_update - - 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" - - expected_output="$test_alt_dest_dir-> -$test_alt_dest_dir/1.txt->$test_src_dir/%ALT_DEST%/1.txt -$test_alt_dest_dir/bar-> -$test_alt_dest_dir/bar/3.txt->$test_src_dir/%ALT_DEST%/bar/3.txt -$test_alt_dest_dir/bar/baz-> -$test_alt_dest_dir/bar/baz/4.txt->$test_src_dir/%ALT_DEST%/bar/baz/4.txt -$test_alt_dest_dir/foo-> -$test_alt_dest_dir/foo/2.txt->$test_src_dir/%ALT_DEST%/foo/2.txt" - - verify_output "$expected_output" "$test_alt_dest_dir" -} - -test_dir_symlink_remove_works() { - # Test that links-remove works for directory symlinks inside --shared-dir. - - new_test_dir_symlink - call_update - call_remove - - local expected_output="$test_dest_dir->" - verify_output "$expected_output" - - expected_output="$test_alt_dest_dir->" - verify_output "$expected_output" "$test_alt_dest_dir" -} - -test_dir_symlink_remove_shared_file() { - # If we remove a shared file, both of the symlinks should be removed. - - new_test_dir_symlink - call_update - - # Remove a random shared file: - rm -- "$test_src_dir/%DEST%/bar/3.txt" - call_update - - 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/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" - - expected_output="$test_alt_dest_dir-> -$test_alt_dest_dir/1.txt->$test_src_dir/%ALT_DEST%/1.txt -$test_alt_dest_dir/bar-> -$test_alt_dest_dir/bar/baz-> -$test_alt_dest_dir/bar/baz/4.txt->$test_src_dir/%ALT_DEST%/bar/baz/4.txt -$test_alt_dest_dir/foo-> -$test_alt_dest_dir/foo/2.txt->$test_src_dir/%ALT_DEST%/foo/2.txt" - - verify_output "$expected_output" "$test_alt_dest_dir" -} - -test_dir_symlink_remove_dir_symlink() { - # If we remove a directory symlink in --shared-dir, all the symlinks - # accessible through this directory symlink should be removed. - - new_test_dir_symlink - call_update - - # Remove the directory symlink: - rm -- "$test_src_dir/%ALT_DEST%" - call_update - - 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" - - expected_output="$test_alt_dest_dir->" - verify_output "$expected_output" "$test_alt_dest_dir" -} - -new_test_symlink() { - new_test "${FUNCNAME[1]}" - - # Create a stupid symlink. - ln -s -- 'bar/3.txt' "$test_src_dir/%DEST%/3_copy.txt" -} - -test_symlink_update_works() { - # Shared files can also be symlinks, pointing to something else. - - new_test_symlink - call_update - - local expected_output="$test_dest_dir-> -$test_dest_dir/1.txt->$test_src_dir/%DEST%/1.txt -$test_dest_dir/3_copy.txt->$test_src_dir/%DEST%/3_copy.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" - - echo - echo 'Verifying 3_copy.txt (the symlink) is valid...' - - local copy_target - copy_target="$( readlink -- "$test_dest_dir/3_copy.txt" )" - test "$copy_target" = "$test_src_dir/%DEST%/3_copy.txt" - - local copy_content - copy_content="$( cat -- "$test_dest_dir/3_copy.txt" )" - test "$copy_content" = '3' -} - -test_symlink_remove_works() { - # Verify that links-remove doesn't delete shared symlinks. - - new_test_symlink - call_update - call_remove - - local expected_output="$test_dest_dir->" - verify_output "$expected_output" - - echo - echo 'Verifying 3_copy.txt (the shared file) is valid...' - - local copy_target - copy_target="$( readlink -e -- "$test_src_dir/%DEST%/3_copy.txt" )" - test "$copy_target" = "$test_src_dir/%DEST%/bar/3.txt" - - local copy_content - copy_content="$( cat -- "$test_src_dir/%DEST%/3_copy.txt" )" - test "$copy_content" = '3' -} - -test_chmod_works() { - # Test that links-chmod works. - - new_test - call_update - - 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" - - echo - echo 'Verifying 1.txt (the shared file) permissions...' - - verify_mode 0644 "$test_src_dir/%DEST%/1.txt" - call_chmod 0600 - verify_mode 0600 "$test_src_dir/%DEST%/1.txt" -} - -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" - - verify_mode "$expected_mode" "$test_src_dir/%DEST%/1.txt" -} - -show_env() { - echo - echo ====================================================================== - echo Environment - echo ====================================================================== - - bash --version -} - -main() { - show_env - - test_update_works - test_remove_works - test_remove_does_not_overwrite_files - test_remove_does_not_delete_used_dirs - - test_dir_symlink_update_works - test_dir_symlink_remove_works - test_dir_symlink_remove_shared_file - test_dir_symlink_remove_dir_symlink - - test_symlink_update_works - test_symlink_remove_works - - test_chmod_works - test_update_chmod -} - -main diff --git a/test/local/tst-chmod.sh b/test/local/tst-chmod.sh new file mode 100644 index 0000000..b118e5e --- /dev/null +++ b/test/local/tst-chmod.sh @@ -0,0 +1,22 @@ +test_run() { + # Test that links-chmod works. + + test_setup + test_run_update + + 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" + test_verify_output "$test_dest_dir" "$expected_output" + + log "Verifying 1.txt's (the shared file) permissions..." + + test_verify_mode "$test_src_dir/%DEST%/1.txt" 0644 + test_run_chmod 0600 + test_verify_mode "$test_src_dir/%DEST%/1.txt" 0600 +} diff --git a/test/local/tst-dir-symlink-delete.sh b/test/local/tst-dir-symlink-delete.sh new file mode 100644 index 0000000..f53d87c --- /dev/null +++ b/test/local/tst-dir-symlink-delete.sh @@ -0,0 +1,25 @@ +test_run() { + # If we remove a directory symlink in --shared-dir, all the symlinks + # accessible through this directory symlink should be removed. + + test_setup_dir_symlink + test_run_update + + # Remove the directory symlink: + rm -- "$test_src_dir/%ALT_DEST%" + test_run_update + + 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" + + test_verify_output "$test_dest_dir" "$expected_output" + + expected_output="$test_alt_dest_dir->" + test_verify_output "$test_alt_dest_dir" "$expected_output" +} diff --git a/test/local/tst-dir-symlink-remove-shared-file.sh b/test/local/tst-dir-symlink-remove-shared-file.sh new file mode 100644 index 0000000..3e20ab0 --- /dev/null +++ b/test/local/tst-dir-symlink-remove-shared-file.sh @@ -0,0 +1,30 @@ +test_run() { + # If we remove a shared file, both of the symlinks should be removed. + + test_setup_dir_symlink + test_run_update + + # Remove a random shared file: + rm -- "$test_src_dir/%DEST%/bar/3.txt" + test_run_update + + 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/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" + + test_verify_output "$test_dest_dir" "$expected_output" + + expected_output="$test_alt_dest_dir-> +$test_alt_dest_dir/1.txt->$test_src_dir/%ALT_DEST%/1.txt +$test_alt_dest_dir/bar-> +$test_alt_dest_dir/bar/baz-> +$test_alt_dest_dir/bar/baz/4.txt->$test_src_dir/%ALT_DEST%/bar/baz/4.txt +$test_alt_dest_dir/foo-> +$test_alt_dest_dir/foo/2.txt->$test_src_dir/%ALT_DEST%/foo/2.txt" + + test_verify_output "$test_alt_dest_dir" "$expected_output" +} diff --git a/test/local/tst-remove-dir-symlink.sh b/test/local/tst-remove-dir-symlink.sh new file mode 100644 index 0000000..c524ee6 --- /dev/null +++ b/test/local/tst-remove-dir-symlink.sh @@ -0,0 +1,13 @@ +test_run() { + # Test that links-remove works for directory symlinks inside --shared-dir. + + test_setup_dir_symlink + test_run_update + test_run_remove + + local expected_output="$test_dest_dir->" + test_verify_output "$test_dest_dir" "$expected_output" + + expected_output="$test_alt_dest_dir->" + test_verify_output "$test_alt_dest_dir" "$expected_output" +} diff --git a/test/local/tst-remove-does-not-delete-user-dirs.sh b/test/local/tst-remove-does-not-delete-user-dirs.sh new file mode 100644 index 0000000..aa013c8 --- /dev/null +++ b/test/local/tst-remove-does-not-delete-user-dirs.sh @@ -0,0 +1,19 @@ +test_run() { + # Check that if a user adds a file to a destination directory, it's not + # deleted by links-remove. + + test_setup + test_run_update + + # User adds his own file to the directory: + echo 'User content' > "$test_dest_dir/bar/my-file" + + test_run_remove + + # bar/ and bar/my-file must be kept: + local expected_output="$test_dest_dir-> +$test_dest_dir/bar-> +$test_dest_dir/bar/my-file->" + + test_verify_output "$test_dest_dir" "$expected_output" +} diff --git a/test/local/tst-remove-does-not-overwrite-files.sh b/test/local/tst-remove-does-not-overwrite-files.sh new file mode 100644 index 0000000..baef2ef --- /dev/null +++ b/test/local/tst-remove-does-not-overwrite-files.sh @@ -0,0 +1,20 @@ +test_run() { + # Check that if a user overwrites a symlink with his own file, links-remove + # keeps it. + + test_setup + test_run_update + + # Simulate a user overwriting one of the symlinks with his own file. + rm -- "$test_dest_dir/bar/3.txt" + echo 'User content' > "$test_dest_dir/bar/3.txt" + + test_run_remove + + # 3.txt must be kept: + local expected_output="$test_dest_dir-> +$test_dest_dir/bar-> +$test_dest_dir/bar/3.txt->" + + test_verify_output "$test_dest_dir" "$expected_output" +} diff --git a/test/local/tst-remove-symlink.sh b/test/local/tst-remove-symlink.sh new file mode 100644 index 0000000..aa27435 --- /dev/null +++ b/test/local/tst-remove-symlink.sh @@ -0,0 +1,20 @@ +test_run() { + # Verify that links-remove doesn't delete shared symlinks. + + test_setup_symlink + test_run_update + test_run_remove + + local expected_output="$test_dest_dir->" + test_verify_output "$test_dest_dir" "$expected_output" + + log 'Verifying 3_copy.txt (the shared file) is valid...' + + local copy_target + copy_target="$( readlink -e -- "$test_src_dir/%DEST%/3_copy.txt" )" + test "$copy_target" = "$test_src_dir/%DEST%/bar/3.txt" + + local copy_content + copy_content="$( cat -- "$test_src_dir/%DEST%/3_copy.txt" )" + test "$copy_content" = '3' +} diff --git a/test/local/tst-remove.sh b/test/local/tst-remove.sh new file mode 100644 index 0000000..c79a3b4 --- /dev/null +++ b/test/local/tst-remove.sh @@ -0,0 +1,11 @@ +test_run() { + # Basic test to make sure links-remove actually removes the created + # symlinks. + + test_setup + test_run_update + test_run_remove + + local expected_output="$test_dest_dir->" + test_verify_output "$test_dest_dir" "$expected_output" +} diff --git a/test/local/tst-update-chmod.sh b/test/local/tst-update-chmod.sh new file mode 100644 index 0000000..a78ee15 --- /dev/null +++ b/test/local/tst-update-chmod.sh @@ -0,0 +1,19 @@ +test_run() { + # Test that links-update --mode works. + + test_setup + local expected_mode='0622' + test_run_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" + + test_verify_output "$test_dest_dir" "$expected_output" + test_verify_mode "$test_src_dir/%DEST%/1.txt" "$expected_mode" +} diff --git a/test/local/tst-update-dir-symlink.sh b/test/local/tst-update-dir-symlink.sh new file mode 100644 index 0000000..27418ab --- /dev/null +++ b/test/local/tst-update-dir-symlink.sh @@ -0,0 +1,29 @@ +test_run() { + # We can symlink files to multiple directories by creating symlinks inside + # --shared-dir. + + test_setup_dir_symlink + test_run_update + + 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" + + test_verify_output "$test_dest_dir" "$expected_output" + + expected_output="$test_alt_dest_dir-> +$test_alt_dest_dir/1.txt->$test_src_dir/%ALT_DEST%/1.txt +$test_alt_dest_dir/bar-> +$test_alt_dest_dir/bar/3.txt->$test_src_dir/%ALT_DEST%/bar/3.txt +$test_alt_dest_dir/bar/baz-> +$test_alt_dest_dir/bar/baz/4.txt->$test_src_dir/%ALT_DEST%/bar/baz/4.txt +$test_alt_dest_dir/foo-> +$test_alt_dest_dir/foo/2.txt->$test_src_dir/%ALT_DEST%/foo/2.txt" + + test_verify_output "$test_alt_dest_dir" "$expected_output" +} diff --git a/test/local/tst-update-symlink.sh b/test/local/tst-update-symlink.sh new file mode 100644 index 0000000..6a90491 --- /dev/null +++ b/test/local/tst-update-symlink.sh @@ -0,0 +1,28 @@ +test_run() { + # Shared files can also be symlinks, pointing to something else. + + test_setup_symlink + test_run_update + + local expected_output="$test_dest_dir-> +$test_dest_dir/1.txt->$test_src_dir/%DEST%/1.txt +$test_dest_dir/3_copy.txt->$test_src_dir/%DEST%/3_copy.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" + + test_verify_output "$test_dest_dir" "$expected_output" + + log 'Verifying 3_copy.txt (the symlink) is valid...' + + local copy_target + copy_target="$( readlink -- "$test_dest_dir/3_copy.txt" )" + test "$copy_target" = "$test_src_dir/%DEST%/3_copy.txt" + + local copy_content + copy_content="$( cat -- "$test_dest_dir/3_copy.txt" )" + test "$copy_content" = '3' +} diff --git a/test/local/tst-update.sh b/test/local/tst-update.sh new file mode 100644 index 0000000..f33b929 --- /dev/null +++ b/test/local/tst-update.sh @@ -0,0 +1,18 @@ +test_run() { + # Basic test to make sure that links-update actually creates the proper + # symlinks. + + test_setup + test_run_update + + 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" + + test_verify_output "$test_dest_dir" "$expected_output" +} |
