From e08fd73f1d8e84540d5ebdc5fdf98c047da0976e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 23 Jun 2022 03:57:23 +0200 Subject: Wrote (and fixed) tests for filename-extension. --- module/hnh/util/path.scm | 8 +++++++- tests/test/util.scm | 34 +++++++++++++++++++++++++++++++++- 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")) -- cgit v1.2.3