diff options
-rw-r--r-- | module/hnh/util/path.scm | 15 | ||||
-rw-r--r-- | tests/test/util.scm | 21 |
2 files changed, 35 insertions, 1 deletions
diff --git a/module/hnh/util/path.scm b/module/hnh/util/path.scm index 49b53897..7eac630b 100644 --- a/module/hnh/util/path.scm +++ b/module/hnh/util/path.scm @@ -60,3 +60,18 @@ (define-public (filename-extension filename) (car (reverse (string-split filename #\.)))) + + +(define-public (realpath filename) + (unless (string? filename) + (scm-error 'wrong-type-arg "realpath" + "filename not a string: ~a" + (list filename) #f)) + (when (string-null? filename) + (scm-error 'wrong-type-arg "realpath" + "filename can't be empty" + #f #f)) + + (if (absolute-file-name? filename) + filename + (path-append (getcwd) filename))) diff --git a/tests/test/util.scm b/tests/test/util.scm index 95fa8da0..3fd926f2 100644 --- a/tests/test/util.scm +++ b/tests/test/util.scm @@ -8,8 +8,9 @@ :use-module (srfi srfi-88) :use-module (srfi srfi-1) :use-module (hnh util) + :use-module (hnh util env) :use-module ((hnh util path) - :select (path-append path-split file-hidden?))) + :select (path-append path-split file-hidden? realpath))) (test-equal "when" 1 (when #t 1)) @@ -272,3 +273,21 @@ (test-assert (file-hidden? "/path/to/.hidden")) (test-assert (not (file-hidden? "/visible/.in/hidden"))) (test-assert (not (file-hidden? ""))) + +(test-equal "Realpath for path fragment" + "/home/hugo" + (with-working-directory + "/home" + (lambda () (realpath "hugo")))) + +(test-equal "Realpath for already absolute path" + "/home/hugo" + (with-working-directory + "/tmp" + (lambda () (realpath "/home/hugo")))) + +(test-equal "Realpath for already absolute path" + "/home/hugo" + (with-working-directory + "/tmp" + (lambda () (realpath "/home/hugo")))) |