1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
// test-server.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../source/core/slang-secure-crt.h"
#include "../../slang-com-helper.h"
#include "../../source/core/slang-string.h"
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-writer.h"
#include "../../source/core/slang-string-util.h"
#include "../../source/core/slang-process-util.h"
#include "../../source/core/slang-shared-library.h"
#include "../../source/core/slang-test-tool-util.h"
#include "../../source/core/slang-http.h"
#include "../../source/compiler-core/slang-source-loc.h"
#include "../../source/compiler-core/slang-diagnostic-sink.h"
#include "../../source/compiler-core/slang-json-parser.h"
#include "../../source/compiler-core/slang-json-rpc.h"
#include "../../source/compiler-core/slang-json-value.h"
#include "test-server-diagnostics.h"
#include "tools/unit-test/slang-unit-test.h"
namespace TestServer
{
using namespace Slang;
class TestReporter : public ITestReporter
{
public:
// ITestReporter
virtual SLANG_NO_THROW void SLANG_MCALL startTest(const char* testName) SLANG_OVERRIDE { }
virtual SLANG_NO_THROW void SLANG_MCALL addResult(TestResult result)SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL addResultWithLocation(TestResult result, const char* testText, const char* file, int line) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL addResultWithLocation(bool testSucceeded, const char* testText, const char* file, int line) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL addExecutionTime(double time) SLANG_OVERRIDE { }
virtual SLANG_NO_THROW void SLANG_MCALL message(TestMessageType type, const char* message) SLANG_OVERRIDE;
virtual SLANG_NO_THROW void SLANG_MCALL endTest() SLANG_OVERRIDE { }
StringBuilder m_buf;
Index m_failCount = 0;
Index m_testCount = 0;
};
class TestServer
{
public:
typedef Slang::TestToolUtil::InnerMainFunc InnerMainFunc;
SlangResult init(int argc, const char* const* argv);
/// Can return nullptr if cannot create the session
slang::IGlobalSession* getGlobalSession();
/// Can return nullptr if cannot load the tool
ISlangSharedLibrary* loadSharedLibrary(const String& name, DiagnosticSink* sink = nullptr);
/// Get a unit test module. Returns nullptr if not found.
IUnitTestModule* getUnitTestModule(const String& name, DiagnosticSink* sink = nullptr);
/// Given a tool name return it's function pointer. Or nullptr on failure.
InnerMainFunc getToolFunction(const String& name, DiagnosticSink* sink = nullptr);
/// Execute the server
SlangResult execute();
/// Dtor
~TestServer();
protected:
SlangResult _executeSingle();
SlangResult _executeUnitTest(JSONContainer* container, const JSONValue& root);
SlangResult _executeTool(JSONContainer* container, const JSONValue& root);
SlangResult _writeResponse(JSONContainer* containers, const JSONValue& root);
bool m_quit = false;
ComPtr<slang::IGlobalSession> m_session;
Dictionary<String, ComPtr<ISlangSharedLibrary>> m_sharedLibraryMap; ///< Maps tool names to the dll
Dictionary<String, IUnitTestModule*> m_unitTestModules;
String m_exePath; ///< Path to executable
DiagnosticSink m_diagnosticSink;
SourceManager m_sourceManager;
RefPtr<HTTPPacketConnection> m_connection;
};
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!! TestServer !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
SlangResult TestServer::init(int argc, const char* const* argv)
{
m_exePath = Path::getParentDirectory(argv[0]);
RefPtr<Stream> stdinStream, stdoutStream;
Process::getStdStream(Process::StreamType::StdIn, stdinStream);
Process::getStdStream(Process::StreamType::StdOut, stdoutStream);
RefPtr<BufferedReadStream> readStream(new BufferedReadStream(stdinStream));
m_connection = new HTTPPacketConnection(readStream, stdoutStream);
m_sourceManager.initialize(nullptr, nullptr);
m_diagnosticSink.init(&m_sourceManager, &JSONLexer::calcLexemeLocation);
return SLANG_OK;
}
TestServer::~TestServer()
{
for (auto& pair : m_unitTestModules)
{
pair.Value->destroy();
}
}
slang::IGlobalSession* TestServer::getGlobalSession()
{
if (!m_session)
{
// Just create the global session in the regular way if there isn't one set
if (SLANG_FAILED(slang_createGlobalSession(SLANG_API_VERSION, m_session.writeRef())))
{
return nullptr;
}
TestToolUtil::setSessionDefaultPreludeFromExePath(m_exePath.getBuffer(), m_session);
}
return m_session;
}
ISlangSharedLibrary* TestServer::loadSharedLibrary(const String& name, DiagnosticSink* sink)
{
ComPtr<ISlangSharedLibrary> lib;
if (m_sharedLibraryMap.TryGetValue(name, lib))
{
return lib;
}
auto loader = DefaultSharedLibraryLoader::getSingleton();
auto toolPath = Path::combine(m_exePath, name);
ComPtr<ISlangSharedLibrary> sharedLibrary;
if (SLANG_FAILED(loader->loadSharedLibrary(toolPath.getBuffer(), sharedLibrary.writeRef())))
{
if (sink)
{
sink->diagnose(SourceLoc(), ServerDiagnostics::unableToLoadSharedLibrary, name);
}
return nullptr;
}
m_sharedLibraryMap.Add(name, sharedLibrary);
return sharedLibrary;
}
IUnitTestModule* TestServer::getUnitTestModule(const String& name, DiagnosticSink* sink)
{
auto unitTestModulePtr = m_unitTestModules.TryGetValue(name);
if (unitTestModulePtr)
{
return *unitTestModulePtr;
}
ISlangSharedLibrary* sharedLibrary = loadSharedLibrary(name, sink);
if (!sharedLibrary)
{
return nullptr;
}
UnownedStringSlice funcName = UnownedStringSlice::fromLiteral("slangUnitTestGetModule");
// get the unit test export name
UnitTestGetModuleFunc getModuleFunc = (UnitTestGetModuleFunc)sharedLibrary->findFuncByName(funcName.begin());
if (!getModuleFunc)
{
if (sink)
{
sink->diagnose(SourceLoc(), ServerDiagnostics::unableToFindFunctionInSharedLibrary, funcName);
}
return nullptr;
}
IUnitTestModule* testModule = getModuleFunc();
if (!testModule)
{
if (sink)
{
sink->diagnose(SourceLoc(), ServerDiagnostics::unableToGetUnitTestModule);
}
return nullptr;
}
m_unitTestModules.Add(name, testModule);
return testModule;
}
TestServer::InnerMainFunc TestServer::getToolFunction(const String& name, DiagnosticSink* sink)
{
StringBuilder sharedLibToolBuilder;
sharedLibToolBuilder.append(name);
sharedLibToolBuilder.append("-tool");
ISlangSharedLibrary* sharedLibrary = loadSharedLibrary(sharedLibToolBuilder, sink);
if (!sharedLibrary)
{
return nullptr;
}
UnownedStringSlice funcName = UnownedStringSlice::fromLiteral("innerMain");
auto func = (InnerMainFunc)sharedLibrary->findFuncByName(funcName.begin());
if (!func && sink)
{
sink->diagnose(SourceLoc(), ServerDiagnostics::unableToFindFunctionInSharedLibrary, funcName);
}
return func;
}
SlangResult TestServer::_writeResponse(JSONContainer* container, const JSONValue& root)
{
// TODO(JS): We may want a non indented style, to reduce size
JSONWriter writer(JSONWriter::IndentationStyle::Allman);
container->traverseRecursively(root, &writer);
const StringBuilder& builder = writer.getBuilder();
return m_connection->write(builder.getBuffer(), builder.getLength());
}
SlangResult TestServer::_executeSingle()
{
// Block waiting for content (or error/closed)
SLANG_RETURN_ON_FAIL(m_connection->waitForResult());
// If we don't have content, we can quit for now
if (!m_connection->hasContent())
{
return SLANG_OK;
}
auto content = m_connection->getContent();
UnownedStringSlice slice((const char*)content.begin(), content.getCount());
// Reset for parse
m_sourceManager.reset();
m_diagnosticSink.reset();
JSONContainer container(&m_sourceManager);
// Parse as RPC JSON
JSONValue root;
{
SlangResult res = JSONRPCUtil::parseJSON(slice, &container, &m_diagnosticSink, root);
// Consume that content/packet
m_connection->consumeContent();
if (SLANG_FAILED(res))
{
return _writeResponse(&container, JSONRPCUtil::createErrorResponse(&container, JSONRPCUtil::ErrorCode::InvalidRequest, UnownedStringSlice::fromLiteral("Unable to parse JSON")));
}
}
JSONRPCUtil::Call call;
{
SlangResult res = JSONRPCUtil::parseCall(&container, root, call);
if (SLANG_FAILED(res))
{
return _writeResponse(&container, JSONRPCUtil::createErrorResponse(&container, Index(JSONRPCUtil::ErrorCode::InvalidRequest), UnownedStringSlice::fromLiteral("Cannot parse call")));
}
}
const auto& method = call.method;
// Do different things
if (method == "quit")
{
m_quit = true;
return SLANG_OK;
}
else if (method == "unitTest")
{
SLANG_RETURN_ON_FAIL(_executeUnitTest(&container, root));
return SLANG_OK;
}
else if (method == "tool")
{
SLANG_RETURN_ON_FAIL(_executeTool(&container, root));
return SLANG_OK;
}
return SLANG_FAIL;
}
static Index _findTestIndex(IUnitTestModule* testModule, const String& name)
{
const auto testCount = testModule->getTestCount();
for (SlangInt i = 0; i < testCount; ++i)
{
auto testName = testModule->getTestName(i);
if (name == testName)
{
return Index(i);
}
}
return -1;
}
SlangResult TestServer::_executeUnitTest(JSONContainer* container, const JSONValue& root)
{
String moduleName;
String testName;
Int enabledApis = 0;
IUnitTestModule* testModule = getUnitTestModule(moduleName, &m_diagnosticSink);
if (!testModule)
{
return SLANG_FAIL;
}
Index testIndex = _findTestIndex(testModule, moduleName);
if (testIndex < 0)
{
m_diagnosticSink.diagnose(SourceLoc(), ServerDiagnostics::unableToFindTest, testName);
return SLANG_FAIL;
}
TestReporter testReporter;
testModule->setTestReporter(&testReporter);
// Assume we will used the shared session
slang::IGlobalSession* session = getGlobalSession();
if (!session)
{
return SLANG_FAIL;
}
UnitTestContext unitTestContext;
unitTestContext.slangGlobalSession = session;
unitTestContext.workDirectory = "";
unitTestContext.enabledApis = RenderApiFlags(enabledApis);
unitTestContext.executableDirectory = m_exePath.getBuffer();
auto testCount = testModule->getTestCount();
SLANG_ASSERT(testIndex >= 0 && testIndex < testCount);
UnitTestFunc testFunc = testModule->getTestFunc(testIndex);
try
{
testFunc(&unitTestContext);
}
catch (...)
{
testReporter.m_failCount++;
}
if (testReporter.m_failCount > 0)
{
// Write out to stderr...
auto writers = StdWriters::createDefault();
writers->getError().put(testReporter.m_buf.getUnownedSlice());
return SLANG_FAIL;
}
if (testReporter.m_testCount == 0)
{
return SLANG_E_NOT_AVAILABLE;
}
return SLANG_OK;
}
SlangResult TestServer::_executeTool(JSONContainer* container, const JSONValue& root)
{
String toolName;
auto func = getToolFunction(toolName, &m_diagnosticSink);
if (!func)
{
// Write out to diagnostics
return SLANG_FAIL;
}
// Assume we will used the shared session
slang::IGlobalSession* session = getGlobalSession();
if (!session)
{
return SLANG_FAIL;
}
// Get the args list
// Work out the args sent to the shared library
List<const char*> args;
RefPtr<StdWriters> stdWriters = StdWriters::createDefault();
const SlangResult res = func(stdWriters, session, int(args.getCount()), args.begin());
if (SLANG_FAILED(res))
{
return res;
}
return res;
}
SlangResult TestServer::execute()
{
DiagnosticSink diagnosticSink;
while (m_connection->isActive() && !m_quit)
{
SlangResult res = _executeSingle();
if (m_quit)
{
break;
}
if (SLANG_FAILED(res))
{
// Return a result
}
}
return SLANG_OK;
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TestReporter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
void TestReporter::message(TestMessageType type, const char* message)
{
if (type == TestMessageType::RunError ||
type == TestMessageType::TestFailure)
{
m_failCount++;
}
m_buf << message << "\n";
}
void TestReporter::addResultWithLocation(TestResult result, const char* testText, const char* file, int line)
{
if (result == TestResult::Fail)
{
addResultWithLocation(false, testText, file, line);
}
else
{
m_testCount++;
}
}
void TestReporter::addResultWithLocation(bool testSucceeded, const char* testText, const char* file, int line)
{
m_testCount++;
if (testSucceeded)
{
return;
}
m_buf << "[Failed]: " << testText << "\n";
m_buf << file << ":" << line << "\n";
m_failCount++;
}
void TestReporter::addResult(TestResult result)
{
if (result == TestResult::Fail)
{
m_failCount++;
}
}
SlangResult _execute(int argc, const char* const* argv)
{
TestServer server;
SLANG_RETURN_ON_FAIL(server.init(argc, argv));
SLANG_RETURN_ON_FAIL(server.execute());
return SLANG_OK;
}
} // namespace TestServer
int main(int argc, const char* const* argv)
{
return (int)Slang::TestToolUtil::getReturnCode(TestServer:: _execute(argc, argv));
}
|