Skip to content

Commit 8de76e3

Browse files
committed
Improve test example.
Demonstrate usage of pytest_discover_tests function with multiple dependent libraries and environment variables.
1 parent dc0efce commit 8de76e3

12 files changed

Lines changed: 143 additions & 31 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: tests
22

33
on:
44
push:
5-
branches: [ main, dev ]
5+
branches: [ main ]
66

77
pull_request:
88
branches: [ main ]

example/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ find_package(Python COMPONENTS Interpreter Development REQUIRED)
66
set(_py_version ${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
77
mark_as_advanced(_py_version)
88

9+
if (WIN32)
10+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
11+
endif()
12+
913
find_package(Boost 1.70.0 COMPONENTS "python${_py_version}" REQUIRED)
1014

1115
if (NOT TARGET Boost::python)

example/src/CMakeLists.txt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
1-
add_library(example MODULE main.cpp)
2-
3-
set_target_properties(example PROPERTIES PREFIX "")
4-
5-
if(WIN32)
6-
set_target_properties(example PROPERTIES SUFFIX ".pyd")
7-
endif()
8-
9-
target_link_libraries(example PUBLIC Boost::python Python::Python)
1+
add_subdirectory(foo)
2+
add_subdirectory(python)

example/src/foo/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_library(foo SHARED foo.cpp)
2+
3+
target_include_directories(foo
4+
PUBLIC
5+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
6+
)

example/src/foo/foo.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "./foo.h"
2+
3+
#include <cstdlib>
4+
#include <iostream>
5+
#include <fstream>
6+
#include <sstream>
7+
#include <string>
8+
#include <stdexcept>
9+
10+
Foo::Foo()
11+
{
12+
const char* settings = std::getenv("FOO_SETTINGS_FILE");
13+
if (settings == nullptr) {
14+
throw std::runtime_error("Environment variable FOO_SETTINGS_FILE is not set.");
15+
}
16+
17+
std::ifstream file(settings);
18+
if (!file.is_open()) {
19+
throw std::runtime_error("Unable to open Foo file.");
20+
}
21+
22+
std::string line;
23+
while (std::getline(file, line)) {
24+
std::istringstream iss(line);
25+
std::string lang, greeting;
26+
if (std::getline(iss, lang, ':') && std::getline(iss, greeting)) {
27+
_map[lang] = greeting;
28+
}
29+
}
30+
file.close();
31+
}
32+
33+
std::string Foo::sayHello(std::string language)
34+
{
35+
if (_map.find(language) == _map.end()) {
36+
throw std::runtime_error("Language not found in Foo file.");
37+
}
38+
39+
return _map[language];
40+
}

example/src/foo/foo.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef EXAMPLE_FOO_H
2+
#define EXAMPLE_FOO_H
3+
4+
#include <string>
5+
#include <unordered_map>
6+
7+
class Foo {
8+
public:
9+
Foo();
10+
virtual ~Foo() = default;
11+
12+
std::string sayHello(std::string language);
13+
14+
private:
15+
std::unordered_map<std::string, std::string> _map;
16+
};
17+
18+
#endif // EXAMPLE_FOO_H

example/src/python/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
add_library(pyFoo MODULE main.cpp)
2+
3+
set_target_properties(pyFoo
4+
PROPERTIES
5+
PREFIX ""
6+
OUTPUT_NAME foo
7+
)
8+
9+
if(WIN32)
10+
set_target_properties(pyFoo PROPERTIES SUFFIX ".pyd")
11+
endif()
12+
13+
target_link_libraries(pyFoo
14+
PUBLIC
15+
foo
16+
Boost::python
17+
Python::Python
18+
)
19+
20+
if (WIN32)
21+
# As of Python v3.8 and newer, DLLs are no longer searched for in the
22+
# PATH environment variable on Windows. Therefore, it is necessary to
23+
# ensure that they are all located in the same directory.
24+
add_custom_command(
25+
TARGET pyFoo POST_BUILD
26+
COMMAND ${CMAKE_COMMAND}
27+
-E copy_if_different
28+
$<TARGET_FILE:foo>
29+
$<TARGET_FILE_DIR:pyFoo>
30+
COMMAND_EXPAND_LISTS
31+
)
32+
endif()
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#include <boost/python.hpp>
2+
#include <foo.h>
23

34
#include <cstdlib>
45
#include <string>
56

67
std::string greet(std::string name)
78
{
8-
const char* value = std::getenv("GREETING_WORD");
9+
Foo foo;
10+
11+
const char* value = std::getenv("DEFAULT_LANGUAGE");
912
if (value != nullptr)
10-
return std::string(value) + ", " + name;
13+
return foo.sayHello(value) + ", " + name;
1114
else
12-
return "bonjour, " + name;
15+
return foo.sayHello("fr") + ", " + name;
1316
}
1417

15-
BOOST_PYTHON_MODULE(example)
18+
BOOST_PYTHON_MODULE(foo)
1619
{
1720
using namespace boost::python;
1821
Py_Initialize();

example/test/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pytest_discover_tests(
22
PythonTest
33
LIBRARY_PATH_PREPEND
4-
$ENV{RUNNER_TEMP}
5-
$<TARGET_FILE_DIR:example>
4+
$<TARGET_FILE_DIR:foo>
5+
$<TARGET_FILE_DIR:pyFoo>
66
PYTHON_PATH_PREPEND
7-
$ENV{RUNNER_TEMP}
8-
$<TARGET_FILE_DIR:example>
7+
$<TARGET_FILE_DIR:pyFoo>
98
TRIM_FROM_NAME "^test_"
10-
DEPENDS example
9+
DEPENDS foo pyFoo
1110
ENVIRONMENT
12-
"GREETING_WORD=hello"
11+
"DEFAULT_LANGUAGE=en"
12+
"FOO_SETTINGS_FILE=${CMAKE_CURRENT_SOURCE_DIR}/resource/foo.txt"
1313
)

example/test/resource/foo.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
en:hello
2+
fr:bonjour
3+
es:hola

0 commit comments

Comments
 (0)