Skip to content

Commit

Permalink
Add TestRig to common.testing to reduce boilerplate for testing (#130)
Browse files Browse the repository at this point in the history
* Add TestRig to reduce boilerplate for testing

* Refactor tests to use TestRig

* Be more forgiving in temp dir clean-up

* Fix typo in whoami test

* Handle unsupported platforms

* Call rig.clean_up with C.atexit

* Now calling C.atexit at the right place

* Moved TestRig to .c.v file for use of C.atexit()
  • Loading branch information
syrmel authored Jan 30, 2024
1 parent ffc90ee commit d50c2ec
Show file tree
Hide file tree
Showing 19 changed files with 176 additions and 328 deletions.
59 changes: 59 additions & 0 deletions common/testing/testrig.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module testing

import os

// TestRig contains the relevant scaffolding for tests to avoid boilerplate in
// the individual <util>_test.v files
pub struct TestRig {
pub:
util string
platform_util string
executable_under_test string
temp_dir string
cmd CommandPair
is_supported_platform bool
}

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

pub fn prepare_rig(config TestRigConfig) TestRig {
platform_util := $if !windows {
config.util
} $else {
'coreutils ${config.util}'
}
exec_under_test := if config.is_supported_platform {
prepare_executable(config.util)
} else {
''
}
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}') }
rig := TestRig{
util: config.util
platform_util: platform_util
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
}
C.atexit(rig.clean_up)
return rig
}

pub fn (rig TestRig) call_for_test(args string) os.Result {
res := os.execute('${rig.executable_under_test} ${args}')
assert res.exit_code == 0
return res
}

pub fn (rig TestRig) clean_up() {
if os.is_dir(rig.temp_dir) {
os.rmdir_all(rig.temp_dir) or {}
}
}
18 changes: 3 additions & 15 deletions src/arch/arch_test.v
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import common.testing
import os

const util = 'arch'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
}
const rig = testing.prepare_rig(util: 'arch')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
Expand Down
22 changes: 5 additions & 17 deletions src/base64/base64_test.v
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import os
import common.testing
import os

const util = 'base64'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
}
const rig = testing.prepare_rig(util: 'base64')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
}

fn test_abcd() {
res := os.execute('${executable_under_test} abcd')
res := os.execute('${rig.executable_under_test} abcd')
assert res.exit_code == 1
assert res.output.trim_space() == 'base64: abcd: No such file or directory'
}
Expand Down
39 changes: 14 additions & 25 deletions src/cksum/cksum_test.v
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
import os
import common.testing
import os

const rig = testing.prepare_rig(util: 'cksum')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test
const eol = testing.output_eol()
const util = 'cksum'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

const temp_dir = testing.temp_folder
const test1_txt_path = os.join_path(temp_dir, 'test1.txt')
const test2_txt_path = os.join_path(temp_dir, 'test2.txt')
const test3_txt_path = os.join_path(temp_dir, 'test3.txt')
const dummy = os.join_path(temp_dir, 'dummy')
const long_over_16k = os.join_path(temp_dir, 'long_over_16k')
const long_under_16k = os.join_path(temp_dir, 'long_under_16k')

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
}
const test1_txt_path = os.join_path(rig.temp_dir, 'test1.txt')
const test2_txt_path = os.join_path(rig.temp_dir, 'test2.txt')
const test3_txt_path = os.join_path(rig.temp_dir, 'test3.txt')
const dummy = os.join_path(rig.temp_dir, 'dummy')
const long_over_16k = os.join_path(rig.temp_dir, 'long_over_16k')
const long_under_16k = os.join_path(rig.temp_dir, 'long_under_16k')

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
os.write_file(test1_txt_path, 'Hello World!\nHow are you?')!
os.write_file(test2_txt_path, 'a'.repeat(128 * 1024 + 5))!
}
Expand All @@ -37,6 +22,10 @@ fn testsuite_end() {
os.rm(test2_txt_path)!
}

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
}

fn test_stdin() {
res := os.execute('cat ${test1_txt_path} | ${executable_under_test}')

Expand Down
21 changes: 4 additions & 17 deletions src/dirname/dirname_test.v
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import common.testing
import os

const util = 'dirname'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const rig = testing.prepare_rig(util: 'dirname')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test
const slash = $if !windows {
'\\'
} $else {
'/'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
}

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
}
Expand All @@ -31,7 +18,7 @@ fn expected_result(input string, output string) {
res := os.execute('${executable_under_test} ${input}')
assert res.exit_code == 0
assert res.output.trim_space() == output
testing.same_results('dirname ${input}', '${executable_under_test} ${input}')
testing.same_results('${rig.util} ${input}', '${executable_under_test} ${input}')
}

fn test_expected() {
Expand Down
19 changes: 4 additions & 15 deletions src/expand/expand_test.v
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import os
import common.testing

const rig = testing.prepare_rig(util: 'expand')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test
const eol = testing.output_eol()

const util = 'expand'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

const test_txt_path = os.join_path(testing.temp_folder, 'test.txt')
const test_txt_path = os.join_path(rig.temp_dir, 'test.txt')

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
mut f := os.open_file(test_txt_path, 'wb')!
for l in testtxtcontent {
f.writeln('${l}')!
Expand Down
17 changes: 2 additions & 15 deletions src/expr/expr_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@ import common
import common.testing
import os

const util = 'expr'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)
const rig = testing.prepare_rig(util: 'expr')
const cmd = rig.cmd

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
Expand Down Expand Up @@ -133,10 +124,6 @@ const tests = [
r"'(' 2 a",
]

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
}

fn test_results() {
mut failed := []string{}
for test in tests {
Expand Down
22 changes: 5 additions & 17 deletions src/factor/factor_test.v
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import os
import common.testing
import os

const util = 'factor'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
}
const rig = testing.prepare_rig(util: 'factor')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
Expand All @@ -31,7 +19,7 @@ fn expected_result(input string, output []string) {
res := os.execute('${executable_under_test} ${input}')
assert res.exit_code == 0
assert res.output.split_into_lines() == output
testing.same_results('factor ${input}', '${executable_under_test} ${input}')
testing.same_results('${rig.util} ${input}', '${executable_under_test} ${input}')
}

fn test_expected() {
Expand Down
29 changes: 9 additions & 20 deletions src/fold/fold_test.v
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import os
import common.testing
import os

const rig = testing.prepare_rig(util: 'fold')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test
const eol = testing.output_eol()

const util = 'fold'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

const test_txt_path = os.join_path(testing.temp_folder, 'test.txt')

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
}
const test_txt_path = os.join_path(rig.temp_dir, 'test.txt')

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
mut f := os.open_file(test_txt_path, 'wb')!
for l in testtxtcontent {
f.writeln('${l}') or {}
Expand All @@ -34,6 +19,10 @@ fn testsuite_end() {
os.rm(test_txt_path)!
}

fn test_help_and_version() {
cmd.ensure_help_and_version_options_work()!
}

fn test_non_existent_file() {
res := os.execute('${executable_under_test} non-existent-file')
assert res.exit_code == 1
Expand Down
21 changes: 5 additions & 16 deletions src/head/head_test.v
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import os
import common.testing
import os

const rig = testing.prepare_rig(util: 'head')
const cmd = rig.cmd
const executable_under_test = rig.executable_under_test
const eol = testing.output_eol()

const util = 'head'

const platform_util = $if !windows {
util
} $else {
'coreutils ${util}'
}

const executable_under_test = testing.prepare_executable(util)

const cmd = testing.new_paired_command(platform_util, executable_under_test)

const test_txt_path = os.join_path(testing.temp_folder, 'test.txt')
const test_txt_path = os.join_path(rig.temp_dir, 'test.txt')

fn testsuite_begin() {
os.chdir(testing.temp_folder)!
mut f := os.open_file(test_txt_path, 'wb')!
for l in testtxtcontent {
f.writeln('${l}')!
Expand Down
Loading

0 comments on commit d50c2ec

Please sign in to comment.