Reduce amount of implicit behavior in desktop file parser

This commit is contained in:
TheAssassin
2018-11-15 18:03:46 +01:00
parent 1bcdcab057
commit 520ac6de9b
2 changed files with 11 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
// local headers
#include "linuxdeploy/core/desktopfile/exceptions.h"
#include "linuxdeploy/core/desktopfile/desktopfile.h"
#include "linuxdeploy/core/log.h"
#include "desktopfilereader.h"
@@ -34,10 +35,15 @@ namespace linuxdeploy {
DesktopFile::DesktopFile() : d(std::make_shared<PrivateData>()) {}
DesktopFile::DesktopFile(const bf::path& path) : DesktopFile() {
if (bf::exists(path)) {
// will throw exceptions in case of issues
read(path);
// if the file doesn't exist, an exception shall be thrown
// otherwise, a user cannot know for sure whether a file was actually read (would need to check this
// manually beforehand
if (!bf::exists(path)) {
throw IOError("Could not find file " + path.string());
}
// will throw exceptions in case of issues
read(path);
};
DesktopFile::DesktopFile(std::istream& is) : DesktopFile() {

View File

@@ -4,6 +4,7 @@
// local headers
#include "linuxdeploy/core/desktopfile/desktopfile.h"
#include "linuxdeploy/core/desktopfile/exceptions.h"
#include "../../src/core/desktopfile/desktopfilereader.h"
using boost::bad_lexical_cast;
@@ -66,8 +67,7 @@ TEST_F(DesktopFileFixture, testDefaultConstructor) {
}
TEST_F(DesktopFileFixture, testPathConstructor) {
DesktopFile nonExistingPath("/a/b/c/d/e/f/g/h/1/2/3/4/5/6/7/8");
EXPECT_TRUE(nonExistingPath.isEmpty());
ASSERT_THROW(DesktopFile nonExistingPath("/a/b/c/d/e/f/g/h/1/2/3/4/5/6/7/8"), IOError);
DesktopFile emptyFile("/dev/null");
EXPECT_TRUE(emptyFile.isEmpty());