diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-01-11 15:37:12 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-11 15:37:12 +0100 |
| commit | bae8a0a45393518bbf7914b10bf36139a977cda6 (patch) | |
| tree | 6974858d868afc1d65dae8d19afc53213fe0ef99 | |
| parent | 29afc301ea05bc907444b698c91b141c26167baa (diff) | |
| download | vis-lspc-detect-root.tar.gz vis-lspc-detect-root.zip | |
handle special cases in util.dirnamedetect-root
| -rw-r--r-- | util.lua | 22 | ||||
| -rwxr-xr-x | util_test.lua | 18 |
2 files changed, 39 insertions, 1 deletions
@@ -127,7 +127,27 @@ end -- @param the pathname -- @return the pathname up to the last '/' function util.dirname(name) - return name:match('(.*[/])') + if name == '.' or name == '..' or name == '/' then + return name + end + + -- strip a trailing path separator + if name:sub(#name, #name) == '/' then + name = name:sub(1, #name - 1) + end + + local dirname = name:match('(.*)[/]') + -- There was no path separator in name. + if not dirname then + return '.' + end + + -- The name started with the root dir. + if dirname == '' then + return '/' + end + + return dirname end --- Create an iterator yielding the nth line of a file diff --git a/util_test.lua b/util_test.lua new file mode 100755 index 0000000..fd6570d --- /dev/null +++ b/util_test.lua @@ -0,0 +1,18 @@ +#!/usr/bin/env lua5.4 + +-- mock vis global +vis = {} -- luacheck: ignore 111 +local util = require('util') + +local lunatest = require('lunatest') + +function test_dirname() -- luacheck: ignore 111 + lunatest.assert_equal('/usr', util.dirname('/usr/lib')) + lunatest.assert_equal('/', util.dirname('/usr/')) + lunatest.assert_equal('.', util.dirname('usr')) + lunatest.assert_equal('.', util.dirname('.')) + lunatest.assert_equal('..', util.dirname('..')) + lunatest.assert_equal('/', util.dirname('/')) +end + +lunatest.run() |
