Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ci automated js ctests #1660

Merged
merged 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/include/core/scopymainwindow_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SCOPY_CORE_EXPORT ScopyMainWindow_API : public ApiObject
Q_INVOKABLE void switchTool(QString devID, QString toolName);
Q_INVOKABLE void switchTool(QString toolName);
Q_INVOKABLE void runScript(QString scriptPath, bool exitApp = true);
Q_INVOKABLE void runScriptList(QStringList scriptPathList, bool exitApp = true);

private:
static bool sortByUUID(const QString &k1, const QString &k2);
Expand Down
7 changes: 7 additions & 0 deletions core/src/cmdlinehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ int CmdLineHandler::handle(QCommandLineParser &parser, ScopyMainWindow_API &scop
QMetaObject::invokeMethod(&scopyApi, "runScript", Qt::QueuedConnection, Q_ARG(QString, scriptPath),
Q_ARG(bool, exitApp));
}

QStringList scriptListPath = parser.values("script-list");
if(!scriptListPath.isEmpty()) {
bool exitApp = !keepRunning;
QMetaObject::invokeMethod(&scopyApi, "runScriptList", Qt::QueuedConnection,
Q_ARG(QStringList, scriptListPath), Q_ARG(bool, exitApp));
}
return EXIT_SUCCESS;
}

Expand Down
12 changes: 12 additions & 0 deletions core/src/scopymainwindow_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ void ScopyMainWindow_API::runScript(QString scriptPath, bool exitApp)
qApp->exit(ret);
}

void ScopyMainWindow_API::runScriptList(QStringList scriptPathList, bool exitApp)
{
foreach(QString scriptPath, scriptPathList) {
runScript(scriptPath, false);
}

if(exitApp) {
int ret = EXIT_SUCCESS;
qApp->exit(ret);
}
}

const QString ScopyMainWindow_API::getScriptContent(QFile *file)
{
QTextStream stream(file);
Expand Down
20 changes: 20 additions & 0 deletions js/scopyDefaultTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//This import path is relative to where scopy is build
evaluateFile("../js/scpycommon.js")

function connectionTest(){
printToConsole("TEST CONNECTION")
connect()
disconnect()
}

function switchToolTest(){
printToConsole("TEST SWITCH TOOL")
connect()
scopy.switchTool("Time")
scopy.switchTool("DataLogger 0")
scopy.switchTool("Debugger")
disconnect()
}

connectionTest()
switchToolTest()
13 changes: 13 additions & 0 deletions js/scpycommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function connect(){
IonutMuthi marked this conversation as resolved.
Show resolved Hide resolved
//TEST WITH EMU
var deviceID = scopy.addDevice("", "ip:127.0.0.0")
//CONNECT TO DEVICE
scopy.connectDevice(deviceID)
msleep(1000)
}

function disconnect(){
//DISCONNECT FROM DEVICE
scopy.disconnectDevice()
exit(0)
}
29 changes: 29 additions & 0 deletions js/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /bin/bash
# set -xe
# get scopy buil path from CMakeCache.txt
IonutMuthi marked this conversation as resolved.
Show resolved Hide resolved
scopyBuildDir=$(grep 'SCOPY_BUILD_PATH' ../CMakeCache.txt | awk -F= '{print $2}')

isEmuRunning=TRUE

#if emu is not running start emu at provided path
if ! pgrep -x "iio-emu" > /dev/null; then
isEmuRunning=FALSE
if [ ! -z $1 ]; then
echo "############# EMU PATH IS " $1;
cd $1
iio-emu generic *.xml &
fi
fi

# run scopy functions of provided script
if [ ! -z $2 ]; then
cd $scopyBuildDir
QT_QPA_PLATFORM=offscreen ./scopy --script=$2
fi

#if emu was started by this script stop emu
if [ "$isEmuRunning" = FALSE ]; then
#stop emu
killall -9 iio-emu
fi

1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ int main(int argc, char *argv[])
parser.addVersionOption();
parser.addOptions({
{{"s", "script"}, "Run given script.", "script"},
{{"S", "script-list"}, "Run given script list.", "script-list"},
{{"r", "keep-running"}, "Keep the application session after running a certain script."},
{{"a", "accept-license"}, "Accept the license in advance."},
{{"l", "logfile"}, "Saves all the logging messages into a file.", "filename"},
Expand Down
5 changes: 5 additions & 0 deletions pluginbase/include/pluginbase/scopyjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QJSEngine>
#include <QObject>
#include <QSocketNotifier>
#include <QFile>

class QJSEngine;

Expand Down Expand Up @@ -58,6 +59,7 @@ class SCOPY_PLUGINBASE_EXPORT ScopyJS : public QObject
Q_INVOKABLE QString readFromConsole(const QString &text);
Q_INVOKABLE void returnToApplication();
Q_INVOKABLE void suppressScopyMessages(bool b);
Q_INVOKABLE QJSValue evaluateFile(const QString &path);

QJSEngine *engine();

Expand All @@ -83,6 +85,9 @@ public Q_SLOTS:
static ScopyJS *pinstance_;
static QLoggingCategory::CategoryFilter oldCategoryFilter;
static void jsCategoryFilter(QLoggingCategory *category);

private:
const QString getScriptContent(QFile *file);
};

} // namespace scopy
Expand Down
33 changes: 33 additions & 0 deletions pluginbase/src/scopyjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,37 @@ void ScopyJS::jsCategoryFilter(QLoggingCategory *category)
}
}

QJSValue ScopyJS::evaluateFile(const QString &path)
{
QTextStream out(stdout);
QFile file(path);
if(!file.open(QFile::ReadOnly)) {
out << "Unable to open the script file: " << path << Qt::endl;
return "";
}
const QString scriptContent = getScriptContent(&file);
QJSValue val = ScopyJS::GetInstance()->engine()->evaluate(scriptContent, path);
if(val.isError()) {
out << "Exception:" << val.toString() << Qt::endl;
} else if(!val.isUndefined()) {
out << val.toString() << Qt::endl;
}

out.flush();

return val;
}

const QString ScopyJS::getScriptContent(QFile *file)
{
QTextStream stream(file);
QString firstLine = stream.readLine();
if(!firstLine.startsWith("#!"))
stream.seek(0);

QString content = stream.readAll();
file->close();
return content;
}

#include "moc_scopyjs.cpp"
38 changes: 38 additions & 0 deletions plugins/datalogger/js/dataLoggerAutomatedTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//This import paht is relative to where scopy is build
IonutMuthi marked this conversation as resolved.
Show resolved Hide resolved
evaluateFile("../js/scpyDefaultJsFunctions.js")
IonutMuthi marked this conversation as resolved.
Show resolved Hide resolved
evaluateFile("../plugins/datalogger/js/dataLoggerFunctions.js")

function dataLogAndLoadTest(){
//TODO REPLACE WITH VAR
IonutMuthi marked this conversation as resolved.
Show resolved Hide resolved
const filePath = "../plugins/datalogger/js/test.csv"

printToConsole("FILE IS " + filePath)

//CONNECT TO EMU
connect()

msleep(1000)
//TEST DATA LOGGER
printToConsole(datalogger.showAvailableMonitors())

//CREATE NEW TOOL
msleep(1000)
var tool = "DataLogger 0"

if (tool !== "") {
//GET 2 CHANNELS OF THE TOOL
scopy.switchTool(tool)
var selectedMonitors = getMonitors(tool)
printToConsole("Monitors : " + selectedMonitors)
//LOG DATA ON THE TOOL CHANNELS
logData(tool, filePath, selectedMonitors)
msleep(1000)
//LOAD DATA FOR THE TOOL CHANNELS
loadData(tool, filePath, selectedMonitors)
msleep(1000)
}
disconnect()
}

dataLogAndLoadTest()

57 changes: 57 additions & 0 deletions plugins/datalogger/js/dataLoggerFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* log data test */
function logData(tool, filePath, selectedMonitors) {
datalogger.enableMonitorOfTool(tool, selectedMonitors[0])
datalogger.enableMonitorOfTool(tool, selectedMonitors[1])
datalogger.clearData()
msleep(500)
datalogger.setRunning(true)
msleep(1000)
datalogger.logAtPathForTool(tool, filePath)
datalogger.continuousLogAtPathForTool(tool, filePath)
msleep(5000)
datalogger.disableMonitorOfTool(tool, selectedMonitors[0])
datalogger.disableMonitorOfTool(tool, selectedMonitors[1])
datalogger.setRunning(false)
datalogger.stopContinuousLogForTool(tool)

printToConsole("Data logged for tool : " + tool + " at :" + filePath)
}

/* load data test */
function loadData(tool, filePath, selectedMonitors) {

datalogger.importDataFromPathForTool(tool, filePath)
msleep(500)
datalogger.enableMonitorOfTool(tool, "Import: test.csv:" + selectedMonitors[0])
datalogger.enableMonitorOfTool(tool, "Import: test.csv:" + selectedMonitors[1])

printToConsole("Data loaded for tool : " + tool + " from :" + filePath)
}

function getMonitors(tool){
var monitors = datalogger.showAvailableMonitors()
printToConsole("Monitors list: " + monitors)
var monitorList = monitors.split(/\n/)
var voltage0Monitor = monitorList[2]
printToConsole("Monitor1 : " + voltage0Monitor)
var voltage1Monitor = monitorList[3]
printToConsole("Monitor2 : " + voltage1Monitor)
var selectedMonitors = []
selectedMonitors[0] = voltage0Monitor
selectedMonitors[1] = voltage1Monitor

return selectedMonitors
}

function createTool(deviceID){
var newTool = datalogger.createTool()
if (newTool !== "") {
scopy.switchTool(deviceID, newTool)
printToConsole("New tool created: " + newTool)
} else {
printToConsole("ERROR: Tool creation failed")
exit(1)
}
return "DataLogger 1"
}

4 changes: 4 additions & 0 deletions resources/emuXml/pluto.xml

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
cmake_minimum_required(VERSION 3.5)

# JS AUTOMATED TESTS
option(ENABLE_AUTOMATED_TESTS "Enable JS Automated Tests" OFF)

if(ENABLE_AUTOMATED_TESTS)
# Set build path as CMAKE CACHE variable to use in tests
set(SCOPY_BUILD_PATH ${CMAKE_BINARY_DIR} CACHE STRING "SCOPY_BUILD_PATH" FORCE)

# SCOPY GENERIC TESTS
add_test(NAME "ScopyDefaultJSTests"
COMMAND bash ${CMAKE_SOURCE_DIR} / js / test.sh "${CMAKE_SOURCE_DIR}/resources/emuXml/"
IonutMuthi marked this conversation as resolved.
Show resolved Hide resolved
"${CMAKE_SOURCE_DIR}/js/scopyDefaultTests.js"
)

# PLUGIN SPECIFIC TESTS

# DATALOGGER
if(ENABLE_PLUGIN_DATALOGGER)
add_test(NAME "DataloggerLogAndLoadJSTests"
COMMAND bash ${CMAKE_SOURCE_DIR} / js / test.sh "${CMAKE_SOURCE_DIR}/resources/emuXml/"
"${CMAKE_SOURCE_DIR}/plugins/datalogger/js/dataLoggerAutomatedTest.js"
)
endif()

endif()
Loading