Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify tests using platform-specific test functionality #140

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ jobs:
run: cd coreutils && make testfmt
- name: Build all
run: cd coreutils && v run build.vsh
- name: Debug uptime_test.v
run: cd coreutils && v -d trace_same_results -stats src/uptime/uptime_test.v
- name: Debug uptime_nix_test.v
run: cd coreutils && v -d trace_same_results -stats src/uptime/uptime_nix_test.v
- name: Run tests
run: cd coreutils && make test

Expand Down
15 changes: 2 additions & 13 deletions common/testing/testrig.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ pub:
executable_under_test string
temp_dir string
cmd CommandPair
is_supported_platform bool
}

pub struct TestRigConfig {
pub:
util string
is_supported_platform bool = true
util string
}

pub fn (rig TestRig) call_for_test(args string) os.Result {
Expand All @@ -49,11 +47,7 @@ pub fn prepare_rig(config TestRigConfig) TestRig {
call_util
}

exec_under_test := if config.is_supported_platform {
prepare_executable(config.util)
} else {
''
}
exec_under_test := prepare_executable(config.util)
temp_dir := os.join_path(temp_folder, config.util)
os.mkdir(temp_dir) or { panic('Unable to make test directory: ${temp_dir}') }
os.chdir(temp_dir) or { panic('Unable to set working directory: ${temp_dir}') }
Expand All @@ -64,7 +58,6 @@ pub fn prepare_rig(config TestRigConfig) TestRig {
cmd: new_paired_command(platform_util, exec_under_test)
executable_under_test: exec_under_test
temp_dir: temp_dir
is_supported_platform: config.is_supported_platform
}
wire_clean_up_at_exit(rig)
return rig
Expand All @@ -77,10 +70,6 @@ pub fn (rig TestRig) clean_up() {
}

pub fn (rig TestRig) assert_platform_util() {
if !rig.is_supported_platform {
return
}

platform_ver := $if !windows {
os.execute('${rig.platform_util_path} --version')
} $else {
Expand Down
13 changes: 1 addition & 12 deletions src/tty/tty_test.v → src/tty/tty_nix_test.v
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import common.testing

const supported_platform = $if windows {
false
} $else {
true
} // No tty in Windows
const rig = testing.prepare_rig(util: 'tty', is_supported_platform: supported_platform)
const rig = testing.prepare_rig(util: 'tty')

fn testsuite_begin() {
rig.assert_platform_util()
}

fn test_help_and_version() {
if !supported_platform {
return
}
rig.assert_help_and_version_options_work()
}

fn test_compare() {
if !supported_platform {
return
}
rig.assert_same_results('')
rig.assert_same_results('-s')
rig.assert_same_results('--silent')
Expand Down
16 changes: 2 additions & 14 deletions src/uptime/uptime_test.v → src/uptime/uptime_nix_test.v
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import common.testing

const supported_platform = $if windows {
false
} $else {
true
} // WinOS lacks currently required utmp support
const rig = testing.prepare_rig(util: 'uptime', is_supported_platform: supported_platform)
const executable_under_test = rig.executable_under_test
const rig = testing.prepare_rig(util: 'uptime')

fn testsuite_begin() {
rig.assert_platform_util()
}

fn test_help_and_version() {
if !supported_platform {
return
}
rig.assert_help_and_version_options_work()
}

fn test_unknown_option() {
if !supported_platform {
return
}
testing.command_fails('${executable_under_test} -x')!
testing.command_fails('${rig.executable_under_test} -x')!
}

// SKIP ~ comparing subsequent runs of `uptime` is a *race condition* causing random failures
Expand Down
24 changes: 24 additions & 0 deletions src/users/users_nix_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import common
import common.testing

const rig = testing.prepare_rig(util: 'users')

fn testsuite_begin() {
rig.assert_platform_util()
}

fn test_help_and_version() {
rig.assert_help_and_version_options_work()
}

fn test_compare() {
rig.assert_same_results('')
rig.assert_same_results('does_not_exist')
unsafe { rig.assert_same_results(cstring_to_vstring(common.utmp_file_charptr)) }
unsafe { rig.assert_same_results(cstring_to_vstring(common.wtmp_file_charptr)) }
}

fn test_call_errors() {
rig.assert_same_exit_code('-x')
rig.assert_same_results('a b c')
}
41 changes: 0 additions & 41 deletions src/users/users_test.v

This file was deleted.

Loading