1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os,tools: add os.vtmp_dir()

Use it to consistently place all temporary files created by tests in a overridable folder specific to the user, that is easy to cleanup later.

NOTE: os.temp_dir() on macos returns `/tmp`, and using `/tmp/v` is a problem when multiple unix users are trying to access/create/write to it.
This commit is contained in:
Delyan Angelov
2022-11-03 09:24:52 +02:00
parent 509f5c7db3
commit f427a5241a
44 changed files with 114 additions and 105 deletions

View File

@ -597,9 +597,12 @@ pub fn header(msg string) {
flush_stdout()
}
// setup_new_vtmp_folder creates a new nested folder inside VTMP, then resets VTMP to it,
// so that V programs/tests will write their temporary files to new location.
// The new nested folder, and its contents, will get removed after all tests/programs succeed.
pub fn setup_new_vtmp_folder() string {
now := time.sys_mono_now()
new_vtmp_dir := os.join_path(os.temp_dir(), 'v', 'tsession_${sync.thread_id().hex()}_$now')
new_vtmp_dir := os.join_path(os.vtmp_dir(), 'tsession_${sync.thread_id().hex()}_$now')
os.mkdir_all(new_vtmp_dir) or { panic(err) }
os.setenv('VTMP', new_vtmp_dir, true)
return new_vtmp_dir

View File

@ -26,7 +26,7 @@ fn get_vexe_path() string {
}
fn new_tdir() string {
dir := os.join_path(os.temp_dir(), 'v', rand.ulid())
dir := os.join_path(os.vtmp_dir(), 'v', rand.ulid())
os.rmdir_all(dir) or {}
os.mkdir_all(dir) or { panic(err) }
C.atexit(cleanup_tdir)

View File

@ -2,7 +2,7 @@ import os
const vexe = @VEXE
const tfolder = os.join_path(os.temp_dir(), 'v', 'vbump')
const tfolder = os.join_path(os.vtmp_dir(), 'v', 'vbump')
fn testsuite_begin() {
os.mkdir_all(tfolder) or {}

View File

@ -21,7 +21,7 @@ const (
show_progress = os.getenv('GITHUB_JOB') == '' && '-silent' !in os.args
non_option_args = cmdline.only_non_options(os.args[2..])
is_verbose = os.getenv('VERBOSE') != ''
vcheckfolder = os.join_path(os.temp_dir(), 'v', 'vcheck_$os.getuid()')
vcheckfolder = os.join_path(os.vtmp_dir(), 'v', 'vcheck_$os.getuid()')
should_autofix = os.getenv('VAUTOFIX') != ''
vexe = @VEXE
)

View File

@ -1,6 +1,6 @@
import os
const test_path = os.join_path(os.temp_dir(), 'v', 'vcreate_test')
const test_path = os.join_path(os.vtmp_dir(), 'v', 'vcreate_test')
fn init_and_check() ! {
os.execute_or_exit('${os.quoted_path(@VEXE)} init')

View File

@ -33,7 +33,7 @@ mut:
const (
formatted_file_token = '\@\@\@' + 'FORMATTED_FILE: '
vtmp_folder = util.get_vtmp_folder()
vtmp_folder = os.vtmp_dir()
term_colors = term.can_show_color_on_stderr()
)

View File

@ -51,7 +51,7 @@ Examples:
Compare screenshots in `/tmp/src` to existing screenshots in `/tmp/dst`
v gret --compare-only /tmp/src /tmp/dst
'
tmp_dir = os.join_path(os.temp_dir(), 'v', tool_name)
tmp_dir = os.join_path(os.vtmp_dir(), 'v', tool_name)
runtime_os = os.user_os()
v_root = os.real_path(@VMODROOT)
)
@ -271,7 +271,7 @@ fn compare_screenshots(opt Options, output_path string, target_path string) ! {
if idiff_exe == '' {
return error('$tool_name need the `idiff` tool installed. It can be installed on Ubuntu with `sudo apt install openimageio-tools`')
}
diff_file := os.join_path(os.temp_dir(), os.file_name(src).all_before_last('.') +
diff_file := os.join_path(os.vtmp_dir(), os.file_name(src).all_before_last('.') +
'.diff.tif')
flags := app_config.compare.flags.join(' ')
diff_cmd := '${os.quoted_path(idiff_exe)} $flags -abs -od -o ${os.quoted_path(diff_file)} -abs ${os.quoted_path(src)} ${os.quoted_path(target)}'
@ -305,18 +305,18 @@ fn compare_screenshots(opt Options, output_path string, target_path string) ! {
eprintln('$fail_src != $fail_target')
}
first := fails.keys()[0]
fail_copy := os.join_path(os.temp_dir(), 'fail.' + first.all_after_last('.'))
fail_copy := os.join_path(os.vtmp_dir(), 'fail.' + first.all_after_last('.'))
os.cp(first, fail_copy)!
eprintln('First failed file `$first` is copied to `$fail_copy`')
diff_file := os.join_path(os.temp_dir(), os.file_name(first).all_before_last('.') +
diff_file := os.join_path(os.vtmp_dir(), os.file_name(first).all_before_last('.') +
'.diff.tif')
diff_copy := os.join_path(os.temp_dir(), 'diff.tif')
diff_copy := os.join_path(os.vtmp_dir(), 'diff.tif')
if os.is_file(diff_file) {
os.cp(diff_file, diff_copy)!
eprintln('First failed diff file `$diff_file` is copied to `$diff_copy`')
eprintln('Removing alpha channel from $diff_copy ...')
final_fail_result_file := os.join_path(os.temp_dir(), 'diff.png')
final_fail_result_file := os.join_path(os.vtmp_dir(), 'diff.png')
opt.verbose_execute('convert ${os.quoted_path(diff_copy)} -alpha off ${os.quoted_path(final_fail_result_file)}')
eprintln('Final diff file: `$final_fail_result_file`')
}

View File

@ -35,7 +35,7 @@ const (
is_stdin_a_pipe = os.is_atty(0) == 0
vexe = os.getenv('VEXE')
vstartup = os.getenv('VSTARTUP')
repl_folder = os.join_path(os.temp_dir(), 'v', 'repl')
repl_folder = os.join_path(os.vtmp_dir(), 'v', 'repl')
)
enum FnType {

View File

@ -1,6 +1,5 @@
import os
import v.pref
import v.util
$if windows {
$if tinyc {
@ -32,7 +31,7 @@ fn main() {
}
fn cleanup_vtmp_folder() {
os.rmdir_all(util.get_vtmp_folder()) or {}
os.rmdir_all(os.vtmp_dir()) or {}
}
fn setup_symlink_github() {

View File

@ -2,12 +2,10 @@ module main
import os
import v.vcache
import v.util
fn main() {
wipe_path(vcache.new_cache_manager([]).basepath, 'V cache')
wipe_path(util.get_vtmp_folder(), 'V tmp.c')
wipe_path(os.join_path(os.temp_dir(), 'v'), 'V tests')
wipe_path(os.vtmp_dir(), 'V tmp.c and tests folder')
}
fn wipe_path(cpath string, label string) {