aboutsummaryrefslogtreecommitdiff
path: root/src/tools/rununits.cpp
blob: ee847d3e072dfdbe6865bf3c492208d478d9bb18 (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
#include <bu/sio.h>
#include <bu/json.h>
#include <bu/process.h>
#include <bu/optparser.h>
#include <bu/blob.h>
#include <bu/blobbuilder.h>
#include <bu/file.h>

#include <sys/types.h>
#include <dirent.h>

typedef Bu::List<Bu::Blob> BlobList;

Bu::Blob getSuiteName( const Bu::Blob &bSuiteExec )
{
    Bu::Process proc(
        Bu::Process::StdOut,
        bSuiteExec.getData(),
        bSuiteExec.getData(),
        "--print-name",
        NULL
        );

    Bu::String sResult = proc.readAll().trimWhitespace();
    Bu::Blob bRet( sResult.getStr(), sResult.getSize() );
    return bRet;
}

Bu::Json *runSuite( const Bu::Blob &bSuiteExec, Bu::Json *pSuiteStatus=NULL )
{
    BlobList lCmd;
    lCmd.append( bSuiteExec );
    lCmd.append( "--interop" );
    if( pSuiteStatus != NULL )
    {
        for( Bu::Json::iterator i = (*pSuiteStatus)["tests"].begin(); i; i++ )
        {
            lCmd.append( (**i)["name"].getString().get() );
        }
    }
    char **argv = new char*[lCmd.getSize()+1];
    int j = 0;
    for( BlobList::iterator i = lCmd.begin(); i; j++, i++ )
    {
        argv[j] = (*i).getData();
    }
    argv[lCmd.getSize()] = NULL;
    Bu::Process proc(
        Bu::Process::StdOut,
        bSuiteExec.getData(),
        argv
        );
    Bu::BlobBuilder bbReport;
    while( proc.isRunning() )
    {
        int iRead = 0;
        char dRead[4096];
        iRead = proc.read( dRead, 4096 );
        if( iRead > 0 )
        {
            bbReport.append( dRead, iRead );
        }
    }
    delete[] argv;
    Bu::Json *pReport = new Bu::Json();
    pReport->parse( bbReport.getBlob() );
    return pReport;
}

BlobList getSuitePaths( const Bu::Blob &bDir )
{
    BlobList lPaths;
    DIR *dir = opendir( bDir.getData() );
    if( dir == NULL )
    {
        Bu::println("Could not open directory: %1").arg( bDir );
        return lPaths;
    }
    struct dirent *de = NULL;
    while( (de = readdir( dir )) != NULL )
    {
        if( de->d_type != DT_REG )
            continue;

        Bu::BlobBuilder bbPath;
        bbPath.append( bDir );
        bbPath.append("/");
        bbPath.append( de->d_name );
        Bu::Blob bPath = bbPath.getBlob();
        if( access( bPath.getData(), X_OK ) != 0 )
            continue;

        lPaths.append( bPath );
    }
    closedir( dir );

    return lPaths;
}

int main( int argc, char *argv[] )
{
    Bu::Blob bDir("unit");
    bool bAll = false;
    bool bHasStatus = false;
    Bu::Json jsLastStatus;

    Bu::OptParser options;
    options.addOption( bDir, 'p', "path", "Directory to look for unit tests.");
    options.addOption( bAll, 'a', "all", "Execute all tests, ignoring state.");
    options.setOverride("all", "true");
    options.addHelpOption();
    options.parse( argc, argv );

    if( !bAll )
    {
        try
        {
            Bu::String sData =
                Bu::File("rununits.status.json", Bu::File::Read )
                .readAll();
            jsLastStatus.parse( sData );
            bHasStatus = true;
            Bu::println("Current status loaded, created: %1")
                .arg( jsLastStatus["date"].getString().get() );
        }
        catch(...)
        {
        }
    }

    BlobList lPaths = getSuitePaths( bDir );
    Bu::Hash<Bu::Blob, Bu::Json *> hStatusSuitesByPath;

    if( bHasStatus )
    {
        BlobList lNewPaths;
        for( Bu::Json::iterator i = jsLastStatus["suites"].begin(); i; i++ )
        {
            hStatusSuitesByPath.insert( (**i)["path"].getString().get(), (*i) );
        }
        for( BlobList::iterator i = lPaths.begin(); i; i++ )
        {
            if( hStatusSuitesByPath.has( *i ) )
                lNewPaths.append( *i );
        }
        lPaths = lNewPaths;
    }

    int iNumLen = Bu::String("%1").arg( lPaths.getSize() ).end().getSize();
    int iMaxSuiteName = 0;
    Bu::Hash<Bu::Blob, Bu::Blob> hName;
    for( BlobList::iterator i = lPaths.begin(); i; i++ )
    {
        Bu::Blob bSuiteName = getSuiteName( *i );
        if( iMaxSuiteName < bSuiteName.getSize() )
            iMaxSuiteName = bSuiteName.getSize();
        hName.insert( *i, bSuiteName );
    }

    Bu::List<Bu::Json *> lReport;

    int iTest = 0;
    for( BlobList::iterator i = lPaths.begin(); i; i++ )
    {
        iTest++;
        Bu::Blob bSuiteName = hName.get( *i );
        Bu::print("\rRunning test suite [%1/%2]: %3")
            .arg( iTest, Bu::Fmt().width( iNumLen ).right() )
            .arg( lPaths.getSize(), Bu::Fmt().width( iNumLen ) )
            .arg( bSuiteName, Bu::Fmt().width( iMaxSuiteName ).fill(' ').left() );
        Bu::sio << Bu::sio.flush;

        Bu::Json *pSuiteStatus = NULL;
        if( bHasStatus )
        {
            pSuiteStatus = hStatusSuitesByPath.get( *i );
        }
        Bu::Json *pReport = runSuite( (*i), pSuiteStatus );
        pReport->insert("path", *i );
        int iUnexpected = 0;
        int iTotal = 0;
        iTotal = (*pReport)["tests"].getSize();
        for( int j = 0; j < iTotal; j++ )
        {
            if( !((*pReport)["tests"][j]["expected"].getString() ==
                (*pReport)["tests"][j]["result"].getString()) )
            {
                iUnexpected++;
            }
        }
        if( iUnexpected == 0 )
        {
            delete pReport;
        }
        else
        {
            lReport.append( pReport );
        }
    }
    Bu::println("\rCompleted %1 unit test suites.%2")
        .arg( lPaths.getSize(), Bu::Fmt().width( iNumLen ) )
        .arg( Bu::Blob(""), Bu::Fmt().width( iMaxSuiteName ).fill(' ').left() );

    if( lReport.getSize() == 0 )
    {
        Bu::println("\nNothing unexpected in unit tests.");
        unlink("rununits.status.json");
    }
    else
    {
        Bu::Json jStatus( Bu::Json::Object );

        time_t tNow = time( NULL );
        jStatus.insert("date", Bu::String(ctime( &tNow )).trimWhitespace() );
        jStatus.insertArray("suites");
        for( Bu::List<Bu::Json *>::iterator i = lReport.begin(); i; i++ )
        {
            Bu::println("\nUnexpected results in: %1")
                .arg( (**i)["suite"].getString().get() );
            Bu::Json &jStSuite = jStatus["suites"].appendObject();
            jStSuite.insert("name", (**i)["suite"].getString() );
            jStSuite.insert("path", (**i)["path"].getString() );
            Bu::Json &jStTests = jStSuite.insertArray("tests");

            for( Bu::Json::iterator iTest = (**i)["tests"].begin();
                    iTest; iTest++ )
            {
                if( (**iTest)["expected"].getString() ==
                    (**iTest)["result"].getString() )
                {
                    continue;
                }
                Bu::Json &jsTest = jStTests.appendObject();
                jsTest.insert("name", (**iTest)["name"].getString() );
                jsTest.insert("result", (**iTest)["result"].getString() );
                jsTest.insert("expected", (**iTest)["expected"].getString() );

                Bu::println("  %1: unexpected %2")
                    .arg( (**iTest)["name"].getString().get() )
                    .arg( (**iTest)["result"].getString().get() );
                if( (**iTest).has("fail") )
                {
                    if( (**iTest)["fail"].has("action") )
                    {
                        Bu::println("    unitTest( %1 );")
                            .arg( (**iTest)["fail"]["action"].getString().get() );
                        Bu::println("    at %1:%2")
                            .arg( (**iTest)["fail"]["file"].getString().get() )
                            .arg( (int)(**iTest)["fail"]["line"].getNumber() );
                    }
                    else if( (**iTest)["fail"].has("what") )
                    {
                        Bu::println("    Unexpected exception: %1")
                            .arg( (**iTest)["fail"]["what"].getString().get() );
                    }
                }
                else
                {
                    Bu::println("    No further details.");
                }
            }
            delete *i;
        }

        Bu::File("rununits.status.json", Bu::File::WriteNew )
            .write( jStatus.toString() );
    }
    
    return 0;
}