mirror of https://github.com/mgba-emu/mgba.git
Python: Clean up build and tests, add VFS tests
This commit is contained in:
parent
3f94175189
commit
0ba31c4e67
|
@ -11,14 +11,14 @@ endforeach()
|
||||||
|
|
||||||
file(GLOB PYTHON_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
|
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)
|
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
|
add_custom_command(OUTPUT lib.c
|
||||||
COMMAND BINDIR=${CMAKE_CURRENT_BINARY_DIR}/.. CPPFLAGS="${INCLUDE_FLAGS}" ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/_builder.py
|
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_
|
||||||
set(PYTHON_LIBRARY ${BINARY_NAME}-pylib PARENT_SCOPE)
|
set(PYTHON_LIBRARY ${BINARY_NAME}-pylib PARENT_SCOPE)
|
||||||
|
|
||||||
add_custom_target(${BINARY_NAME}-py ALL
|
add_custom_target(${BINARY_NAME}-py ALL
|
||||||
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py build
|
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py egg_info -e ${CMAKE_CURRENT_BINARY_DIR} ${PYLIB_VERSION}
|
||||||
DEPENDS ${BINARY_NAME}-pylib ${CMAKE_CURRENT_BINARY_DIR}/build/lib/${BINARY_NAME}/__init__.py)
|
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)
|
file(GLOB BASE_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/test_*.py)
|
||||||
foreach(TEST IN LISTS TESTS)
|
file(GLOB SUBTESTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/*/test_*.py)
|
||||||
|
foreach(TEST IN LISTS BASE_TESTS SUBTESTS)
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
set(PATH DYLD_LIBRARY_PATH)
|
set(PATH DYLD_LIBRARY_PATH)
|
||||||
elseif(WIN32)
|
elseif(WIN32)
|
||||||
|
@ -46,8 +53,8 @@ foreach(TEST IN LISTS TESTS)
|
||||||
else()
|
else()
|
||||||
set(PATH LD_LIBRARY_PATH)
|
set(PATH LD_LIBRARY_PATH)
|
||||||
endif()
|
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}")
|
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}/..")
|
set_tests_properties(python-${TEST_NAME} PROPERTIES ENVIRONMENT "${PATH}=${CMAKE_CURRENT_BINARY_DIR}/..")
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
|
@ -11,6 +11,7 @@ import os
|
||||||
def _vfpClose(vf):
|
def _vfpClose(vf):
|
||||||
vfp = ffi.cast("struct VFilePy*", vf)
|
vfp = ffi.cast("struct VFilePy*", vf)
|
||||||
ffi.from_handle(vfp.fileobj).close()
|
ffi.from_handle(vfp.fileobj).close()
|
||||||
|
return True
|
||||||
|
|
||||||
@ffi.def_extern()
|
@ffi.def_extern()
|
||||||
def _vfpSeek(vf, offset, whence):
|
def _vfpSeek(vf, offset, whence):
|
||||||
|
@ -93,14 +94,17 @@ def openPath(path, mode="r"):
|
||||||
if "x" in mode[1:]:
|
if "x" in mode[1:]:
|
||||||
flags |= os.O_EXCL
|
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:
|
class VFile:
|
||||||
def __init__(self, vf):
|
def __init__(self, vf):
|
||||||
self.handle = vf
|
self.handle = vf
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
return self.handle.close(self.handle)
|
return bool(self.handle.close(self.handle))
|
||||||
|
|
||||||
def seek(self, offset, whence):
|
def seek(self, offset, whence):
|
||||||
return self.handle.seek(self.handle, offset, whence)
|
return self.handle.seek(self.handle, offset, whence)
|
||||||
|
|
|
@ -17,7 +17,7 @@ classifiers = [
|
||||||
]
|
]
|
||||||
|
|
||||||
setup(name="${BINARY_NAME}",
|
setup(name="${BINARY_NAME}",
|
||||||
version=re.sub("/", "-", "${VERSION_STRING}"),
|
version="${LIB_VERSION_STRING}",
|
||||||
author="Jeffrey Pfau",
|
author="Jeffrey Pfau",
|
||||||
author_email="jeffrey@endrift.com",
|
author_email="jeffrey@endrift.com",
|
||||||
url="http://github.com/mgba-emu/mgba/",
|
url="http://github.com/mgba-emu/mgba/",
|
||||||
|
@ -25,7 +25,7 @@ setup(name="${BINARY_NAME}",
|
||||||
setup_requires=['cffi>=1.6', 'pytest-runner'],
|
setup_requires=['cffi>=1.6', 'pytest-runner'],
|
||||||
install_requires=['cffi>=1.6', 'cached-property'],
|
install_requires=['cffi>=1.6', 'cached-property'],
|
||||||
extras_require={'pil': ['Pillow>=2.3'], 'cinema': ['pyyaml', 'pytest']},
|
extras_require={'pil': ['Pillow>=2.3'], 'cinema': ['pyyaml', 'pytest']},
|
||||||
tests_require=['Pillow>=2.3', 'pyyaml', 'pytest'],
|
tests_require=['pytest'],
|
||||||
cffi_modules=["_builder.py:ffi"],
|
cffi_modules=["_builder.py:ffi"],
|
||||||
license="MPL 2.0",
|
license="MPL 2.0",
|
||||||
classifiers=classifiers
|
classifiers=classifiers
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
def test_core_import():
|
||||||
|
try:
|
||||||
|
import mgba.core
|
||||||
|
except:
|
||||||
|
raise AssertionError
|
|
@ -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
|
Loading…
Reference in New Issue