path: root/tests/auto/corelib/io/qfile/tst_qfile.cpp
diff options
authorEdward Welbourne <>2018-07-23 20:06:04 +0200
committerEdward Welbourne <>2018-07-30 15:04:31 +0000
commitcb505bedde4940d927b593ecc3668789e9f1c0a0 (patch)
tree47a002a0519d728491a354af340a4c5b831c7464 /tests/auto/corelib/io/qfile/tst_qfile.cpp
parent5da8e5ddd424a11435a3750dc9b955295aeaed9c (diff)
Actually truncate when asked to
On Unix, we wouldn't even *try* to truncate if the file was open for appending. The combination may be an eccentric choice but - at least when it's combined with reading - I can imagine use-cases for it; and we should (at least try to) deliver what we're asked for, even if we can't think why anyone would want it. So actually enable truncation when asked to. Amended some tests to check this works and corrected the QIODevice documentation of mode flags (which misdescribed the special case that implies Truncate). Removed special-case code, to apply truncate when writing but not reading, since it's been made redundant by the pre-processing of mode done in QFSFileEngine::processOpenModeFlags(). [ChangeLog][QtCore][QFile] When opening a file, if Truncate is asked for, or implied by other flags, it shall be attempted, regardless of what other options are selected. We previously did this on Windows; now we do so also on Unix (even when appending). Task-number: QTBUG-13470 Change-Id: I1e08d02cfbae102725fccbbc3aab5c7bf8830687 Reviewed-by: Oswald Buddenhagen <> Reviewed-by: Thiago Macieira <>
Diffstat (limited to 'tests/auto/corelib/io/qfile/tst_qfile.cpp')
1 files changed, 10 insertions, 3 deletions
diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp
index de4dbfc2a9..2d01dbb90a 100644
--- a/tests/auto/corelib/io/qfile/tst_qfile.cpp
+++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp
@@ -1293,6 +1293,12 @@ void tst_QFile::append()
QCOMPARE(int(f.size()), 2);
+ QVERIFY2( | QIODevice::Truncate), msgOpenFailed(f).constData());
+ QCOMPARE(f.pos(), 0);
+ f.putChar('a');
+ f.close();
+ QCOMPARE(int(f.size()), 1);
void tst_QFile::permissions_data()
@@ -1829,13 +1835,14 @@ void tst_QFile::encodeName()
void tst_QFile::truncate()
- for (int i = 0; i < 2; ++i) {
+ const QIODevice::OpenModeFlag modes[] = { QFile::ReadWrite, QIODevice::WriteOnly, QIODevice::Append };
+ for (auto mode : modes) {
QFile file("truncate.txt");
QVERIFY2(, msgOpenFailed(file).constData());
file.write(QByteArray(200, '@'));
- QVERIFY2( ? QFile::WriteOnly : QFile::ReadWrite) | QFile::Truncate), msgOpenFailed(file).constData());
+ QVERIFY2( | QFile::Truncate), msgOpenFailed(file).constData());
file.write(QByteArray(100, '$'));
@@ -2753,7 +2760,7 @@ void tst_QFile::renameMultiple()
void tst_QFile::appendAndRead()
QFile writeFile(QLatin1String("appendfile.txt"));
- QVERIFY2( | QIODevice::Truncate), msgOpenFailed(writeFile).constData());
+ QVERIFY2( | QIODevice::Truncate), msgOpenFailed(writeFile).constData());
QFile readFile(QLatin1String("appendfile.txt"));
QVERIFY2(, msgOpenFailed(readFile).constData());