Python: Clean up build and tests, add VFS tests
Vicki Pfau vi@endrift.com
Sun, 13 Aug 2017 17:34:05 -0700
7 files changed,
92 insertions(+),
17 deletions(-)
M
src/platform/python/CMakeLists.txt
→
src/platform/python/CMakeLists.txt
@@ -11,14 +11,14 @@ endforeach()
file(GLOB PYTHON_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h) +if(NOT GIT_TAG) + if(GIT_BRANCH STREQUAL "master" OR NOT GIT_BRANCH) + set(PYLIB_VERSION -b -${GIT_REV}-${GIT_COMMIT_SHORT}) + else() + set(PYLIB_VERSION -b -${GIT_BRANCH}-${GIT_REV}-${GIT_COMMIT_SHORT}) + endif() +endif() configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in ${CMAKE_CURRENT_BINARY_DIR}/setup.py) -add_custom_command(OUTPUT build/lib/${BINARY_NAME}/__init__.py - COMMAND BINDIR=${CMAKE_CURRENT_BINARY_DIR}/.. CPPFLAGS="${INCLUDE_FLAGS}" ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py build --build-base ${CMAKE_CURRENT_BINARY_DIR} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS ${BINARY_NAME} - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/setup.py - DEPENDS ${PYTHON_HEADERS} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_builder.py) add_custom_command(OUTPUT lib.c COMMAND BINDIR=${CMAKE_CURRENT_BINARY_DIR}/.. CPPFLAGS="${INCLUDE_FLAGS}" ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/_builder.py@@ -34,11 +34,18 @@ set_target_properties(${BINARY_NAME}-pylib PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
set(PYTHON_LIBRARY ${BINARY_NAME}-pylib PARENT_SCOPE) add_custom_target(${BINARY_NAME}-py ALL - COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py build - DEPENDS ${BINARY_NAME}-pylib ${CMAKE_CURRENT_BINARY_DIR}/build/lib/${BINARY_NAME}/__init__.py) + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py egg_info -e ${CMAKE_CURRENT_BINARY_DIR} ${PYLIB_VERSION} + COMMAND BINDIR=${CMAKE_CURRENT_BINARY_DIR}/.. CPPFLAGS="${INCLUDE_FLAGS}" ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py build -b ${CMAKE_CURRENT_BINARY_DIR} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS ${BINARY_NAME} + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/setup.py + DEPENDS ${PYTHON_HEADERS} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_builder.py + DEPENDS ${BINARY_NAME}-pylib) -file(GLOB TESTS ${CMAKE_CURRENT_SOURCE_DIR}/test_*.py) -foreach(TEST IN LISTS TESTS) +file(GLOB BASE_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/test_*.py) +file(GLOB SUBTESTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/*/test_*.py) +foreach(TEST IN LISTS BASE_TESTS SUBTESTS) if(APPLE) set(PATH DYLD_LIBRARY_PATH) elseif(WIN32)@@ -46,8 +53,8 @@ set(PATH PATH)
else() set(PATH LD_LIBRARY_PATH) endif() - string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/test_" "" TEST_NAME "${TEST}") + string(REGEX REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/(tests/.*/)?test_" "" TEST_NAME "${TEST}") string(REPLACE ".py" "" TEST_NAME "${TEST_NAME}") - add_test(python-${TEST_NAME} ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py pytest --addopts ${TEST}) + add_test(python-${TEST_NAME} ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py build -b ${CMAKE_CURRENT_BINARY_DIR} pytest --extras --addopts ${TEST}) set_tests_properties(python-${TEST_NAME} PROPERTIES ENVIRONMENT "${PATH}=${CMAKE_CURRENT_BINARY_DIR}/..") endforeach()
M
src/platform/python/mgba/vfs.py
→
src/platform/python/mgba/vfs.py
@@ -11,6 +11,7 @@ @ffi.def_extern()
def _vfpClose(vf): vfp = ffi.cast("struct VFilePy*", vf) ffi.from_handle(vfp.fileobj).close() + return True @ffi.def_extern() def _vfpSeek(vf, offset, whence):@@ -93,14 +94,17 @@ flags |= os.O_RDWR
if "x" in mode[1:]: flags |= os.O_EXCL - return VFile(lib.VFileOpen(path.encode("UTF-8"), flags)) + vf = lib.VFileOpen(path.encode("UTF-8"), flags); + if vf == ffi.NULL: + return None + return VFile(vf) class VFile: def __init__(self, vf): self.handle = vf def close(self): - return self.handle.close(self.handle) + return bool(self.handle.close(self.handle)) def seek(self, offset, whence): return self.handle.seek(self.handle, offset, whence)
M
src/platform/python/setup.py.in
→
src/platform/python/setup.py.in
@@ -17,7 +17,7 @@ "Topic :: System :: Emulators"
] setup(name="${BINARY_NAME}", - version=re.sub("/", "-", "${VERSION_STRING}"), + version="${LIB_VERSION_STRING}", author="Jeffrey Pfau", author_email="jeffrey@endrift.com", url="http://github.com/mgba-emu/mgba/",@@ -25,7 +25,7 @@ packages=["mgba"],
setup_requires=['cffi>=1.6', 'pytest-runner'], install_requires=['cffi>=1.6', 'cached-property'], extras_require={'pil': ['Pillow>=2.3'], 'cinema': ['pyyaml', 'pytest']}, - tests_require=['Pillow>=2.3', 'pyyaml', 'pytest'], + tests_require=['pytest'], cffi_modules=["_builder.py:ffi"], license="MPL 2.0", classifiers=classifiers
A
src/platform/python/tests/mgba/test_core.py
@@ -0,0 +1,7 @@
+import pytest + +def test_core_import(): + try: + import mgba.core + except: + raise AssertionError
A
src/platform/python/tests/mgba/test_vfs.py
@@ -0,0 +1,57 @@
+import pytest +import os + +import mgba.vfs as vfs +from mgba._pylib import ffi + +def test_vfs_open(): + with open(__file__) as f: + vf = vfs.open(f) + assert vf + assert vf.close() + +def test_vfs_openPath(): + vf = vfs.openPath(__file__) + assert vf + assert vf.close() + +def test_vfs_read(): + vf = vfs.openPath(__file__) + buffer = ffi.new('char[13]') + assert vf.read(buffer, 13) == 13 + assert ffi.string(buffer) == 'import pytest' + vf.close() + +def test_vfs_readline(): + vf = vfs.openPath(__file__) + buffer = ffi.new('char[16]') + linelen = vf.readline(buffer, 16) + assert linelen in (14, 15) + if linelen == 14: + assert ffi.string(buffer) == 'import pytest\n' + elif linelen == 15: + assert ffi.string(buffer) == 'import pytest\r\n' + vf.close() + +def test_vfs_readAllSize(): + vf = vfs.openPath(__file__) + buffer = vf.readAll() + assert buffer + assert len(buffer) + assert len(buffer) == vf.size() + vf.close() + +def test_vfs_seek(): + vf = vfs.openPath(__file__) + assert vf.seek(0, os.SEEK_SET) == 0 + assert vf.seek(1, os.SEEK_SET) == 1 + assert vf.seek(1, os.SEEK_CUR) == 2 + assert vf.seek(-1, os.SEEK_CUR) == 1 + assert vf.seek(0, os.SEEK_CUR) == 1 + assert vf.seek(0, os.SEEK_END) == vf.size() + assert vf.seek(-1, os.SEEK_END) == vf.size() -1 + vf.close() + +def test_vfs_openPath_invalid(): + vf = vfs.openPath('.invalid') + assert not vf