Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Build Amulet-IO
run: |
pip install .[dev]
python tests/compile.py
python tools/compile_tests.py

- name: Test with unittest
run: python -m unittest discover -v -s tests
41 changes: 41 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.13)

project(amulet_io LANGUAGES CXX)

# Set C++20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Set platform variables
if (WIN32)
# set windows 7 as the minimum version
add_definitions(-D_WIN32_WINNT=0x0601)
elseif(APPLE)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15")
else()
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

if (MSVC)
add_definitions("/MP")
endif()

# Find C++ files
file(REAL_PATH src SOURCE_PATH)
file(GLOB_RECURSE HEADERS LIST_DIRECTORIES false ${SOURCE_PATH}/amulet/*.hpp)

# Add implementation
add_library(amulet_io INTERFACE)
target_include_directories(amulet_io INTERFACE "src")
target_sources(amulet_io PRIVATE ${HEADERS})
foreach(FILE ${HEADERS})
file(RELATIVE_PATH REL_PATH ${SOURCE_PATH} ${FILE})
get_filename_component(GROUP ${REL_PATH} DIRECTORY)
string(REPLACE "/" "\\" GROUP ${GROUP})
source_group(${GROUP} FILES ${FILE})
endforeach()

if (TEST_AMULET_IO_DIR)
add_subdirectory(tests)
endif()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dev = [
"versioneer",
"wheel",
"pybind11[global]==2.13.6",
"amulet-test-utils~=1.0",
"black>=22.3",
]

Expand Down
6 changes: 4 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ endif()
# Find dependencies
find_package(pybind11 CONFIG REQUIRED)
find_package(amulet_io CONFIG REQUIRED)
find_package(amulet_test_utils CONFIG REQUIRED)

pybind11_add_module(_test_amulet_io "_test_amulet_io.py.cpp")
pybind11_add_module(_test_amulet_io "test_amulet_io/_test_amulet_io.py.cpp")
target_compile_definitions(_test_amulet_io PRIVATE PYBIND11_DETAILED_ERROR_MESSAGES)
target_link_libraries(_test_amulet_io PRIVATE amulet_io)
target_link_libraries(_test_amulet_io PRIVATE amulet_test_utils)

# Install
install(TARGETS _test_amulet_io DESTINATION ".")
install(TARGETS _test_amulet_io DESTINATION ${TEST_AMULET_IO_DIR})
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,13 @@

#include <string>

#include <amulet/test_utils/test_utils.hpp>

#include <amulet/io/binary_reader.hpp>
#include <amulet/io/binary_writer.hpp>

namespace py = pybind11;

template <typename T>
std::string cast_to_string(const T& obj)
{
if constexpr (std::is_arithmetic_v<T>) {
return std::to_string(obj);
} else if constexpr (std::is_same_v<T, std::string> || std::is_convertible_v<T, std::string>) {
return obj;
} else {
return "";
}
}

#define ASSERT_EQUAL(CLS, A, B) \
{ \
CLS a; \
try { \
a = A; \
} catch (const std::exception& e) { \
std::string msg; \
msg.reserve(200); \
msg += "Failed evaluating A in file "; \
msg += __FILE__; \
msg += " at line "; \
msg += std::to_string(__LINE__); \
msg += ". "; \
msg += e.what(); \
throw std::runtime_error(msg); \
} \
CLS b; \
try { \
b = B; \
} catch (const std::exception& e) { \
std::string msg; \
msg.reserve(200); \
msg += "Failed evaluating B in file "; \
msg += __FILE__; \
msg += " at line "; \
msg += std::to_string(__LINE__); \
msg += ". "; \
msg += e.what(); \
throw std::runtime_error(msg); \
} \
if (a != b) { \
std::string msg; \
msg.reserve(200); \
msg += "Values are not equal in file "; \
msg += __FILE__; \
msg += " at line "; \
msg += std::to_string(__LINE__); \
msg += "."; \
auto a_msg = cast_to_string(a); \
if (!a_msg.empty()) { \
msg += " Expected \"" + a_msg + "\"."; \
} \
auto b_msg = cast_to_string(b); \
if (!a_msg.empty()) { \
msg += " Got \"" + b_msg + "\"."; \
} \
throw std::runtime_error(msg); \
} \
}

#define ASSERT_RAISES(EXC, A) \
{ \
bool err_raised = false; \
try { \
A; \
} catch (const EXC&) { \
err_raised = true; \
} catch (...) { \
std::string msg; \
msg.reserve(200); \
msg += "Other exception raised in file "; \
msg += __FILE__; \
msg += " at line "; \
msg += std::to_string(__LINE__); \
msg += ". "; \
throw std::runtime_error(msg); \
} \
if (!err_raised) { \
std::string msg; \
msg.reserve(200); \
msg += "Exception not raised in file "; \
msg += __FILE__; \
msg += " at line "; \
msg += std::to_string(__LINE__); \
msg += ". "; \
throw std::runtime_error(msg); \
} \
}

static const std::string NumericReadBufferBig("\x01\x00\x02\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x3f\x80\x00\x00\x3f\xf0\x00\x00\x00\x00\x00\x00", 27);
static const std::string NumericReadBufferLittle("\x01\x02\x00\x03\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x3f\x00\x00\x00\x00\x00\x00\xf0\x3f", 27);

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from _test_amulet_io import (
from test_amulet_io._test_amulet_io import (
EndianState,
test_read_numeric,
test_read_string,
Expand Down
49 changes: 49 additions & 0 deletions tools/cmake_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sys
import subprocess
import os
import shutil

import pybind11
import amulet.test_utils


def fix_path(path: str) -> str:
return os.path.realpath(path).replace(os.sep, "/")


RootDir = fix_path(os.path.dirname(os.path.dirname(__file__)))


def main():
platform_args = []
if sys.platform == "win32":
platform_args.extend(["-G", "Visual Studio 17 2022"])
if sys.maxsize > 2**32:
platform_args.extend(["-A", "x64"])
else:
platform_args.extend(["-A", "Win32"])
platform_args.extend(["-T", "v143"])

os.chdir(RootDir)
shutil.rmtree(os.path.join(RootDir, "build", "CMakeFiles"), ignore_errors=True)

if subprocess.run(
[
"cmake",
*platform_args,
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-Dpybind11_DIR={fix_path(pybind11.get_cmake_dir())}",
f"-Damulet_io_DIR={os.path.join(RootDir, 'src', 'amulet', 'io')}",
f"-DCMAKE_INSTALL_PREFIX=install",
# test args
f"-Damulet_test_utils_DIR={fix_path(amulet.test_utils.__path__[0])}",
f"-DTEST_AMULET_IO_DIR={os.path.join(RootDir, 'tests', 'test_amulet_io')}",
"-B",
"build",
]
).returncode:
raise RuntimeError("Error configuring amulet_utils")


if __name__ == "__main__":
main()
38 changes: 16 additions & 22 deletions tests/compile.py → tools/compile_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,21 @@
import sys
import shutil
import os
import importlib.util

import pybind11
import amulet.io
import amulet.test_utils


def get_package_path(name: str) -> str:
try:
spec = importlib.util.find_spec(name)
if spec is None:
raise RuntimeError(f"Could not find {name}")
module_path = spec.origin
if module_path is None:
raise RuntimeError(f"Could not find {name}")
if not module_path.endswith("__init__.py"):
raise RuntimeError(f"{name} is not a package.")
return os.path.dirname(module_path).replace(os.sep, "/")
except:
print(f"Failed finding {name}. Falling back to importing.")
return importlib.import_module(name).__path__[0].replace(os.sep, "/")
def fix_path(path: str) -> str:
return os.path.realpath(path).replace(os.sep, "/")


def main() -> None:
os.chdir(os.path.dirname(__file__))
RootDir = os.path.dirname(os.path.dirname(__file__))
TestsDir = os.path.join(RootDir, "tests")

if os.path.isdir("build/CMakeFiles"):
shutil.rmtree("build/CMakeFiles")

def main() -> None:
platform_args = []
if sys.platform == "win32":
platform_args.extend(["-G", "Visual Studio 17 2022"])
Expand All @@ -38,14 +26,20 @@ def main() -> None:
platform_args.extend(["-A", "Win32"])
platform_args.extend(["-T", "v143"])

os.chdir(TestsDir)
shutil.rmtree(os.path.join(TestsDir, "build", "CMakeFiles"), ignore_errors=True)

if subprocess.run(
[
"cmake",
*platform_args,
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-Dpybind11_DIR={pybind11.get_cmake_dir().replace(os.sep, '/')}",
f"-Damulet_io_DIR={get_package_path('amulet.io')}",
f"-DCMAKE_INSTALL_PREFIX={os.path.dirname(__file__).replace(os.sep, '/')}",
f"-Dpybind11_DIR={fix_path(pybind11.get_cmake_dir())}",
f"-Damulet_io_DIR={fix_path(amulet.io.__path__[0])}",
f"-DCMAKE_INSTALL_PREFIX=install",
# test args
f"-Damulet_test_utils_DIR={fix_path(amulet.test_utils.__path__[0])}",
f"-DTEST_AMULET_IO_DIR={fix_path(os.path.join(TestsDir, 'test_amulet_io'))}",
"-B",
"build",
]
Expand Down