test: improve test suite
- Remove unnecessary debug print statements. - Remove redundant initialization calls. - Improve test cache handling and error reporting. - Refactor code for better readability and maintainability. - Update test suite to include additional tests. - Improve code formatting and style consistency.
This commit is contained in:
@@ -62,7 +62,6 @@ pub fn get(args_ GitStructureArgGet) !&GitStructure {
|
||||
cachereset()!
|
||||
}
|
||||
rediskey_ := rediskey(args.coderoot)
|
||||
// println(rediskey_)
|
||||
|
||||
// Return existing instance if already created.
|
||||
if rediskey_ in gsinstances {
|
||||
@@ -96,8 +95,6 @@ pub fn get(args_ GitStructureArgGet) !&GitStructure {
|
||||
|
||||
if args.reload {
|
||||
gs.load()!
|
||||
} else {
|
||||
gs.init()!
|
||||
}
|
||||
|
||||
gsinstances[rediskey_] = &gs
|
||||
|
||||
@@ -42,9 +42,9 @@ pub mut:
|
||||
// - args (StatusUpdateArgs): Arguments controlling the reload behavior.
|
||||
pub fn (mut gitstructure GitStructure) load(args StatusUpdateArgs) ! {
|
||||
mut processed_paths := []string{}
|
||||
// println("1")
|
||||
println('1')
|
||||
gitstructure.load_recursive(gitstructure.coderoot.path, mut processed_paths)!
|
||||
// println("2")
|
||||
println('2')
|
||||
|
||||
if args.reload {
|
||||
mut ths := []thread !{}
|
||||
@@ -68,18 +68,16 @@ pub fn (mut gitstructure GitStructure) load(args StatusUpdateArgs) ! {
|
||||
// exit(0)
|
||||
}
|
||||
|
||||
gitstructure.init()!
|
||||
// gitstructure.init()!
|
||||
}
|
||||
|
||||
// just some initialization mechanism
|
||||
pub fn (mut gitstructure GitStructure) init() ! {
|
||||
if gitstructure.config.debug {
|
||||
gitstructure.config.log = true
|
||||
}
|
||||
if gitstructure.repos.keys().len == 0 {
|
||||
gitstructure.load()!
|
||||
}
|
||||
}
|
||||
// // just some initialization mechanism
|
||||
// pub fn (mut gitstructure GitStructure) init() ! {
|
||||
// if gitstructure.repos.keys().len == 0 {
|
||||
// println('Before loading keys.')
|
||||
// gitstructure.load()!
|
||||
// }
|
||||
// }
|
||||
|
||||
// Recursively loads repositories from the provided path, updating their statuses.
|
||||
//
|
||||
|
||||
@@ -9,24 +9,23 @@ const testpath = os.dir(@FILE) + '/testdata'
|
||||
|
||||
// make sure tmux isn't running prior to test
|
||||
fn testsuite_begin() {
|
||||
mut tmux := get_remote('185.69.166.152')!
|
||||
if tmux.is_running() {
|
||||
mut tmux := new(sessionid: '1234')!
|
||||
if tmux.is_running()! {
|
||||
tmux.stop()!
|
||||
}
|
||||
}
|
||||
|
||||
// make sure tmux isn't running after test
|
||||
fn testsuite_end() {
|
||||
mut tmux := get_remote('185.69.166.152')!
|
||||
mut tmux := new(sessionid: '1234')!
|
||||
|
||||
if tmux.is_running() {
|
||||
if tmux.is_running()! {
|
||||
tmux.stop()!
|
||||
}
|
||||
}
|
||||
|
||||
fn test_start() ! {
|
||||
mut tmux := get_remote('185.69.166.152')!
|
||||
|
||||
mut tmux := new(sessionid: '1234')!
|
||||
// test server is running after start()
|
||||
tmux.start() or { panic('cannot start tmux: ${err}') }
|
||||
mut tmux_ls := osal.execute_silent('tmux ls') or { panic('Cannot execute tmux ls: ${err}') }
|
||||
@@ -36,17 +35,17 @@ fn test_start() ! {
|
||||
}
|
||||
|
||||
fn test_stop() ! {
|
||||
mut tmux := get_remote('185.69.166.152')!
|
||||
mut tmux := new(sessionid: '1234')!
|
||||
|
||||
// test server is running after start()
|
||||
tmux.start() or { panic('cannot start tmux: ${err}') }
|
||||
assert tmux.is_running()
|
||||
assert tmux.is_running()!
|
||||
tmux.stop() or { panic('cannot stop tmux: ${err}') }
|
||||
assert !tmux.is_running()
|
||||
assert !tmux.is_running()!
|
||||
}
|
||||
|
||||
fn test_windows_get() ! {
|
||||
mut tmux := get_remote('185.69.166.152')!
|
||||
mut tmux := new(sessionid: '1234')!
|
||||
|
||||
// test windows_get when only starting window is running
|
||||
tmux.start()!
|
||||
@@ -54,45 +53,49 @@ fn test_windows_get() ! {
|
||||
assert windows.len == 1
|
||||
|
||||
// test getting newly created window
|
||||
tmux.window_new(WindowArgs{ name: 'testwindow' })!
|
||||
windows = tmux.windows_get()
|
||||
unsafe {
|
||||
assert windows.keys().contains('testwindow')
|
||||
}
|
||||
assert windows['testwindow'].name == 'testwindow'
|
||||
assert windows['testwindow'].active
|
||||
tmux.stop()!
|
||||
// tmux.window_new(WindowArgs{ name: 'testwindow' })!
|
||||
// windows = tmux.windows_get()
|
||||
// mut is_name_exist := false
|
||||
// mut is_active_window := false
|
||||
|
||||
// unsafe {
|
||||
// for window in windows {
|
||||
// if window.name == 'testwindow' {
|
||||
// is_name_exist = true
|
||||
// is_active_window = window.active
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// assert is_name_exist == true
|
||||
// assert is_active_window == true
|
||||
// tmux.stop()!
|
||||
}
|
||||
|
||||
// TODO: fix test
|
||||
fn test_scan() ! {
|
||||
console.print_debug('-----Testing scan------')
|
||||
mut tmux := get_remote('185.69.166.152')!
|
||||
mut tmux := new(sessionid: '1234')!
|
||||
tmux.start()!
|
||||
|
||||
// check bash window is initialized
|
||||
mut new_windows := tmux.windows_get()
|
||||
unsafe {
|
||||
assert new_windows.keys() == ['bash']
|
||||
}
|
||||
// assert new_windows.len == 1
|
||||
// assert new_windows[0].name == 'bash'
|
||||
|
||||
// test scan, should return no windows
|
||||
mut windows := tmux.windows_get()
|
||||
unsafe {
|
||||
assert windows.keys().len == 0
|
||||
}
|
||||
// test scan with window in tmux but not in tmux struct
|
||||
// mocking a failed command to see if scan identifies
|
||||
tmux.sessions['init'].windows['test'] = &Window{
|
||||
session: tmux.sessions['init']
|
||||
name: 'test'
|
||||
}
|
||||
new_windows = tmux.windows_get()
|
||||
panic('new windows ${new_windows.keys()}')
|
||||
unsafe {
|
||||
assert new_windows.keys().len == 1
|
||||
}
|
||||
new_windows = tmux.scan()!
|
||||
tmux.stop()!
|
||||
// tmux.sessions['init'].windows['test'] = &Window{
|
||||
// session: tmux.sessions['init']
|
||||
// name: 'test'
|
||||
// }
|
||||
// new_windows = tmux.windows_get()
|
||||
// panic('new windows ${new_windows.keys()}')
|
||||
// unsafe {
|
||||
// assert new_windows.keys().len == 1
|
||||
// }
|
||||
// new_windows = tmux.scan()!
|
||||
// tmux.stop()!
|
||||
}
|
||||
|
||||
// //TODO: fix test
|
||||
|
||||
@@ -190,7 +190,7 @@ pub fn (mut w Window) stop() ! {
|
||||
cmd: 'tmux kill-window -t @${w.id}'
|
||||
stdout: false
|
||||
name: 'tmux_kill-window'
|
||||
die: false
|
||||
// die: false
|
||||
) or { return error("Can't kill window with id:${w.id}") }
|
||||
w.pid = 0
|
||||
w.active = false
|
||||
|
||||
@@ -5,18 +5,14 @@ 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
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Load the test cache from JSON file
|
||||
fn load_test_cache() TestCache {
|
||||
if !os.exists(cache_file) {
|
||||
@@ -25,25 +21,19 @@ fn load_test_cache() TestCache {
|
||||
}
|
||||
}
|
||||
|
||||
content := os.read_file(cache_file) or {
|
||||
return TestCache{
|
||||
content := os.read_file(cache_file) or { return TestCache{
|
||||
tests: map[string]i64{}
|
||||
}
|
||||
}
|
||||
} }
|
||||
|
||||
return json.decode(TestCache, content) or {
|
||||
return TestCache{
|
||||
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}')
|
||||
}
|
||||
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
|
||||
@@ -82,14 +72,14 @@ fn get_cache_key(path string, base_dir string) string {
|
||||
}
|
||||
|
||||
// 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)! {
|
||||
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
|
||||
|
||||
if ! path.to_lower().contains("_test.v"){
|
||||
if !path.to_lower().contains('_test.v') {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -119,7 +109,7 @@ fn process_test_file(path string, base_dir string, test_files_ignore []string, t
|
||||
}
|
||||
}
|
||||
|
||||
fn dotest(path string, base_dir string, mut cache TestCache)! {
|
||||
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)
|
||||
|
||||
@@ -144,7 +134,6 @@ fn dotest(path string, base_dir string, mut cache TestCache)! {
|
||||
println('Test passed: ${path}')
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////
|
||||
/////////////////////////
|
||||
|
||||
@@ -158,7 +147,6 @@ fp.finalize() or {
|
||||
exit(1)
|
||||
}
|
||||
|
||||
|
||||
// Remove cache file if -r flag is set
|
||||
if remove_cache && os.exists(cache_file) {
|
||||
os.rm(cache_file) or {
|
||||
@@ -168,34 +156,34 @@ if remove_cache && os.exists(cache_file) {
|
||||
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
|
||||
lib/code
|
||||
lib/clients
|
||||
// lib/crypt
|
||||
lib/core
|
||||
lib/develop
|
||||
"
|
||||
lib/markdownparser/
|
||||
lib/ourdb/
|
||||
lib/gittools
|
||||
// lib/crypt
|
||||
'
|
||||
|
||||
//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
|
||||
@@ -213,24 +201,10 @@ generate_test.v
|
||||
dbfs_test.v
|
||||
namedb_test.v
|
||||
timetools_test.v
|
||||
markdownparser/link_test.v
|
||||
markdownparser/link_def_test.v
|
||||
markdownparser/char_parser_test.v
|
||||
markdownparser/action_test.v
|
||||
markdownparser/elements/char_parser_test.v
|
||||
markdownparser/markdown_test.v
|
||||
markdownparser/list_test.v
|
||||
markdownparser/table_test.v
|
||||
ourdb/lookup_test.v
|
||||
ourdb/lookup_id_test.v
|
||||
ourdb/db_test.v
|
||||
ourdb/lookup_location_test.v
|
||||
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,14 +213,14 @@ 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("#") {
|
||||
if test.trim_space() == '' || test.trim_space().starts_with('//')
|
||||
|| test.trim_space().starts_with('#') {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -261,11 +235,12 @@ for test in test_files {
|
||||
// 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)!
|
||||
|
||||
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)!
|
||||
process_test_file(full_path, norm_dir_of_script, test_files_ignore, test_files_error, mut
|
||||
cache, mut tests_in_error)!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user