Skip to content

Commit

Permalink
BUG: Fix ctkUtilsCopyDirRecursivelyTest on windows creating hidden files
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed Jan 13, 2024
1 parent 3944cc8 commit 2cc640a
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions Libs/Core/Testing/Cpp/ctkUtilsCopyDirRecursivelyTest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#include <cstdlib>
#include <iostream>

// Windows includes
#ifdef Q_OS_WIN
#include <windows.h> // For SetFileAttributesW
#endif

namespace
{
//-----------------------------------------------------------------------------
Expand All @@ -56,6 +61,49 @@ bool createFile(int line, const QDir& dir, const QString& relativePath, const QS
return true;
}

//-----------------------------------------------------------------------------
bool hideFile(int line, const QDir& dir, const QString& relativePath)
{
QString filePath = QFileInfo(dir, relativePath).filePath();

if (!QFile::exists(filePath))
{
std::cerr << "Line " << line << " - File" << qPrintable(filePath) << "does not exist" <<std::endl;
return false;
}

#ifdef Q_OS_WIN
const wchar_t* filePathW = filePath.toStdWString().c_str();
DWORD dwAttrs = GetFileAttributesW(filePathW);
if ((dwAttrs & FILE_ATTRIBUTE_HIDDEN) == 0)
{
SetFileAttributesW(filePathW, dwAttrs | FILE_ATTRIBUTE_HIDDEN);
}
#endif

return true;
}

//-----------------------------------------------------------------------------
bool isFileHidden(int line, const QDir& dir, const QString& relativePath)
{
QString filePath = QFileInfo(dir, relativePath).filePath();

if (!QFile::exists(filePath))
{
std::cerr << "Line " << line << " - File" << qPrintable(filePath) << "does not exist" <<std::endl;
return false;
}

if (!QFileInfo(filePath).isHidden())
{
std::cerr << "Line " << line << " - File" << qPrintable(filePath) << "is expected to be hidden" << std::endl;
return false;
}

return true;
}

} // end of anonymous namespace


Expand Down Expand Up @@ -112,15 +160,23 @@ int ctkUtilsCopyDirRecursivelyTest1(int argc, char * argv [] )
{
return EXIT_FAILURE;
}
if (!createFile(__LINE__, tmp, "foo/zoo", ".hidden.txt"))
if (!(createFile(__LINE__, tmp, "foo/zoo", ".hidden.txt")
&& hideFile(__LINE__, tmp, "foo/zoo/.hidden.txt")
&& isFileHidden(__LINE__, tmp, "foo/zoo/.hidden.txt")))
{
return EXIT_FAILURE;
}
if (!createFile(__LINE__, tmp, "foo/.hidden", "c.txt"))
if (!(createFile(__LINE__, tmp, "foo/.hidden", "c.txt")
&& hideFile(__LINE__, tmp, "foo/.hidden")
&& isFileHidden(__LINE__, tmp, "foo/.hidden")))
{
return EXIT_FAILURE;
}
if (!createFile(__LINE__, tmp, "foo/.hidden", ".hidden.txt"))
if (!(createFile(__LINE__, tmp, "foo/.hidden", ".hidden.txt")
&& hideFile(__LINE__, tmp, "foo/.hidden")
&& hideFile(__LINE__, tmp, "foo/.hidden/.hidden.txt")
&& isFileHidden(__LINE__, tmp, "foo/.hidden")
&& isFileHidden(__LINE__, tmp, "foo/.hidden/.hidden.txt")))
{
return EXIT_FAILURE;
}
Expand Down

0 comments on commit 2cc640a

Please sign in to comment.