change 2 argument semantics to make more sense
authorIan Kelling <ian@iankelling.org>
Mon, 9 Jun 2014 17:35:01 +0000 (10:35 -0700)
committerIan Kelling <ian@iankelling.org>
Mon, 9 Jun 2014 17:35:01 +0000 (10:35 -0700)
lnf
lnf-function
test/test

diff --git a/lnf b/lnf
index fa9b59937108e5b6514cd983d629c3d4f8bfc34b..bf497615a23b7196700cbfc75767b1606762f5e3 100755 (executable)
--- a/lnf
+++ b/lnf
@@ -6,7 +6,9 @@ lnf() {
     local help="lnf [--help] LN_ARGUMENTS...
 Create symlinks conveniently and forcefully.
 Remove existing file/links using trash-put or rm -rf if it is not available.
     local help="lnf [--help] LN_ARGUMENTS...
 Create symlinks conveniently and forcefully.
 Remove existing file/links using trash-put or rm -rf if it is not available.
-Create directory if needed. Finally, ln -s -- LN_ARGUMENTS"
+Create directory if needed. Small change to ln argument semantics: for 2 arguments,
+the second is the link, never a directory to put the link in.
+LN_ARGUMENTS are passed to ln -s --, meaning no ln option arguments are supported."
     if [[ $1 == --help ]]; then
         echo "$help"
         return 0
     if [[ $1 == --help ]]; then
         echo "$help"
         return 0
@@ -19,19 +21,21 @@ Create directory if needed. Finally, ln -s -- LN_ARGUMENTS"
         remove="rm -rf"
     fi
     
         remove="rm -rf"
     fi
     
-    if [[ $# -ge 3 && ! -d ${!#} ]]; then
-        mkdir -p "${!#}"
-    elif [[ $# -ge 2 && -d ${!#} ]]; then
-        local oldcwd=$PWD
-        cd ${!#} # last arg
-        for x in "${@:1:$(($#-1))}"; do # all but last arg
-            # remove any trailing slashes
-            x="${x%%+(/)}"
-            # remove any leading directory components
-            x="${x##*/}"
-            [[ -e "$x" || -L "$x" ]] && $remove "$x"
-        done
-        cd "$oldcwd"
+    if [[ $# -ge 3 ]]; then
+        if [[ -d ${!#} ]]; then
+            local oldcwd=$PWD
+            cd ${!#} # last arg
+            for x in "${@:1:$(($#-1))}"; do # all but last arg
+                # remove any trailing slashes
+                x="${x%%+(/)}"
+                # remove any leading directory components
+                x="${x##*/}"
+                [[ -e "$x" || -L "$x" ]] && $remove "$x"
+            done
+            cd "$oldcwd"
+        else
+            mkdir -p "${!#}"
+        fi
     elif [[ $# -eq 2 ]]; then
         if [[ -e "$2" || -L "$2" ]]; then
             $remove "$2"
     elif [[ $# -eq 2 ]]; then
         if [[ -e "$2" || -L "$2" ]]; then
             $remove "$2"
index 04fe9242c35f80e8cb579e7d85a9e0fa14dae919..7bc88f0c5847ccab79a067b7c3b1ad7313eb6b7b 100644 (file)
@@ -6,7 +6,9 @@ lnf() {
     local help="lnf [--help] LN_ARGUMENTS...
 Create symlinks conveniently and forcefully.
 Remove existing file/links using trash-put or rm -rf if it is not available.
     local help="lnf [--help] LN_ARGUMENTS...
 Create symlinks conveniently and forcefully.
 Remove existing file/links using trash-put or rm -rf if it is not available.
-Create directory if needed. Finally, ln -s -- LN_ARGUMENTS"
+Create directory if needed. Small change to ln argument semantics: for 2 arguments,
+the second is the link, never a directory to put the link in.
+LN_ARGUMENTS are passed to ln -s --, meaning no ln option arguments are supported."
     if [[ $1 == --help ]]; then
         echo "$help"
         return 0
     if [[ $1 == --help ]]; then
         echo "$help"
         return 0
@@ -19,19 +21,21 @@ Create directory if needed. Finally, ln -s -- LN_ARGUMENTS"
         remove="rm -rf"
     fi
     
         remove="rm -rf"
     fi
     
-    if [[ $# -ge 3 && ! -d ${!#} ]]; then
-        mkdir -p "${!#}"
-    elif [[ $# -ge 2 && -d ${!#} ]]; then
-        local oldcwd=$PWD
-        cd ${!#} # last arg
-        for x in "${@:1:$(($#-1))}"; do # all but last arg
-            # remove any trailing slashes
-            x="${x%%+(/)}"
-            # remove any leading directory components
-            x="${x##*/}"
-            [[ -e "$x" || -L "$x" ]] && $remove "$x"
-        done
-        cd "$oldcwd"
+    if [[ $# -ge 3 ]]; then
+        if [[ -d ${!#} ]]; then
+            local oldcwd=$PWD
+            cd ${!#} # last arg
+            for x in "${@:1:$(($#-1))}"; do # all but last arg
+                # remove any trailing slashes
+                x="${x%%+(/)}"
+                # remove any leading directory components
+                x="${x##*/}"
+                [[ -e "$x" || -L "$x" ]] && $remove "$x"
+            done
+            cd "$oldcwd"
+        else
+            mkdir -p "${!#}"
+        fi
     elif [[ $# -eq 2 ]]; then
         if [[ -e "$2" || -L "$2" ]]; then
             $remove "$2"
     elif [[ $# -eq 2 ]]; then
         if [[ -e "$2" || -L "$2" ]]; then
             $remove "$2"
index c2bdb6a95bb612573967deba3f82f8ae00f05b81..218b67c1ddd981ebadb9dd2c863c7730cd7ddfa9 100755 (executable)
--- a/test/test
+++ b/test/test
@@ -1,42 +1,43 @@
 #!/bin/bash
 # some basic sanity / verification
 #!/bin/bash
 # some basic sanity / verification
-# assumes we have trash-put in path
+
 # I don't recommend actually running this unless you have a reason to,
 # since it creates & deletes files, and being a test, it is not as thoroughly
 # inspected for bugs
 
 # I don't recommend actually running this unless you have a reason to,
 # since it creates & deletes files, and being a test, it is not as thoroughly
 # inspected for bugs
 
-err() {
-    echo error
-}
-# target linkname
+# assumes we have trash-put in path, and that lnf-function is in .. to this files location
+
+
+# 2 arguments, test that a file in the link location is removed and replaed with a link
 case1() {
     touch b
     lnf a b
 case1() {
     touch b
     lnf a b
-    [[ -L b ]] || err
+    [[ -L b ]] 
 }
 
 }
 
-# target directory
+# 2 arguments, test that directory in link location is removed and replaced with a link
 case2() {
     mkdir b
     lnf a b
 case2() {
     mkdir b
     lnf a b
-    [[ -L b/a ]] || err
+    [[ -L b ]] 
 }
 
 }
 
-# target
+# single argument, test that an existing non-empty directory is removed and replaced by a link
 case3() {
     mkdir a
     touch a/b
     lnf ../a
 case3() {
     mkdir a
     touch a/b
     lnf ../a
-    [[ -L a ]] || err
+    [[ -L a ]] 
 }
 
 }
 
-# target target directory
+# 4 arguments, 2 of the link locations already contain files.
+# test that they got removed and replaced by links
 case4() {
     mkdir a
     touch a/b
     touch a/c
     lnf b c d a
 case4() {
     mkdir a
     touch a/b
     touch a/c
     lnf b c d a
-    [[ -L a/b && -L a/c && -L a/d ]] || err
+    [[ -L a/b && -L a/c && -L a/d ]] 
 }
 
 cleanup() {
 }
 
 cleanup() {
@@ -58,14 +59,17 @@ docases() {
 source ${0%/*}/../lnf-function
 # might want to undo this if things go wrong
 # set -x
 source ${0%/*}/../lnf-function
 # might want to undo this if things go wrong
 # set -x
-set -eE;
+
+# trap errors, and output a simple bash stack trace
+set -E;
 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\"  }\$?=$?"' ERR
 cd $(mktemp -d)
 docases
 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\"  }\$?=$?"' ERR
 cd $(mktemp -d)
 docases
-trash-put() {
-    rm -rf "$@"
-}
+# test again, using rm -rf in place of trash-put.
+# assumes that rm is in /bin and trash-put is in /usr/bin
+PATH="${PATH//:\/usr\/bin}"
+PATH="${PATH//\/usr\/bin:}"
 docases
 
 docases
 
-echo success
+echo tests concluded