From 56f2622338c1b0b8aeca36d98edb435057a400c0 Mon Sep 17 00:00:00 2001 From: despiegk Date: Wed, 25 Dec 2024 10:01:06 +0100 Subject: [PATCH] the base --- README.md | 4 ++ test_basic.vsh | 156 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100755 test_basic.vsh diff --git a/README.md b/README.md index 3fa08ffe..dc61bce2 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,10 @@ Examples: vtest ~/code/github/freeflowuniverse/herolib/lib/osal/package_test.v #for a full dir vtest ~/code/github/freeflowuniverse/herolib/lib/osal + +#to do al basic tests +~/code/github/freeflowuniverse/herolib/test_basic.vsh + ``` vtest is an alias to test functionality diff --git a/test_basic.vsh b/test_basic.vsh new file mode 100755 index 00000000..26d99b0a --- /dev/null +++ b/test_basic.vsh @@ -0,0 +1,156 @@ +#!/usr/bin/env -S v -n -w -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals run + +import os +import flag +import net +import time + + +// Check if Redis is available +fn check_redis() bool { + mut redis_available := false + mut sock := net.dial_tcp('127.0.0.1:6379') or { return false } + sock.close() or {} + return true +} + +// Set Redis key with expiration +fn redis_set(key string) ! { + mut sock := net.dial_tcp('127.0.0.1:6379')! + defer { sock.close() or {} } + + // SET key value EX seconds + cmd := 'SET vtests.${key} 1 EX 600\r\n' // 600 seconds = 10 minutes + sock.write_string(cmd)! +} + +// Check if key exists in Redis +fn redis_exists(key string) bool { + mut sock := net.dial_tcp('127.0.0.1:6379') or { return false } + defer { sock.close() or {} } + + // EXISTS key + cmd := 'EXISTS vtests.${key}\r\n' + sock.write_string(cmd) or { return false } + + response := sock.read_line() + return response.trim_space() == ':1' +} + +// Delete Redis key +fn redis_del(key string) ! { + mut sock := net.dial_tcp('127.0.0.1:6379')! + defer { sock.close() or {} } + + // DEL key + cmd := 'DEL vtests.${key}\r\n' + sock.write_string(cmd)! +} + +fn dotest(path string, use_redis bool)! { + if use_redis { + // Use absolute path as Redis key + abs_path := os.abs_path(path) + redis_key := abs_path.replace('/', '_') + + // Check if test result is cached + if redis_exists(redis_key) { + println('Test cached (passed): ${path}') + return + } + } + + cmd := 'vtest ${path}' + println(cmd) + result := os.execute(cmd) + + if result.exit_code != 0 { + eprintln('Test failed: ${path}') + eprintln(result.output) + exit(1) + } + + if use_redis { + // Cache successful test result + abs_path := os.abs_path(path) + redis_key := abs_path.replace('/', '_') + redis_set(redis_key) or { + eprintln('Failed to cache test result: ${err}') + } + } + + println('Test passed: ${path}') +} + + +///////////////////////// +///////////////////////// + + +abs_dir_of_script := dir(@FILE) +os.chdir(abs_dir_of_script) or { panic(err) } + +tests := " +lib/osal +" + +tests_ignore := " +net_test.v +systemd_process_test.v +rpc_test.v +screen_test.v +tmux_session_test.v +tmux_window_test.v +tmux_test.v +startupmanager_test.v +" + +// Split tests into array and remove empty lines +test_files := tests.split('\n').filter(it.trim_space() != '') +test_files_ignore := tests_ignore.split('\n').filter(it.trim_space() != '') + + +// Check if Redis is available +redis_available := check_redis() +if redis_available { + println('Redis cache enabled') +} else { + println('Redis not available, running without cache') +} + +// Run each test with proper v command flags +for test in test_files { + if test.trim_space() == '' { + 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 + files := os.walk_ext(full_path, '.v') + for file in files { + base_file := os.base(file) + if base_file in test_files_ignore { + println('Ignoring test: ${file}') + continue + } + dotest(file, redis_available)! + } + } else if os.is_file(full_path) { + // If single file, run test if not in ignore list + base_file := os.base(full_path) + if base_file !in test_files_ignore { + dotest(full_path, redis_available)! + } else { + println('Ignoring test: ${full_path}') + } + } +} + +println('All tests ok')