Debian Codegraph @

tests/auto/qtattributionsscanner/tst_qtattributionsscanner.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtCore/qdir.h>
5 #include <QtCore/qdebug.h>
6 #include <QtCore/qfile.h>
7 #include <QtCore/qbytearray.h>
8 #include <QtCore/qjsondocument.h>
9 #include <QtCore/qlibraryinfo.h>
10 #include <QtCore/qprocess.h>
11
12 #include <QtTest/qtest.h>
13
14 class tst_qtattributionsscanner : public QObject
15 {
16 Q_OBJECT
17 public:
18 tst_qtattributionsscanner();
19
20 private slots:
21 void test_data();
22 void test();
23
24 private:
25 void readExpectedFile(const QString &baseDir, const QString &fileName, QByteArray *content);
26
27 QString m_cmd;
28 QString m_basePath;
29 };
30
31
32 tst_qtattributionsscanner::tst_qtattributionsscanner()
33 {
34 QString binPath = QLibraryInfo::path(QLibraryInfo::LibraryExecutablesPath);
35 m_cmd = binPath + QLatin1String("/qtattributionsscanner");
36 m_basePath = QFINDTESTDATA("testdata");
37 }
38
39
40 void tst_qtattributionsscanner::test_data()
41 {
42 QTest::addColumn<QString>("input");
43 QTest::addColumn<bool>("success");
44 QTest::addColumn<QString>("stdout_file");
45 QTest::addColumn<QString>("stderr_file");
46
47 QTest::newRow("good") << QStringLiteral("good") << true << QStringLiteral("good/expected.json")
48 << QStringLiteral("good/expected.error");
49 QTest::newRow("warnings (incomplete)") << QStringLiteral("warnings/incomplete") << false
50 << QStringLiteral("warnings/incomplete/expected.json")
51 << QStringLiteral("warnings/incomplete/expected.error");
52 QTest::newRow("warnings (unknown attribute)")
53 << QStringLiteral("warnings/unknown") << false
54 << QStringLiteral("warnings/unknown/expected.json")
55 << QStringLiteral("warnings/unknown/expected.error");
56 QTest::newRow("singlefile") << QStringLiteral("good/minimal/qt_attribution_test.json") << true
57 << QStringLiteral("good/minimal/expected.json")
58 << QStringLiteral("good/minimal/expected.error");
59 QTest::newRow("variants") << QStringLiteral("good/variants/qt_attribution_test.json") << true
60 << QStringLiteral("good/variants/expected.json")
61 << QStringLiteral("good/variants/expected.error");
62 QTest::newRow("local_license") << QStringLiteral("good/local_license/qt_attribution_test.json") << true
63 << QStringLiteral("good/local_license/expected.json")
64 << QStringLiteral("good/local_license/expected.error");
65 }
66
67 void tst_qtattributionsscanner::readExpectedFile(const QString &baseDir, const QString &fileName, QByteArray *content)
68 {
69 QFile file(QDir(m_basePath).absoluteFilePath(fileName));
70 QVERIFY2(file.open(QIODevice::ReadOnly | QIODevice::Text), "Could not open " + file.fileName().toLocal8Bit());
71 *content = file.readAll();
72 content->replace("%{PWD}", baseDir.toUtf8());
73
74 QDir licensesDir(QStringLiteral(QTTOOLS_LICENSES_DIR));
75 content->replace("%{LICENSES_DIR}", licensesDir.canonicalPath().toUtf8());
76 }
77
78 void tst_qtattributionsscanner::test()
79 {
80 QFETCH(QString, input);
81 QFETCH(bool, success);
82 QFETCH(QString, stdout_file);
83 QFETCH(QString, stderr_file);
84
85 QString dir = QDir(m_basePath).absoluteFilePath(input);
86 if (QFileInfo(dir).isFile())
87 dir = QFileInfo(dir).absolutePath();
88
89 QProcess proc;
90 const QStringList arguments{dir, "--output-format", "json"};
91 QString command = m_cmd + ' ' + arguments.join(' ');
92 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
93 env.insert("QT_ATTRIBUTIONSSCANNER_TEST", "1");
94 proc.setProcessEnvironment(env);
95 proc.start(m_cmd, arguments, QIODevice::ReadWrite | QIODevice::Text);
96
97 QVERIFY2(proc.waitForStarted(), qPrintable(command + QLatin1String(" :") + proc.errorString()));
98 QVERIFY2(proc.waitForFinished(30000), qPrintable(command));
99
100 QVERIFY2(proc.exitStatus() == QProcess::NormalExit,
101 "\"qtattributionsscanner " + m_cmd.toLatin1() + "\" crashed");
102 QVERIFY2(success ? (proc.exitCode() == 0) : (proc.exitCode() != 0),
103 "\"qtattributionsscanner " + m_cmd.toLatin1() + "\" exited with code "
104 + QByteArray::number(proc.exitCode()));
105
106 { // compare error output
107 QByteArray stdErr = proc.readAllStandardError();
108 stdErr.replace(QDir::separator().toLatin1(), "/");
109
110 QByteArray expectedErrorOutput;
111 readExpectedFile(dir, stderr_file, &expectedErrorOutput);
112
113 QCOMPARE(stdErr, expectedErrorOutput);
114 }
115
116 if (proc.exitCode() == 0) {
117 // compare json output
118 QByteArray stdOut = proc.readAllStandardOutput();
119
120 QJsonParseError jsonError;
121 QJsonDocument actualJson = QJsonDocument::fromJson(stdOut, &jsonError);
122 QVERIFY2(!actualJson.isNull(), "Invalid output: " + jsonError.errorString().toLatin1());
123
124 QByteArray expectedOutput;
125 readExpectedFile(dir, stdout_file, &expectedOutput);
126 QJsonDocument expectedJson = QJsonDocument::fromJson(expectedOutput);
127
128 if (!QTest::qCompare(actualJson, expectedJson, "actualJson", "expectedJson", __FILE__, __LINE__)) {
129 qWarning() << "Actual (actualJson) :" << actualJson;
130 qWarning() << "Expected (expectedJson):" << expectedJson;
131 return;
132 }
133 }
134 }
135
136 QTEST_MAIN(tst_qtattributionsscanner)
137 #include "tst_qtattributionsscanner.moc"