From e9066c95193ab7aee9d000afa14680c1cc983b72 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 3 Jul 2015 13:06:56 +0100 Subject: [PATCH] Basic working root link setup --- src/lib/attributes/source-path.sh | 7 +++++++ src/lib/internals/setup-root-link.sh | 13 +++++++++++- test/integration/root-link.test.sh | 31 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100755 test/integration/root-link.test.sh diff --git a/src/lib/attributes/source-path.sh b/src/lib/attributes/source-path.sh index ee4c310..33ac91e 100644 --- a/src/lib/attributes/source-path.sh +++ b/src/lib/attributes/source-path.sh @@ -5,6 +5,13 @@ dotify-get-source-path() { echo "$(dirname "$dotfile")" } +dotify-get-absolute-source-path() { + local dotfile="$(dotify-get-dotfile-path)" + if [ "$?" != "0" ]; then return 1; fi + + echo "$(abs_dirname "$dotfile")" +} + dotify-valid-source-path() { dotify-get-source-path >/dev/null 2>&1 return "$?" diff --git a/src/lib/internals/setup-root-link.sh b/src/lib/internals/setup-root-link.sh index 50a5aae..7e3beb2 100644 --- a/src/lib/internals/setup-root-link.sh +++ b/src/lib/internals/setup-root-link.sh @@ -1,3 +1,14 @@ dotify-setup-root-link() { - return 0 + if [ -n "$DOTIFY_ROOT_LINK_IS_SETUP" ]; then + return 0 + fi + + local source="$(dotify-get-absolute-source-path)" + local target="$(dotify-get-target-path)/$(dotify-get-root-link)" + + if [ "$source" != "$(abs_path "$target")" ]; then + dotify-create-symlink "$source" "$target" + fi + + DOTIFY_ROOT_LINK_IS_SETUP="1" } diff --git a/test/integration/root-link.test.sh b/test/integration/root-link.test.sh new file mode 100755 index 0000000..f94ca06 --- /dev/null +++ b/test/integration/root-link.test.sh @@ -0,0 +1,31 @@ +#! /usr/bin/env bash +source "../test-helper.sh" + +# +# Integration test: root link +# + +# Create temp files/folders used for tests. +TEST_SOURCE="tmp/source" +TEST_TARGET="tmp/target" +ABS_TEST_SOURCE="$(pwd)/$TEST_SOURCE" +MY_DOTFILE="$TEST_SOURCE/Dotfile" +mkdir -p "$TEST_SOURCE" "$TEST_TARGET" + +PROFILE_TXT="# I am a .profile file" +echo "$PROFILE_TXT" > "$TEST_SOURCE/profile" + +# Creates root link. +echo -e "profile -> .profile" > "$MY_DOTFILE" +assert "dotify -f '$MY_DOTFILE' -t '$TEST_TARGET' | head -n 1" \ + " create: $TEST_TARGET/.dotfiles --> $ABS_TEST_SOURCE" +assert "readlink '$TEST_TARGET/.dotfiles'" "$ABS_TEST_SOURCE" +rm "$TEST_TARGET/.dotfiles" +rm "$TEST_TARGET/.profile" +rm "$MY_DOTFILE" + +# Remove temp files/folders used for locate-dotfile() tests. +rm "$TEST_SOURCE/profile" +rmdir "$TEST_SOURCE" "$TEST_TARGET" "tmp" + +assert_end 'Integration: root link'