aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-06-23 03:57:23 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-06-23 03:57:23 +0200
commite08fd73f1d8e84540d5ebdc5fdf98c047da0976e (patch)
treedc8381d2cbeb79e1527102c3b2082fe66dfdd96b
parentDocument filename-extension and realpath. (diff)
downloadcalp-e08fd73f1d8e84540d5ebdc5fdf98c047da0976e.tar.gz
calp-e08fd73f1d8e84540d5ebdc5fdf98c047da0976e.tar.xz
Wrote (and fixed) tests for filename-extension.
-rw-r--r--module/hnh/util/path.scm8
-rw-r--r--tests/test/util.scm34
2 files changed, 40 insertions, 2 deletions
diff --git a/module/hnh/util/path.scm b/module/hnh/util/path.scm
index ac6df491..ea081e85 100644
--- a/module/hnh/util/path.scm
+++ b/module/hnh/util/path.scm
@@ -66,7 +66,13 @@
(char=? #\. (string-ref base 0))))
(define (filename-extension filename)
- (car (reverse (string-split filename #\.))))
+ (let ((components (-> filename
+ ;; Path split removes potential trailing directory separator
+ path-split last
+ basename
+ (string-split #\.))))
+ (if (>= 1 (length components))
+ "" (last components))))
(define (realpath filename)
(unless (string? filename)
diff --git a/tests/test/util.scm b/tests/test/util.scm
index 3fd926f2..e6b0c214 100644
--- a/tests/test/util.scm
+++ b/tests/test/util.scm
@@ -10,7 +10,11 @@
:use-module (hnh util)
:use-module (hnh util env)
:use-module ((hnh util path)
- :select (path-append path-split file-hidden? realpath)))
+ :select (path-append
+ path-split
+ file-hidden?
+ realpath
+ filename-extension)))
(test-equal "when"
1 (when #t 1))
@@ -274,6 +278,8 @@
(test-assert (not (file-hidden? "/visible/.in/hidden")))
(test-assert (not (file-hidden? "")))
+;; TODO test realpath with .. and similar
+
(test-equal "Realpath for path fragment"
"/home/hugo"
(with-working-directory
@@ -291,3 +297,29 @@
(with-working-directory
"/tmp"
(lambda () (realpath "/home/hugo"))))
+
+
+(test-equal "Extension of simple file"
+ "txt" (filename-extension "file.txt"))
+
+(test-equal "Extension of file with directory"
+ "txt" (filename-extension "/direcotry/file.txt"))
+
+(test-equal "Extension of file with multiple"
+ "gz" (filename-extension "filename.tar.gz"))
+
+(test-equal "Filename extension when none is present"
+ "" (filename-extension "filename"))
+
+(test-equal "Filename extension when none is present, but directory has"
+ "" (filename-extension "config.d/filename"))
+
+(test-equal "Filename extension of directory"
+ "d" (filename-extension "config.d/"))
+
+
+(test-equal "Extension of hidden file"
+ "sh" (filename-extension ".bashrc.sh"))
+
+(test-equal "Extension of hidden file without extension"
+ "bashrc" (filename-extension ".bashrc"))