Files
qt-creator/tests/auto/utils/unixdevicefileaccess/tst_unixdevicefileaccess.cpp
Marcus Tillmanns c41d30711a Utils: Make UnixDeviceFileAccess macOS compatible
"stat" on macOS has slightly different formatting options.

Also adds unittests for UnixDeviceFileAccess

Task-number: QTCREATORBUG-28142
Change-Id: Ib42fc1c22ef2771365e915df34f2286e2c705568
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
2023-02-25 09:45:47 +00:00

85 lines
2.1 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QDebug>
#include <QRandomGenerator>
#include <QtCore/qiodevice.h>
#include <QtTest>
#include <utils/commandline.h>
#include <utils/devicefileaccess.h>
#include <utils/hostosinfo.h>
#include <utils/link.h>
//TESTED_COMPONENT=src/libs/utils
using namespace Utils;
namespace QTest {
template<>
char *toString(const FilePath &filePath)
{
return qstrdup(filePath.toString().toLocal8Bit().constData());
}
} // namespace QTest
class TestDFA : public UnixDeviceFileAccess
{
public:
using UnixDeviceFileAccess::UnixDeviceFileAccess;
virtual RunResult runInShell(const CommandLine &cmdLine,
const QByteArray &inputData = {}) const override
{
QProcess p;
p.setProgram(cmdLine.executable().toString());
p.setArguments(cmdLine.splitArguments());
p.setProcessChannelMode(QProcess::SeparateChannels);
p.start();
p.waitForStarted();
if (inputData.size() > 0) {
p.write(inputData);
p.closeWriteChannel();
}
p.waitForFinished();
return {p.exitCode(), p.readAllStandardOutput(), p.readAllStandardError()};
}
};
class tst_unixdevicefileaccess : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
if (HostOsInfo::isWindowsHost())
QSKIP("This test is only for Unix hosts");
m_fileSizeTestFile.writeFileContents(QByteArray(1024, 'a'));
}
void osType()
{
const auto osType = m_dfaPtr->osType({});
QCOMPARE(osType, HostOsInfo::hostOs());
}
void fileSize()
{
const auto size = m_dfaPtr->fileSize(m_fileSizeTestFile);
QCOMPARE(size, 1024);
}
private:
TestDFA m_dfa;
DeviceFileAccess *m_dfaPtr = &m_dfa;
QTemporaryDir m_tempDir;
FilePath m_fileSizeTestFile = FilePath::fromString(m_tempDir.filePath("size-test"));
};
QTEST_GUILESS_MAIN(tst_unixdevicefileaccess)
#include "tst_unixdevicefileaccess.moc"