Skip to content

Commit

Permalink
tests: unit: maintainers_test: Add failing tests
Browse files Browse the repository at this point in the history
The kw 'maintainers' feature lacks tests for its multiple corner cases
where it should fail. Adding such tests ensures a larger test coverage
for expected behaviors of this feature, possibly preventing future
accidental undesirable changes.

Signed-off-by: Rodrigo Siqueira <siqueirajordao@riseup.net>
Co-authored-by: Luiza Soezima <lbrsoezima@usp.br>
Co-authored-by: Sabrina Araujo <sabrinaaraujo@usp.br>
Signed-off-by: Lincoln Yuji <lincolnyuji@hotmail.com>
Reviewed-by: Rodrigo Siqueira <siqueirajordao@riseup.net>
  • Loading branch information
3 people authored and rodrigosiqueira committed Jun 13, 2024
1 parent 278cfd4 commit defa940
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 24 deletions.
187 changes: 163 additions & 24 deletions tests/unit/maintainers_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Maintainers already in 'To:' field of update_patch_test.patch"

FAKE_KERNEL="tests/.tmp"

# Original directory path to go back before each test function
ORIGINAL_DIR="$PWD"

function oneTimeSetUp()
{
# This creates tests/.tmp which should mock a kernel tree root. A .git
Expand All @@ -55,8 +58,9 @@ function oneTimeSetUp()
cp -f tests/unit/samples/MAINTAINERS "$FAKE_KERNEL"/MAINTAINERS
cp -f tests/unit/samples/external/get_maintainer.pl "$FAKE_KERNEL"/scripts/
cp -f tests/unit/samples/update_patch_test{_model,}{,2}.patch "$FAKE_KERNEL"/
cp --force 'tests/unit/samples/cover_letter_test.patch' "$FAKE_KERNEL"
cd "$FAKE_KERNEL" || {
fail "($LINENO) It was not possible to move to temporary directory"
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
touch fs/some_file
Expand All @@ -66,7 +70,7 @@ function oneTimeSetUp()
git config user.email kw@kw
git commit --quiet -m "Test message"
cd "$original_dir" || {
fail "($LINENO) It was not possible to move back from temp directory"
fail "(${LINENO}) It was not possible to move back from temp directory"
return
}

Expand All @@ -78,6 +82,27 @@ function oneTimeTearDown()
rm -rf "$FAKE_KERNEL"
}

function setUp()
{
# Ensure each test function starts running in ORIGINAL_DIR.
cd "$ORIGINAL_DIR" || {
fail "(${LINENO}) It was not possible to move to original dir"
return
}
}

function tearDown()
{
# Check if SHUNIT_TMPDIR can be safely removed and then remove it and remake it.
is_safe_path_to_remove "$SHUNIT_TMPDIR"
if [[ "$?" == 0 ]]; then
rm --recursive --force "$SHUNIT_TMPDIR"
mkdir --parents "$SHUNIT_TMPDIR"
else
fail 'It was not possible to safely remove SHUNIT tmp directory.'
fi
}

function test_print_files_authors()
{
local -r ret=$(print_files_authors "tests/unit/samples/print_file_author_test_dir/code1.c")
Expand All @@ -93,13 +118,12 @@ function test_print_files_authors_from_dir()
function test_maintainers_main()
{
local ret
local -r original_dir="$PWD"

ret="$(maintainers_main tests/.tmp)"
multilineAssertEquals "$ret" "$CORRECT_TMP_MSG"

cd "$FAKE_KERNEL" || {
fail "($LINENO) It was not possible to move to temporary directory"
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
ret="$(maintainers_main .)"
Expand All @@ -109,57 +133,172 @@ function test_maintainers_main()
multilineAssertEquals "$CORRECT_TMP_FS_MSG" "$ret"

cd fs || {
fail "($LINENO) It was not possible to move to fs directory"
fail "(${LINENO}) It was not possible to move to fs directory"
return
}
ret="$(maintainers_main ..)"
multilineAssertEquals "$CORRECT_TMP_MSG" "$ret"

ret="$(maintainers_main .)"
multilineAssertEquals "$CORRECT_TMP_FS_MSG" "$ret"
cd "$original_dir" || {
fail "($LINENO) It was not possible to move back from temp directory"
}

# This function tests expected behaviors for failure in
# parse_maintainers_options.
function test_parse_maintainers_options()
{
local return_status

cd "$FAKE_KERNEL" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}

# Invalid option (typo)
unset options_values
declare -gA options_values
parse_maintainers_options --update-pacth # Messy argument
return_status="$?"
assertEquals "(${LINENO})" 22 "$return_status"

# Invalid option (even with a correct one following it)
unset options_values
declare -gA options_values
parse_maintainers_options --update-patch --invalid-option
return_status="$?"
assertEquals "(${LINENO})" 22 "$return_status"

# Too many arguments
unset options_values
declare -gA options_values
parse_maintainers_options --update-patch '.' 'some_file'
return_status="$?"
assertEquals "(${LINENO})" 22 "$return_status"
}

function test_maintainers_main_update_patch_invalid_inputs()
{
local return_status
local output_from_maintainers_main

cd "$FAKE_KERNEL" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}

output_from_maintainers_main="$(maintainers_main --update-patch 'fs/some_file')"
return_status="$?"
assertEquals "(${LINENO})" 'Option --update-patch was passed but given path is not a patch.' "$output_from_maintainers_main"
assertEquals "(${LINENO})" 1 "$return_status"

output_from_maintainers_main="$(maintainers_main --update-patch 'cover_letter_test.patch')"
return_status="$?"
assertEquals "(${LINENO})" 'Option --update-patch was passed but given path is not a patch.' "$output_from_maintainers_main"
assertEquals "(${LINENO})" 1 "$return_status"
}

# This function tests cases where the maintainers_main is called outside
# of a kernel tree and the given path is not in a kernel tree either.
function test_maintainers_main_no_kernel()
{
local return_status
local output_from_maintainers_main

# Going outside of a kernel tree
cd "$SHUNIT_TMPDIR" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}
mkdir 'not-a-kernel'

# Testing with default value
output_from_maintainers_main="$(maintainers_main)"
return_status="$?"
assertEquals "(${LINENO})" 'Neither the given path nor the working path is in a kernel tree.' "$output_from_maintainers_main"
assertEquals "(${LINENO})" 1 "$return_status"

# Testing while giving a path
output_from_maintainers_main="$(maintainers_main ./not-a-kernel)"
return_status="$?"
assertEquals "(${LINENO})" 'Neither the given path nor the working path is in a kernel tree.' "$output_from_maintainers_main"
assertEquals "(${LINENO})" 1 "$return_status"
}

# This function tests cases where the maintainers_main is called inside
# a kernel tree, but the given path is not a patch and is outside a kernel tree.
function test_maintainers_main_path_out_of_tree()
{
local return_status
local output_from_maintainers_main

cd "$FAKE_KERNEL" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}

output_from_maintainers_main="$(maintainers_main ..)"
return_status="$?"
assertEquals "(${LINENO})" 'The given file is not a patch and is outside a kernel tree.' "$output_from_maintainers_main"
assertEquals "(${LINENO})" 1 "$return_status"
}

# This function tests cases where the given paths to maintainers_main are invalid
function test_maintainers_main_invalid_paths()
{
local return_status
local output_from_maintainers_main

cd "$FAKE_KERNEL" || {
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}

# Test for files that don't exist in current directory
output_from_maintainers_main="$(maintainers_main 'file-does-not-exist.c')"
return_status="$?"
assertEquals "(${LINENO})" 'Invalid path' "$output_from_maintainers_main"
assertEquals "(${LINENO})" 1 "$return_status"

# Test for files that don't exist in an other existing directory
output_from_maintainers_main="$(maintainers_main "$SHUNIT_TMPDIR/not-a-file.c")"
return_status="$?"
assertEquals "(${LINENO})" 'Invalid path' "$output_from_maintainers_main"
assertEquals "(${LINENO})" 1 "$return_status"
}

function test_maintainers_main_patch()
{
local original_dir="$PWD"
local return_status

cd "$FAKE_KERNEL" || {
fail "($LINENO) It was not possible to move to temporary directory"
fail "(${LINENO}) It was not possible to move to temporary directory"
return
}

ret="$(maintainers_main update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_MSG" "$ret"
return_status="$(maintainers_main update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_MSG" "$return_status"

# test -u
cp -f update_patch_test.patch{,.bak}
ret="$(maintainers_main -u update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH_MSG" "$ret"
return_status="$(maintainers_main -u update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH_MSG" "$return_status"
assertFileEquals update_patch_test{,_model}.patch
cp -f update_patch_test.patch{.bak,}

# test --update-patch
ret="$(maintainers_main --update-patch update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH_MSG" "$ret"
return_status="$(maintainers_main --update-patch update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH_MSG" "$return_status"
assertFileEquals update_patch_test{,_model}.patch

# test for already existing maintainers
ret="$(maintainers_main -u update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH_ALREADY_IN_MSG" "$ret"
return_status="$(maintainers_main -u update_patch_test.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH_ALREADY_IN_MSG" "$return_status"
assertFileEquals update_patch_test{,_model}.patch

# test for already existing "To:" field without maintainers
ret="$(maintainers_main -u update_patch_test2.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH2_MSG" "$ret"
return_status="$(maintainers_main -u update_patch_test2.patch)"
multilineAssertEquals "$CORRECT_TMP_PATCH2_MSG" "$return_status"
assertFileEquals update_patch_test{,_model}2.patch

cd "$original_dir" || {
fail "($LINENO) It was not possible to move back from temp directory"
return
}
}

invoke_shunit
21 changes: 21 additions & 0 deletions tests/unit/samples/cover_letter_test.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
From 2741c6a546f4e0b148f7211def92a8843b9e0972 Mon Sep 17 00:00:00 2001
From: kw <kw@example.net>
Date: Fri, 23 May 2023 09:04:44 -0300
Subject: [PATCH 0/2] Cover letter example

This is an example of a cover letter for testing purposes.
There are not actual pacthes related to this file and the
following modified files do not exist.

kw (2):
src: art3435: Add failure paths as corner cases
test: unit: art3435_test: Add failing tests

src/art3435.c | 34 ++++++++++++++++++
test/unit/art3435.sh | 86 ++++++++++++++++++++++++++++++++++

2 files changed, 120 insertions(+)

--
2.34.1

0 comments on commit defa940

Please sign in to comment.