Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
* Remove ConfigContext addition
  • Loading branch information
droidmonkey committed Oct 25, 2021
1 parent 3ed6baf commit 5166f83
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 111 deletions.
1 change: 0 additions & 1 deletion src/gui/ApplicationSettingsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define KEEPASSX_SETTINGSWIDGET_H

#include "gui/EditWidget.h"
#include <core/Config.h>

namespace Ui
{
Expand Down
13 changes: 6 additions & 7 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1879,16 +1879,16 @@ bool DatabaseWidget::performSave(QString& errorMessage, const QString& fileName)
}
}

bool create_backup = config()->get(Config::BackupBeforeSave).toBool();
QString backupFilePath = QString();
if (create_backup) {
QString backupFilePath;
if (config()->get(Config::BackupBeforeSave).toBool()) {
backupFilePath = config()->get(Config::BackupFilePathPattern).toString();
// Fall back to default
if (backupFilePath.isEmpty()) {
backupFilePath = config()->getDefault(Config::BackupFilePathPattern).toString();
}
// Replace backup pattern
backupFilePath.replace(QString("{DB_FILENAME}"), QFileInfo(m_db->filePath()).completeBaseName());
QFileInfo dbFileInfo(m_db->filePath());
backupFilePath.replace(QString("{DB_FILENAME}"), dbFileInfo.completeBaseName());

auto re = QRegularExpression("\\{TIME(?::(.+))?\\}");
auto matches = re.globalMatch(backupFilePath);
Expand All @@ -1904,13 +1904,12 @@ bool DatabaseWidget::performSave(QString& errorMessage, const QString& fileName)

matches = re.globalMatch(backupFilePath);
}

// Note that we cannot guarantee that backupFilePath is actually a valid filename. QT currently provides
// no function for this. Moreover, we don't check if backupFilePath is a file and not a directory.
// If this isn't the case, just let the backup fail.

if (QDir::isRelativePath(backupFilePath)) {
auto dbDir = QFileInfo(m_db->filePath()).absolutePath();
backupFilePath = QDir::cleanPath(dbDir + QDir::separator() + backupFilePath);
backupFilePath = QDir::cleanPath(dbFileInfo.absolutePath() + QDir::separator() + backupFilePath);
}
}

Expand Down
3 changes: 1 addition & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ set(testsupport_SOURCES
modeltest.cpp
FailDevice.cpp
mock/MockClock.cpp
util/TemporaryFile.cpp
util/ConfigContext.cpp)
util/TemporaryFile.cpp)
add_library(testsupport STATIC ${testsupport_SOURCES})
target_link_libraries(testsupport Qt5::Core Qt5::Concurrent Qt5::Widgets Qt5::Test)

Expand Down
2 changes: 1 addition & 1 deletion tests/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)

add_unit_test(NAME testgui SOURCES TestGui.cpp ../util/TemporaryFile.cpp ../util/ConfigContext.cpp LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testgui SOURCES TestGui.cpp ../util/TemporaryFile.cpp LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testguipixmaps SOURCES TestGuiPixmaps.cpp LIBS ${TEST_LIBRARIES})

if(WITH_XC_BROWSER)
Expand Down
33 changes: 18 additions & 15 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
#include "gui/group/GroupView.h"
#include "gui/wizard/NewDatabaseWizard.h"
#include "keys/FileKey.h"
#include "util/ConfigContext.hpp"

#define TEST_MODAL_NO_WAIT(TEST_CODE) \
bool dialogFinished = false; \
Expand Down Expand Up @@ -90,16 +89,6 @@ void TestGui::initTestCase()
{
QVERIFY(Crypto::init());
Config::createTempFileInstance();
// Disable autosave so we can test the modified file indicator
config()->set(Config::AutoSaveAfterEveryChange, false);
config()->set(Config::AutoSaveOnExit, false);
// Enable the tray icon so we can test hiding/restoring the windowQByteArray
config()->set(Config::GUI_ShowTrayIcon, true);
// Disable advanced settings mode (activate within individual tests to test advanced settings)
config()->set(Config::GUI_AdvancedSettings, false);
// Disable the update check first time alert
config()->set(Config::UpdateCheckMessageShown, true);

Application::bootstrap();

m_mainWindow.reset(new MainWindow());
Expand All @@ -108,9 +97,23 @@ void TestGui::initTestCase()
m_mainWindow->resize(1024, 768);
}

// Every test starts with opening the temp database
// Every test starts with reseting config settings and opening the temp database
void TestGui::init()
{
// Disable autosave so we can test the modified file indicator
config()->set(Config::AutoSaveAfterEveryChange, false);
config()->set(Config::AutoSaveOnExit, false);
config()->set(Config::AutoReloadOnChange, true);
// Enable the tray icon so we can test hiding/restoring the windowQByteArray
config()->set(Config::GUI_ShowTrayIcon, true);
// Disable advanced settings mode (activate within individual tests to test advanced settings)
config()->set(Config::GUI_AdvancedSettings, false);
// Disable the update check first time alert
config()->set(Config::UpdateCheckMessageShown, true);
// Reset backup settings
config()->set(Config::BackupBeforeSave, false);
config()->set(Config::BackupFilePathPattern, config()->getDefault(Config::BackupFilePathPattern));

// Copy the test database file to the temporary file
QVERIFY(m_dbFile.copyFromFile(dbFileName));

Expand Down Expand Up @@ -1290,7 +1293,7 @@ void TestGui::testSaveBackupPath_data()
QTest::addColumn<QString>("expectedBackupFile");

// Absolute paths should remain absolute
TemporaryFile safe_abs_path{};
TemporaryFile safe_abs_path;
QTest::newRow("Absolute backup path") << safe_abs_path.fileName() << safe_abs_path.fileName();
// relative paths should be resolved to database parent directory
QTest::newRow("Relative backup path (implicit)") << "other_dir/test.old.kdbx"
Expand Down Expand Up @@ -1322,7 +1325,7 @@ void TestGui::testSaveBackupPath()
QTRY_COMPARE(m_tabWidget->tabText(m_tabWidget->currentIndex()), QString("testBackupPathPattern"));

// Enable automatic backups
auto config_context = ConfigContext(Config::BackupBeforeSave, true);
config()->set(Config::BackupBeforeSave, true);

// Get test data
QFETCH(QString, backupFilePathPattern);
Expand All @@ -1337,7 +1340,7 @@ void TestGui::testSaveBackupPath()
expectedBackupFile.replace("{DB_FILENAME}", QFileInfo(m_dbFileName).completeBaseName());

// Modify the config
auto pattern_config_context = ConfigContext(Config::BackupFilePathPattern, backupFilePathPattern);
config()->set(Config::BackupFilePathPattern, backupFilePathPattern);

// Save a modified database
auto prevName = m_db->metadata()->name();
Expand Down
24 changes: 0 additions & 24 deletions tests/util/ConfigContext.cpp

This file was deleted.

61 changes: 0 additions & 61 deletions tests/util/ConfigContext.hpp

This file was deleted.

0 comments on commit 5166f83

Please sign in to comment.