Skip to content

Commit 0e5c08c

Browse files
Added parameter allowing script.system to execute synchronously
1 parent 4002ac3 commit 0e5c08c

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
script.system("sleep 2 && echo 123 > test");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
script.system("sleep 2 && echo 123 > test", true);

tests/trikScriptRunnerTests/trikScriptRunnerTest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <QtCore/QScopedPointer>
1818
#include <QtCore/QEventLoop>
19+
#include <QtCore/QFile>
20+
#include <QtCore/QTimer>
1921

2022
#include <QtScript/QScriptContext>
2123
#include <QtScript/QScriptEngine>
@@ -70,6 +72,13 @@ void TrikScriptRunnerTest::runFromFile(const QString &fileName)
7072
run(fileContents);
7173
}
7274

75+
void TrikScriptRunnerTest::wait(int msec)
76+
{
77+
QEventLoop waitingLoop;
78+
QTimer::singleShot(msec, &waitingLoop, SLOT(quit()));
79+
waitingLoop.exec();
80+
}
81+
7382
TEST_F(TrikScriptRunnerTest, sanityCheck)
7483
{
7584
run("1 + 1");
@@ -79,3 +88,23 @@ TEST_F(TrikScriptRunnerTest, fileTest)
7988
{
8089
runFromFile("file-test.js");
8190
}
91+
92+
TEST_F(TrikScriptRunnerTest, asyncSystemTest)
93+
{
94+
QFile testFile("test");
95+
testFile.remove();
96+
ASSERT_FALSE(testFile.exists());
97+
runFromFile("async-system-test.js");
98+
ASSERT_FALSE(testFile.exists());
99+
wait(2100);
100+
ASSERT_TRUE(testFile.exists());
101+
}
102+
103+
TEST_F(TrikScriptRunnerTest, syncSystemTest)
104+
{
105+
QFile testFile("test");
106+
testFile.remove();
107+
ASSERT_FALSE(testFile.exists());
108+
runFromFile("sync-system-test.js");
109+
ASSERT_TRUE(testFile.exists());
110+
}

tests/trikScriptRunnerTests/trikScriptRunnerTest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TrikScriptRunnerTest : public testing::Test
3030

3131
void run(const QString &script);
3232
void runFromFile(const QString &fileName);
33-
33+
static void wait(int msec);
3434

3535
private:
3636
QScopedPointer<trikControl::BrickInterface> mBrick;

tests/trikScriptRunnerTests/trikScriptRunnerTests.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ OTHER_FILES += \
3030
uses(trikKernel trikControl trikScriptRunner trikNetwork)
3131

3232
copyToDestdir($$PWD/data/, now)
33+
34+
DISTFILES += \
35+
data/sync-system-test.js \
36+
data/async-system-test.js

trikScriptRunner/src/scriptExecutionControl.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ void ScriptExecutionControl::quit()
8888
emit quitSignal();
8989
}
9090

91-
void ScriptExecutionControl::system(const QString &command)
91+
void ScriptExecutionControl::system(const QString &command, bool synchronously)
9292
{
93-
QStringList args{"-c", command};
94-
QLOG_INFO() << "Running: " << "sh" << args;
95-
QProcess::startDetached("sh", args);
93+
if (!synchronously) {
94+
QStringList args{"-c", command};
95+
QLOG_INFO() << "Running: " << "sh" << args;
96+
QProcess::startDetached("sh", args);
97+
} else {
98+
QLOG_INFO() << "Running synchronously: " << command;
99+
::system(command.toStdString().c_str());
100+
}
96101
}
97102

98103
void ScriptExecutionControl::writeToFile(const QString &file, const QString &text)

trikScriptRunner/src/scriptExecutionControl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public slots:
5454
/// Aborts script execution.
5555
void quit();
5656

57-
/// Asynchronously execute given sh command.
58-
void system(const QString &command);
57+
/// Execute given sh command.
58+
void system(const QString &command, bool synchronously = false);
5959

6060
/// Appends given text to the end of a file.
6161
void writeToFile(const QString &file, const QString &text);

0 commit comments

Comments
 (0)