| 1 |
project('fs module test', 'c') |
| 2 |
|
| 3 |
is_windows = build_machine.system() == 'windows' |
| 4 |
|
| 5 |
fs = import('fs') |
| 6 |
|
| 7 |
f = files('meson.build') |
| 8 |
btgt = executable('btgt', 'btgt.c') |
| 9 |
ctgt = fs.copyfile('ctgt.txt') |
| 10 |
|
| 11 |
assert(fs.exists('meson.build'), 'Existing file reported as missing.') |
| 12 |
assert(not fs.exists('nonexisting'), 'Nonexisting file was found.') |
| 13 |
|
| 14 |
if not is_windows and build_machine.system() != 'cygwin' |
| 15 |
# Symlinks on Windows have specific requirements including: |
| 16 |
# * Meson running under Python >= 3.8 |
| 17 |
# * Windows user permissions to create symlinks, and/or Windows in Developer mode |
| 18 |
# so at this time the symlink test is skipped for Windows. |
| 19 |
symlink = meson.current_build_dir() / 'a_symlink' |
| 20 |
run_command('ln', '-s', '-f', meson.current_source_dir() / 'meson.build', symlink, check: true) |
| 21 |
assert(fs.is_symlink(symlink), 'Symlink not detected.') |
| 22 |
assert(not fs.is_symlink('meson.build'), 'Regular file detected as symlink.') |
| 23 |
assert(not fs.is_symlink(f[0]), 'Regular file detected as symlink.') |
| 24 |
endif |
| 25 |
|
| 26 |
assert(fs.is_file('meson.build'), 'File not detected as a file.') |
| 27 |
assert(not fs.is_file('subprojects'), 'Directory detected as a file.') |
| 28 |
assert(not fs.is_file('nonexisting'), 'Bad path detected as a file.') |
| 29 |
|
| 30 |
assert(fs.is_dir('subprojects'), 'Dir not detected correctly.') |
| 31 |
assert(not fs.is_dir('meson.build'), 'File detected as a dir.') |
| 32 |
assert(not fs.is_dir('nonexisting'), 'Bad path detected as a dir.') |
| 33 |
|
| 34 |
# These do not work with pbuilder for some reason. |
| 35 |
# I have not been able to replicate this manually, |
| 36 |
# even with 'pbuilder login'. |
| 37 |
# |
| 38 |
#assert(fs.is_dir('~'), 'home directory not detected') |
| 39 |
#assert(not fs.is_file('~'), 'home directory detected as file') |
| 40 |
# |
| 41 |
## -- expanduser |
| 42 |
#assert(fs.expanduser('~') != '~','expanduser failed') |
| 43 |
#assert(fs.expanduser('~/foo').endswith('foo'), 'expanduser with tail failed') |
| 44 |
|
| 45 |
# -- as_posix |
| 46 |
assert(fs.as_posix('/') == '/', 'as_posix idempotent') |
| 47 |
assert(fs.as_posix('\\') == '/', 'as_posix simple') |
| 48 |
# Python 3.12 changed how these paths are handled, so deal with both. |
| 49 |
drivepath = fs.as_posix('\\\\') |
| 50 |
assert(drivepath == '/' or drivepath == '//', 'as_posix simple') |
| 51 |
assert(fs.as_posix('foo\\bar/baz') == 'foo/bar/baz', 'as_posix mixed slash') |
| 52 |
|
| 53 |
# -- is_absolute |
| 54 |
winabs = 'q:/foo' |
| 55 |
unixabs = '/foo' |
| 56 |
if is_windows |
| 57 |
assert(fs.is_absolute(winabs), 'is_absolute windows not detected') |
| 58 |
assert(not fs.is_absolute(unixabs), 'is_absolute unix false positive') |
| 59 |
assert(fs.is_absolute('//foo'), 'is_absolute failed on incomplete UNC path') |
| 60 |
else |
| 61 |
assert(fs.is_absolute(unixabs), 'is_absolute unix not detected') |
| 62 |
assert(not fs.is_absolute(winabs), 'is_absolute windows false positive') |
| 63 |
endif |
| 64 |
|
| 65 |
# -- replace_suffix |
| 66 |
|
| 67 |
original = 'foo' |
| 68 |
assert(fs.replace_suffix(original, '') == original, 'replace_suffix idempotent') |
| 69 |
assert(fs.replace_suffix(f[0], '') == 'meson', 'replace_suffix trim') |
| 70 |
|
| 71 |
original = 'foo.txt' |
| 72 |
new = fs.replace_suffix(original, '.ini') |
| 73 |
assert(new == 'foo.ini', 'replace_suffix failed') |
| 74 |
|
| 75 |
new = fs.replace_suffix(f[0], '.ini') |
| 76 |
assert(new == 'meson.ini', 'replace_suffix failed') |
| 77 |
|
| 78 |
original = 'foo' |
| 79 |
new = fs.replace_suffix(original, '.ini') |
| 80 |
assert(new == 'foo.ini', 'replace_suffix did not add suffix to suffixless file') |
| 81 |
|
| 82 |
original = 'foo.dll.a' |
| 83 |
new = fs.replace_suffix(original, '.so') |
| 84 |
assert(new == 'foo.dll.so', 'replace_suffix did not only modify last suffix') |
| 85 |
|
| 86 |
original = 'foo.dll' |
| 87 |
new = fs.replace_suffix(original, '') |
| 88 |
assert(new == 'foo', 'replace_suffix did not only delete last suffix') |
| 89 |
|
| 90 |
# `/` on windows is interpreted like `.drive` which in general may not be `c:/` |
| 91 |
# the files need not exist for fs.replace_suffix() |
| 92 |
original = is_windows ? 'j:\\foo\\bar.txt' : '/foo/bar.txt' |
| 93 |
new_check = is_windows ? 'j:\\foo\\bar.ini' : '/foo/bar.ini' |
| 94 |
|
| 95 |
new = fs.replace_suffix(original, '.ini') |
| 96 |
assert(new == new_check, 'absolute path replace_suffix failed') |
| 97 |
|
| 98 |
new = fs.replace_suffix(btgt, '.ini') |
| 99 |
assert(new == 'btgt.ini', 'replace_suffix failed for build target') |
| 100 |
new = fs.replace_suffix(ctgt, '.ini') |
| 101 |
assert(new == 'ctgt.ini', 'replace_suffix failed for custom target') |
| 102 |
new = fs.replace_suffix(ctgt[0], '.ini') |
| 103 |
assert(new == 'ctgt.ini', 'replace_suffix failed for custom target index') |
| 104 |
|
| 105 |
# -- hash |
| 106 |
|
| 107 |
md5 = fs.hash('subdir/subdirfile.txt', 'md5') |
| 108 |
sha256 = fs.hash('subdir/subdirfile.txt', 'sha256') |
| 109 |
assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match') |
| 110 |
assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match') |
| 111 |
|
| 112 |
f = files('subdir/subdirfile.txt') |
| 113 |
md5 = fs.hash(f[0], 'md5') |
| 114 |
assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match') |
| 115 |
sha256 = fs.hash(f[0], 'sha256') |
| 116 |
assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match') |
| 117 |
|
| 118 |
# -- size |
| 119 |
|
| 120 |
size = fs.size('subdir/subdirfile.txt') |
| 121 |
assert(size == 19, 'file size not found correctly') |
| 122 |
|
| 123 |
size = fs.size(f[0]) |
| 124 |
assert(size == 19, 'file size not found correctly') |
| 125 |
|
| 126 |
# -- are filenames referring to the same file? |
| 127 |
f1 = 'meson.build' |
| 128 |
f2 = 'subdir/../meson.build' |
| 129 |
assert(fs.is_samepath(f1, f2), 'is_samepath not detecting same files') |
| 130 |
assert(fs.is_samepath(meson.source_root(), 'subdir/..'), 'is_samepath not detecting same directory') |
| 131 |
assert(fs.is_samepath(meson.project_source_root(), 'subdir/..'), 'is_samepath not detecting same directory') |
| 132 |
assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / 'subdir/..'), 'is_samepath not detecting same directory') |
| 133 |
assert(fs.is_samepath(meson.global_source_root(), meson.current_source_dir()), 'is_samepath not detecting same directory') |
| 134 |
assert(fs.is_samepath(meson.global_build_root(), meson.current_build_dir()), 'is_samepath not detecting same directory') |
| 135 |
assert(not fs.is_samepath(f1, 'subdir/subdirfile.txt'), 'is_samepath known bad comparison') |
| 136 |
assert(not fs.is_samepath('not-a-path', f2), 'is_samepath should not error if path(s) do not exist') |
| 137 |
|
| 138 |
f = files('meson.build', 'subdir/../meson.build') |
| 139 |
assert(fs.is_samepath(f[0], f[1]), 'is_samepath not detecting same files') |
| 140 |
|
| 141 |
if not is_windows and build_machine.system() != 'cygwin' |
| 142 |
assert(fs.is_samepath(symlink, 'meson.build'), 'symlink is_samepath fail') |
| 143 |
endif |
| 144 |
|
| 145 |
# parts of path |
| 146 |
assert(fs.parent('foo/bar') == 'foo', 'failed to get dirname') |
| 147 |
assert(fs.parent(f[1]) == 'subdir/..', 'failed to get dirname for file') |
| 148 |
assert(fs.parent(btgt) == '.', 'failed to get dirname for build target') |
| 149 |
assert(fs.parent(ctgt) == '.', 'failed to get dirname for custom target') |
| 150 |
assert(fs.parent(ctgt[0]) == '.', 'failed to get dirname for custom target index') |
| 151 |
|
| 152 |
assert(fs.name('foo/bar') == 'bar', 'failed to get basename') |
| 153 |
assert(fs.name(f[1]) == 'meson.build', 'failed to get basename') |
| 154 |
assert(fs.name('foo/bar/baz.dll.a') == 'baz.dll.a', 'failed to get basename with compound suffix') |
| 155 |
if host_machine.system() in ['cygwin', 'windows'] |
| 156 |
assert(fs.name(btgt) == 'btgt.exe', 'failed to get basename of build target') |
| 157 |
assert(fs.suffix(btgt) == '.exe', 'failed to get build target suffix') |
| 158 |
else |
| 159 |
assert(fs.name(btgt) == 'btgt', 'failed to get basename of build target') |
| 160 |
assert(fs.suffix(btgt) == '', 'failed to get build target suffix') |
| 161 |
endif |
| 162 |
assert(fs.name(ctgt) == 'ctgt.txt', 'failed to get basename of custom target') |
| 163 |
assert(fs.name(ctgt[0]) == 'ctgt.txt', 'failed to get basename of custom target index') |
| 164 |
assert(fs.stem('foo/bar/baz.dll') == 'baz', 'failed to get stem with suffix') |
| 165 |
assert(fs.stem('foo/bar/baz.dll.a') == 'baz.dll', 'failed to get stem with compound suffix') |
| 166 |
assert(fs.stem(btgt) == 'btgt', 'failed to get stem of build target') |
| 167 |
assert(fs.stem(ctgt) == 'ctgt', 'failed to get stem of custom target') |
| 168 |
assert(fs.stem(ctgt[0]) == 'ctgt', 'failed to get stem of custom target index') |
| 169 |
assert(fs.suffix('foo/bar/baz') == '', 'failed to get missing suffix') |
| 170 |
assert(fs.suffix('foo/bar/baz.') == '.', 'failed to get empty suffix') |
| 171 |
assert(fs.suffix('foo/bar/baz.dll') == '.dll', 'failed to get plain suffix') |
| 172 |
assert(fs.suffix('foo/bar/baz.dll.a') == '.a', 'failed to get final suffix') |
| 173 |
assert(fs.suffix(ctgt) == '.txt', 'failed to get suffix of custom target') |
| 174 |
assert(fs.suffix(ctgt[0]) == '.txt', 'failed to get suffix of custom target index') |
| 175 |
|
| 176 |
# relative_to |
| 177 |
if build_machine.system() == 'windows' |
| 178 |
# strings |
| 179 |
assert(fs.relative_to('c:\\prefix\\lib\\foo', 'c:\\prefix') == 'lib\\foo') |
| 180 |
assert(fs.relative_to('c:\\prefix\\lib', 'c:\\prefix\\bin') == '..\\lib') |
| 181 |
assert(fs.relative_to('c:\\proj1\\foo', 'd:\\proj1\\bar') == 'c:\\proj1\\foo') |
| 182 |
assert(fs.relative_to('prefix\\lib\\foo', 'prefix') == 'lib\\foo') |
| 183 |
assert(fs.relative_to('prefix\\lib', 'prefix\\bin') == '..\\lib') |
| 184 |
assert(fs.relative_to('proj1\\foo', 'proj1\\bar') == '..\\foo') |
| 185 |
assert(fs.relative_to('subdir/subdirfile.txt', meson.current_source_dir()) == 'subdir\\subdirfile.txt') |
| 186 |
assert(fs.relative_to(files('meson.build'), files('subdir/meson.build')) == '..\\..\\meson.build') |
| 187 |
assert(fs.relative_to(files('meson.build'), 'subdir/meson.build') == '..\\..\\meson.build') |
| 188 |
else |
| 189 |
# strings |
| 190 |
assert(fs.relative_to('/prefix/lib/foo', '/prefix') == 'lib/foo') |
| 191 |
assert(fs.relative_to('/prefix/lib', '/prefix/bin') == '../lib') |
| 192 |
assert(fs.relative_to('prefix/lib/foo', 'prefix') == 'lib/foo') |
| 193 |
assert(fs.relative_to('prefix/lib', 'prefix/bin') == '../lib') |
| 194 |
assert(fs.relative_to('subdir/subdirfile.txt', meson.current_source_dir()) == 'subdir/subdirfile.txt') |
| 195 |
assert(fs.relative_to(files('meson.build'), files('subdir/meson.build')) == '../../meson.build') |
| 196 |
assert(fs.relative_to(files('meson.build'), 'subdir/meson.build') == '../../meson.build') |
| 197 |
endif |
| 198 |
|
| 199 |
subdir('subdir') |
| 200 |
|
| 201 |
subproject('subbie') |
| 202 |
|
| 203 |
testcase expect_error('File notfound does not exist.') |
| 204 |
fs.read('notfound') |
| 205 |
endtestcase |