From bbecd315c0976c1e4ab3133c2e8551526cc9bdef Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 18 Jun 2012 12:15:58 +0200 Subject: Fix tst_qfile opening of stdin/out/err: don't assume The standard streams can be redirected to a file, so don't assume anything, but try to get the actual size and position from the OS and from the C library (stdout is usually buffered, so the result of lseek might be different from ftell). (cherry-picked from qtbase commit ba7f664a7fbb5612126e5144e58240ab05968259) Change-Id: Ice4a0aa21726671928f56a13cc07cc0e4b52091d Reviewed-by: Richard J. Moore Reviewed-by: Giuseppe D'Angelo --- tests/auto/qfile/tst_qfile.cpp | 70 ++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/tests/auto/qfile/tst_qfile.cpp b/tests/auto/qfile/tst_qfile.cpp index 5bed1e7..6298117 100644 --- a/tests/auto/qfile/tst_qfile.cpp +++ b/tests/auto/qfile/tst_qfile.cpp @@ -3186,6 +3186,30 @@ void tst_QFile::openDirectory() f1.close(); } +static qint64 streamExpectedSize(int fd) +{ + QT_STATBUF sb; + if (QT_FSTAT(fd, &sb) != -1) + return sb.st_size; + return 0; +} + +static qint64 streamCurrentPosition(int fd) +{ + QT_OFF_T pos = QT_LSEEK(fd, 0, SEEK_CUR); + if (pos != -1) + return pos; + return 0; +} + +static qint64 streamCurrentPosition(FILE *f) +{ + QT_OFF_T pos = QT_FTELL(f); + if (pos != -1) + return pos; + return 0; +} + void tst_QFile::openStandardStreamsFileDescriptors() { #ifdef Q_WS_WINCE @@ -3194,41 +3218,33 @@ void tst_QFile::openStandardStreamsFileDescriptors() //it does not have functions to simply open them like below . QSKIP("Opening standard streams on Windows CE via descriptor not implemented", SkipAll); #endif - /* in/out/err.isSequential() are only true when run in a console (CI); - * it is false when they are redirected from/to files. - * Prevent failures in case someone runs tests with stdout/stderr redirected. */ + { QFile in; in.open(STDIN_FILENO, QIODevice::ReadOnly); - if (!in.isSequential()) - QSKIP("Standard input redirected.", SkipSingle); - QCOMPARE( in.pos(), (qint64)0 ); - QCOMPARE( in.size(), (qint64)0 ); + QCOMPARE( in.pos(), streamCurrentPosition(STDIN_FILENO) ); + QCOMPARE( in.size(), streamExpectedSize(STDIN_FILENO) ); } { QFile out; - out.open(STDOUT_FILENO, QIODevice::WriteOnly); - if (!out.isSequential()) - QSKIP("Standard output redirected.", SkipSingle); - QCOMPARE( out.pos(), (qint64)0 ); - QCOMPARE( out.size(), (qint64)0 ); + QVERIFY(out.open(STDOUT_FILENO, QIODevice::WriteOnly)); + QCOMPARE( out.pos(), streamCurrentPosition(STDOUT_FILENO) ); + QCOMPARE( out.size(), streamExpectedSize(STDOUT_FILENO) ); } { QFile err; err.open(STDERR_FILENO, QIODevice::WriteOnly); - if (!err.isSequential()) - QSKIP("Standard error redirected.", SkipSingle); - QCOMPARE( err.pos(), (qint64)0 ); - QCOMPARE( err.size(), (qint64)0 ); + QCOMPARE( err.pos(), streamCurrentPosition(STDERR_FILENO) ); + QCOMPARE( err.size(), streamExpectedSize(STDERR_FILENO) ); } } void tst_QFile::openStandardStreamsBufferedStreams() { -#if defined (Q_OS_WIN) || defined(Q_OS_SYMBIAN) - QSKIP("Unix only test.", SkipAll); +#if defined (Q_OS_WINCE) || defined(Q_OS_SYMBIAN) + QSKIP("Not tested on Windows CE or Symbian."); #endif // Using streams { @@ -3237,28 +3253,22 @@ void tst_QFile::openStandardStreamsBufferedStreams() * Prevent failures in case someone runs tests with stdout/stderr redirected. */ QFile in; in.open(stdin, QIODevice::ReadOnly); - if (!in.isSequential()) - QSKIP("Standard input redirected.", SkipSingle); - QCOMPARE( in.pos(), (qint64)0 ); - QCOMPARE( in.size(), (qint64)0 ); + QCOMPARE( in.pos(), streamCurrentPosition(stdin) ); + QCOMPARE( in.size(), streamExpectedSize(QT_FILENO(stdin)) ); } { QFile out; out.open(stdout, QIODevice::WriteOnly); - if (!out.isSequential()) - QSKIP("Standard output redirected.", SkipSingle); - QCOMPARE( out.pos(), (qint64)0 ); - QCOMPARE( out.size(), (qint64)0 ); + QCOMPARE( out.pos(), streamCurrentPosition(stdout) ); + QCOMPARE( out.size(), streamExpectedSize(QT_FILENO(stdout)) ); } { QFile err; err.open(stderr, QIODevice::WriteOnly); - if (!err.isSequential()) - QSKIP("Standard error redirected.", SkipSingle); - QCOMPARE( err.pos(), (qint64)0 ); - QCOMPARE( err.size(), (qint64)0 ); + QCOMPARE( err.pos(), streamCurrentPosition(stderr) ); + QCOMPARE( err.size(), streamExpectedSize(QT_FILENO(stderr)) ); } } -- cgit v0.12