aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-05-18 00:15:54 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-05-18 00:15:54 +0200
commitbd3e62661c43d6198fb59d454e4a864726995e62 (patch)
tree358fce681b77077ee3a735894aa2bbb1e38a4851
parentAdd check rule to makefile. (diff)
downloadcalp-bd3e62661c43d6198fb59d454e4a864726995e62.tar.gz
calp-bd3e62661c43d6198fb59d454e4a864726995e62.tar.xz
Add procedure realpath.
-rw-r--r--module/hnh/util/path.scm15
-rw-r--r--tests/test/util.scm21
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"))))