From 9f6e49963e1a2455d8fab0274acc27d4ca700cb8 Mon Sep 17 00:00:00 2001 From: mariobassem Date: Mon, 27 Jan 2025 18:27:50 +0200 Subject: [PATCH] wip: test: improve tmux tests - Fix bugs found during testing. Co-authored-by: mahmmoud.hassanein --- lib/osal/tmux/testdata/tmux_window_test.v | 67 ------------------- lib/osal/tmux/tmux.v | 43 ++++++------ lib/osal/tmux/tmux_scan.v | 5 +- .../tmux/{testdata => }/tmux_session_test.v | 6 +- lib/osal/tmux/tmux_window.v | 2 +- lib/osal/tmux/tmux_window_test.v | 67 +++++++++++++++++++ test_basic.vsh | 1 - 7 files changed, 96 insertions(+), 95 deletions(-) delete mode 100644 lib/osal/tmux/testdata/tmux_window_test.v rename lib/osal/tmux/{testdata => }/tmux_session_test.v (95%) create mode 100644 lib/osal/tmux/tmux_window_test.v diff --git a/lib/osal/tmux/testdata/tmux_window_test.v b/lib/osal/tmux/testdata/tmux_window_test.v deleted file mode 100644 index 686d742e..00000000 --- a/lib/osal/tmux/testdata/tmux_window_test.v +++ /dev/null @@ -1,67 +0,0 @@ -module tmux - -import freeflowuniverse.herolib.osal -import freeflowuniverse.herolib.installers.tmux -import freeflowuniverse.herolib.ui.console - -// uses single tmux instance for all tests -__global ( - tmux Tmux -) - -fn init() { - tmux = get_remote('185.69.166.152')! - - // reset tmux for tests - if tmux.is_running() { - tmux.stop() or { panic('Cannot stop tmux') } - } -} - -fn testsuite_end() { - if tmux.is_running() { - tmux.stop()! - } -} - -fn test_window_new() { - tmux.start() or { panic("can't start tmux: ${err}") } - - // test window new with only name arg - window_args := WindowArgs{ - name: 'TestWindow' - } - - assert !tmux.sessions.keys().contains('main') - - mut window := tmux.window_new(window_args) or { panic("Can't create new window: ${err}") } - assert tmux.sessions.keys().contains('main') - window.delete() or { panic('Cant delete window') } -} - -// // tests creating duplicate windows -// fn test_window_new0() { - -// -// installer := tmux.get_install( - -// mut tmux := Tmux { -// node: node_ssh -// } - -// window_args := WindowArgs { -// name: 'TestWindow0' -// } - -// // console.print_debug(tmux) -// mut window := tmux.window_new(window_args) or { -// panic("Can't create new window: $err") -// } -// assert tmux.sessions.keys().contains('main') -// mut window_dup := tmux.window_new(window_args) or { -// panic("Can't create new window: $err") -// } -// console.print_debug(node_ssh.exec('tmux ls') or { panic("fail:$err")}) -// window.delete() or { panic("Cant delete window") } -// // console.print_debug(tmux) -// } diff --git a/lib/osal/tmux/tmux.v b/lib/osal/tmux/tmux.v index 7dbb3686..3c34e9e6 100644 --- a/lib/osal/tmux/tmux.v +++ b/lib/osal/tmux/tmux.v @@ -23,20 +23,20 @@ pub fn new(args TmuxNewArgs) !Tmux { mut t := Tmux{ sessionid: args.sessionid } - t.load()! + // t.load()! t.scan()! return t } -// loads tmux session, populate the object -pub fn (mut tmux Tmux) load() ! { - isrunning := tmux.is_running()! - if !isrunning { - tmux.start()! - } - // console.print_debug("SCAN") - tmux.scan()! -} +// // loads tmux session, populate the object +// pub fn (mut tmux Tmux) load() ! { +// // isrunning := tmux.is_running()! +// // if !isrunning { +// // tmux.start()! +// // } +// // console.print_debug("SCAN") +// tmux.scan()! +// } pub fn (mut t Tmux) stop() ! { $if debug { @@ -91,19 +91,18 @@ pub fn (mut t Tmux) windows_get() []&Window { // checks whether tmux server is running pub fn (mut t Tmux) is_running() !bool { - res := osal.exec(cmd: 'tmux info', stdout: false, name: 'tmux_info', raise_error: false) or { - panic('bug') - } - if res.error.contains('no server running') { - // console.print_debug(" TMUX NOT RUNNING") - return false - } - if res.error.contains('no current client') { - return true - } - if res.exit_code > 0 { - return error('could not execute tmux info.\n${res}') + res := os.execute('tmux info') + if res.exit_code != 0 { + if res.output.contains('no server running') { + // console.print_debug(" TMUX NOT RUNNING") + return false + } + if res.output.contains('no current client') { + return true + } + return error('could not execute tmux info.\n${res.output}') } + return true } diff --git a/lib/osal/tmux/tmux_scan.v b/lib/osal/tmux/tmux_scan.v index f385262d..bbe2ae33 100644 --- a/lib/osal/tmux/tmux_scan.v +++ b/lib/osal/tmux/tmux_scan.v @@ -58,6 +58,9 @@ pub fn (mut t Tmux) scan() ! { cmd_list_session := "tmux list-sessions -F '#{session_name}'" exec_list := osal.exec(cmd: cmd_list_session, stdout: false, name: 'tmux_list') or { + if err.msg().contains('no server running') { + return + } return error('could not execute list sessions.\n${err}') } @@ -80,7 +83,7 @@ pub fn (mut t Tmux) scan() ! { } console.print_debug(t) - + println('t: ${t}') // mut done := map[string]bool{} cmd := "tmux list-panes -a -F '#{session_name}|#{window_name}|#{window_id}|#{pane_active}|#{pane_id}|#{pane_pid}|#{pane_start_command}'" out := osal.execute_silent(cmd) or { return error("Can't execute ${cmd} \n${err}") } diff --git a/lib/osal/tmux/testdata/tmux_session_test.v b/lib/osal/tmux/tmux_session_test.v similarity index 95% rename from lib/osal/tmux/testdata/tmux_session_test.v rename to lib/osal/tmux/tmux_session_test.v index 5be65c1d..e4822c41 100644 --- a/lib/osal/tmux/testdata/tmux_session_test.v +++ b/lib/osal/tmux/tmux_session_test.v @@ -1,7 +1,7 @@ module tmux import freeflowuniverse.herolib.osal -import freeflowuniverse.herolib.installers.tmux +// import freeflowuniverse.herolib.installers.tmux // fn testsuite_end() { @@ -26,13 +26,13 @@ fn test_session_create() { mut s := Session{ tmux: &tmux - windows: map[string]&Window{} + windows: []&Window{} name: 'testsession' } mut s2 := Session{ tmux: &tmux - windows: map[string]&Window{} + windows: []&Window{} name: 'testsession2' } diff --git a/lib/osal/tmux/tmux_window.v b/lib/osal/tmux/tmux_window.v index ad60d4c0..de047078 100644 --- a/lib/osal/tmux/tmux_window.v +++ b/lib/osal/tmux/tmux_window.v @@ -191,7 +191,7 @@ pub fn (mut w Window) stop() ! { stdout: false name: 'tmux_kill-window' // die: false - ) or { return error("Can't kill window with id:${w.id}") } + ) or { return error("Can't kill window with id:${w.id}: ${err}") } w.pid = 0 w.active = false } diff --git a/lib/osal/tmux/tmux_window_test.v b/lib/osal/tmux/tmux_window_test.v new file mode 100644 index 00000000..5bb228d0 --- /dev/null +++ b/lib/osal/tmux/tmux_window_test.v @@ -0,0 +1,67 @@ +module tmux + +import freeflowuniverse.herolib.osal +import freeflowuniverse.herolib.ui.console +import time + +// uses single tmux instance for all tests + +fn testsuite_begin() { + muttmux := new() or { panic('Cannot create tmux: ${err}') } + + // reset tmux for tests + is_running := tmux.is_running() or { panic('cannot check if tmux is running: ${err}') } + if is_running { + tmux.stop() or { panic('Cannot stop tmux: ${err}') } + } +} + +fn testsuite_end() { + is_running := is_running() or { panic('cannot check if tmux is running: ${err}') } + if is_running { + stop() or { panic('Cannot stop tmux: ${err}') } + } +} + +fn test_window_new() ! { + mut tmux_ := new()! + + // test window new with only name arg + window_args := WindowArgs{ + name: 'TestWindow' + } + + assert tmux_.sessions.filter(it.name == 'main').len == 0 + + mut window := tmux_.window_new(window_args)! + assert tmux_.sessions.filter(it.name == 'main').len > 0 + // time.sleep(1000 * time.millisecond) + // window.stop()! +} + +// tests creating duplicate windows +fn test_window_new0() { + + + installer := tmux.get_install( + + mut tmux := Tmux { + node: node_ssh + } + + window_args := WindowArgs { + name: 'TestWindow0' + } + + // console.print_debug(tmux) + mut window := tmux.window_new(window_args) or { + panic("Can't create new window: $err") + } + assert tmux.sessions.keys().contains('main') + mut window_dup := tmux.window_new(window_args) or { + panic("Can't create new window: $err") + } + console.print_debug(node_ssh.exec('tmux ls') or { panic("fail:$err")}) + window.delete() or { panic("Cant delete window") } + // console.print_debug(tmux) +} diff --git a/test_basic.vsh b/test_basic.vsh index 5aa61294..3fbac5e0 100755 --- a/test_basic.vsh +++ b/test_basic.vsh @@ -185,7 +185,6 @@ clients/livekit ' tests_error := ' -tmux_session_test.v tmux_window_test.v tmux_test.v startupmanager_test.v