v / vlib / os
Raw file | 47 loc (46 sloc) | 1.14 KB | Latest commit hash 017ace6ea
1import os
2
3fn test_find_abs_path_of_executable() {
4 tfolder := os.join_path(os.vtmp_dir(), 'v', 'tests', 'filepath_test')
5 os.rmdir_all(tfolder) or {}
6 assert !os.is_dir(tfolder)
7 os.mkdir_all(tfolder)!
8 defer {
9 os.rmdir_all(tfolder) or {}
10 }
11 //
12 original_path := os.getenv('PATH')
13 original_wdir := os.getwd()
14 defer {
15 os.chdir(original_wdir) or {}
16 }
17 //
18 new_path := tfolder + os.path_delimiter + original_path
19 os.setenv('PATH', new_path, true)
20 //
21 mut myclang_file := 'myclang'
22 $if windows {
23 myclang_file += '.bat'
24 }
25 //
26 os.chdir(tfolder)!
27 os.write_file(myclang_file, 'echo hello')!
28 os.chmod(myclang_file, 0o0777)!
29 dump(os.real_path(myclang_file))
30 dump(os.is_executable(myclang_file))
31 defer {
32 os.rm(myclang_file) or {}
33 }
34 //
35 fpath := os.find_abs_path_of_executable('myclang') or {
36 assert false
37 return
38 }
39 dump(fpath)
40 //
41 os.setenv('PATH', original_path, true)
42 os.chdir(os.home_dir())! // change to a *completely* different folder, to avoid the original PATH containing `.`
43 if x := os.find_abs_path_of_executable('myclang') {
44 eprintln('> find_abs_path_of_executable should have failed, but instead it found: ${x}')
45 assert false
46 }
47}