build(docs): add cmake target for docs (#2748)

This commit is contained in:
ReenigneArcher
2024-06-24 12:12:31 -04:00
committed by GitHub
parent 13f94f113a
commit 4683bcaf36
16 changed files with 176 additions and 315 deletions

View File

@@ -194,204 +194,3 @@ protected:
private:
std::unique_ptr<platf::deinit_t> deinit_guard;
};
class DocsPythonVenvBase: public virtual BaseTest {
protected:
void
SetUp() override {
#if defined TESTS_ENABLE_VENV_TESTS && TESTS_ENABLE_VENV_TESTS == 0
GTEST_SKIP_("TESTS_ENABLE_VENV_TESTS is disabled by CMake");
#else
std::cout << "DocsPythonVenvTest:: starting Fixture SetUp" << std::endl;
std::string pythonBinDirArray[] = { "bin", "Scripts" };
std::filesystem::path pythonPath = "python";
std::string binPath;
std::string command;
int exit_code;
std::filesystem::path venvPath = ".venv";
std::filesystem::path fullVenvPath = BaseTest::testBinaryDir / venvPath;
// check for existence of venv, and create it if necessary
std::cout << "DocsPythonVenvTest:: checking for venv" << std::endl;
if (!std::filesystem::exists(fullVenvPath)) {
std::cout << "DocsPythonVenvTest:: venv not found" << std::endl;
// create the venv
command = "\"" TESTS_PYTHON_EXECUTABLE "\" -m venv " + fullVenvPath.string();
std::cout << "DocsPythonVenvTest:: trying to create venv with command: " << command << std::endl;
exit_code = BaseTest::exec(command.c_str());
if (exit_code != 0) {
if (!std::filesystem::exists(fullVenvPath)) {
FAIL() << "Command failed: " << command << " with exit code: " << exit_code;
}
else {
// venv command will randomly complain that some files already exist...
std::cout << "DocsPythonVenvTest:: exit code (" << exit_code << ") indicates venv creation failed, but venv exists" << std::endl;
}
}
}
// determine if bin directory is `bin` (Unix) or `Scripts` (Windows)
// cannot assume `Scripts` on Windows, as it could be `bin` if using MSYS2, cygwin, etc.
std::cout << "DocsPythonVenvTest:: checking structure of venv" << std::endl;
for (const std::string &binDir : pythonBinDirArray) {
// check if bin directory exists
if (std::filesystem::exists(fullVenvPath / binDir)) {
binPath = binDir;
std::cout << "DocsPythonVenvTest:: found binPath: " << binPath << std::endl;
break;
}
}
if (binPath.empty()) {
FAIL() << "Python venv not found";
}
// set fullPythonPath and fullPythonBinPath
fullPythonPath = fullVenvPath / binPath / pythonPath;
fullPythonBinPath = fullVenvPath / binPath;
std::cout << "DocsPythonVenvTest:: fullPythonPath: " << fullPythonPath << std::endl;
std::cout << "DocsPythonVenvTest:: fullPythonBinPath: " << fullPythonBinPath << std::endl;
std::filesystem::path requirements_path = std::filesystem::path(TESTS_DOCS_DIR) / "requirements.txt";
// array of commands to run
std::string CommandArray[] = {
"\"" + fullPythonPath.string() + "\" -m pip install -r " + requirements_path.string(),
};
for (const std::string &_command : CommandArray) {
std::cout << "DocsPythonVenvTest:: running command: " << _command << std::endl;
exit_code = BaseTest::exec(_command.c_str());
if (exit_code != 0) {
FAIL() << "Command failed: " << command << " with exit code: " << exit_code;
}
}
// Save the original PATH
originalEnvPath = std::getenv("PATH") ? std::getenv("PATH") : "";
std::cout << "DocsPythonVenvTest:: originalEnvPath: " << originalEnvPath << std::endl;
// Set the temporary PATH
std::string tempPath;
std::string envPathSep;
#ifdef _WIN32
envPathSep = ";";
#else
envPathSep = ":";
#endif
tempPath = fullPythonBinPath.string() + envPathSep + originalEnvPath;
std::cout << "DocsPythonVenvTest:: tempPath: " << tempPath << std::endl;
setEnv("PATH", tempPath);
std::cout << "DocsPythonVenvTest:: finished Fixture SetUp" << std::endl;
#endif
}
void
TearDown() override {
std::cout << "DocsPythonVenvTest:: starting Fixture TearDown" << std::endl;
// Restore the original PATH
if (!originalEnvPath.empty()) {
std::cout << "DocsPythonVenvTest:: restoring originalEnvPath: " << originalEnvPath << std::endl;
setEnv("PATH", originalEnvPath);
}
std::cout << "DocsPythonVenvTest:: finished Fixture TearDown" << std::endl;
}
// functions and variables
std::filesystem::path fullPythonPath;
std::filesystem::path fullPythonBinPath;
std::string originalEnvPath;
};
class DocsPythonVenvTest: public virtual BaseTest, public DocsPythonVenvBase {
protected:
void
SetUp() override {
BaseTest::SetUp();
DocsPythonVenvBase::SetUp();
}
void
TearDown() override {
DocsPythonVenvBase::TearDown();
BaseTest::TearDown();
}
};
class DocsWorkingDirectoryBase: public virtual BaseTest {
protected:
void
SetUp() override {
#if defined TESTS_ENABLE_VENV_TESTS && TESTS_ENABLE_VENV_TESTS == 1
std::cout << "DocsWorkingDirectoryTest:: starting Fixture SetUp" << std::endl;
temp_dir = TESTS_DOCS_DIR;
std::cout << "DocsWorkingDirectoryTest:: temp_dir: " << temp_dir << std::endl;
// change directory to `docs`
original_dir = std::filesystem::current_path(); // save original directory
std::cout << "DocsWorkingDirectoryTest:: original_dir: " << original_dir << std::endl;
std::filesystem::current_path(temp_dir);
std::cout << "DocsWorkingDirectoryTest:: working directory set to: " << std::filesystem::current_path() << std::endl;
std::cout << "DocsWorkingDirectoryTest:: finished Fixture SetUp" << std::endl;
#endif
}
void
TearDown() override {
#if defined TESTS_ENABLE_VENV_TESTS && TESTS_ENABLE_VENV_TESTS == 1
std::cout << "DocsWorkingDirectoryTest:: starting Fixture TearDown" << std::endl;
// change directory back to original
std::filesystem::current_path(original_dir);
std::cout << "DocsWorkingDirectoryTest:: working directory set to: " << std::filesystem::current_path() << std::endl;
std::cout << "DocsWorkingDirectoryTest:: finished Fixture TearDown" << std::endl;
#endif
}
// functions and variables
std::filesystem::path original_dir;
std::filesystem::path temp_dir;
};
class DocsWorkingDirectoryTest: public virtual BaseTest, public DocsWorkingDirectoryBase {
protected:
void
SetUp() override {
BaseTest::SetUp();
DocsWorkingDirectoryBase::SetUp();
}
void
TearDown() override {
DocsWorkingDirectoryBase::TearDown();
BaseTest::TearDown();
}
};
class DocsTestFixture: public virtual BaseTest, public DocsPythonVenvBase, public DocsWorkingDirectoryBase {
protected:
void
SetUp() override {
BaseTest::SetUp();
DocsPythonVenvBase::SetUp();
DocsWorkingDirectoryBase::SetUp();
}
void
TearDown() override {
DocsWorkingDirectoryBase::TearDown();
DocsPythonVenvBase::TearDown();
BaseTest::TearDown();
}
};