os: make chdir() return error

pull/11328/head
Alexander Medvednikov 2021-08-28 09:35:39 +03:00
parent e85311c2ba
commit 858ba25d55
12 changed files with 25 additions and 26 deletions

View File

@ -744,11 +744,10 @@ pub fn is_link(path string) bool {
} }
// chdir changes the current working directory to the new directory in `path`. // chdir changes the current working directory to the new directory in `path`.
pub fn chdir(path string) { pub fn chdir(path string) ? {
$if windows { ret := $if windows { C._wchdir(path.to_wide()) } $else { C.chdir(&char(path.str)) }
C._wchdir(path.to_wide()) if ret == -1 {
} $else { return error_with_code(posix_get_error_msg(C.errno), C.errno)
_ = C.chdir(&char(path.str))
} }
} }

View File

@ -17,7 +17,7 @@ fn testsuite_begin() {
os.rmdir_all(tfolder) or {} os.rmdir_all(tfolder) or {}
assert !os.is_dir(tfolder) assert !os.is_dir(tfolder)
os.mkdir_all(tfolder) or { panic(err) } os.mkdir_all(tfolder) or { panic(err) }
os.chdir(tfolder) os.chdir(tfolder) or {}
assert os.is_dir(tfolder) assert os.is_dir(tfolder)
// println('args_at_start: $args_at_start') // println('args_at_start: $args_at_start')
assert args_at_start.len > 0 assert args_at_start.len > 0
@ -25,7 +25,7 @@ fn testsuite_begin() {
} }
fn testsuite_end() { fn testsuite_end() {
os.chdir(os.wd_at_startup) os.chdir(os.wd_at_startup) or {}
os.rmdir_all(tfolder) or {} os.rmdir_all(tfolder) or {}
assert !os.is_dir(tfolder) assert !os.is_dir(tfolder)
// eprintln('testsuite_end , tfolder = $tfolder removed.') // eprintln('testsuite_end , tfolder = $tfolder removed.')
@ -326,9 +326,9 @@ fn test_realpath_removes_dots() {
fn test_realpath_absolutizes_existing_relative_paths() { fn test_realpath_absolutizes_existing_relative_paths() {
old_wd := os.getwd() old_wd := os.getwd()
defer { defer {
os.chdir(old_wd) os.chdir(old_wd) or { panic(err) }
} }
os.chdir(@VEXEROOT) os.chdir(@VEXEROOT) or { panic(err) }
examples_folder := os.join_path('vlib', 'v', '..', '..', 'cmd', '.', '..', 'examples') examples_folder := os.join_path('vlib', 'v', '..', '..', 'cmd', '.', '..', 'examples')
real_path_of_examples_folder := os.real_path(examples_folder) real_path_of_examples_folder := os.real_path(examples_folder)
assert os.is_abs_path(real_path_of_examples_folder) assert os.is_abs_path(real_path_of_examples_folder)

View File

@ -11,7 +11,7 @@ const (
) )
fn test_szip_create_temp_files() ? { fn test_szip_create_temp_files() ? {
os.chdir(os.temp_dir()) os.chdir(os.temp_dir()) or {}
os.rmdir_all(test_path) or {} os.rmdir_all(test_path) or {}
os.mkdir(test_path) ? os.mkdir(test_path) ?
os.write_file(fpath1, 'file one') ? os.write_file(fpath1, 'file one') ?
@ -45,7 +45,7 @@ fn test_reading_zipping_files() ? {
file_name_list << 'file_${i:02}.txt' file_name_list << 'file_${i:02}.txt'
} }
os.chdir(os.temp_dir()) os.chdir(os.temp_dir()) or {}
os.rmdir_all(test_path) or {} os.rmdir_all(test_path) or {}
os.mkdir(test_path) ? os.mkdir(test_path) ?
for c, f_name in file_name_list { for c, f_name in file_name_list {

View File

@ -132,7 +132,7 @@ fn (mut v Builder) rebuild_cached_module(vexe string, imp_path string) string {
// do run `v build-module x` always in main vfolder; x can be a relative path // do run `v build-module x` always in main vfolder; x can be a relative path
pwd := os.getwd() pwd := os.getwd()
vroot := os.dir(vexe) vroot := os.dir(vexe)
os.chdir(vroot) os.chdir(vroot) or {}
boptions := v.pref.build_options.join(' ') boptions := v.pref.build_options.join(' ')
rebuild_cmd := '$vexe $boptions build-module $imp_path' rebuild_cmd := '$vexe $boptions build-module $imp_path'
vcache.dlog('| Builder.' + @FN, 'vexe: $vexe | imp_path: $imp_path | rebuild_cmd: $rebuild_cmd') vcache.dlog('| Builder.' + @FN, 'vexe: $vexe | imp_path: $imp_path | rebuild_cmd: $rebuild_cmd')
@ -140,7 +140,7 @@ fn (mut v Builder) rebuild_cached_module(vexe string, imp_path string) string {
rebuilded_o := v.pref.cache_manager.exists('.o', imp_path) or { rebuilded_o := v.pref.cache_manager.exists('.o', imp_path) or {
panic('could not rebuild cache module for $imp_path, error: $err.msg') panic('could not rebuild cache module for $imp_path, error: $err.msg')
} }
os.chdir(pwd) os.chdir(pwd) or {}
return rebuilded_o return rebuilded_o
} }
return res return res
@ -638,7 +638,7 @@ fn (mut v Builder) cc() {
} }
} }
// //
os.chdir(vdir) os.chdir(vdir) or {}
tried_compilation_commands << cmd tried_compilation_commands << cmd
v.show_cc(cmd, response_file, response_file_content) v.show_cc(cmd, response_file, response_file_content)
// Run // Run
@ -649,7 +649,7 @@ fn (mut v Builder) cc() {
if v.pref.show_c_output { if v.pref.show_c_output {
v.show_c_compiler_output(res) v.show_c_compiler_output(res)
} }
os.chdir(original_pwd) os.chdir(original_pwd) or {}
vcache.dlog('| Builder.' + @FN, '> v.pref.use_cache: $v.pref.use_cache | v.pref.retry_compilation: $v.pref.retry_compilation') vcache.dlog('| Builder.' + @FN, '> v.pref.use_cache: $v.pref.use_cache | v.pref.retry_compilation: $v.pref.retry_compilation')
vcache.dlog('| Builder.' + @FN, '> cmd res.exit_code: $res.exit_code | cmd: $cmd') vcache.dlog('| Builder.' + @FN, '> cmd res.exit_code: $res.exit_code | cmd: $cmd')
vcache.dlog('| Builder.' + @FN, '> response_file_content:\n$response_file_content') vcache.dlog('| Builder.' + @FN, '> response_file_content:\n$response_file_content')
@ -957,7 +957,7 @@ fn (mut v Builder) build_thirdparty_obj_file(path string, moduleflags []cflag.CF
// //
// prepare for tcc, it needs relative paths to thirdparty/tcc to work: // prepare for tcc, it needs relative paths to thirdparty/tcc to work:
current_folder := os.getwd() current_folder := os.getwd()
os.chdir(os.dir(pref.vexe_path())) os.chdir(os.dir(pref.vexe_path())) or {}
// //
mut all_options := []string{} mut all_options := []string{}
all_options << v.pref.third_party_option all_options << v.pref.third_party_option
@ -970,7 +970,7 @@ fn (mut v Builder) build_thirdparty_obj_file(path string, moduleflags []cflag.CF
println('>>> build_thirdparty_obj_files cmd: $cmd') println('>>> build_thirdparty_obj_files cmd: $cmd')
} }
res := os.execute(cmd) res := os.execute(cmd)
os.chdir(current_folder) os.chdir(current_folder) or {}
if res.exit_code != 0 { if res.exit_code != 0 {
eprintln('failed thirdparty object build cmd:\n$cmd') eprintln('failed thirdparty object build cmd:\n$cmd')
verror(res.output) verror(res.output)

View File

@ -14,7 +14,7 @@ const diff_cmd = diff.find_working_diff_command() or { '' }
fn test_out_files() ? { fn test_out_files() ? {
println(term.colorize(term.green, '> testing whether .out files match:')) println(term.colorize(term.green, '> testing whether .out files match:'))
os.chdir(vroot) os.chdir(vroot) or {}
output_path := os.join_path(os.temp_dir(), 'coutput', 'out') output_path := os.join_path(os.temp_dir(), 'coutput', 'out')
os.mkdir_all(output_path) ? os.mkdir_all(output_path) ?
defer { defer {
@ -82,7 +82,7 @@ fn test_out_files() ? {
fn test_c_must_have_files() ? { fn test_c_must_have_files() ? {
println(term.colorize(term.green, '> testing whether `.c.must_have` files match:')) println(term.colorize(term.green, '> testing whether `.c.must_have` files match:'))
os.chdir(vroot) os.chdir(vroot) or {}
output_path := os.join_path(os.temp_dir(), 'coutput', 'c_must_have') output_path := os.join_path(os.temp_dir(), 'coutput', 'c_must_have')
os.mkdir_all(output_path) ? os.mkdir_all(output_path) ?
defer { defer {

View File

@ -4,7 +4,7 @@ import v.pref
import v.ast import v.ast
fn test_macho() { fn test_macho() {
os.chdir(os.temp_dir()) os.chdir(os.temp_dir()) or {}
mut g := native.Gen{ mut g := native.Gen{
pref: &pref.Preferences{} pref: &pref.Preferences{}
out_name: 'test.bin' out_name: 'test.bin'

View File

@ -13,7 +13,7 @@ fn test_all() {
mut total_errors := 0 mut total_errors := 0
vexe := os.getenv('VEXE') vexe := os.getenv('VEXE')
vroot := os.dir(vexe) vroot := os.dir(vexe)
os.chdir(vroot) os.chdir(vroot) or {}
diff_cmd := diff.find_working_diff_command() or { '' } diff_cmd := diff.find_working_diff_command() or { '' }
dir := 'vlib/v/tests/inout' dir := 'vlib/v/tests/inout'
files := os.ls(dir) or { panic(err) } files := os.ls(dir) or { panic(err) }

View File

@ -10,7 +10,7 @@ fn test_vexe_exists() {
} }
fn test_v_profile_works() { fn test_v_profile_works() {
os.chdir(vroot) os.chdir(vroot) or {}
program_source := os.join_path(vroot, 'vlib/v/tests/profile/profile_test_1.v') program_source := os.join_path(vroot, 'vlib/v/tests/profile/profile_test_1.v')
res := os.execute('"$vexe" -profile - run $program_source') res := os.execute('"$vexe" -profile - run $program_source')
// eprintln('res: $res') // eprintln('res: $res')

View File

@ -117,7 +117,7 @@ pub fn new_options() RunnerOptions {
if os.args.len > 1 { if os.args.len > 1 {
files = os.args[1..] files = os.args[1..]
} else { } else {
os.chdir(os.dir(vexec)) os.chdir(os.dir(vexec)) or {}
wd = os.getwd() wd = os.getwd()
files = os.walk_ext('.', '.repl') files = os.walk_ext('.', '.repl')
} }

View File

@ -2,7 +2,7 @@ import v.vmod
import os import os
fn test_from_file() { fn test_from_file() {
os.chdir(os.dir(os.getenv('VEXE'))) os.chdir(os.dir(os.getenv('VEXE'))) or {}
data := vmod.from_file('./v.mod') or { panic(err) } data := vmod.from_file('./v.mod') or { panic(err) }
assert data.name == 'V' assert data.name == 'V'
assert data.description == 'The V programming language.' assert data.description == 'The V programming language.'

View File

@ -95,7 +95,7 @@ fn test_readme_exists_and_is_readable() {
} }
fn testsuite_end() { fn testsuite_end() {
os.chdir(os.wd_at_startup) os.chdir(os.wd_at_startup) or {}
os.rmdir_all(vcache_folder) or {} os.rmdir_all(vcache_folder) or {}
assert !os.is_dir(vcache_folder) assert !os.is_dir(vcache_folder)
} }

View File

@ -18,7 +18,7 @@ const (
// setup of vweb webserver // setup of vweb webserver
fn testsuite_begin() { fn testsuite_begin() {
os.chdir(vroot) os.chdir(vroot) or {}
if os.exists(serverexe) { if os.exists(serverexe) {
os.rm(serverexe) or {} os.rm(serverexe) or {}
} }