diff options
-rw-r--r-- | docs/markdown/Fs-module.md | 16 | ||||
-rw-r--r-- | mesonbuild/modules/fs.py | 9 | ||||
-rw-r--r-- | test cases/common/227 fs module/meson.build | 8 |
3 files changed, 28 insertions, 5 deletions
diff --git a/docs/markdown/Fs-module.md b/docs/markdown/Fs-module.md index 36b4c4a..3b33e7f 100644 --- a/docs/markdown/Fs-module.md +++ b/docs/markdown/Fs-module.md @@ -37,7 +37,7 @@ by the string is a symbolic link. ### is_absolute -Return a boolean indicating if the path string specified is absolute for this computer, WITHOUT expanding `~`. +Return a boolean indicating if the path string specified is absolute, WITHOUT expanding `~`. Examples: @@ -96,7 +96,19 @@ fs.is_samepath(p, s) # false ## Filename modification -The files need not actually exist yet for these methods, as they are just string manipulation. +The files need not actually exist yet for these path string manipulation methods. + +### expanduser + +A path string with a leading `~` is expanded to the user home directory + +Examples: + +```meson +fs.expanduser('~') # home directory + +fs.expanduser('~/foo') # <homedir>/foo +``` ### as_posix diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py index 9b2cb8b..f006500 100644 --- a/mesonbuild/modules/fs.py +++ b/mesonbuild/modules/fs.py @@ -14,7 +14,7 @@ import typing as T import hashlib -from pathlib import Path, PurePath +from pathlib import Path, PurePath, PureWindowsPath from .. import mlog from . import ExtensionModule @@ -62,6 +62,13 @@ class FSModule(ExtensionModule): @stringArgs @noKwargs + def expanduser(self, state: 'ModuleState', args: T.Sequence[str], kwargs: dict) -> ModuleReturnValue: + if len(args) != 1: + raise MesonException('fs.expanduser takes exactly one argument.') + return ModuleReturnValue(str(Path(args[0]).expanduser()), []) + + @stringArgs + @noKwargs def is_absolute(self, state: 'ModuleState', args: T.Sequence[str], kwargs: dict) -> ModuleReturnValue: if len(args) != 1: raise MesonException('fs.is_absolute takes exactly one argument.') diff --git a/test cases/common/227 fs module/meson.build b/test cases/common/227 fs module/meson.build index 670ffed..8a77fdf 100644 --- a/test cases/common/227 fs module/meson.build +++ b/test cases/common/227 fs module/meson.build @@ -30,8 +30,12 @@ assert(fs.is_dir('subprojects'), 'Dir not detected correctly.') assert(not fs.is_dir('meson.build'), 'File detected as a dir.') assert(not fs.is_dir('nonexisting'), 'Bad path detected as a dir.') -assert(fs.is_dir('~'), 'expanduser not working') -assert(not fs.is_file('~'), 'expanduser not working') +assert(fs.is_dir('~'), 'home directory not detected') +assert(not fs.is_file('~'), 'home directory detected as file') + +# -- expanduser +assert(fs.expanduser('~') != '~','expanduser failed') +assert(fs.expanduser('~/foo').endswith('foo'), 'expanduser with tail failed') # -- as_posix assert(fs.as_posix('/') == '/', 'as_posix idempotent') |