1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "googletest.h"
27 
28 #include "eventspy.h"
29 
30 #include <processcreator.h>
31 #include <processexception.h>
32 #include <processstartedevent.h>
33 
34 #include <utils/hostosinfo.h>
35 
36 #include <QProcess>
37 
38 #include <future>
39 
40 using testing::NotNull;
41 
42 using ClangBackEnd::ProcessCreator;
43 using ClangBackEnd::ProcessException;
44 using ClangBackEnd::ProcessStartedEvent;
45 
46 namespace  {
47 
48 class ProcessCreator : public testing::Test
49 {
50 protected:
51     void SetUp();
52 
53 protected:
54     ::ProcessCreator processCreator;
55     QStringList m_arguments = {QStringLiteral("connectionName")};
56 };
57 
TEST_F(ProcessCreator,ProcessIsNotNull)58 TEST_F(ProcessCreator, ProcessIsNotNull)
59 {
60     auto future = processCreator.createProcess();
61     auto process = future.get();
62 
63     ASSERT_THAT(process.get(), NotNull());
64 }
65 
TEST_F(ProcessCreator,ProcessIsRunning)66 TEST_F(ProcessCreator, ProcessIsRunning)
67 {
68     auto future = processCreator.createProcess();
69     auto process = future.get();
70 
71     ASSERT_THAT(process->state(), QProcess::Running);
72 }
73 
TEST_F(ProcessCreator,ProcessPathIsNotExisting)74 TEST_F(ProcessCreator, ProcessPathIsNotExisting)
75 {
76     processCreator.setProcessPath(Utils::HostOsInfo::withExecutableSuffix(ECHOSERVER"fail"));
77 
78     auto future = processCreator.createProcess();
79     ASSERT_THROW(future.get(), ProcessException);
80 }
81 
TEST_F(ProcessCreator,ProcessStartIsSucessfull)82 TEST_F(ProcessCreator, ProcessStartIsSucessfull)
83 {
84     auto future = processCreator.createProcess();
85     ASSERT_NO_THROW(future.get());
86 }
87 
TEST_F(ProcessCreator,ProcessObserverGetsEvent)88 TEST_F(ProcessCreator, ProcessObserverGetsEvent)
89 {
90     EventSpy eventSpy(ProcessStartedEvent::ProcessStarted);
91     processCreator.setObserver(&eventSpy);
92     auto future = processCreator.createProcess();
93 
94     eventSpy.waitForEvent();
95 }
96 
TEST_F(ProcessCreator,TemporayPathIsSetForDefaultInitialization)97 TEST_F(ProcessCreator, TemporayPathIsSetForDefaultInitialization)
98 {
99     QString path = processCreator.temporaryDirectory().path();
100 
101     ASSERT_THAT(path.size(), Gt(0));
102 }
103 
TEST_F(ProcessCreator,TemporayPathIsResetted)104 TEST_F(ProcessCreator, TemporayPathIsResetted)
105 {
106     std::string oldPath = processCreator.temporaryDirectory().path().toStdString();
107 
108     processCreator.resetTemporaryDirectory();
109 
110     ASSERT_THAT(processCreator.temporaryDirectory().path().toStdString(),
111                 AllOf(Not(IsEmpty()), Ne(oldPath)));
112 }
113 
SetUp()114 void ProcessCreator::SetUp()
115 {
116     processCreator.setTemporaryDirectoryPattern("process-XXXXXXX");
117     processCreator.resetTemporaryDirectory();
118     processCreator.setProcessPath(Utils::HostOsInfo::withExecutableSuffix(ECHOSERVER));
119     processCreator.setArguments(m_arguments);
120 }
121 }
122