Skip to content

Commit

Permalink
add testing via TestPythonSML.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowJonathan authored and garfieldnate committed May 17, 2024
1 parent d63d7ee commit 7cb7ac7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
32 changes: 27 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ jobs:
fail-fast: true
matrix:
os: [
ubuntu-latest,
# 20.04 to preserve compatibility with testing stage
ubuntu-20.04,
# latest available X86_64 target
macos-12,
# latest is ARM
Expand Down Expand Up @@ -322,6 +323,19 @@ jobs:
with:
package-dir: Core/ClientSMLSWIG/Python/

- name: Ensure unit tests built (ubuntu-20.04)
if: matrix.os == 'ubuntu-20.04'
# On Ubuntu, we run cibuildwheel inside docker containers, which don't retain the outputs.
# However, we want out/SoarUnitTests/ to be availible for future tests, so we build it again here.
run: python scons/scons.py tests

# Save out/SoarUnitTests/
- uses: actions/cache/save@v3
id: cache
with:
path: out/SoarUnitTests/
key: ${{ runner.os }}-${{ github.sha }}

- name: ABI3Audit (*nix)
if: matrix.os != 'windows-latest'
run: pipx run abi3audit -S -v wheelhouse/*
Expand Down Expand Up @@ -378,6 +392,9 @@ jobs:
- Posix
- Windows
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: actions/download-artifact@v4
with:
pattern: cibw-wheels-*
Expand All @@ -392,14 +409,19 @@ jobs:
# Older python versions fail install if we dont do this: https://github.com/actions/setup-python/issues/866
PIP_TRUSTED_HOST: "pypi.python.org pypi.org files.pythonhosted.org"

# Restore out/SoarUnitTests/
- uses: actions/cache/restore@v3
id: cache
with:
path: out/SoarUnitTests/
key: ${{ runner.os }}-${{ github.sha }}
fail-on-cache-miss: true

- name: Run python test command
# TODO: replace with actual test command: https://github.com/SoarGroup/Soar/issues/460
run: |
pip3.${{ matrix.python-minor }} install soar-sml -f wheelhouse --no-index
python3.${{ matrix.python-minor }} -c "import soar_sml;
k=soar_sml.Kernel.CreateKernelInNewThread();
a=k.CreateAgent('soar');
assert(a.ExecuteCommandLine('echo hello world').strip()=='hello world')"
python3.${{ matrix.python-minor }} Core/ClientSMLSWIG/Python/TestPythonSML.py
python_push_dev:
name: Publish to test.pypi.org
Expand Down
30 changes: 19 additions & 11 deletions Core/ClientSMLSWIG/Python/TestPythonSML.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,40 @@
# several kinds of callbacks, reinitializing, agent destruction, and kernel
# destruction (and maybe some other things, too).
#
from dataclasses import dataclass
# This file needs to be compatible with python 3.5, to run on CI jobs testing the lowest supported python version.
from pathlib import Path
import sys
import os
import time

import Python_sml_ClientInterface
from Python_sml_ClientInterface import *
try:
import Python_sml_ClientInterface
from Python_sml_ClientInterface import *

AGENT_DIR = Path(Python_sml_ClientInterface.__file__).parent / 'SoarUnitTests'
BASE_DIR = Path(Python_sml_ClientInterface.__file__).parent
except ImportError:
from soar_sml import *

BASE_DIR = Path(os.environ.get("SOAR_UNIT_TEST_BASE_DIR", os.path.abspath("out/")))


AGENT_DIR = BASE_DIR / 'SoarUnitTests'

@dataclass
class CalledSignal:
called: bool = False
called = False

# towers_of_hanoi_file = AGENT_DIR / 'test-towers-of-hanoi-SML.soar'
towers_of_hanoi_file = AGENT_DIR / 'Chunking' / 'tests' / 'towers-of-hanoi-recursive' / 'towers-of-hanoi-recursive' / 'towers-of-hanoi-recursive_source.soar'
toh_test_file = AGENT_DIR / 'TOHtest.soar'
for source_file in (towers_of_hanoi_file, toh_test_file):
if not source_file.is_file():
raise FileNotFoundError(f"Source file doesn't exist: {source_file}")
raise FileNotFoundError("Source file doesn't exist: %s" % source_file)

def PrintCallback(id, userData, agent, message):
print(message)

def ProductionExcisedCallback(id, signal: CalledSignal, agent, prodName, instantiation):
print(f"removing {prodName} ({instantiation})")
print("removing %s (%s)" % (prodName, instantiation))
signal.called = True

def ProductionFiredCallback(id, signal: CalledSignal, agent, prodName, instantiation):
Expand Down Expand Up @@ -72,7 +80,7 @@ def UpdateEventCallback(id, signal: CalledSignal, kernel, runFlags):

def UserMessageCallback(id, tester, agent, clientName, message):
print("Agent", agent.GetAgentName(), "received usermessage event for clientName '", clientName, "' with message '", message, "'")
assert tester(clientName, message), f"❌ UserMessageCallback called with unexpected clientName '{clientName}' or message '{message}'"
assert tester(clientName, message), ("❌ UserMessageCallback called with unexpected clientName '%s' or message '%s'" % (clientName, message))
return ""

kernel = Kernel.CreateKernelInNewThread()
Expand Down Expand Up @@ -148,7 +156,7 @@ def test_excise(kernel):
assert not prod_removed_handler_signal.called, "❌ Production excise handler called before excise"
result = kernel.ExecuteCommandLine("excise towers-of-hanoi*monitor*operator-execution*move-disk", "Soar1")
assert prod_removed_handler_signal.called, "❌ Production excise handler not called"
print(f"✅ Production excise: {result}")
print("✅ Production excise: %s" % result)

test_excise(kernel)

Expand All @@ -161,7 +169,7 @@ def test_excise(kernel):
def check_rhs_handler_called(kernel):
s1 = kernel.ExecuteCommandLine("print s1", "Soar1")
if s1.find("^rhstest success") == -1:
print(f"\n❌RHS test FAILED; s1 is {s1}", file=sys.stderr)
print("\n❌RHS test FAILED; s1 is %s" % s1, file=sys.stderr)
sys.exit(1)
else:
print("\n✅RHS test PASSED")
Expand Down
24 changes: 11 additions & 13 deletions Core/ClientSMLSWIG/Python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,22 @@ skip = [
build = "cp39*"
# Inside CIs, we want to build the limited API, as we control SWIG's version there, so we opt-in.
environment = { SOAR_PYTHON_ABI3="1" }
# This test command:
# - imports soar_sml
# - creates the kernel
# - creates an agent
# - executes a command on that agent
# - fails if the result of this command isn't as expected
# to test if Soar properly boots and runs.
test-command = """\
python -c "import soar_sml;\
k=soar_sml.Kernel.CreateKernelInNewThread();\
a=k.CreateAgent('soar');\
assert(a.ExecuteCommandLine('echo hello world').strip()=='hello world')"
"""
# Create the unit tests under out/SoarUnitTests/
before-test = "python scons/scons.py tests"
# This test command will ruin the unit tests to make sure the build succeeded without oddities.
test-command = "SOAR_UNIT_TEST_BASE_DIR={project}/out/ python {project}/Core/ClientSMLSWIG/Python/TestPythonSML.py"

# Skip testing python 3.8 on ARM64. This is due to a limitation with cibuildwheel:
# https://github.com/pypa/cibuildwheel/pull/1169#issuecomment-1178727618
test-skip = "cp38-macosx_*:arm64"

[tool.cibuildwheel.windows]
# For windows we need a slightly different command
test-command = """\
set SOAR_UNIT_TEST_BASE_DIR={project}/out/
python {project}/Core/ClientSMLSWIG/Python/TestPythonSML.py
"""

[tool.cibuildwheel.linux]
# Specific instructions for cibuildwheel when running on linux:
# - add the /project/out directory for the linker to look at.
Expand Down

0 comments on commit 7cb7ac7

Please sign in to comment.