Skip to content

Commit

Permalink
Tests: Take screenshot after a failed test on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Aug 25, 2023
1 parent 14676e5 commit c20a1c2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,17 @@ jobs:
- name: Create gnupg directory for tests
run: mkdir -p ~/.gnupg

- name: Create screenshots directory for tests
run: mkdir -p '${{runner.workspace}}/screenshots'

- name: Run tests
env:
COPYQ_TESTS_POST_FAILURE_CMD: 'scrot ${{runner.workspace}}/screenshots/%time_%test.png'
working-directory: '${{runner.workspace}}/install/bin'
run: '${{github.workspace}}/utils/github/test-linux.sh'
with:
name: CopyQ.dmg
path: '${{runner.workspace}}/screenshots'

- name: Update coverage
if: matrix.coverage
Expand Down
35 changes: 32 additions & 3 deletions src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,10 @@ class TestInterfaceImpl final : public TestInterface {
QByteArray cleanup() override
{
m_ignoreError.clear();
addFailedTest();
if ( QTest::currentTestFailed() ) {
addFailedTest();
runPostFailureCommand();
}
return QByteArray();
}

Expand Down Expand Up @@ -702,8 +705,34 @@ class TestInterfaceImpl final : public TestInterface {
private:
void addFailedTest()
{
if ( QTest::currentTestFailed() )
m_failed.append( QString::fromUtf8(QTest::currentTestFunction()) );
m_failed.append( QString::fromUtf8(QTest::currentTestFunction()) );
}

void runPostFailureCommand()
{
QString cmd = m_env.value("COPYQ_TESTS_POST_FAILURE_CMD");
if ( cmd.isEmpty() )
return;

const auto dateFormat = "yyyy-MM-dd_hh_mm_ss";
const auto dateTime = QDateTime::currentDateTime();
const auto now = dateTime.toString(dateFormat);
cmd.replace("%time", now);

const auto test = QString::fromUtf8(QTest::currentTestFunction());
cmd.replace("%test", test);

qInfo() << QString::fromLatin1("Running post-failure command:") << cmd;

const auto args = cmd.split(' ');
QProcess p;
p.closeWriteChannel();
p.setProcessChannelMode(QProcess::ForwardedChannels);
p.start(args[0], args.mid(1), QIODevice::ReadWrite);
if ( !p.waitForFinished() ) {
qWarning() << "Post-failure command failed to finish in time";
terminateProcess(&p);
}
}

void verifyConfiguration()
Expand Down
1 change: 1 addition & 0 deletions utils/github/install-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ packages=(
# tests
xvfb
openbox
scrot
)

sudo apt-get update
Expand Down

0 comments on commit c20a1c2

Please sign in to comment.