From 0db3a23b21fe2856f64c5007b27e46a8140f10c0 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 1 Jan 2018 06:42:19 +0100 Subject: Fail build if project.conf contains invalid object or field --- include/Conf.hpp | 1 + src/Conf.cpp | 37 ++++++++++++++++++++++++++++++++++- tests/src/confTest/confTest.cpp | 18 ++++++++++++++++- tests/src/confTest/invalidField.conf | 10 ++++++++++ tests/src/confTest/invalidObject.conf | 12 ++++++++++++ tests/src/confTest/project.conf | 9 --------- tests/src/confTest/validProject.conf | 9 +++++++++ 7 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 tests/src/confTest/invalidField.conf create mode 100644 tests/src/confTest/invalidObject.conf delete mode 100644 tests/src/confTest/project.conf create mode 100644 tests/src/confTest/validProject.conf diff --git a/include/Conf.hpp b/include/Conf.hpp index f5fb382..75076fa 100644 --- a/include/Conf.hpp +++ b/include/Conf.hpp @@ -228,6 +228,7 @@ namespace sibs virtual void processObject(StringView name) override; virtual void processField(StringView name, const ConfigValue &value) override; virtual void finished() override; + void failInvalidFieldUnderObject(const StringView &fieldName) const; private: void validatePackageName() const; protected: diff --git a/src/Conf.cpp b/src/Conf.cpp index c487e28..27713d8 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -494,6 +494,14 @@ namespace sibs validatePackageName(); } + else if(name.equals("version")) + { + // TODO: Use version for info output when building + } + else if(name.equals("authors")) + { + // TODO: Use authors for something? + } else if(name.equals("type")) { if (value.isSingle()) @@ -618,6 +626,8 @@ namespace sibs else throw ParserException("Expected package.ignore_dirs to be a list, was a single value"); } + else + failInvalidFieldUnderObject(name); } else if(currentObject.equals("config")) { @@ -638,6 +648,8 @@ namespace sibs throw ParserException(errMsg); } } + else + failInvalidFieldUnderObject(name); } else if (currentObject.equals(CONFIG_SYSTEM_PLATFORM)) { @@ -658,6 +670,8 @@ namespace sibs throw ParserException(errMsg); } } + else + failInvalidFieldUnderObject(name); } else if (currentObject.equals(CONFIG_STATIC_DEBUG_PLATFORM)) { @@ -678,6 +692,8 @@ namespace sibs throw ParserException(errMsg); } } + else + failInvalidFieldUnderObject(name); } else if (currentObject.equals(CONFIG_STATIC_RELEASE_PLATFORM)) { @@ -698,6 +714,8 @@ namespace sibs throw ParserException(errMsg); } } + else + failInvalidFieldUnderObject(name); } else if(currentObject.equals("dependencies")) { @@ -712,6 +730,13 @@ namespace sibs else throw ParserException("Expected field under dependencies to be a single value, was a list"); } + else + { + string errMsg = "Invalid config object \""; + errMsg += string(currentObject.data, currentObject.size); + errMsg += "\""; + throw ParserException(errMsg); + } } void SibsConfig::finished() @@ -734,7 +759,17 @@ namespace sibs throw ParserException(errMsg); } } - + + void SibsConfig::failInvalidFieldUnderObject(const StringView &fieldName) const + { + string errMsg = "Invalid field \""; + errMsg += string(fieldName.data, fieldName.size); + errMsg += "\" under object \""; + errMsg += string(currentObject.data, currentObject.size); + errMsg += "\""; + throw ParserException(errMsg); + } + void SibsTestConfig::processObject(StringView name) { currentObject = name; diff --git a/tests/src/confTest/confTest.cpp b/tests/src/confTest/confTest.cpp index 5bab144..17b5995 100644 --- a/tests/src/confTest/confTest.cpp +++ b/tests/src/confTest/confTest.cpp @@ -6,7 +6,7 @@ using namespace sibs; TEST_CASE("parse config") { SibsConfig sibsConfig(Compiler::GCC, TINYDIR_STRING("tests/src/confTest")); - Result result = Config::readFromFile(TINYDIR_STRING("tests/src/confTest/project.conf"), sibsConfig); + Result result = Config::readFromFile(TINYDIR_STRING("tests/src/confTest/validProject.conf"), sibsConfig); if(result.isErr()) { fprintf(stderr, "%s", result.getErrMsg().c_str()); @@ -28,3 +28,19 @@ TEST_CASE("parse config") REQUIRE(catch2Dependency.name == "catch2"); REQUIRE(catch2Dependency.version == "1.0.0"); } + +TEST_CASE("parse config - invalid object") +{ + SibsConfig sibsConfig(Compiler::GCC, TINYDIR_STRING("tests/src/confTest")); + Result result = Config::readFromFile(TINYDIR_STRING("tests/src/confTest/invalidObject.conf"), sibsConfig); + REQUIRE(result.isErr()); + REQUIRE(result.getErrMsg() == "Invalid config object \"invalidObj\""); +} + +TEST_CASE("parse config - invalid field") +{ + SibsConfig sibsConfig(Compiler::GCC, TINYDIR_STRING("tests/src/confTest")); + Result result = Config::readFromFile(TINYDIR_STRING("tests/src/confTest/invalidField.conf"), sibsConfig); + REQUIRE(result.isErr()); + REQUIRE(result.getErrMsg() == "Invalid field \"invalidField\" under object \"package\""); +} diff --git a/tests/src/confTest/invalidField.conf b/tests/src/confTest/invalidField.conf new file mode 100644 index 0000000..c0f5892 --- /dev/null +++ b/tests/src/confTest/invalidField.conf @@ -0,0 +1,10 @@ +[package] +name = "confTest" +version = "0.1.0" +type = "library" +platforms = ["linux64", "win64"] +invalidField = "value" + +[dependencies] +xxhash = "0.1.0" +catch2 = "1.0.0" diff --git a/tests/src/confTest/invalidObject.conf b/tests/src/confTest/invalidObject.conf new file mode 100644 index 0000000..fe9fc86 --- /dev/null +++ b/tests/src/confTest/invalidObject.conf @@ -0,0 +1,12 @@ +[package] +name = "confTest" +version = "0.1.0" +type = "library" +platforms = ["linux64", "win64"] + +[dependencies] +xxhash = "0.1.0" +catch2 = "1.0.0" + +[invalidObj] +field = "value" diff --git a/tests/src/confTest/project.conf b/tests/src/confTest/project.conf deleted file mode 100644 index de5112d..0000000 --- a/tests/src/confTest/project.conf +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "confTest" -version = "0.1.0" -type = "library" -platforms = ["linux64", "win64"] - -[dependencies] -xxhash = "0.1.0" -catch2 = "1.0.0" diff --git a/tests/src/confTest/validProject.conf b/tests/src/confTest/validProject.conf new file mode 100644 index 0000000..de5112d --- /dev/null +++ b/tests/src/confTest/validProject.conf @@ -0,0 +1,9 @@ +[package] +name = "confTest" +version = "0.1.0" +type = "library" +platforms = ["linux64", "win64"] + +[dependencies] +xxhash = "0.1.0" +catch2 = "1.0.0" -- cgit v1.2.3