Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Fix tempfile handling and disable broken MIME type detection check #4600

Merged
merged 2 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 4 additions & 47 deletions src/test/mixxxtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,50 +84,6 @@ void MixxxTest::saveAndReloadConfig() {

namespace mixxxtest {

FileRemover::~FileRemover() {
VERIFY_OR_DEBUG_ASSERT(
m_fileName.isEmpty() ||
QFile::remove(m_fileName) ||
!QFile::exists(m_fileName)) {
// unexpected
}
}

QString generateTemporaryFileName(const QString& fileNameTemplate) {
auto tmpFile = QTemporaryFile(fileNameTemplate);
// The file must be opened to create it and to obtain
// its file name!
VERIFY_OR_DEBUG_ASSERT(tmpFile.open()) {
return QString();
}
const auto tmpFileName = tmpFile.fileName();
DEBUG_ASSERT(!tmpFileName.isEmpty());
// The empty temporary file will be removed upon returning
// from this function
return tmpFileName;
}

QString createEmptyTemporaryFile(const QString& fileNameTemplate) {
auto emptyFile = QTemporaryFile(fileNameTemplate);
VERIFY_OR_DEBUG_ASSERT(emptyFile.open()) {
return QString();
}

// Retrieving the file's name after opening it is required to actually
// create a named file on Linux.
const auto fileName = emptyFile.fileName();
DEBUG_ASSERT(!fileName.isEmpty());
VERIFY_OR_DEBUG_ASSERT(emptyFile.exists()) {
return QString();
}
VERIFY_OR_DEBUG_ASSERT(emptyFile.size() == 0) {
return QString();
}

emptyFile.setAutoRemove(false);
return fileName;
}

bool copyFile(const QString& srcFileName, const QString& dstFileName) {
auto srcFile = QFile(srcFileName);
DEBUG_ASSERT(srcFile.exists());
Expand All @@ -140,15 +96,16 @@ bool copyFile(const QString& srcFileName, const QString& dstFileName) {
<< dstFileName;
return false;
}
auto dstFileRemover = FileRemover(dstFileName);
auto dstFile = QFile(dstFileName);
VERIFY_OR_DEBUG_ASSERT(dstFile.exists()) {
return false;
}
VERIFY_OR_DEBUG_ASSERT(srcFile.size() == dstFile.size()) {
if (srcFile.size() != dstFile.size()) {
dstFile.remove();
DEBUG_ASSERT(!"dstFile size does not match srcFile");
return false;
}
dstFileRemover.keepFile();

return true;
}

Expand Down
25 changes: 0 additions & 25 deletions src/test/mixxxtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,6 @@ class MixxxTest : public testing::Test {

namespace mixxxtest {

/// Returns the full, non-empty file path on success.
///
/// For the format of fileNameTemplate refer to QTemporaryFile.
QString generateTemporaryFileName(const QString& fileNameTemplate);

/// Returns the full, non-empty file path on success.
///
/// For the format of fileNameTemplate refer to QTemporaryFile.
QString createEmptyTemporaryFile(const QString& fileNameTemplate);

bool copyFile(const QString& srcFileName, const QString& dstFileName);

class FileRemover final {
public:
explicit FileRemover(const QString& fileName)
: m_fileName(fileName) {
}
~FileRemover();

void keepFile() {
m_fileName = QString();
}

private:
QString m_fileName;
};

} // namespace mixxxtest
42 changes: 28 additions & 14 deletions src/test/soundproxy_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <QTemporaryDir>
#include <QTemporaryFile>
#include <QtDebug>

Expand Down Expand Up @@ -193,13 +194,20 @@ TEST_F(SoundSourceProxyTest, open) {

TEST_F(SoundSourceProxyTest, openEmptyFile) {
const QStringList fileNameSuffixes = getFileNameSuffixes();

for (const auto& fileNameSuffix : fileNameSuffixes) {
const auto tmpFileName =
mixxxtest::createEmptyTemporaryFile("emptyXXXXXX" + fileNameSuffix);
const mixxxtest::FileRemover tmpFileRemover(tmpFileName);
QTemporaryFile tmpFile("emptyXXXXXX" + fileNameSuffix);
ASSERT_FALSE(QFile::exists(tmpFile.fileName()));
ASSERT_TRUE(tmpFile.open());

ASSERT_TRUE(QFile::exists(tmpFileName));
// Retrieving the file's name after opening it is required to actually
// create a named file on Linux.
const auto tmpFileName = tmpFile.fileName();
ASSERT_TRUE(!tmpFileName.isEmpty());

tmpFile.close();
ASSERT_TRUE(QFile::exists(tmpFileName));

ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(tmpFileName));
auto pTrack = Track::newTemporary(tmpFileName);
SoundSourceProxy proxy(pTrack);
Expand Down Expand Up @@ -710,24 +718,30 @@ TEST_F(SoundSourceProxyTest, regressionTestCachingReaderChunkJumpForward) {
}

TEST_F(SoundSourceProxyTest, getTypeFromFile) {
QTemporaryDir tempDir;
ASSERT_TRUE(tempDir.isValid());

// Generate file names for the temporary file
const QString filePathWithoutSuffix =
mixxxtest::generateTemporaryFileName("file_with_no_file_suffix");
tempDir.filePath("file_with_no_file_suffix");
const QString filePathWithEmptySuffix =
mixxxtest::generateTemporaryFileName("file_with_empty_suffix.");
tempDir.filePath("file_with_empty_suffix.");
const QString filePathWithUnknownSuffix =
mixxxtest::generateTemporaryFileName("file_with.unknown_suffix");
const QString filePathWithWrongSuffix =
mixxxtest::generateTemporaryFileName("file_with_wrong_suffix.wav");
tempDir.filePath("file_with.unknown_suffix");
// TODO: Currently, our SoundSource::getTypeFromFile() can not detect the
// file type of files with a known but wrong file extension properly, so
// this test needs to be disabled.
//const QString filePathWithWrongSuffix =
// tempDir.filePath("file_with_wrong_suffix.wav");
const QString filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix =
mixxxtest::generateTemporaryFileName("file_with_uppercase_suffix. MP3 ");
tempDir.filePath("file_with_uppercase_suffix. MP3 ");

// Create the temporary files by copying an existing file
const QString validFilePath = kTestDir.absoluteFilePath(QStringLiteral("empty.mp3"));
mixxxtest::copyFile(validFilePath, filePathWithoutSuffix);
mixxxtest::copyFile(validFilePath, filePathWithEmptySuffix);
mixxxtest::copyFile(validFilePath, filePathWithUnknownSuffix);
mixxxtest::copyFile(validFilePath, filePathWithWrongSuffix);
//mixxxtest::copyFile(validFilePath, filePathWithWrongSuffix);
mixxxtest::copyFile(validFilePath, filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix);

ASSERT_STREQ(qPrintable("mp3"), qPrintable(mixxx::SoundSource::getTypeFromFile(validFilePath)));
Expand All @@ -741,9 +755,9 @@ TEST_F(SoundSourceProxyTest, getTypeFromFile) {
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithUnknownSuffix)));
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithWrongSuffix)));
//EXPECT_STREQ(qPrintable("mp3"),
// qPrintable(mixxx::SoundSource::getTypeFromFile(
// filePathWithWrongSuffix)));
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix)));
Expand Down
8 changes: 4 additions & 4 deletions src/test/taglibtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class TagLibTest : public testing::Test {
};

TEST_F(TagLibTest, WriteID3v2Tag) {
QTemporaryDir tempDir;
ASSERT_TRUE(tempDir.isValid());

// Generate a file name for the temporary file
const QString tmpFileName = mixxxtest::generateTemporaryFileName("no_id3v1_mp3");
const QString tmpFileName = tempDir.filePath("no_id3v1_mp3");

// Create the temporary file by copying an existing file
mixxxtest::copyFile(kTestDir.absoluteFilePath("empty.mp3"), tmpFileName);

// Ensure that the temporary file is removed after the test
mixxxtest::FileRemover tmpFileRemover(tmpFileName);

// Verify that the file has no tags
{
TagLib::MPEG::File mpegFile(
Expand Down