From 71a505870c1f5a4fc89a8ad4e4dc3bb008a35118 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Tue, 27 Sep 2016 21:29:20 +0200 Subject: [PATCH] server-mode: Add project data for unit tests Do some basic unit tests for "codemodel", "cmakeInputs" and "cache" commands of the cmake server. This just calls the commands right now and makes sure the server thinks it can reply to the request. The data itself is currently not validated. --- Tests/Server/CMakeLists.txt | 2 + Tests/Server/buildsystem1/CMakeLists.txt | 22 +++++++++++ Tests/Server/buildsystem1/foo.cpp | 5 +++ Tests/Server/buildsystem1/main.cpp | 5 +++ .../Server/buildsystem1/subdir/CMakeLists.txt | 5 +++ Tests/Server/buildsystem1/subdir/empty.cpp | 5 +++ Tests/Server/cmakelib.py | 20 +++++++--- Tests/Server/server-test.py | 37 +++++++++++-------- Tests/Server/tc_buildsystem1.json | 27 ++++++++++++++ 9 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 Tests/Server/buildsystem1/CMakeLists.txt create mode 100644 Tests/Server/buildsystem1/foo.cpp create mode 100644 Tests/Server/buildsystem1/main.cpp create mode 100644 Tests/Server/buildsystem1/subdir/CMakeLists.txt create mode 100644 Tests/Server/buildsystem1/subdir/empty.cpp create mode 100644 Tests/Server/tc_buildsystem1.json diff --git a/Tests/Server/CMakeLists.txt b/Tests/Server/CMakeLists.txt index 03f50429d..8913406fc 100644 --- a/Tests/Server/CMakeLists.txt +++ b/Tests/Server/CMakeLists.txt @@ -10,6 +10,7 @@ macro(do_test bsname file) "${CMAKE_SOURCE_DIR}/${file}" "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" + "${CMAKE_GENERATOR}" RESULT_VARIABLE test_result ) @@ -20,5 +21,6 @@ endmacro() do_test("test_handshake" "tc_handshake.json") do_test("test_globalSettings" "tc_globalSettings.json") +do_test("test_buildsystem1" "tc_buildsystem1.json") add_executable(Server empty.cpp) diff --git a/Tests/Server/buildsystem1/CMakeLists.txt b/Tests/Server/buildsystem1/CMakeLists.txt new file mode 100644 index 000000000..d690472d1 --- /dev/null +++ b/Tests/Server/buildsystem1/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.4) + +project(buildsystem2) + +set(var1 123) + +set(var2 345) + +add_executable(main main.cpp) + +add_executable(m_other main.cpp) + +add_library(foo foo.cpp) + +function(f1) +endfunction() + +set(var3 345) + +add_library(someImportedLib UNKNOWN IMPORTED) + +add_subdirectory(subdir) diff --git a/Tests/Server/buildsystem1/foo.cpp b/Tests/Server/buildsystem1/foo.cpp new file mode 100644 index 000000000..7f39d7175 --- /dev/null +++ b/Tests/Server/buildsystem1/foo.cpp @@ -0,0 +1,5 @@ + +int foo() +{ + return 0; +} diff --git a/Tests/Server/buildsystem1/main.cpp b/Tests/Server/buildsystem1/main.cpp new file mode 100644 index 000000000..766b7751b --- /dev/null +++ b/Tests/Server/buildsystem1/main.cpp @@ -0,0 +1,5 @@ + +int main() +{ + return 0; +} diff --git a/Tests/Server/buildsystem1/subdir/CMakeLists.txt b/Tests/Server/buildsystem1/subdir/CMakeLists.txt new file mode 100644 index 000000000..915731286 --- /dev/null +++ b/Tests/Server/buildsystem1/subdir/CMakeLists.txt @@ -0,0 +1,5 @@ +set(bar4 something) + +set(bar5 more) + +add_executable(ooo empty.cpp) diff --git a/Tests/Server/buildsystem1/subdir/empty.cpp b/Tests/Server/buildsystem1/subdir/empty.cpp new file mode 100644 index 000000000..7f39d7175 --- /dev/null +++ b/Tests/Server/buildsystem1/subdir/empty.cpp @@ -0,0 +1,5 @@ + +int foo() +{ + return 0; +} diff --git a/Tests/Server/cmakelib.py b/Tests/Server/cmakelib.py index 8beaeefa2..94384ebab 100644 --- a/Tests/Server/cmakelib.py +++ b/Tests/Server/cmakelib.py @@ -102,10 +102,20 @@ def waitForMessage(cmakeCommand, expected): sys.exit(-1) return packet -def waitForReply(cmakeCommand, originalType, cookie): - packet = waitForRawMessage(cmakeCommand) - if packet['cookie'] != cookie or packet['type'] != 'reply' or packet['inReplyTo'] != originalType: +def waitForReply(cmakeCommand, originalType, cookie, skipProgress): + gotResult = False + while True: + packet = waitForRawMessage(cmakeCommand) + t = packet['type'] + if packet['cookie'] != cookie or packet['inReplyTo'] != originalType: + sys.exit(1) + if t == 'message' or t == 'progress': + if skipProgress: + continue + if t == 'reply': + break sys.exit(1) + return packet def waitForError(cmakeCommand, originalType, cookie, message): @@ -126,10 +136,10 @@ def handshake(cmakeCommand, major, minor, source, build, generator, extraGenerat writePayload(cmakeCommand, { 'type': 'handshake', 'protocolVersion': version, 'cookie': 'TEST_HANDSHAKE', 'sourceDirectory': source, 'buildDirectory': build, 'generator': generator, 'extraGenerator': extraGenerator }) - waitForReply(cmakeCommand, 'handshake', 'TEST_HANDSHAKE') + waitForReply(cmakeCommand, 'handshake', 'TEST_HANDSHAKE', False) def validateGlobalSettings(cmakeCommand, cmakeCommandPath, data): - packet = waitForReply(cmakeCommand, 'globalSettings', '') + packet = waitForReply(cmakeCommand, 'globalSettings', '', False) capabilities = packet['capabilities'] diff --git a/Tests/Server/server-test.py b/Tests/Server/server-test.py index d2bf92ebb..72f82ba4c 100644 --- a/Tests/Server/server-test.py +++ b/Tests/Server/server-test.py @@ -1,24 +1,25 @@ -import sys, cmakelib, json +import sys, cmakelib, json, os, shutil debug = True cmakeCommand = sys.argv[1] testFile = sys.argv[2] sourceDir = sys.argv[3] -buildDir = sys.argv[4] +buildDir = sys.argv[4] + "/" + os.path.splitext(os.path.basename(testFile))[0] +cmakeGenerator = sys.argv[5] -print("SourceDir: ", sourceDir, " -- BuildDir: ", buildDir) +print("Test:", testFile, + "\n-- SourceDir:", sourceDir, + "\n-- BuildDir:", buildDir, + "\n-- Generator:", cmakeGenerator) + +if os.path.exists(buildDir): + shutil.rmtree(buildDir) proc = cmakelib.initProc(cmakeCommand) with open(testFile) as f: - testText = f.read() - testText = testText.replace('%BUILDDIR%', buildDir) - testText = testText.replace('%SOURCEDIR%', sourceDir) - testData = json.loads(testText) - -buildDir = sys.argv[3] -sourceDir = sys.argv[4] + testData = json.loads(f.read()) for obj in testData: if 'sendRaw' in obj: @@ -38,9 +39,11 @@ for obj in testData: if debug: print("Waiting for reply:", json.dumps(data)) originalType = "" cookie = "" + skipProgress = False; if 'cookie' in data: cookie = data['cookie'] if 'type' in data: originalType = data['type'] - cmakelib.waitForReply(proc, originalType, cookie) + if 'skipProgress' in data: skipProgress = data['skipProgress'] + cmakelib.waitForReply(proc, originalType, cookie, skipProgress) elif 'error' in obj: data = obj['error'] if debug: print("Waiting for error:", json.dumps(data)) @@ -68,8 +71,8 @@ for obj in testData: if debug: print("Doing handshake:", json.dumps(data)) major = -1 minor = -1 - generator = 'Ninja' - extraGenerator = 'CodeBlocks' + generator = cmakeGenerator + extraGenerator = '' sourceDirectory = sourceDir buildDirectory = buildDir if 'major' in data: major = data['major'] @@ -78,14 +81,18 @@ for obj in testData: if 'sourceDirectory' in data: sourceDirectory = data['sourceDirectory'] if 'generator' in data: generator = data['generator'] if 'extraGenerator' in data: extraGenerator = data['extraGenerator'] + if not os.path.isabs(buildDirectory): + buildDirectory = buildDir + "/" + buildDirectory + if not os.path.isabs(sourceDirectory): + sourceDirectory = sourceDir + "/" + sourceDirectory cmakelib.handshake(proc, major, minor, sourceDirectory, buildDirectory, generator, extraGenerator) elif 'validateGlobalSettings' in obj: data = obj['validateGlobalSettings'] if not 'buildDirectory' in data: data['buildDirectory'] = buildDir if not 'sourceDirectory' in data: data['sourceDirectory'] = sourceDir - if not 'generator' in data: data['generator'] = 'Ninja' - if not 'extraGenerator' in data: data['extraGenerator'] = 'CodeBlocks' + if not 'generator' in data: data['generator'] = cmakeGenerator + if not 'extraGenerator' in data: data['extraGenerator'] = '' cmakelib.validateGlobalSettings(proc, cmakeCommand, data) elif 'message' in obj: print("MESSAGE:", obj["message"]) diff --git a/Tests/Server/tc_buildsystem1.json b/Tests/Server/tc_buildsystem1.json new file mode 100644 index 000000000..08831b7c4 --- /dev/null +++ b/Tests/Server/tc_buildsystem1.json @@ -0,0 +1,27 @@ +[ +{ "message": "Testing globalSettings" }, + +{ "handshake": {"major": 1, "sourceDirectory":"buildsystem1","buildDirectory":"buildsystem1"} }, + +{ "message": "Configure:" }, +{ "send": { "type": "configure", "cookie":"CONFIG" } }, +{ "reply": { "type": "configure", "cookie":"CONFIG", "skipProgress":true } }, + +{ "message": "Compute:" }, +{ "send": { "type": "compute", "cookie":"COMPUTE" } }, +{ "reply": { "type": "compute", "cookie":"COMPUTE", "skipProgress":true } }, + +{ "message": "Codemodel:" }, +{ "send": { "type": "codemodel", "cookie":"CODEMODEL" } }, +{ "reply": { "type": "codemodel", "cookie":"CODEMODEL" } }, + +{ "message": "CMake Inputs:"}, +{ "send": { "type": "cmakeInputs", "cookie":"INPUTS" } }, +{ "reply": { "type": "cmakeInputs", "cookie":"INPUTS" } }, + +{ "message": "Cache:"}, +{ "send": { "type": "cache", "cookie":"CACHE" } }, +{ "reply": { "type": "cache", "cookie":"CACHE" } }, + +{ "message": "Everything ok." } +]