Skip to content

Commit

Permalink
Run basic native tests using hl2sdk-mock in CI (#2015)
Browse files Browse the repository at this point in the history
This tests the sorting.inc API similar to the existing test plugin in
the plugins/testsuite folder. Tests fail if the srcds output contains "FAIL".
  • Loading branch information
peace-maker committed Jul 12, 2023
1 parent cb85415 commit 2ef874d
Show file tree
Hide file tree
Showing 3 changed files with 462 additions and 1 deletion.
104 changes: 104 additions & 0 deletions .github/workflows/mocktest.yml
@@ -0,0 +1,104 @@
name: hl2sdk-mock tests
on:
push:
branches:
- master
- '[0-9]+.[0-9]+-dev'
pull_request:
branches:
- master
- '[0-9]+.[0-9]+-dev'
jobs:
mock:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
name: Clone sourcemod
with:
submodules: recursive
path: sourcemod

- uses: actions/checkout@v3
name: Clone metamod-source
with:
repository: alliedmodders/metamod-source
submodules: recursive
path: metamod-source

- uses: actions/checkout@v3
name: Clone hl2sdk-mock
with:
repository: alliedmodders/hl2sdk-mock
submodules: recursive
path: hl2sdk-mock

- uses: actions/setup-python@v4
name: Setup Python 3.10
with:
python-version: "3.10"

- name: Install AMBuild
run: |
python -m pip install --upgrade pip setuptools wheel
pip install git+https://github.com/alliedmodders/ambuild
- name: Build MetaMod:Source
working-directory: metamod-source
run: |
python configure.py --enable-optimize --sdks=mock --targets=x86_64
ambuild objdir
- name: Build SourceMod
working-directory: sourcemod
run: |
python configure.py --no-mysql --enable-optimize --sdks=mock --targets=x86_64
ambuild objdir
- name: Build hl2sdk-mock
working-directory: hl2sdk-mock
run: |
python configure.py --enable-optimize --targets=x86_64
ambuild objdir
- name: Setup gamedir
working-directory: hl2sdk-mock
shell: bash
run: |
mkdir ../gamedir
./build_gamedir.sh ../gamedir ../metamod-source/objdir/package
./build_gamedir.sh ../gamedir ../sourcemod/objdir/package
- name: Compile testsuite
working-directory: hl2sdk-mock
shell: bash
run: |
mkdir ../gamedir/addons/sourcemod/plugins/optional
for f in ../sourcemod/plugins/testsuite/mock/*.sp; do
echo "Compiling $(basename $f)"
../gamedir/addons/sourcemod/scripting/spcomp64 -i ../gamedir/addons/sourcemod/scripting/include -o "../gamedir/addons/sourcemod/plugins/optional/$(basename $f .sp).smx" -E "$f"
done
- name: Test
working-directory: hl2sdk-mock
shell: bash
run: |
for f in ../gamedir/addons/sourcemod/plugins/optional/*.smx; do
echo "==================================="
echo "Running $(basename $f)..."
echo "==================================="
timeout 60 ./objdir/dist/x86_64/srcds -game_dir ../gamedir +map de_thunder -command "sm plugins load optional/$(basename $f)" -run -run-ticks 20 |
{
failed=0
while IFS= read -r line; do
echo "$line"
if [[ "$line" == *"FAIL"* ]]; then
failed=1
fi
done
if [ "$failed" = "1" ]; then
echo "$(basename $f) failed."
exit 1
fi
}
done
50 changes: 49 additions & 1 deletion plugins/include/testing.inc
Expand Up @@ -38,7 +38,7 @@ stock void SetTestContext(const char[] context)
strcopy(TestContext, sizeof(TestContext), context);
}

stock void AssertEq(const char[] text, int cell1, int cell2)
stock void AssertEq(const char[] text, any cell1, any cell2)
{
TestNumber++;
if (cell1 == cell2)
Expand All @@ -52,6 +52,39 @@ stock void AssertEq(const char[] text, int cell1, int cell2)
}
}

stock void AssertArrayEq(const char[] text, const any[] value, const any[] expected, int len)
{
TestNumber++;
for (int i = 0; i < len; ++i)
{
if (value[i] != expected[i])
{
PrintToServer("[%d] %s FAIL: %s should be %d at index %d, got %d", TestNumber, TestContext, text, expected[i], i, value[i]);
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
break;
}
}
PrintToServer("[%d] %s: '%s' arrays are equal OK", TestNumber, TestContext, text);
}

stock void AssertArray2DEq(const char[] text, const any[][] value, const any[][] expected, int len, int innerlen)
{
TestNumber++;
for (int i=0; i < len; ++i)
{
for (int j=0; j < innerlen; ++j)
{
if (value[i][j] != expected[i][j])
{
PrintToServer("[%d] %s FAIL: %s should be %d at index [%d][%d], got %d", TestNumber, TestContext, text, expected[i][j], i, j, value[i][j]);
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
break;
}
}
}
PrintToServer("[%d] %s: '%s' 2D arrays are equal OK", TestNumber, TestContext, text);
}

stock void AssertFalse(const char[] text, bool value)
{
TestNumber++;
Expand Down Expand Up @@ -93,3 +126,18 @@ stock void AssertStrEq(const char[] text, const char[] value, const char[] expec
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
}
}

stock void AssertStrArrayEq(const char[] text, const char[][] value, const char[][] expected, int len)
{
TestNumber++;
for (int i = 0; i < len; ++i)
{
if (!StrEqual(value[i], expected[i]))
{
PrintToServer("[%d] %s FAIL: %s should be '%s' at index %d, got '%s'", TestNumber, TestContext, text, expected[i], i, value[i]);
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
break;
}
}
PrintToServer("[%d] %s: '%s' arrays are equal OK", TestNumber, TestContext, text);
}

0 comments on commit 2ef874d

Please sign in to comment.