2022-11-03 10:24:52 +03:00
|
|
|
import os
|
2022-06-23 03:36:15 +03:00
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_find_abs_path_of_executable() {
|
2022-11-03 10:24:52 +03:00
|
|
|
tfolder := os.join_path(os.vtmp_dir(), 'v', 'tests', 'filepath_test')
|
|
|
|
os.rmdir_all(tfolder) or {}
|
|
|
|
assert !os.is_dir(tfolder)
|
|
|
|
os.mkdir_all(tfolder)!
|
2022-06-23 03:36:15 +03:00
|
|
|
defer {
|
2022-11-03 10:24:52 +03:00
|
|
|
os.rmdir_all(tfolder) or {}
|
2022-06-23 03:36:15 +03:00
|
|
|
}
|
|
|
|
//
|
2022-11-03 10:24:52 +03:00
|
|
|
original_path := os.getenv('PATH')
|
|
|
|
original_wdir := os.getwd()
|
2022-06-23 03:36:15 +03:00
|
|
|
defer {
|
2022-11-03 10:24:52 +03:00
|
|
|
os.chdir(original_wdir) or {}
|
2022-06-23 03:36:15 +03:00
|
|
|
}
|
|
|
|
//
|
2022-11-03 10:24:52 +03:00
|
|
|
new_path := tfolder + os.path_delimiter + original_path
|
|
|
|
os.setenv('PATH', new_path, true)
|
2022-06-23 03:36:15 +03:00
|
|
|
//
|
|
|
|
mut myclang_file := 'myclang'
|
|
|
|
$if windows {
|
|
|
|
myclang_file += '.bat'
|
|
|
|
}
|
|
|
|
//
|
2022-11-03 10:24:52 +03:00
|
|
|
os.chdir(tfolder)!
|
|
|
|
os.write_file(myclang_file, 'echo hello')!
|
|
|
|
os.chmod(myclang_file, 0o0777)!
|
|
|
|
dump(os.real_path(myclang_file))
|
|
|
|
dump(os.is_executable(myclang_file))
|
2022-06-23 03:36:15 +03:00
|
|
|
defer {
|
2022-11-03 10:24:52 +03:00
|
|
|
os.rm(myclang_file) or {}
|
2022-06-23 03:36:15 +03:00
|
|
|
}
|
|
|
|
//
|
2022-11-03 10:24:52 +03:00
|
|
|
fpath := os.find_abs_path_of_executable('myclang') or {
|
2022-06-23 03:36:15 +03:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dump(fpath)
|
|
|
|
//
|
2022-11-03 10:24:52 +03:00
|
|
|
os.setenv('PATH', original_path, true)
|
|
|
|
os.chdir(os.home_dir())! // change to a *completely* different folder, to avoid the original PATH containing `.`
|
|
|
|
if x := os.find_abs_path_of_executable('myclang') {
|
2022-06-23 03:36:15 +03:00
|
|
|
eprintln('> find_abs_path_of_executable should have failed, but instead it found: $x')
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|