Debian Codegraph @

vendored-meson/meson/test cases/common/220 fs module/meson.build

raw
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 assert(fs.is_dir('~'), 'home directory not detected')
35 assert(not fs.is_file('~'), 'home directory detected as file')
36
37 # -- expanduser
38 assert(fs.expanduser('~') != '~','expanduser failed')
39 assert(fs.expanduser('~/foo').endswith('foo'), 'expanduser with tail failed')
40
41 # -- as_posix
42 assert(fs.as_posix('/') == '/', 'as_posix idempotent')
43 assert(fs.as_posix('\\') == '/', 'as_posix simple')
44 # Python 3.12 changed how these paths are handled, so deal with both.
45 drivepath = fs.as_posix('\\\\')
46 assert(drivepath == '/' or drivepath == '//', 'as_posix simple')
47 assert(fs.as_posix('foo\\bar/baz') == 'foo/bar/baz', 'as_posix mixed slash')
48
49 # -- is_absolute
50 winabs = 'q:/foo'
51 unixabs = '/foo'
52 if is_windows
53 assert(fs.is_absolute(winabs), 'is_absolute windows not detected')
54 assert(not fs.is_absolute(unixabs), 'is_absolute unix false positive')
55 assert(fs.is_absolute('//foo'), 'is_absolute failed on incomplete UNC path')
56 else
57 assert(fs.is_absolute(unixabs), 'is_absolute unix not detected')
58 assert(not fs.is_absolute(winabs), 'is_absolute windows false positive')
59 endif
60
61 # -- replace_suffix
62
63 original = 'foo'
64 assert(fs.replace_suffix(original, '') == original, 'replace_suffix idempotent')
65 assert(fs.replace_suffix(f[0], '') == 'meson', 'replace_suffix trim')
66
67 original = 'foo.txt'
68 new = fs.replace_suffix(original, '.ini')
69 assert(new == 'foo.ini', 'replace_suffix failed')
70
71 new = fs.replace_suffix(f[0], '.ini')
72 assert(new == 'meson.ini', 'replace_suffix failed')
73
74 original = 'foo'
75 new = fs.replace_suffix(original, '.ini')
76 assert(new == 'foo.ini', 'replace_suffix did not add suffix to suffixless file')
77
78 original = 'foo.dll.a'
79 new = fs.replace_suffix(original, '.so')
80 assert(new == 'foo.dll.so', 'replace_suffix did not only modify last suffix')
81
82 original = 'foo.dll'
83 new = fs.replace_suffix(original, '')
84 assert(new == 'foo', 'replace_suffix did not only delete last suffix')
85
86 # `/` on windows is interpreted like `.drive` which in general may not be `c:/`
87 # the files need not exist for fs.replace_suffix()
88 original = is_windows ? 'j:\\foo\\bar.txt' : '/foo/bar.txt'
89 new_check = is_windows ? 'j:\\foo\\bar.ini' : '/foo/bar.ini'
90
91 new = fs.replace_suffix(original, '.ini')
92 assert(new == new_check, 'absolute path replace_suffix failed')
93
94 new = fs.replace_suffix(btgt, '.ini')
95 assert(new == 'btgt.ini', 'replace_suffix failed for build target')
96 new = fs.replace_suffix(ctgt, '.ini')
97 assert(new == 'ctgt.ini', 'replace_suffix failed for custom target')
98 new = fs.replace_suffix(ctgt[0], '.ini')
99 assert(new == 'ctgt.ini', 'replace_suffix failed for custom target index')
100
101 # -- hash
102
103 md5 = fs.hash('subdir/subdirfile.txt', 'md5')
104 sha256 = fs.hash('subdir/subdirfile.txt', 'sha256')
105 assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match')
106 assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match')
107
108 f = files('subdir/subdirfile.txt')
109 md5 = fs.hash(f[0], 'md5')
110 assert(md5 == 'd0795db41614d25affdd548314b30b3b', 'md5sum did not match')
111 sha256 = fs.hash(f[0], 'sha256')
112 assert(sha256 == 'be2170b0dae535b73f6775694fffa3fd726a43b5fabea11b7342f0605917a42a', 'sha256sum did not match')
113
114 # -- size
115
116 size = fs.size('subdir/subdirfile.txt')
117 assert(size == 19, 'file size not found correctly')
118
119 size = fs.size(f[0])
120 assert(size == 19, 'file size not found correctly')
121
122 # -- are filenames referring to the same file?
123 f1 = 'meson.build'
124 f2 = 'subdir/../meson.build'
125 assert(fs.is_samepath(f1, f2), 'is_samepath not detecting same files')
126 assert(fs.is_samepath(meson.source_root(), 'subdir/..'), 'is_samepath not detecting same directory')
127 assert(fs.is_samepath(meson.project_source_root(), 'subdir/..'), 'is_samepath not detecting same directory')
128 assert(fs.is_samepath(meson.project_build_root(), meson.current_build_dir() / 'subdir/..'), 'is_samepath not detecting same directory')
129 assert(fs.is_samepath(meson.global_source_root(), meson.current_source_dir()), 'is_samepath not detecting same directory')
130 assert(fs.is_samepath(meson.global_build_root(), meson.current_build_dir()), 'is_samepath not detecting same directory')
131 assert(not fs.is_samepath(f1, 'subdir/subdirfile.txt'), 'is_samepath known bad comparison')
132 assert(not fs.is_samepath('not-a-path', f2), 'is_samepath should not error if path(s) do not exist')
133
134 f = files('meson.build', 'subdir/../meson.build')
135 assert(fs.is_samepath(f[0], f[1]), 'is_samepath not detecting same files')
136
137 if not is_windows and build_machine.system() != 'cygwin'
138 assert(fs.is_samepath(symlink, 'meson.build'), 'symlink is_samepath fail')
139 endif
140
141 # parts of path
142 assert(fs.parent('foo/bar') == 'foo', 'failed to get dirname')
143 assert(fs.parent(f[1]) == 'subdir/..', 'failed to get dirname for file')
144 assert(fs.parent(btgt) == '.', 'failed to get dirname for build target')
145 assert(fs.parent(ctgt) == '.', 'failed to get dirname for custom target')
146 assert(fs.parent(ctgt[0]) == '.', 'failed to get dirname for custom target index')
147
148 assert(fs.name('foo/bar') == 'bar', 'failed to get basename')
149 assert(fs.name(f[1]) == 'meson.build', 'failed to get basename')
150 assert(fs.name('foo/bar/baz.dll.a') == 'baz.dll.a', 'failed to get basename with compound suffix')
151 if host_machine.system() in ['cygwin', 'windows']
152 assert(fs.name(btgt) == 'btgt.exe', 'failed to get basename of build target')
153 assert(fs.suffix(btgt) == '.exe', 'failed to get build target suffix')
154 else
155 assert(fs.name(btgt) == 'btgt', 'failed to get basename of build target')
156 assert(fs.suffix(btgt) == '', 'failed to get build target suffix')
157 endif
158 assert(fs.name(ctgt) == 'ctgt.txt', 'failed to get basename of custom target')
159 assert(fs.name(ctgt[0]) == 'ctgt.txt', 'failed to get basename of custom target index')
160 assert(fs.stem('foo/bar/baz.dll') == 'baz', 'failed to get stem with suffix')
161 assert(fs.stem('foo/bar/baz.dll.a') == 'baz.dll', 'failed to get stem with compound suffix')
162 assert(fs.stem(btgt) == 'btgt', 'failed to get stem of build target')
163 assert(fs.stem(ctgt) == 'ctgt', 'failed to get stem of custom target')
164 assert(fs.stem(ctgt[0]) == 'ctgt', 'failed to get stem of custom target index')
165 assert(fs.suffix('foo/bar/baz') == '', 'failed to get missing suffix')
166 assert(fs.suffix('foo/bar/baz.') == '.', 'failed to get empty suffix')
167 assert(fs.suffix('foo/bar/baz.dll') == '.dll', 'failed to get plain suffix')
168 assert(fs.suffix('foo/bar/baz.dll.a') == '.a', 'failed to get final suffix')
169 assert(fs.suffix(ctgt) == '.txt', 'failed to get suffix of custom target')
170 assert(fs.suffix(ctgt[0]) == '.txt', 'failed to get suffix of custom target index')
171
172 # relative_to
173 if build_machine.system() == 'windows'
174 # strings
175 assert(fs.relative_to('c:\\prefix\\lib\\foo', 'c:\\prefix') == 'lib\\foo')
176 assert(fs.relative_to('c:\\prefix\\lib', 'c:\\prefix\\bin') == '..\\lib')
177 assert(fs.relative_to('c:\\proj1\\foo', 'd:\\proj1\\bar') == 'c:\\proj1\\foo')
178 assert(fs.relative_to('prefix\\lib\\foo', 'prefix') == 'lib\\foo')
179 assert(fs.relative_to('prefix\\lib', 'prefix\\bin') == '..\\lib')
180 assert(fs.relative_to('proj1\\foo', 'proj1\\bar') == '..\\foo')
181 assert(fs.relative_to('subdir/subdirfile.txt', meson.current_source_dir()) == 'subdir\\subdirfile.txt')
182 assert(fs.relative_to(files('meson.build'), files('subdir/meson.build')) == '..\\..\\meson.build')
183 assert(fs.relative_to(files('meson.build'), 'subdir/meson.build') == '..\\..\\meson.build')
184 else
185 # strings
186 assert(fs.relative_to('/prefix/lib/foo', '/prefix') == 'lib/foo')
187 assert(fs.relative_to('/prefix/lib', '/prefix/bin') == '../lib')
188 assert(fs.relative_to('prefix/lib/foo', 'prefix') == 'lib/foo')
189 assert(fs.relative_to('prefix/lib', 'prefix/bin') == '../lib')
190 assert(fs.relative_to('subdir/subdirfile.txt', meson.current_source_dir()) == 'subdir/subdirfile.txt')
191 assert(fs.relative_to(files('meson.build'), files('subdir/meson.build')) == '../../meson.build')
192 assert(fs.relative_to(files('meson.build'), 'subdir/meson.build') == '../../meson.build')
193 endif
194
195 subdir('subdir')
196
197 subproject('subbie')
198
199 testcase expect_error('File notfound does not exist.')
200 fs.read('notfound')
201 endtestcase