1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#!/bin/bash
. ./src/get_maintainer_wrapper.sh --source-only
. ./src/kwlib.sh --source-only
. ./tests/utils --source-only
function suite
{
suite_addTest "linuxRootCheckTest"
suite_addTest "cmdManagerTESTMODETest"
suite_addTest "cmdManagerSILENTTest"
suite_addTest "cmdManagerSAY_COMPLAIN_WARNING_SUCCESS_Test"
suite_addTest "detectDistroTest"
suite_addTest "joinPathTest"
suite_addTest "findKernelRootTest"
suite_addTest "isAPatchTest"
}
function setupFakeOSInfo
{
mkdir -p tests/.tmp/detect_distro/{arch,debian}/etc
cp -f tests/samples/os/arch/* tests/.tmp/detect_distro/arch/etc
cp -f tests/samples/os/debian/* tests/.tmp/detect_distro/debian/etc
}
function setupPatch
{
mkdir -p "tests/.tmp"
cp -f tests/samples/test.patch tests/.tmp/
}
function setupFakeKernelRepo
{
# This creates tests/.tmp which should mock a kernel tree root. A .git
# dir is also created inside tests/.tmp so that get_maintainer.pl thinks
# it is a git repo. This is done in order to avoid some warnings that
# get_maintainer.pl prints when no .git is found.
mkdir -p "tests/.tmp"
cd "tests/.tmp"
touch "COPYING"
touch "CREDITS"
touch "Kbuild"
touch "Makefile"
touch "README"
mkdir -p "Documentation"
mkdir -p "arch"
mkdir -p "include"
mkdir -p "drivers"
mkdir -p "fs"
mkdir -p "init"
mkdir -p "ipc"
mkdir -p "kernel"
mkdir -p "lib"
mkdir -p "scripts"
mkdir -p ".git"
cd ../../
cp -f tests/samples/MAINTAINERS tests/.tmp/MAINTAINERS
cp -f tests/external/get_maintainer.pl tests/.tmp/scripts/
}
function tearDownSetup
{
rm -rf "tests/.tmp"
}
function linuxRootCheckTest
{
setupFakeKernelRepo
is_kernel_root "tests/.tmp"
[[ "$?" != 0 ]] && fail "Failed to check if a directory is a kernel root."
tearDownSetup
true # Reset return value
}
function cmdManagerSILENTTest
{
setupFakeKernelRepo
cd "tests/.tmp"
ret=$(cmd_manager SILENT ls)
assertFalse "We used SILENT mode, we should not find ls" '[[ $ret =~ ls ]]'
assertTrue "We expected to find MAINTAINERS" '[[ $ret =~ MAINTAINERS ]]'
assertTrue "We expected to find CREDITS" '[[ $ret =~ CREDITS ]]'
assertTrue "We expected to find README" '[[ $ret =~ README ]]'
assertTrue "We expected to find arch" '[[ $ret =~ arch ]]'
assertTrue "We expected to find scripts" '[[ $ret =~ scripts ]]'
# Test command with parameters
ret=$(cmd_manager SILENT pwd --help)
assertTrue "We expected to find -P" '[[ $ret =~ -P ]]'
assertTrue "We expected to find -L" '[[ $ret =~ -L ]]'
cd ../../
tearDownSetup
}
# The difference between say, complain, warning, and success it is the color
# because of this we test all of them together
function cmdManagerSAY_COMPLAIN_WARNING_SUCCESS_Test
{
setupFakeKernelRepo
cd "tests/.tmp"
ret=$(cmd_manager ls)
assertTrue "We expected to find the ls command" '[[ $ret =~ ls ]]'
assertTrue "We expected to find MAINTAINERS" '[[ $ret =~ MAINTAINERS ]]'
assertTrue "We expected to find arch" '[[ $ret =~ arch ]]'
assertTrue "We expected to find scripts" '[[ $ret =~ scripts ]]'
# TODO: There's an alternative to discover the color?
ret=$(cmd_manager COMPLAIN pwd --help)
assertTrue "We expected to find the ls command" '[[ $ret =~ --help ]]'
assertTrue "We expected to find -P" '[[ $ret =~ -P ]]'
assertTrue "We expected to find -L" '[[ $ret =~ -L ]]'
ret=$(cmd_manager WARNING ls)
assertTrue "We expected to find the ls command" '[[ $ret =~ ls ]]'
assertTrue "We expected to find MAINTAINERS" '[[ $ret =~ MAINTAINERS ]]'
assertTrue "We expected to find arch" '[[ $ret =~ arch ]]'
assertTrue "We expected to find scripts" '[[ $ret =~ scripts ]]'
ret=$(cmd_manager SUCCESS ls)
assertTrue "We expected to find the ls command" '[[ $ret =~ ls ]]'
assertTrue "We expected to find MAINTAINERS" '[[ $ret =~ MAINTAINERS ]]'
assertTrue "We expected to find arch" '[[ $ret =~ arch ]]'
assertTrue "We expected to find scripts" '[[ $ret =~ scripts ]]'
cd ../../
tearDownSetup
}
function cmdManagerTESTMODETest
{
ret=$(cmd_manager TEST_MODE pwd)
assertEquals "Expected pwd, but we got $ret" "$ret" "pwd"
ret=$(cmd_manager TEST_MODE ls -lah)
assertEquals "Expected ls -lah, but we got $ret" "$ret" "ls -lah"
}
function detectDistroTest
{
setupFakeOSInfo
local root_path="tests/.tmp/detect_distro/arch"
local ret=$(detect_distro $root_path)
assertEquals "We got $ret." "$ret" "arch"
root_path="tests/.tmp/detect_distro/debian"
ret=$(detect_distro $root_path)
assertEquals "We got $ret." "$ret" "debian"
root_path="tests/.tmp/detect_distro/debian/etc/lala"
ret=$(detect_distro $root_path)
assertEquals "We got $ret." "$ret" "none"
}
function joinPathTest
{
local base="/lala/xpto"
local ret=$(join_path "/lala" "///xpto")
assertEquals "Expect /lala/xpto" "$ret" "$base"
ret=$(join_path "/lala" "/xpto////")
assertEquals "Expect /lala/xpto" "$ret" "$base"
ret=$(join_path "/lala" "////xpto////")
assertEquals "Expect /lala/xpto" "$ret" "$base"
ret=$(join_path "/lala/")
assertEquals "Expect /lala/" "$ret" "/lala/"
}
function findKernelRootTest
{
setupFakeKernelRepo
local fake_path="tests/.tmp/lala/xpto"
mkdir -p $fake_path
local kernel_path=$(find_kernel_root $fake_path)
assertEquals "We expected to find a kernel path" "$kernel_path" "tests/.tmp"
kernel_path=$(find_kernel_root "/tmp")
assertEquals "We should not find a path" "$kernel_path" ""
kernel_path=$(find_kernel_root "test/")
assertEquals "We should not find a path" "$kernel_path" ""
tearDownSetup
}
function isAPatchTest
{
setupPatch
is_a_patch "tests/.tmp/test.patch"
[[ "$?" != 0 ]] && fail "Failed to check if a file is a patch."
tearDownSetup
true # Reset return value
}
invoke_shunit
|