From f25be20e21b9b5f67ea8d698d5d25b726f1bf3a4 Mon Sep 17 00:00:00 2001 From: mariobassem Date: Sun, 29 Dec 2024 14:05:09 +0200 Subject: [PATCH] fixes in rclone module --- lib/clients/rclone/config.v | 17 +- lib/clients/rclone/rclone.v | 1 - lib/clients/rclone/rclone_factory_.v | 7 +- lib/clients/rclone/rclone_model.v | 12 +- test_basic.vsh | 279 +++++++++++++-------------- 5 files changed, 151 insertions(+), 165 deletions(-) diff --git a/lib/clients/rclone/config.v b/lib/clients/rclone/config.v index f6800c13..6d8e782c 100644 --- a/lib/clients/rclone/config.v +++ b/lib/clients/rclone/config.v @@ -2,8 +2,9 @@ module rclone import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.core.texttools +import os -const configfile = '${HOME}/.config/rclone/rclone.conf' +const configfile = '${os.home_dir()}/.config/rclone/rclone.conf' // will look for personal configuration file in ~/hero/config . // this file is in heroscript format and will have all required info to configure rclone @@ -17,13 +18,15 @@ const configfile = '${HOME}/.config/rclone/rclone.conf' // url:'' //``` pub fn configure() ! { - actions := playbook.new( - path: configfile - actor_filter: ['config'] - action_filter: [ - 's3server_define', - ] + mut plbook := playbook.new( + path: rclone.configfile + // actor_filter: ['config'] + // action_filter: [ + // 's3server_define', + // ] )! + + actions := plbook.find(filter: 'config.s3server_define')! mut out := '' for action in actions { mut name := action.params.get_default('name', '')! diff --git a/lib/clients/rclone/rclone.v b/lib/clients/rclone/rclone.v index 99caf1b7..ffe44013 100644 --- a/lib/clients/rclone/rclone.v +++ b/lib/clients/rclone/rclone.v @@ -1,7 +1,6 @@ module rclone import os -import freeflowuniverse.herolib.core.texttools // // RCloneClient represents a configured rclone instance // pub struct RCloneClient { diff --git a/lib/clients/rclone/rclone_factory_.v b/lib/clients/rclone/rclone_factory_.v index 7d18df2f..c5ac3a67 100644 --- a/lib/clients/rclone/rclone_factory_.v +++ b/lib/clients/rclone/rclone_factory_.v @@ -2,7 +2,6 @@ module rclone import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook -import freeflowuniverse.herolib.ui.console __global ( rclone_global map[string]&RCloneClient @@ -91,9 +90,9 @@ pub fn play(args_ PlayArgs) ! { if install_actions.len > 0 { for install_action in install_actions { mut p := install_action.params - mycfg := cfg_play(p)! - console.print_debug('install action rclone.configure\n${mycfg}') - set(mycfg)! + cfg_play(p)! + // console.print_debug('install action rclone.configure\n${mycfg}') + // set(mycfg)! } } } diff --git a/lib/clients/rclone/rclone_model.v b/lib/clients/rclone/rclone_model.v index a43ad437..44acf3c5 100644 --- a/lib/clients/rclone/rclone_model.v +++ b/lib/clients/rclone/rclone_model.v @@ -34,7 +34,7 @@ pub fn heroscript_default() !string { pub struct RCloneClient { pub mut: name string = 'default' - type_ string = 's3' // remote type (s3, sftp, etc) + type_ string = 's3' // remote type (s3, sftp, etc) provider string = 'aws' // provider for s3 (aws, minio, etc) access_key string // access key for authentication secret_key string // secret key for authentication @@ -44,13 +44,13 @@ pub mut: fn cfg_play(p paramsparser.Params) ! { mut mycfg := RCloneClient{ - name: p.get_default('name', 'default')! - type_: p.get_default('type', 's3')! - provider: p.get_default('provider', 'aws')! + name: p.get_default('name', 'default')! + type_: p.get_default('type', 's3')! + provider: p.get_default('provider', 'aws')! access_key: p.get('access_key')! secret_key: p.get('secret_key')! - region: p.get_default('region', 'us-east-1')! - endpoint: p.get_default('endpoint', '')! + region: p.get_default('region', 'us-east-1')! + endpoint: p.get_default('endpoint', '')! } set(mycfg)! } diff --git a/test_basic.vsh b/test_basic.vsh index 164c0986..4a157cb9 100755 --- a/test_basic.vsh +++ b/test_basic.vsh @@ -5,146 +5,135 @@ import flag import time import json -const ( - cache_file = '/tmp/herolib_tests.json' - test_expiry_seconds = 3600 // 1 hour -) +const cache_file = '/tmp/herolib_tests.json' +const test_expiry_seconds = 3600 // 1 hour struct TestCache { mut: - tests map[string]i64 // Map of test paths to last successful run timestamp + tests map[string]i64 // Map of test paths to last successful run timestamp } - - // Load the test cache from JSON file fn load_test_cache() TestCache { - if !os.exists(cache_file) { - return TestCache{ - tests: map[string]i64{} - } - } - - content := os.read_file(cache_file) or { - return TestCache{ - tests: map[string]i64{} - } - } - - return json.decode(TestCache, content) or { - return TestCache{ - tests: map[string]i64{} - } - } + if !os.exists(cache_file) { + return TestCache{ + tests: map[string]i64{} + } + } + + content := os.read_file(cache_file) or { return TestCache{ + tests: map[string]i64{} + } } + + return json.decode(TestCache, content) or { return TestCache{ + tests: map[string]i64{} + } } } // Save the test cache to JSON file fn save_test_cache(cache TestCache) { - json_str := json.encode_pretty(cache) - os.write_file(cache_file, json_str) or { - eprintln('Failed to save test cache: ${err}') - } + json_str := json.encode_pretty(cache) + os.write_file(cache_file, json_str) or { eprintln('Failed to save test cache: ${err}') } } // Check if a test needs to be rerun based on timestamp fn should_rerun_test(cache TestCache, test_key string) bool { - last_run := cache.tests[test_key] or { return true } - now := time.now().unix() - return (now - last_run) > test_expiry_seconds + last_run := cache.tests[test_key] or { return true } + now := time.now().unix() + return (now - last_run) > test_expiry_seconds } // Update test timestamp in cache fn update_test_cache(mut cache TestCache, test_key string) { - cache.tests[test_key] = time.now().unix() - save_test_cache(cache) + cache.tests[test_key] = time.now().unix() + save_test_cache(cache) } // Normalize a path for consistent handling fn normalize_path(path string) string { - mut norm_path := os.abs_path(path) - norm_path = norm_path.replace('//', '/') // Remove any double slashes - return norm_path + mut norm_path := os.abs_path(path) + norm_path = norm_path.replace('//', '/') // Remove any double slashes + return norm_path } // Get normalized and relative path fn get_normalized_paths(path string, base_dir_norm string) (string, string) { - // base_dir_norm is already normalized - norm_path := normalize_path(path) - rel_path := norm_path.replace(base_dir_norm + '/', '') - return norm_path, rel_path + // base_dir_norm is already normalized + norm_path := normalize_path(path) + rel_path := norm_path.replace(base_dir_norm + '/', '') + return norm_path, rel_path } // Generate a cache key from a path fn get_cache_key(path string, base_dir string) string { - _, rel_path := get_normalized_paths(path, base_dir) - // Create consistent key format - return rel_path.replace('/', '_').trim('_').to_lower() + _, rel_path := get_normalized_paths(path, base_dir) + // Create consistent key format + return rel_path.replace('/', '_').trim('_').to_lower() } // Check if a file should be ignored or marked as error based on its path -fn process_test_file(path string, base_dir string, test_files_ignore []string, test_files_error []string, mut cache TestCache, mut tests_in_error []string)! { - // Get normalized paths - norm_path, rel_path := get_normalized_paths(path, base_dir) - - mut should_ignore := false - mut is_error := false +fn process_test_file(path string, base_dir string, test_files_ignore []string, test_files_error []string, mut cache TestCache, mut tests_in_error []string) ! { + // Get normalized paths + norm_path, rel_path := get_normalized_paths(path, base_dir) - if ! path.to_lower().contains("_test.v"){ - return - } - - // Check if any ignore pattern matches the path - for pattern in test_files_ignore { - if pattern.trim_space() != '' && rel_path.contains(pattern) { - should_ignore = true - break - } - } - - // Check if any error pattern matches the path - for pattern in test_files_error { - if pattern.trim_space() != '' && rel_path.contains(pattern) { - is_error = true - break - } - } - - if !should_ignore && !is_error { - dotest(norm_path, base_dir, mut cache)! - } else { - println('Ignoring test: ${rel_path}') - if !should_ignore { - tests_in_error << rel_path - } - } + mut should_ignore := false + mut is_error := false + + if !path.to_lower().contains('_test.v') { + return + } + + // Check if any ignore pattern matches the path + for pattern in test_files_ignore { + if pattern.trim_space() != '' && rel_path.contains(pattern) { + should_ignore = true + break + } + } + + // Check if any error pattern matches the path + for pattern in test_files_error { + if pattern.trim_space() != '' && rel_path.contains(pattern) { + is_error = true + break + } + } + + if !should_ignore && !is_error { + dotest(norm_path, base_dir, mut cache)! + } else { + println('Ignoring test: ${rel_path}') + if !should_ignore { + tests_in_error << rel_path + } + } } -fn dotest(path string, base_dir string, mut cache TestCache)! { - norm_path, _ := get_normalized_paths(path, base_dir) - test_key := get_cache_key(norm_path, base_dir) - - // Check if test result is cached and still valid - if !should_rerun_test(cache, test_key) { - println('Test cached (passed): ${path}') - return - } +fn dotest(path string, base_dir string, mut cache TestCache) ! { + norm_path, _ := get_normalized_paths(path, base_dir) + test_key := get_cache_key(norm_path, base_dir) - cmd := 'v -stats -enable-globals -n -w -gc none -no-retry-compilation -cc tcc test ${norm_path}' - println(cmd) - result := os.execute(cmd) - eprintln(result) - if result.exit_code != 0 { - eprintln('Test failed: ${path}') - eprintln(result.output) - exit(1) - } - - // Update cache with successful test run - update_test_cache(mut cache, test_key) - println('Test passed: ${path}') + // Check if test result is cached and still valid + if !should_rerun_test(cache, test_key) { + println('Test cached (passed): ${path}') + return + } + + cmd := 'v -stats -enable-globals -n -w -gc none -no-retry-compilation -cc tcc test ${norm_path}' + println(cmd) + result := os.execute(cmd) + eprintln(result) + if result.exit_code != 0 { + eprintln('Test failed: ${path}') + eprintln(result.output) + exit(1) + } + + // Update cache with successful test run + update_test_cache(mut cache, test_key) + println('Test passed: ${path}') } - ///////////////////////// ///////////////////////// @@ -152,31 +141,27 @@ fn dotest(path string, base_dir string, mut cache TestCache)! { mut fp := flag.new_flag_parser(os.args) fp.application('test_basic') fp.description('Run tests for herolib') -remove_cache := fp.bool('r', `r`, false, 'Remove cache file before running tests', flag.FlagConfig{}) +remove_cache := fp.bool('r', `r`, false, 'Remove cache file before running tests') fp.finalize() or { - eprintln(err) - exit(1) + eprintln(err) + exit(1) } - // Remove cache file if -r flag is set if remove_cache && os.exists(cache_file) { - os.rm(cache_file) or { - eprintln('Failed to remove cache file: ${err}') - exit(1) - } - println('Removed cache file: ${cache_file}') + os.rm(cache_file) or { + eprintln('Failed to remove cache file: ${err}') + exit(1) + } + println('Removed cache file: ${cache_file}') } - abs_dir_of_script := dir(@FILE) norm_dir_of_script := normalize_path(abs_dir_of_script) os.chdir(abs_dir_of_script) or { panic(err) } - - // can use // inside this list as well to ignore temporary certain dirs, useful for testing -tests := " +tests := ' lib/data lib/osal lib/lang @@ -185,17 +170,17 @@ lib/clients // lib/crypt lib/core lib/develop -" +' -//the following tests have no prio and can be ignored -tests_ignore := " +// the following tests have no prio and can be ignored +tests_ignore := ' notifier_test.v clients/meilisearch clients/zdb systemd_process_test.v -" +' -tests_error := " +tests_error := ' net_test.v osal/package_test.v rpc_test.v @@ -229,8 +214,7 @@ encoderhero/encoder_test.v encoderhero/decoder_test.v code/codeparser gittools_test.v -" - +' // Split tests into array and remove empty lines test_files := tests.split('\n').filter(it.trim_space() != '') @@ -239,42 +223,43 @@ test_files_error := tests_error.split('\n').filter(it.trim_space() != '') mut tests_in_error := []string{} - // Load test cache mut cache := load_test_cache() println('Test cache loaded from ${cache_file}') // Run each test with proper v command flags for test in test_files { - if test.trim_space() == '' || test.trim_space().starts_with("//") || test.trim_space().starts_with("#") { - continue - } - - full_path := os.join_path(abs_dir_of_script, test) - - if !os.exists(full_path) { - eprintln('Path does not exist: ${full_path}') - exit(1) - } - - if os.is_dir(full_path) { - // If directory, run tests for each .v file in it recursively - files := os.walk_ext(full_path, '.v') - for file in files { - process_test_file(file, norm_dir_of_script, test_files_ignore, test_files_error, mut cache, mut tests_in_error)! - - } - } else if os.is_file(full_path) { - process_test_file(full_path, norm_dir_of_script, test_files_ignore, test_files_error, mut cache, mut tests_in_error)! - } + if test.trim_space() == '' || test.trim_space().starts_with('//') + || test.trim_space().starts_with('#') { + continue + } + + full_path := os.join_path(abs_dir_of_script, test) + + if !os.exists(full_path) { + eprintln('Path does not exist: ${full_path}') + exit(1) + } + + if os.is_dir(full_path) { + // If directory, run tests for each .v file in it recursively + files := os.walk_ext(full_path, '.v') + for file in files { + process_test_file(file, norm_dir_of_script, test_files_ignore, test_files_error, mut + cache, mut tests_in_error)! + } + } else if os.is_file(full_path) { + process_test_file(full_path, norm_dir_of_script, test_files_ignore, test_files_error, mut + cache, mut tests_in_error)! + } } println('All (non skipped) tests ok') if tests_in_error.len > 0 { - println('\n\033[31mTests that need to be fixed (not executed):') - for test in tests_in_error { - println(' ${test}') - } - println('\033[0m') + println('\n\033[31mTests that need to be fixed (not executed):') + for test in tests_in_error { + println(' ${test}') + } + println('\033[0m') }