diff options
author | Jonathan White <support@dmapps.us> | 2019-10-21 01:23:11 +0300 |
---|---|---|
committer | Jonathan White <support@dmapps.us> | 2019-10-21 01:56:41 +0300 |
commit | 1e694271a3c6c233736301dcb2f1a7ced081fc3f (patch) | |
tree | 1fa647b44bf8427cc6d4cdd50fe38aee82b242dd /tests/util | |
parent | 744b4abce801440ff4e143fb2bb416b3d7a91e07 (diff) |
Improve Database and CLI tests
Diffstat (limited to 'tests/util')
-rw-r--r-- | tests/util/TemporaryFile.cpp | 30 | ||||
-rw-r--r-- | tests/util/TemporaryFile.h | 15 |
2 files changed, 30 insertions, 15 deletions
diff --git a/tests/util/TemporaryFile.cpp b/tests/util/TemporaryFile.cpp index 476313b02..19622faed 100644 --- a/tests/util/TemporaryFile.cpp +++ b/tests/util/TemporaryFile.cpp @@ -17,7 +17,7 @@ #include "TemporaryFile.h" -#ifdef Q_OS_WIN +#include <QTextStream> TemporaryFile::TemporaryFile() : TemporaryFile(nullptr) @@ -47,9 +47,35 @@ TemporaryFile::TemporaryFile(const QString& templateName, QObject* parent) tmp.close(); } +TemporaryFile::~TemporaryFile() +{ + remove(); +} + bool TemporaryFile::open() { return QFile::open(QIODevice::ReadWrite); } -#endif +bool TemporaryFile::copyFromFile(const QString& otherFileName) +{ + close(); + if (!open(QFile::WriteOnly | QFile::Truncate)) { + return false; + } + + QFile otherFile(otherFileName); + if (!otherFile.open(QFile::ReadOnly)) { + close(); + return false; + } + + QByteArray data; + while(!(data = otherFile.read(1024)).isEmpty()) { + write(data); + } + + otherFile.close(); + close(); + return true; +} diff --git a/tests/util/TemporaryFile.h b/tests/util/TemporaryFile.h index 1fca110a5..3c1cc6aa0 100644 --- a/tests/util/TemporaryFile.h +++ b/tests/util/TemporaryFile.h @@ -20,31 +20,20 @@ #include <QTemporaryFile> -#ifdef Q_OS_WIN -/** - * QTemporaryFile does not actually close a file when close() is - * called, which causes the file to be locked on Windows. - * This class extends a QFile with the extra functionality - * of a QTemporaryFile to circumvent this problem. - */ class TemporaryFile : public QFile -#else -class TemporaryFile : public QTemporaryFile -#endif { Q_OBJECT -#ifdef Q_OS_WIN public: TemporaryFile(); explicit TemporaryFile(const QString& templateName); explicit TemporaryFile(QObject* parent); TemporaryFile(const QString& templateName, QObject* parent); - ~TemporaryFile() override = default; + ~TemporaryFile() override; using QFile::open; bool open(); -#endif + bool copyFromFile(const QString& otherFileName); }; #endif // KEEPASSXC_TEMPORARYFILE_H |