Add tests for openmw options

In attempt to document current behaviour. Add commented out checks as desired
behaviour.
pull/3152/head
elsid 3 years ago
parent e24937df3b
commit 035307b012
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -2,6 +2,7 @@
set(GAME
main.cpp
engine.cpp
options.cpp
${CMAKE_SOURCE_DIR}/files/windows/openmw.rc
${CMAKE_SOURCE_DIR}/files/windows/openmw.exe.manifest

@ -7,6 +7,7 @@
#include <components/misc/rng.hpp>
#include "engine.hpp"
#include "options.hpp"
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
@ -39,108 +40,11 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
namespace bpo = boost::program_options;
typedef std::vector<std::string> StringsVector;
bpo::options_description desc("Syntax: openmw <options>\nAllowed options");
desc.add_options()
("help", "print help message")
("version", "print version information and quit")
("replace", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "settings where the values from the current source should replace those from lower-priority sources instead of being appended")
("data", bpo::value<Files::EscapePathContainer>()->default_value(Files::EscapePathContainer(), "data")
->multitoken()->composing(), "set data directories (later directories have higher priority)")
("data-local", bpo::value<Files::EscapePath>()->default_value(Files::EscapePath(), ""),
"set local data directory (highest priority)")
("fallback-archive", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "fallback-archive")
->multitoken()->composing(), "set fallback BSA archives (later archives have higher priority)")
("resources", bpo::value<Files::EscapePath>()->default_value(Files::EscapePath(), "resources"),
"set resources directory")
("start", bpo::value<Files::EscapeHashString>()->default_value(""),
"set initial cell")
("content", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "content file(s): esm/esp, or omwgame/omwaddon")
("groundcover", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "groundcover content file(s): esm/esp, or omwgame/omwaddon")
("lua-scripts", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "file(s) with a list of global Lua scripts: omwscripts")
("no-sound", bpo::value<bool>()->implicit_value(true)
->default_value(false), "disable all sounds")
("script-all", bpo::value<bool>()->implicit_value(true)
->default_value(false), "compile all scripts (excluding dialogue scripts) at startup")
("script-all-dialogue", bpo::value<bool>()->implicit_value(true)
->default_value(false), "compile all dialogue scripts at startup")
("script-console", bpo::value<bool>()->implicit_value(true)
->default_value(false), "enable console-only script functionality")
("script-run", bpo::value<Files::EscapeHashString>()->default_value(""),
"select a file containing a list of console commands that is executed on startup")
("script-warn", bpo::value<int>()->implicit_value (1)
->default_value (1),
"handling of warnings when compiling scripts\n"
"\t0 - ignore warning\n"
"\t1 - show warning but consider script as correctly compiled anyway\n"
"\t2 - treat warnings as errors")
("script-blacklist", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "ignore the specified script (if the use of the blacklist is enabled)")
("script-blacklist-use", bpo::value<bool>()->implicit_value(true)
->default_value(true), "enable script blacklisting")
("load-savegame", bpo::value<Files::EscapePath>()->default_value(Files::EscapePath(), ""),
"load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory)")
("skip-menu", bpo::value<bool>()->implicit_value(true)
->default_value(false), "skip main menu on game startup")
("new-game", bpo::value<bool>()->implicit_value(true)
->default_value(false), "run new game sequence (ignored if skip-menu=0)")
("fs-strict", bpo::value<bool>()->implicit_value(true)
->default_value(false), "strict file system handling (no case folding)")
("encoding", bpo::value<Files::EscapeHashString>()->
default_value("win1252"),
"Character encoding used in OpenMW game messages:\n"
"\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n"
"\n\twin1251 - Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages\n"
"\n\twin1252 - Western European (Latin) alphabet, used by default")
("fallback", bpo::value<FallbackMap>()->default_value(FallbackMap(), "")
->multitoken()->composing(), "fallback values")
("no-grab", bpo::value<bool>()->implicit_value(true)->default_value(false), "Don't grab mouse cursor")
("export-fonts", bpo::value<bool>()->implicit_value(true)
->default_value(false), "Export Morrowind .fnt fonts to PNG image and XML file in current directory")
("activate-dist", bpo::value <int> ()->default_value (-1), "activation distance override")
("random-seed", bpo::value <unsigned int> ()
->default_value(Misc::Rng::generateDefaultSeed()),
"seed value for random number generator")
;
bpo::parsed_options valid_opts = bpo::command_line_parser(argc, argv)
.options(desc).allow_unregistered().run();
bpo::options_description desc = OpenMW::makeOptionsDescription();
bpo::variables_map variables;
// Runtime options override settings from all configs
bpo::store(valid_opts, variables);
Files::parseArgs(argc, argv, variables, desc);
bpo::notify(variables);
if (variables.count ("help"))
@ -158,9 +62,9 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
return false;
}
bpo::variables_map composingVariables = cfgMgr.separateComposingVariables(variables, desc);
bpo::variables_map composingVariables = Files::separateComposingVariables(variables, desc);
cfgMgr.readConfiguration(variables, desc);
cfgMgr.mergeComposingVariables(variables, composingVariables, desc);
Files::mergeComposingVariables(variables, composingVariables, desc);
Version::Version v = Version::getOpenmwVersion(variables["resources"].as<Files::EscapePath>().mPath.string());
Log(Debug::Info) << v.describe();

@ -0,0 +1,115 @@
#include "options.hpp"
#include <components/fallback/validate.hpp>
#include <components/files/escape.hpp>
#include <components/misc/rng.hpp>
#include <boost/program_options.hpp>
namespace
{
namespace bpo = boost::program_options;
}
namespace OpenMW
{
bpo::options_description makeOptionsDescription()
{
bpo::options_description desc("Syntax: openmw <options>\nAllowed options");
desc.add_options()
("help", "print help message")
("version", "print version information and quit")
("replace", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "settings where the values from the current source should replace those from lower-priority sources instead of being appended")
("data", bpo::value<Files::EscapePathContainer>()->default_value(Files::EscapePathContainer(), "data")
->multitoken()->composing(), "set data directories (later directories have higher priority)")
("data-local", bpo::value<Files::EscapePath>()->default_value(Files::EscapePath(), ""),
"set local data directory (highest priority)")
("fallback-archive", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "fallback-archive")
->multitoken()->composing(), "set fallback BSA archives (later archives have higher priority)")
("resources", bpo::value<Files::EscapePath>()->default_value(Files::EscapePath(), "resources"),
"set resources directory")
("start", bpo::value<Files::EscapeHashString>()->default_value(""),
"set initial cell")
("content", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "content file(s): esm/esp, or omwgame/omwaddon")
("groundcover", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "groundcover content file(s): esm/esp, or omwgame/omwaddon")
("lua-scripts", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "file(s) with a list of global Lua scripts: omwscripts")
("no-sound", bpo::value<bool>()->implicit_value(true)
->default_value(false), "disable all sounds")
("script-all", bpo::value<bool>()->implicit_value(true)
->default_value(false), "compile all scripts (excluding dialogue scripts) at startup")
("script-all-dialogue", bpo::value<bool>()->implicit_value(true)
->default_value(false), "compile all dialogue scripts at startup")
("script-console", bpo::value<bool>()->implicit_value(true)
->default_value(false), "enable console-only script functionality")
("script-run", bpo::value<Files::EscapeHashString>()->default_value(""),
"select a file containing a list of console commands that is executed on startup")
("script-warn", bpo::value<int>()->implicit_value (1)
->default_value (1),
"handling of warnings when compiling scripts\n"
"\t0 - ignore warning\n"
"\t1 - show warning but consider script as correctly compiled anyway\n"
"\t2 - treat warnings as errors")
("script-blacklist", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
->multitoken()->composing(), "ignore the specified script (if the use of the blacklist is enabled)")
("script-blacklist-use", bpo::value<bool>()->implicit_value(true)
->default_value(true), "enable script blacklisting")
("load-savegame", bpo::value<Files::EscapePath>()->default_value(Files::EscapePath(), ""),
"load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory)")
("skip-menu", bpo::value<bool>()->implicit_value(true)
->default_value(false), "skip main menu on game startup")
("new-game", bpo::value<bool>()->implicit_value(true)
->default_value(false), "run new game sequence (ignored if skip-menu=0)")
("fs-strict", bpo::value<bool>()->implicit_value(true)
->default_value(false), "strict file system handling (no case folding)")
("encoding", bpo::value<Files::EscapeHashString>()->
default_value("win1252"),
"Character encoding used in OpenMW game messages:\n"
"\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n"
"\n\twin1251 - Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages\n"
"\n\twin1252 - Western European (Latin) alphabet, used by default")
("fallback", bpo::value<Fallback::FallbackMap>()->default_value(Fallback::FallbackMap(), "")
->multitoken()->composing(), "fallback values")
("no-grab", bpo::value<bool>()->implicit_value(true)->default_value(false), "Don't grab mouse cursor")
("export-fonts", bpo::value<bool>()->implicit_value(true)
->default_value(false), "Export Morrowind .fnt fonts to PNG image and XML file in current directory")
("activate-dist", bpo::value <int> ()->default_value (-1), "activation distance override")
("random-seed", bpo::value <unsigned int> ()
->default_value(Misc::Rng::generateDefaultSeed()),
"seed value for random number generator")
;
return desc;
}
}

@ -0,0 +1,11 @@
#ifndef APPS_OPENMW_OPTIONS_H
#define APPS_OPENMW_OPTIONS_H
#include <boost/program_options/options_description.hpp>
namespace OpenMW
{
boost::program_options::options_description makeOptionsDescription();
}
#endif

@ -40,6 +40,9 @@ if (GTEST_FOUND AND GMOCK_FOUND)
shader/parsedefines.cpp
shader/parsefors.cpp
shader/shadermanager.cpp
../openmw/options.cpp
openmw/options.cpp
)
source_group(apps\\openmw_test_suite FILES openmw_test_suite.cpp ${UNITTEST_SRC_FILES})

@ -0,0 +1,379 @@
#include <apps/openmw/options.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/files/escape.hpp>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <array>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
namespace
{
using namespace testing;
using namespace OpenMW;
namespace bpo = boost::program_options;
std::vector<std::string> generateSupportedCharacters(std::vector<std::string>&& base = {})
{
std::vector<std::string> result = std::move(base);
for (int i = 1; i <= std::numeric_limits<char>::max(); ++i)
if (i != '&' && i != '"' && i != ' ' && i != '@' && i != '\n')
result.push_back(std::string(1, i));
return result;
}
constexpr std::array supportedAtSignEscapings {
std::pair {'a', '@'},
std::pair {'h', '#'},
};
MATCHER_P(IsEscapePath, v, "") { return arg.mPath.string() == v; }
template <class T>
void parseArgs(const T& arguments, bpo::variables_map& variables, bpo::options_description& description)
{
Files::parseArgs(static_cast<int>(arguments.size()), arguments.data(), variables, description);
}
TEST(OpenMWOptionsFromArguments, should_support_equality_to_separate_flag_and_value)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame=save.omwsave"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save.omwsave");
}
TEST(OpenMWOptionsFromArguments, should_support_single_word_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", "save.omwsave"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save.omwsave");
}
TEST(OpenMWOptionsFromArguments, should_support_multi_component_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", "/home/user/openmw/save.omwsave"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "/home/user/openmw/save.omwsave");
}
TEST(OpenMWOptionsFromArguments, should_support_windows_multi_component_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", R"(C:\OpenMW\save.omwsave)"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(C:\OpenMW\save.omwsave)");
}
TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_spaces)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", "my save.omwsave"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "my");
// EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "my save.omwsave");
}
TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_number_sign)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", "my#save.omwsave"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "my#save.omwsave");
}
TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_at_sign)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", "my@save.omwsave"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "my?ave.omwsave");
// EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "my@save.omwsave");
}
TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_quote)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", R"(my"save.omwsave)"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(my"save.omwsave)");
}
TEST(OpenMWOptionsFromArguments, should_support_quted_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", R"("save".omwsave)"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(save)");
// EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"("save".omwsave)");
}
TEST(OpenMWOptionsFromArguments, should_support_quoted_load_savegame_path_with_escaped_quote_by_ampersand)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", R"("save&".omwsave")"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(save".omwsave)");
}
TEST(OpenMWOptionsFromArguments, should_support_quoted_load_savegame_path_with_escaped_ampersand)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", R"("save.omwsave&&")"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save.omwsave&");
}
TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_ampersand)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", "save&.omwsave"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save&.omwsave");
}
TEST(OpenMWOptionsFromArguments, should_support_load_savegame_path_with_multiple_quotes)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", R"(my"save".omwsave)"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(my"save".omwsave)");
}
TEST(OpenMWOptionsFromArguments, should_compose_data)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--data", "1", "--data", "2"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_THAT(variables["data"].as<Files::EscapePathContainer>(), ElementsAre(IsEscapePath("1"), IsEscapePath("2")));
}
TEST(OpenMWOptionsFromArguments, should_compose_data_from_single_flag)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--data", "1", "2"};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_THAT(variables["data"].as<Files::EscapePathContainer>(), ElementsAre(IsEscapePath("1"), IsEscapePath("2")));
}
TEST(OpenMWOptionsFromArguments, should_throw_on_multiple_load_savegame)
{
bpo::options_description description = makeOptionsDescription();
const std::array arguments {"openmw", "--load-savegame", "1.omwsave", "--load-savegame", "2.omwsave"};
bpo::variables_map variables;
EXPECT_THROW(parseArgs(arguments, variables, description), std::exception);
}
struct OpenMWOptionsFromArgumentsStrings : TestWithParam<std::string> {};
TEST_P(OpenMWOptionsFromArgumentsStrings, should_support_paths_with_certain_characters_in_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
const std::string path = "save_" + std::string(GetParam()) + ".omwsave";
const std::string pathArgument = "\"" + path + "\"";
const std::array arguments {"openmw", "--load-savegame", pathArgument.c_str()};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), path);
}
INSTANTIATE_TEST_SUITE_P(
SupportedCharacters,
OpenMWOptionsFromArgumentsStrings,
ValuesIn(generateSupportedCharacters({u8"👍", u8"Ъ", u8"Ǽ", "\n"}))
);
struct OpenMWOptionsFromArgumentsEscapings : TestWithParam<std::pair<char, char>> {};
TEST_P(OpenMWOptionsFromArgumentsEscapings, should_support_escaping_with_at_sign_in_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
const std::string path = "save_@" + std::string(1, GetParam().first) + ".omwsave";
const std::array arguments {"openmw", "--load-savegame", path.c_str()};
bpo::variables_map variables;
parseArgs(arguments, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(),
"save_" + std::string(1, GetParam().second) + ".omwsave");
}
INSTANTIATE_TEST_SUITE_P(
SupportedEscapingsWithAtSign,
OpenMWOptionsFromArgumentsEscapings,
ValuesIn(supportedAtSignEscapings)
);
TEST(OpenMWOptionsFromConfig, should_support_single_word_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("load-savegame=save.omwsave");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save.omwsave");
}
TEST(OpenMWOptionsFromConfig, should_strip_quotes_from_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream(R"(load-savegame="save.omwsave")");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save.omwsave");
}
TEST(OpenMWOptionsFromConfig, should_strip_outer_quotes_from_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream(R"(load-savegame=""save".omwsave")");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "");
// EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(""save".omwsave")");
}
TEST(OpenMWOptionsFromConfig, should_strip_quotes_from_load_savegame_path_with_space)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream(R"(load-savegame="my save.omwsave")");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "my save.omwsave");
}
TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_number_sign)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("load-savegame=save#.omwsave");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save#.omwsave");
}
TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_at_sign)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("load-savegame=save@.omwsave");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save@.omwsave");
}
TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_quote)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream(R"(load-savegame=save".omwsave)");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(save".omwsave)");
}
TEST(OpenMWOptionsFromConfig, should_ignore_commented_option)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("#load-savegame=save.omwsave");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "");
}
TEST(OpenMWOptionsFromConfig, should_throw_on_multiple_load_savegame)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("load-savegame=1.omwsave\nload-savegame=2.omwsave");
bpo::variables_map variables;
EXPECT_THROW(Files::parseConfig(stream, variables, description), std::exception);
}
TEST(OpenMWOptionsFromConfig, should_support_multi_component_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("load-savegame=/home/user/openmw/save.omwsave");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "/home/user/openmw/save.omwsave");
}
TEST(OpenMWOptionsFromConfig, should_support_windows_multi_component_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream(R"(load-savegame=C:\OpenMW\save.omwsave)");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(C:\OpenMW\save.omwsave)");
}
TEST(OpenMWOptionsFromConfig, should_compose_data)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("data=1\ndata=2");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_THAT(variables["data"].as<Files::EscapePathContainer>(), ElementsAre(IsEscapePath("1"), IsEscapePath("2")));
}
TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_escaped_quote_by_ampersand)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream(R"(load-savegame="save&".omwsave")");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), R"(save".omwsave)");
}
TEST(OpenMWOptionsFromConfig, should_support_quoted_load_savegame_path_with_escaped_ampersand)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream(R"(load-savegame="save.omwsave&&")");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save.omwsave&");
}
TEST(OpenMWOptionsFromConfig, should_support_load_savegame_path_with_ampersand)
{
bpo::options_description description = makeOptionsDescription();
std::istringstream stream("load-savegame=save&.omwsave");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), "save&.omwsave");
}
struct OpenMWOptionsFromConfigStrings : TestWithParam<std::string> {};
TEST_P(OpenMWOptionsFromConfigStrings, should_support_paths_with_certain_characters_in_load_savegame_path)
{
bpo::options_description description = makeOptionsDescription();
const std::string path = "save_" + std::string(GetParam()) + ".omwsave";
std::istringstream stream("load-savegame=\"" + path + "\"");
bpo::variables_map variables;
Files::parseConfig(stream, variables, description);
EXPECT_EQ(variables["load-savegame"].as<Files::EscapePath>().mPath.string(), path);
}
INSTANTIATE_TEST_SUITE_P(
SupportedCharacters,
OpenMWOptionsFromConfigStrings,
ValuesIn(generateSupportedCharacters({u8"👍", u8"Ъ", u8"Ǽ"}))
);
}

@ -82,7 +82,8 @@ void ConfigurationManager::readConfiguration(boost::program_options::variables_m
mSilent = silent;
}
boost::program_options::variables_map ConfigurationManager::separateComposingVariables(boost::program_options::variables_map & variables, boost::program_options::options_description& description)
boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map & variables,
boost::program_options::options_description& description)
{
boost::program_options::variables_map composingVariables;
for (auto itr = variables.begin(); itr != variables.end();)
@ -98,7 +99,8 @@ boost::program_options::variables_map ConfigurationManager::separateComposingVar
return composingVariables;
}
void ConfigurationManager::mergeComposingVariables(boost::program_options::variables_map & first, boost::program_options::variables_map & second, boost::program_options::options_description& description)
void mergeComposingVariables(boost::program_options::variables_map& first, boost::program_options::variables_map& second,
boost::program_options::options_description& description)
{
// There are a few places this assumes all variables are present in second, but it's never crashed in the wild, so it looks like that's guaranteed.
std::set<std::string> replacedVariables;
@ -234,13 +236,10 @@ bool ConfigurationManager::loadConfig(const boost::filesystem::path& path,
Log(Debug::Info) << "Loading config file: " << cfgFile.string();
boost::filesystem::ifstream configFileStreamUnfiltered(cfgFile);
boost::iostreams::filtering_istream configFileStream;
configFileStream.push(escape_hash_filter());
configFileStream.push(configFileStreamUnfiltered);
if (configFileStreamUnfiltered.is_open())
{
boost::program_options::store(boost::program_options::parse_config_file(
configFileStream, description, true), variables);
parseConfig(configFileStreamUnfiltered, variables, description);
return true;
}
@ -300,4 +299,26 @@ const boost::filesystem::path& ConfigurationManager::getScreenshotPath() const
return mScreenshotPath;
}
void parseArgs(int argc, const char* const argv[], boost::program_options::variables_map& variables,
boost::program_options::options_description& description)
{
boost::program_options::store(
boost::program_options::command_line_parser(argc, argv).options(description).allow_unregistered().run(),
variables
);
}
void parseConfig(std::istream& stream, boost::program_options::variables_map& variables,
boost::program_options::options_description& description)
{
boost::iostreams::filtering_istream configFileStream;
configFileStream.push(escape_hash_filter());
configFileStream.push(stream);
boost::program_options::store(
boost::program_options::parse_config_file(configFileStream, description, true),
variables
);
}
} /* namespace Cfg */

@ -25,10 +25,6 @@ struct ConfigurationManager
void readConfiguration(boost::program_options::variables_map& variables,
boost::program_options::options_description& description, bool quiet=false);
boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map& variables, boost::program_options::options_description& description);
void mergeComposingVariables(boost::program_options::variables_map& first, boost::program_options::variables_map& second, boost::program_options::options_description& description);
void processPaths(Files::PathContainer& dataDirs, bool create = false);
///< \param create Try creating the directory, if it does not exist.
@ -68,6 +64,19 @@ struct ConfigurationManager
bool mSilent;
};
boost::program_options::variables_map separateComposingVariables(boost::program_options::variables_map& variables,
boost::program_options::options_description& description);
void mergeComposingVariables(boost::program_options::variables_map& first, boost::program_options::variables_map& second,
boost::program_options::options_description& description);
void parseArgs(int argc, const char* const argv[], boost::program_options::variables_map& variables,
boost::program_options::options_description& description);
void parseConfig(std::istream& stream, boost::program_options::variables_map& variables,
boost::program_options::options_description& description);
} /* namespace Cfg */
#endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */

Loading…
Cancel
Save