1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Linguist of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtCore/qdir.h> |
30 | #include <QtCore/qdebug.h> |
31 | #include <QtCore/qfile.h> |
32 | #include <QtCore/qbytearray.h> |
33 | #include <QtCore/qjsondocument.h> |
34 | #include <QtCore/qlibraryinfo.h> |
35 | #include <QtCore/qprocess.h> |
36 | |
37 | #include <QtTest/qtest.h> |
38 | |
39 | class tst_qtattributionsscanner : public QObject |
40 | { |
41 | Q_OBJECT |
42 | public: |
43 | tst_qtattributionsscanner(); |
44 | |
45 | private slots: |
46 | void test_data(); |
47 | void test(); |
48 | |
49 | private: |
50 | void readExpectedFile(const QString &baseDir, const QString &fileName, QByteArray *content); |
51 | |
52 | QString m_cmd; |
53 | QString m_basePath; |
54 | }; |
55 | |
56 | |
57 | tst_qtattributionsscanner::tst_qtattributionsscanner() |
58 | { |
59 | QString binPath = QLibraryInfo::location(QLibraryInfo::BinariesPath); |
60 | m_cmd = binPath + QLatin1String("/qtattributionsscanner" ); |
61 | m_basePath = QFINDTESTDATA("testdata" ); |
62 | } |
63 | |
64 | |
65 | void tst_qtattributionsscanner::test_data() |
66 | { |
67 | QTest::addColumn<QString>(name: "input" ); |
68 | QTest::addColumn<QString>(name: "stdout_file" ); |
69 | QTest::addColumn<QString>(name: "stderr_file" ); |
70 | |
71 | QTest::newRow(dataTag: "good" ) |
72 | << QStringLiteral("good" ) |
73 | << QStringLiteral("good/expected.json" ) |
74 | << QStringLiteral("good/expected.error" ); |
75 | QTest::newRow(dataTag: "warnings (incomplete)" ) |
76 | << QStringLiteral("warnings/incomplete" ) |
77 | << QStringLiteral("warnings/incomplete/expected.json" ) |
78 | << QStringLiteral("warnings/incomplete/expected.error" ); |
79 | QTest::newRow(dataTag: "warnings (unknown attribute)" ) |
80 | << QStringLiteral("warnings/unknown" ) |
81 | << QStringLiteral("warnings/unknown/expected.json" ) |
82 | << QStringLiteral("warnings/unknown/expected.error" ); |
83 | QTest::newRow(dataTag: "singlefile" ) |
84 | << QStringLiteral("good/minimal/qt_attribution_test.json" ) |
85 | << QStringLiteral("good/minimal/expected.json" ) |
86 | << QStringLiteral("good/minimal/expected.error" ); |
87 | } |
88 | |
89 | void tst_qtattributionsscanner::readExpectedFile(const QString &baseDir, const QString &fileName, QByteArray *content) |
90 | { |
91 | QFile file(QDir(m_basePath).absoluteFilePath(fileName)); |
92 | QVERIFY2(file.open(QIODevice::ReadOnly | QIODevice::Text), "Could not open " + file.fileName().toLocal8Bit()); |
93 | *content = file.readAll(); |
94 | content->replace(before: "%{PWD}" , after: baseDir.toUtf8()); |
95 | } |
96 | |
97 | void tst_qtattributionsscanner::test() |
98 | { |
99 | QFETCH(QString, input); |
100 | QFETCH(QString, stdout_file); |
101 | QFETCH(QString, stderr_file); |
102 | |
103 | QString dir = QDir(m_basePath).absoluteFilePath(fileName: input); |
104 | if (QFileInfo(dir).isFile()) |
105 | dir = QFileInfo(dir).absolutePath(); |
106 | |
107 | QProcess proc; |
108 | const QStringList arguments{dir, "--output-format" , "json" }; |
109 | QString command = m_cmd + ' ' + arguments.join(sep: ' '); |
110 | QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
111 | env.insert(name: "QT_ATTRIBUTIONSSCANNER_TEST" , value: "1" ); |
112 | proc.setProcessEnvironment(env); |
113 | proc.start(program: m_cmd, arguments, mode: QIODevice::ReadWrite | QIODevice::Text); |
114 | |
115 | QVERIFY2(proc.waitForStarted(), qPrintable(command + QLatin1String(" :" ) + proc.errorString())); |
116 | QVERIFY2(proc.waitForFinished(30000), qPrintable(command)); |
117 | |
118 | QVERIFY2(proc.exitStatus() == QProcess::NormalExit, |
119 | "\"qtattributionsscanner " + m_cmd.toLatin1() + "\" crashed" ); |
120 | QVERIFY2(!proc.exitCode(), |
121 | "\"qtattributionsscanner " + m_cmd.toLatin1() + "\" exited with code " + |
122 | QByteArray::number(proc.exitCode())); |
123 | |
124 | { // compare error output |
125 | QByteArray stdErr = proc.readAllStandardError(); |
126 | stdErr.replace(before: QDir::separator().toLatin1(), c: "/" ); |
127 | |
128 | QByteArray expectedErrorOutput; |
129 | readExpectedFile(baseDir: dir, fileName: stderr_file, content: &expectedErrorOutput); |
130 | |
131 | QCOMPARE(stdErr, expectedErrorOutput); |
132 | } |
133 | |
134 | { // compare json output |
135 | QByteArray stdOut = proc.readAllStandardOutput(); |
136 | |
137 | QJsonParseError jsonError; |
138 | QJsonDocument actualJson = QJsonDocument::fromJson(json: stdOut, error: &jsonError); |
139 | QVERIFY2(!actualJson.isNull(), "Invalid output: " + jsonError.errorString().toLatin1()); |
140 | |
141 | QByteArray expectedOutput; |
142 | readExpectedFile(baseDir: dir, fileName: stdout_file, content: &expectedOutput); |
143 | QJsonDocument expectedJson = QJsonDocument::fromJson(json: expectedOutput); |
144 | |
145 | if (!QTest::qCompare(t1: actualJson, t2: expectedJson, actual: "actualJson" , expected: "expectedJson" , __FILE__, __LINE__)) { |
146 | qWarning() << "Actual (actualJson) :" << actualJson; |
147 | qWarning() << "Expected (expectedJson):" << expectedJson; |
148 | return; |
149 | } |
150 | } |
151 | } |
152 | |
153 | QTEST_MAIN(tst_qtattributionsscanner) |
154 | #include "tst_qtattributionsscanner.moc" |
155 | |