summaryrefslogtreecommitdiffstats
path: root/tools/test-proxy/test-proxy-main.cpp
blob: 9a3523d71dce5f2d436ab4005281c43d207fc521 (plain)
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
// main.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 "tools/unit-test/slang-unit-test.h"

namespace TestProxy
{
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;
};


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++;
    }
}

static SlangResult _createSlangSession(const char* exePath, int argc, const char* const* argv, ComPtr<slang::IGlobalSession>& outSession)
{
    
    ComPtr<slang::IGlobalSession> session;

    // The sharedSession always has a pre-loaded stdlib, is sharedSession is not nullptr.
    // This differed test checks if the command line has an option to setup the stdlib.
    // If so we *don't* use the sharedSession, and create a new stdlib-less session just for this compilation. 
    if (TestToolUtil::hasDeferredStdLib(argc, argv))
    {
        SLANG_RETURN_ON_FAIL(slang_createGlobalSessionWithoutStdLib(SLANG_API_VERSION, session.writeRef()));
        TestToolUtil::setSessionDefaultPreludeFromExePath(exePath, session);
    }
    else
    {
        // Just create the global session in the regular way if there isn't one set
        SLANG_RETURN_ON_FAIL(slang_createGlobalSession(SLANG_API_VERSION, session.writeRef()));
        TestToolUtil::setSessionDefaultPreludeFromExePath(exePath, session);
    }

    outSession.swap(session);
    return SLANG_OK;
}

static void _makeStdStreamsUnbuffered()
{
    // Make these streams unbuffered
    setvbuf(stdout, nullptr, _IONBF, 0);
    setvbuf(stderr, nullptr, _IONBF, 0);
}

static SlangResult _outputCount(int argc, const char* const* argv)
{
    if (argc < 3)
    {
        return SLANG_FAIL;
    }
    // Get the count
    const Index count = StringToInt(argv[2]);

    // If we want to crash
    Index crashIndex = -1;
    if (argc > 3)
    {
        // When we crash, we want to make sure everything is flushed...
        _makeStdStreamsUnbuffered();
        crashIndex = StringToInt(argv[3]);
    }

    FILE* fileOut = stdout;

    StringBuilder buf;
    for (Index i = 0; i < count; ++i)
    {
        buf.Clear();
        buf << i << "\n";

        fwrite(buf.getBuffer(), 1, buf.getLength(), fileOut);

        if (i == crashIndex)
        {
            // Use to simulate a crash.
            SLANG_BREAKPOINT(0);
            throw;
        }
    }

    // NOTE! There is no flush here, we want to see if everything works if we just stream out.
    return SLANG_OK;
}

static SlangResult _outputReflect()
{
    // Read lines from std input.
    // When hit line with just 'end', terminate

    // Get in as Stream

    RefPtr<Stream> stdinStream;
    Process::getStdStream(Process::StreamType::StdIn, stdinStream);

    FILE* fileOut = stdout;

    List<Byte> buffer;

    Index lineCount = 0;
    Index startIndex = 0; 

    while (true)
    {
        SLANG_RETURN_ON_FAIL(StreamUtil::read(stdinStream, 0, buffer));

        while (true)
        {
            UnownedStringSlice slice((const char*)buffer.begin() + startIndex, (const char*)buffer.end());
            UnownedStringSlice line;

            if (!StringUtil::extractLine(slice, line) || slice.begin() == nullptr)
            {
                break;
            }

            // Process the line
            if (line == UnownedStringSlice::fromLiteral("end"))
            {
                return SLANG_OK;
            }

            // Write the text to the output stream
            fwrite(line.begin(), 1, line.getLength(), fileOut);
            fputc('\n', fileOut);
            
            lineCount++;

            // Move the start index forward
            const Index newStartIndex = slice.begin() ? Index(slice.begin() - (const char*)buffer.getBuffer()) : buffer.getCount();
            SLANG_ASSERT(newStartIndex > startIndex);
            startIndex = newStartIndex;
        }
    }
}

static SlangResult _httpReflect(int argc, const char* const* argv)
{
    SLANG_UNUSED(argc);
    SLANG_UNUSED(argv);

    RefPtr<Stream> stdinStream, stdoutStream;

    Process::getStdStream(Process::StreamType::StdIn, stdinStream);
    Process::getStdStream(Process::StreamType::StdOut, stdoutStream);

    RefPtr<BufferedReadStream> readStream(new BufferedReadStream(stdinStream));

    RefPtr<HTTPPacketConnection> packetConnection = new HTTPPacketConnection(readStream, stdoutStream);

    while (packetConnection->isActive())
    {
        // Block waiting for content (or error/closed)
        SLANG_RETURN_ON_FAIL(packetConnection->waitForContent());

        // If we have content do something with it
        if (packetConnection->hasContent())
        {
            auto content = packetConnection->getContent();

            // If it just holds 'end' then we are done
            const UnownedStringSlice slice((const char*)content.begin(), content.getCount());

            if (slice == UnownedStringSlice::fromLiteral("end"))
            {
                break;
            }

            // Else reflect it back
            SLANG_RETURN_ON_FAIL(packetConnection->write(content.begin(), content.getCount()));

            // Consume that content/packet
            packetConnection->consumeContent();
        }
    }

    return SLANG_OK;
}

static SlangResult execute(int argc, const char*const* argv)
{
    typedef Slang::TestToolUtil::InnerMainFunc InnerMainFunc;

    if (argc < 2)
    {
        return SLANG_FAIL;
    }

    String exePath = Path::getParentDirectory(argv[0]);

    // The 'exeName' of the tool. 
    const String toolName = argv[1];

    if (toolName == "reflect")
    {
        return _outputReflect();
    }

    if (toolName == "count")
    {
        return _outputCount(argc, argv);
    }

    if (toolName == "http-reflect")
    {
        return _httpReflect(argc, argv);
    }

    {
        StringBuilder sharedLibToolBuilder;
        sharedLibToolBuilder.append(toolName);
        sharedLibToolBuilder.append("-tool");

        auto toolPath = Path::combine(exePath, sharedLibToolBuilder);

        ComPtr<ISlangSharedLibrary> sharedLibrary;
        if (SLANG_SUCCEEDED(DefaultSharedLibraryLoader::getSingleton()->loadSharedLibrary(toolPath.getBuffer(), sharedLibrary.writeRef())))
        {
            // Assume we will used the shared session
            ComPtr<slang::IGlobalSession> session;
            SLANG_RETURN_ON_FAIL(_createSlangSession(argv[0], argc - 2, argv + 2, session));

            auto func = (InnerMainFunc)sharedLibrary->findFuncByName("innerMain");
            if (!func)
            {
                return SLANG_FAIL;
            }

            // Work out the args sent to the shared library
            List<const char*> args;
            args.add(argv[0]);
            args.addRange(argv + 2, Index(argc - 2));

            RefPtr<StdWriters> stdWriters = StdWriters::createDefault();

            const SlangResult res = func(stdWriters, session, int(args.getCount()), args.begin());
            return res;
        }
    }

    // Okay lets assume it's a unit test
    {
        auto toolPath = Path::combine(exePath, toolName);

        ComPtr<ISlangSharedLibrary> sharedLibrary;
        SLANG_RETURN_ON_FAIL(SLANG_SUCCEEDED(DefaultSharedLibraryLoader::getSingleton()->loadSharedLibrary(toolPath.getBuffer(), sharedLibrary.writeRef())));

        // get the unit test export name

        UnitTestGetModuleFunc getModuleFunc = (UnitTestGetModuleFunc)sharedLibrary->findFuncByName("slangUnitTestGetModule");
        if (!getModuleFunc)
            return SLANG_FAIL;

        IUnitTestModule* testModule = getModuleFunc();
        if (!testModule)
            return SLANG_FAIL;

        // argv[0] exe,
        // argv[1] tool
        // argv[2] test name
        // argv[3] test index
        // argv[4] the enabled apis

        if (argc < 5)
        {
            return SLANG_FAIL;
        }

        TestReporter testReporter;

        testModule->setTestReporter(&testReporter);

        Int enabledApis;
        SLANG_RETURN_ON_FAIL(StringUtil::parseInt(UnownedStringSlice(argv[4]), enabledApis));

        Int testIndex;
        SLANG_RETURN_ON_FAIL(StringUtil::parseInt(UnownedStringSlice(argv[3]), testIndex));

        // Assume we will used the shared session
        ComPtr<slang::IGlobalSession> session;
        SLANG_RETURN_ON_FAIL(_createSlangSession(argv[0], argc - 5, argv + 5, session));

        UnitTestContext unitTestContext;
        unitTestContext.slangGlobalSession = session;
        unitTestContext.workDirectory = "";
        unitTestContext.enabledApis = RenderApiFlags(enabledApis);
        unitTestContext.executableDirectory = 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;
    }

    return SLANG_E_NOT_AVAILABLE;
}

} // namespace TestProxy

int main(int argc, const char* const* argv)
{
    using namespace TestProxy;
    SlangResult res = execute(argc, argv);
    return (int)TestToolUtil::getReturnCode(res);
}