From 8cb8761e42a229f9ace325dd52a428f6bfa0d0a1 Mon Sep 17 00:00:00 2001 From: Allan CORNET Date: Sat, 4 Jan 2025 12:07:38 +0100 Subject: [PATCH] test: rewrite fuzzy tests to address random failures --- .../tests/test_restoredefaultpath.m | 9 ++ .../src/cpp/PathFunctionIndexerManager.cpp | 119 +++++++----------- .../src/cpp/GetVariableEnvironment.cpp | 37 ++---- .../tests/test_QObject_findchildren.m | 8 +- 4 files changed, 73 insertions(+), 100 deletions(-) diff --git a/modules/functions_manager/tests/test_restoredefaultpath.m b/modules/functions_manager/tests/test_restoredefaultpath.m index d5b612e8e0..5ac25a5c70 100644 --- a/modules/functions_manager/tests/test_restoredefaultpath.m +++ b/modules/functions_manager/tests/test_restoredefaultpath.m @@ -7,14 +7,23 @@ % SPDX-License-Identifier: LGPL-3.0-or-later % LICENCE_BLOCK_END %============================================================================= +u1 = userpath(); +%============================================================================= restoredefaultpath(); %============================================================================= +u2 = userpath(); +assert_isequal(u1, u2); +%============================================================================= REF = path(); path(''); P1 = path(); +u3 = userpath(); +assert_isequal(u1, u2); assert_isequal(P1, ''); %============================================================================= restoredefaultpath(); +u3 = userpath(); P2 = path(); +assert_isequal(u1, u3); assert_isequal(REF, P2); %============================================================================= diff --git a/modules/interpreter/src/cpp/PathFunctionIndexerManager.cpp b/modules/interpreter/src/cpp/PathFunctionIndexerManager.cpp index 165679f909..f215bc72be 100644 --- a/modules/interpreter/src/cpp/PathFunctionIndexerManager.cpp +++ b/modules/interpreter/src/cpp/PathFunctionIndexerManager.cpp @@ -71,8 +71,7 @@ PathFunctionIndexerManager::destroy() void PathFunctionIndexerManager::startFileWatcher() { - for (auto it = _pathWatchFuncVector.begin(); it != _pathWatchFuncVector.end(); ++it) { - PathFunctionIndexer* pf = *it; + for (auto& pf : _pathWatchFuncVector) { pf->startFileWatcher(); } if (_userPath) { @@ -88,24 +87,19 @@ bool PathFunctionIndexerManager::hashOnFileWatcher() { bool result = false; - for (auto it = _pathWatchFuncVector.begin(); it != _pathWatchFuncVector.end(); ++it) { - PathFunctionIndexer* pf = *it; + for (auto& pf : _pathWatchFuncVector) { if (pf->wasModified()) { pf->rehash(); result = true; } } - if (_userPath) { - if (_userPath->wasModified()) { - _userPath->rehash(); - result = true; - }; + if (_userPath && _userPath->wasModified()) { + _userPath->rehash(); + result = true; } - if (_currentPath) { - if (_currentPath->wasModified()) { - _currentPath->rehash(); - result = true; - }; + if (_currentPath && _currentPath->wasModified()) { + _currentPath->rehash(); + result = true; } if (result) { @@ -118,8 +112,7 @@ void PathFunctionIndexerManager::refreshFunctionsMap() { _pathFuncMap.clear(); - for (auto it = _pathFuncVector.begin(); it != _pathFuncVector.end(); ++it) { - PathFunctionIndexer* pf = *it; + for (auto& pf : _pathFuncVector) { auto fileFunctions = pf->getAllFileFunctions(); _pathFuncMap.reserve(_pathFuncMap.size() + fileFunctions.size()); _pathFuncMap.insert(fileFunctions.begin(), fileFunctions.end()); @@ -246,24 +239,20 @@ bool PathFunctionIndexerManager::addPath(const std::wstring& path, bool begin, bool frozen) { bool res = false; - std::vector::iterator it = std::find_if( + auto it = std::find_if( _pathFuncVector.begin(), _pathFuncVector.end(), [path](PathFunctionIndexer* x) { return FileSystemWrapper::Path::equivalent(x->getPath(), path); }); if (it != _pathFuncVector.end()) { return false; } - PathFunctionIndexer* pf = nullptr; + try { - pf = new PathFunctionIndexer( + auto pf = new PathFunctionIndexer( FileSystemWrapper::Path::normalize(path), frozen ? false : true); if (_filesWatcherStarted) { pf->startFileWatcher(); } - } catch (const std::bad_alloc&) { - pf = nullptr; - } - if (pf != nullptr) { FunctionsInMemory::getInstance()->clearMapCache(); if (begin) { _pathFuncVector.insert(_pathFuncVector.begin(), pf); @@ -273,8 +262,10 @@ PathFunctionIndexerManager::addPath(const std::wstring& path, bool begin, bool f if (!frozen) { _pathWatchFuncVector.push_back(pf); } - res = true; refreshFunctionsMap(); + res = true; + } catch (const std::bad_alloc&) { + res = false; } return res; } @@ -572,61 +563,47 @@ PathFunctionIndexerManager::userpathCompute() { clearUserPath(); std::wstring userpathEnv = GetVariableEnvironment(L"NELSON_USERPATH", L""); - bool bSet = false; - if (!userpathEnv.empty()) { - if (FileSystemWrapper::Path::is_directory(userpathEnv)) { - setUserPath(userpathEnv); - bSet = true; - } + if (!userpathEnv.empty() && FileSystemWrapper::Path::is_directory(userpathEnv)) { + setUserPath(userpathEnv); + return; } - if (!bSet) { - std::wstring prefDir; - std::wstring userPathFile; - try { - prefDir = NelsonConfiguration::getInstance()->getNelsonPreferencesDirectory(); - } catch (const Exception&) { - prefDir.clear(); - } - - userPathFile = prefDir + L"/userpath.conf"; - bool bIsFile = FileSystemWrapper::Path::is_regular_file(userPathFile); - if (bIsFile) { - std::wstring preferedUserPath = loadUserPathFromFile(); - if (!preferedUserPath.empty()) { - if (FileSystemWrapper::Path::is_directory(preferedUserPath)) { - setUserPath(preferedUserPath); - bSet = true; - } - } else { - bSet = true; - } + std::wstring prefDir; + try { + prefDir = NelsonConfiguration::getInstance()->getNelsonPreferencesDirectory(); + } catch (const Exception&) { + prefDir.clear(); + } + std::wstring userPathFile = prefDir + L"/userpath.conf"; + if (FileSystemWrapper::Path::is_regular_file(userPathFile)) { + std::wstring preferedUserPath = loadUserPathFromFile(); + if (!preferedUserPath.empty() && FileSystemWrapper::Path::is_directory(preferedUserPath)) { + setUserPath(preferedUserPath); + return; } } - if (!bSet) { #ifdef _MSC_VER - std::wstring userprofileEnv = GetVariableEnvironment(L"USERPROFILE", L""); - if (!userprofileEnv.empty()) { - std::wstring userpathDir = userprofileEnv + std::wstring(L"/Documents/Nelson"); - if (!FileSystemWrapper::Path::is_directory(userpathDir)) { - FileSystemWrapper::Path::create_directories(userpathDir); - } - if (FileSystemWrapper::Path::is_directory(userpathDir)) { - setUserPath(userpathDir); - } + std::wstring userprofileEnv = GetVariableEnvironment(L"USERPROFILE", L""); + if (!userprofileEnv.empty()) { + std::wstring userpathDir = userprofileEnv + std::wstring(L"/Documents/Nelson"); + if (!FileSystemWrapper::Path::is_directory(userpathDir)) { + FileSystemWrapper::Path::create_directories(userpathDir); + } + if (FileSystemWrapper::Path::is_directory(userpathDir)) { + setUserPath(userpathDir); } + } #else - std::wstring homeEnv = GetVariableEnvironment(L"HOME", L""); - if (homeEnv != L"") { - std::wstring userpathDir = homeEnv + std::wstring(L"/Documents/Nelson"); - if (!FileSystemWrapper::Path::is_directory(userpathDir)) { - FileSystemWrapper::Path::create_directories(userpathDir); - } - if (FileSystemWrapper::Path::is_directory(userpathDir)) { - setUserPath(userpathDir); - } + std::wstring homeEnv = GetVariableEnvironment(L"HOME", L""); + if (homeEnv != L"") { + std::wstring userpathDir = homeEnv + std::wstring(L"/Documents/Nelson"); + if (!FileSystemWrapper::Path::is_directory(userpathDir)) { + FileSystemWrapper::Path::create_directories(userpathDir); + } + if (FileSystemWrapper::Path::is_directory(userpathDir)) { + setUserPath(userpathDir); } -#endif } +#endif } //============================================================================= std::wstring diff --git a/modules/os_functions/src/cpp/GetVariableEnvironment.cpp b/modules/os_functions/src/cpp/GetVariableEnvironment.cpp index 5122e00602..cbe4dfc08f 100644 --- a/modules/os_functions/src/cpp/GetVariableEnvironment.cpp +++ b/modules/os_functions/src/cpp/GetVariableEnvironment.cpp @@ -22,43 +22,30 @@ GetVariableEnvironment(const std::wstring& envVarName, const std::wstring& defau { std::wstring str(defaultValue); #ifdef _MSC_VER -#define DEFAULT_SIZE_ENV 4096 - wchar_t* buf = nullptr; - try { - buf = new wchar_t[DEFAULT_SIZE_ENV]; - } catch (const std::bad_alloc&) { + const DWORD DEFAULT_SIZE_ENV = 4096; + std::unique_ptr buf(new (std::nothrow) wchar_t[DEFAULT_SIZE_ENV]); + if (!buf) { return str; } - DWORD dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf, DEFAULT_SIZE_ENV); + + DWORD dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf.get(), DEFAULT_SIZE_ENV); if (dwRet == 0) { // error - if (buf != nullptr) { - delete[] buf; - buf = nullptr; - } return str; } if (dwRet > DEFAULT_SIZE_ENV) { // we resize the buffer - delete[] buf; - try { - buf = new wchar_t[dwRet + 1]; - } catch (const std::bad_alloc&) { + buf.reset(new (std::nothrow) wchar_t[dwRet + 1]); + if (!buf) { return str; } - dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf, dwRet); - } - if (dwRet == 0) { - // error - if (buf != nullptr) { - delete[] buf; - buf = nullptr; + dwRet = ::GetEnvironmentVariableW(envVarName.c_str(), buf.get(), dwRet + 1); + if (dwRet == 0) { + // error + return str; } - return str; } - str = buf; - delete[] buf; - + str.assign(buf.get(), dwRet); #else std::string s1 = wstring_to_utf8(envVarName); str = defaultValue; diff --git a/modules/qml_engine/tests/test_QObject_findchildren.m b/modules/qml_engine/tests/test_QObject_findchildren.m index 5108b64dd8..b7047be750 100644 --- a/modules/qml_engine/tests/test_QObject_findchildren.m +++ b/modules/qml_engine/tests/test_QObject_findchildren.m @@ -7,7 +7,7 @@ % SPDX-License-Identifier: LGPL-3.0-or-later % LICENCE_BLOCK_END %============================================================================= -% <--GUI MODE--> +% <--ADV-CLI MODE--> % <--WITH DISPLAY--> %============================================================================= if semver(qt_version(), '>=6.0') @@ -16,11 +16,11 @@ qml_file_ok = [modulepath('qml_engine', 'tests'), '/test_qml_loadfile_window_qt5.qml']; end qobj = qml_loadfile(qml_file_ok); -child = QObject_findchildren(QObject_root(), 'text1', false); -assert_isequal(size(child), [0 0]); %============================================================================= -child = QObject_findchildren(QObject_root(), 'text1', true); +child = QObject_findchildren(qobj, 'text1', true); +assert_isequal(class(child), 'QObject'); assert_isequal(size(child), [1 1]); +assert_isequal(child.className, 'QQuickText'); %============================================================================= delete(qobj); %=============================================================================