Repository: mesonbuild/meson

Issue:
Support fs module methods for File objects
**Describe the bug**

Nearly all functions provided by the fs module don't support `File` objects created with the builtin `files()` they all operate only on strings.
I have the following use case where is would be great if the fs methods would also accept `File` as argument type:

We would like to structure our test cases in a one directory per submodule tree rather than in a single flat directory.

	tests/ - for generic tests
	tests/io/ - tests for the io subsystem
	tests/utils/ - tests for utility code

A test would be defined as a dictionary and appended to a global list.
This list would be used to build the actual test objects.
All source files used to build a test would be an array of `File` objects created with `files()` in the test dictionary.

Furthermore we have the convention that a test's name equals its main source file without a suffix.

To prevent duplication of names for all test cases like:

```meson
tests = [{'sources': files('foo.cpp'), 'name': 'foo'}]

foreach test_dict: tests
	sources = test_dict['sources']
	name = test_dict['name']
	exe = executable(test_name, sources, ...)
	test(test_name, exe, ...)
endforeach
```

I would like to calculate the test name from its sources:

```meson

tests = [{'sources': files('foo.cpp')]

foreach test_dict: tests
	sources = test_dict['sources']
	name = fs.stem(sources[0])
	exe = executable(test_name, sources, ...)
	test(name, exe, ...)
endforeach
```

Unfortunately `fs.stem` expects a `str` argument and I don't know of a way to convert a `File` object into a `str` in meson.

To solve this we could either introduce a way to convert a `File` object to `str`.
Or we could extend the fs methods to take `FileOrString` arguments and transparently convert a `File` argument to `str`.

**To Reproduce**

foo.cpp:

```c++
int main() {
    return 0;
}
```

meson.build

```meson
project('fs_files_reproducer', 'cpp')                                           

fs = import('fs')

f = files('foo.cpp')
executable(fs.stem(f[0]), f)
```

**Expected behavior**

Either

	executable(fs.stem(f[0]), f)

or

	executable(fs.stem(f[0]).to_string(), f)

should work.

I am willing to implement both approaches if this is something meson wants
and there is no obvious solution I am unaware of.

**system parameters**
* Arch Linux
* meson 0.57.1



Discussion:

Resolving patch:
diff --git a/mesonbuild/modules/fs.py b/mesonbuild/modules/fs.py
--- a/mesonbuild/modules/fs.py
+++ b/mesonbuild/modules/fs.py
@@ -39,13 +39,15 @@ def __init__(self, interpreter: 'Interpreter') -> None:
         super().__init__(interpreter)
         self.snippets.add('generate_dub_file')
 
-    def _absolute_dir(self, state: 'ModuleState', arg: str) -> Path:
+    def _absolute_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path:
         """
         make an absolute path from a relative path, WITHOUT resolving symlinks
         """
+        if isinstance(arg, File):
+            return Path(arg.absolute_path(state.source_root, self.interpreter.environment.get_build_dir()))
         return Path(state.source_root) / Path(state.subdir) / Path(arg).expanduser()
 
-    def _resolve_dir(self, state: 'ModuleState', arg: str) -> Path:
+    def _resolve_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path:
         """
         resolves symlinks and makes absolute a directory relative to calling meson.build,
         if not already absolute
@@ -59,7 +61,7 @@ def _resolve_dir(self, state: 'ModuleState', arg: str) -> Path:
             pass
         return path
 
-    def _check(self, check: str, state: 'ModuleState', args: T.Sequence[str]) -> ModuleReturnValue:
+    def _check(self, check: str, state: 'ModuleState', args: T.Sequence['FileOrString']) -> ModuleReturnValue:
         test_file = self._resolve_dir(state, args[0])
         val = getattr(test_file, check)()
         if isinstance(val, Path):
@@ -74,9 +76,11 @@ def expanduser(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[st
 
     @noKwargs
     @FeatureNew('fs.is_absolute', '0.54.0')
-    @typed_pos_args('fs.is_absolute', str)
-    def is_absolute(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
-        return ModuleReturnValue(PurePath(args[0]).is_absolute(), [])
+    @typed_pos_args('fs.is_absolute', (str, File))
+    def is_absolute(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.is_absolute_file', '0.59.0').use(state.subproject)
+        return ModuleReturnValue(PurePath(str(args[0])).is_absolute(), [])
 
     @noKwargs
     @FeatureNew('fs.as_posix', '0.54.0')
@@ -94,8 +98,10 @@ def exists(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T
         return self._check('exists', state, args)
 
     @noKwargs
-    @typed_pos_args('fs.is_symlink', str)
-    def is_symlink(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+    @typed_pos_args('fs.is_symlink', (str, File))
+    def is_symlink(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.is_symlink_file', '0.59.0').use(state.subproject)
         return ModuleReturnValue(self._absolute_dir(state, args[0]).is_symlink(), [])
 
     @noKwargs
@@ -109,8 +115,10 @@ def is_dir(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T
         return self._check('is_dir', state, args)
 
     @noKwargs
-    @typed_pos_args('fs.hash', str, str)
-    def hash(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+    @typed_pos_args('fs.hash', (str, File), str)
+    def hash(self, state: 'ModuleState', args: T.Tuple['FileOrString', str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.hash_file', '0.59.0').use(state.subproject)
         file = self._resolve_dir(state, args[0])
         if not file.is_file():
             raise MesonException(f'{file} is not a file and therefore cannot be hashed')
@@ -123,8 +131,10 @@ def hash(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.Dict[str
         return ModuleReturnValue(h.hexdigest(), [])
 
     @noKwargs
-    @typed_pos_args('fs.size', str)
-    def size(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+    @typed_pos_args('fs.size', (str, File))
+    def size(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.size_file', '0.59.0').use(state.subproject)
         file = self._resolve_dir(state, args[0])
         if not file.is_file():
             raise MesonException(f'{file} is not a file and therefore cannot be sized')
@@ -134,8 +144,10 @@ def size(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.A
             raise MesonException('{} size could not be determined'.format(args[0]))
 
     @noKwargs
-    @typed_pos_args('fs.is_samepath', str, str)
-    def is_samepath(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+    @typed_pos_args('fs.is_samepath', (str, File), (str, File))
+    def is_samepath(self, state: 'ModuleState', args: T.Tuple['FileOrString', 'FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File) or isinstance(args[1], File):
+            FeatureNew('fs.is_samepath_file', '0.59.0').use(state.subproject)
         file1 = self._resolve_dir(state, args[0])
         file2 = self._resolve_dir(state, args[1])
         if not file1.exists():
@@ -148,31 +160,39 @@ def is_samepath(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.D
             return ModuleReturnValue(False, [])
 
     @noKwargs
-    @typed_pos_args('fs.replace_suffix', str, str)
-    def replace_suffix(self, state: 'ModuleState', args: T.Tuple[str, str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
-        original = PurePath(args[0])
+    @typed_pos_args('fs.replace_suffix', (str, File), str)
+    def replace_suffix(self, state: 'ModuleState', args: T.Tuple['FileOrString', str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.replace_suffix_file', '0.59.0').use(state.subproject)
+        original = PurePath(str(args[0]))
         new = original.with_suffix(args[1])
         return ModuleReturnValue(str(new), [])
 
     @noKwargs
-    @typed_pos_args('fs.parent', str)
-    def parent(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
-        original = PurePath(args[0])
+    @typed_pos_args('fs.parent', (str, File))
+    def parent(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.parent_file', '0.59.0').use(state.subproject)
+        original = PurePath(str(args[0]))
         new = original.parent
         return ModuleReturnValue(str(new), [])
 
     @noKwargs
-    @typed_pos_args('fs.name', str)
-    def name(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
-        original = PurePath(args[0])
+    @typed_pos_args('fs.name', (str, File))
+    def name(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.name_file', '0.59.0').use(state.subproject)
+        original = PurePath(str(args[0]))
         new = original.name
         return ModuleReturnValue(str(new), [])
 
     @noKwargs
-    @typed_pos_args('fs.stem', str)
+    @typed_pos_args('fs.stem', (str, File))
     @FeatureNew('fs.stem', '0.54.0')
-    def stem(self, state: 'ModuleState', args: T.Tuple[str], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
-        original = PurePath(args[0])
+    def stem(self, state: 'ModuleState', args: T.Tuple['FileOrString'], kwargs: T.Dict[str, T.Any]) -> ModuleReturnValue:
+        if isinstance(args[0], File):
+            FeatureNew('fs.stem_file', '0.59.0').use(state.subproject)
+        original = PurePath(str(args[0]))
         new = original.stem
         return ModuleReturnValue(str(new), [])
 


Test patch:
diff --git a/test cases/common/220 fs module/meson.build b/test cases/common/220 fs module/meson.build
--- a/test cases/common/220 fs module/meson.build	
+++ b/test cases/common/220 fs module/meson.build	
@@ -4,6 +4,8 @@ is_windows = build_machine.system() == 'windows'
 
 fs = import('fs')
 
+f = files('meson.build')
+
 assert(fs.exists('meson.build'), 'Existing file reported as missing.')
 assert(not fs.exists('nonexisting'), 'Nonexisting file was found.')
 
@@ -16,6 +18,7 @@ if not is_windows and build_machine.system() != 'cygwin'
   run_command('ln', '-s', meson.current_source_dir() / 'meson.build', symlink)
   assert(fs.is_symlink(symlink), 'Symlink not detected.')
   assert(not fs.is_symlink('meson.build'), 'Regular file detected as symlink.')
+  assert(not fs.is_symlink(f[0]), 'Regular file detected as symlink.')
 endif
 
 assert(fs.is_file('meson.build'), 'File not detected as a file.')
@@ -54,11 +57,15 @@ endif
 
 original = 'foo'
 assert(fs.replace_suffix(original, '') == original, 'replace_suffix idempotent')
+assert(fs.replace_suffix(f[0], '') == 'meson', 'replace_suffix trim')
 
 original = 'foo.txt'
 new = fs.replace_suffix(original, '.ini')
 assert(new == 'foo.ini', 'replace_suffix failed')
 
+new = fs.replace_suffix(f[0], '.ini')
+assert(new == 'meson.ini', 'replace_suffix failed')
+
 original = 'foo'
 new = fs.replace_suffix(original, '.ini')
 assert(new == 'foo.ini', 'replace_suffix did not add suffix to suffixless file')
@@ -86,15 +93,24 @@ sha256 = fs.hash('subdir/subdirfile.txt', 'sha256')
 assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match')
 assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match')
 
+f = files('subdir/subdirfile.txt')
+md5 = fs.hash(f[0], 'md5')
+assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match')
+sha256 = fs.hash(f[0], 'sha256')
+assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match')
+
 # -- size
 
 size = fs.size('subdir/subdirfile.txt')
 assert(size == 19, 'file size not found correctly')
 
+size = fs.size(f[0])
+assert(size == 19, 'file size not found correctly')
+
 # -- are filenames referring to the same file?
 f1 = 'meson.build'
 f2 = 'subdir/../meson.build'
-assert(fs.is_samepath(f1, f2), 'is_samepath not detercting same files')
+assert(fs.is_samepath(f1, f2), 'is_samepath not detecting same files')
 assert(fs.is_samepath(meson.source_root(), 'subdir/..'), 'is_samepath not detecting same directory')
 assert(fs.is_samepath(meson.project_source_root(), 'subdir/..'), 'is_samepath not detecting same directory')
 assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / 'subdir/..'), 'is_samepath not detecting same directory')
@@ -103,13 +119,22 @@ assert(fs.is_samepath(meson.global_build_root(), meson.current_build_dir()), 'is
 assert(not fs.is_samepath(f1, 'subdir/subdirfile.txt'), 'is_samepath known bad comparison')
 assert(not fs.is_samepath('not-a-path', f2), 'is_samepath should not error if path(s) do not exist')
 
+f = files('meson.build', 'subdir/../meson.build')
+assert(fs.is_samepath(f[0], f[1]), 'is_samepath not detercting same files')
+
 if not is_windows and build_machine.system() != 'cygwin'
   assert(fs.is_samepath(symlink, 'meson.build'), 'symlink is_samepath fail')
 endif
 
 # parts of path
 assert(fs.parent('foo/bar') == 'foo', 'failed to get dirname')
+if not is_windows
+assert(fs.parent(f[1]) == 'subdir/..', 'failed to get dirname')
+else
+assert(fs.parent(f[1]) == 'subdir\..', 'failed to get dirname')
+endif
 assert(fs.name('foo/bar') == 'bar', 'failed to get basename')
+assert(fs.name(f[1]) == 'meson.build', 'failed to get basename')
 assert(fs.name('foo/bar/baz.dll.a') == 'baz.dll.a', 'failed to get basename with compound suffix')
 assert(fs.stem('foo/bar/baz.dll') == 'baz', 'failed to get stem with suffix')
 assert(fs.stem('foo/bar/baz.dll.a') == 'baz.dll', 'failed to get stem with compound suffix')
diff --git a/test cases/common/220 fs module/subdir/meson.build b/test cases/common/220 fs module/subdir/meson.build
--- a/test cases/common/220 fs module/subdir/meson.build	
+++ b/test cases/common/220 fs module/subdir/meson.build	
@@ -1,3 +1,6 @@
+subdirfiles = files('subdirfile.txt')
 assert(fs.exists('subdirfile.txt'), 'Subdir file lookup is broken.')
 assert(fs.is_samepath(meson.project_source_root(), '..'), 'is_samepath not detecting same directory')
 assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / '..'), 'is_samepath not detecting same directory')
+
+assert(fs.is_samepath(subdirfiles[0], 'subdirfile.txt'), 'is_samepath not detecting same directory when using File and str')