TestCommandLine.cc
6.21 KB
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
/*
* TestCommandLine.cc
*
* Created on: Jan 10, 2013
* Author: f.casimir
*/
#include <string.h>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
//#include <boost/xpressive/xpressive.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include "CSlim/SlimUtil.h"
#include "CSlim/SlimList.h"
#include "CSlim/Fixtures.h"
#include "CSlimTools.hh"
#include "TestCommandLine.hh"
#include "PgmResult.hh"
using namespace AMDA::CSlimFixtures;
using namespace boost;
//using namespace xpressive;
namespace AMDA {
namespace CSlimFixtures {
int gTesterCount = 0;
TestCommandLine::TestCommandLine() : _logToDisplay(NULL) {
std::cerr << "Creation: " << gTesterCount++ << std::endl;
}
TestCommandLine::~TestCommandLine() {
// Delete all PgmResult
for ( auto c : _pgmResultList) {
delete c;
}
delete [] _logToDisplay;
}
TestCommandLine* TestCommandLine::from(void *fixtureStorage) {
return reinterpret_cast<TestCommandLine*>(fixtureStorage);
}
PgmResult* TestCommandLine::getLastPgmResult() {
return _pgmResultList[_pgmResultList.size()-1];
}
PgmResult* TestCommandLine::getNewPgmResult(const char* pAlias, const char* pCommand) {
PgmResult* lPgmResult = new PgmResult();
lPgmResult->_alias=pAlias;
lPgmResult->_cmd=pCommand;
_pgmResultList.push_back(lPgmResult);
/*
std::cerr << "Debug new Pgm result: " << lPgmResult->_alias
<< " output " << lPgmResult->_stdOutFile
<< " error " << lPgmResult->_stdErrFile
<< std::endl;
*/
return lPgmResult;
}
PgmResult* TestCommandLine::getPgmResult(const char* alias) {
PgmResult* lPgmResult = nullptr;
for ( auto pgm : _pgmResultList) {
if ( pgm->_alias == alias) { lPgmResult = pgm; break; }
}
return lPgmResult;
}
/**
* @class Formater
* @brief Used into TestCommandLine::interpret by the boostregex_replace and replace ${alias:variable}
*/
class Formater {
public:
std::string operator()(smatch const &what)
{
std::cerr << "debug: " << what[1].str() << "=>" << what[2].str() << "->" << what[3].str() << std::endl;
std::string result = what[1].str();
PgmResult* lPgm = _context->getPgmResult(what[2].str().c_str());
if (lPgm) {
const std::string& lValue = what[3].str();
if (lValue == "COMMAND") {
result=lPgm->_cmd;
} else {
if ( lValue == "RESULT") {
result=lPgm->_result;
} else {
if ( lValue == "OUTPUT") {
result=lPgm->_stdOutFile;
} else {
if ( lValue == "ERRPUT") {
result=lPgm->_stdErrFile;
}
}
}
}
}
//std::cerr << "Debug: " << what[1].str() << "=>" << what[2].str() << "->" << what[3].str() << "=>" << result << std::endl;
return result;
}
TestCommandLine* _context;
};
std::string TestCommandLine::interpret(std::string originalCommand) {
const char* pattern = "(\\$\\{([^:]+):([^}]+)\\})";
regex varReg(pattern, boost::regex::extended);
Formater fmt;
fmt._context = this;
try {
return boost::regex_replace( originalCommand, varReg, fmt);
} catch (...) {
return originalCommand;
}
return originalCommand;
}
} /* namespace CSlimFixtures */
} /* namespace AMDA */
extern "C" {
void* TestCommandLine_Create(StatementExecutor* /*errorHandler*/, SlimList* /*args*/) {
return new AMDA::CSlimFixtures::TestCommandLine();
}
void TestCommandLine_Destroy(void *fixture) {
delete AMDA::CSlimFixtures::TestCommandLine::from(fixture);
}
/**
* @brief Trace the Test Id.
*/
static char* referenceIdTest(void *fixture, SlimList *args) {
std::cout << std::endl <<"***************************************************************************"<<std::endl;
std::cout << "Test " << SlimList_GetStringAt(args, 0)<<std::endl;
std::cerr << "Test " << SlimList_GetStringAt(args, 0)<<std::endl;
TestCommandLine* context = TestCommandLine::from(fixture);
const char *chaine = SlimList_GetStringAt(args, 0);
std::stringstream ss;
for (unsigned int it=0 ; it < strlen(chaine); it++ ) {
if(isalnum(chaine[it])) {
ss << chaine[it];
}
else {
ss << "_";
}
}
context->_currentNameTest = ss.str();
return const_cast<char *>("");
}
/**
* @brief Execute the command send by FitNesse with the system function.
* @return the execution result.
*/
static char* executeCommandAlias(void *fixture, SlimList *args) {
TestCommandLine* context = TestCommandLine::from(fixture);
PgmResult* lPgmResult = context->getNewPgmResult(SlimList_GetStringAt(args, 1),SlimList_GetStringAt(args, 0));
std::stringstream ss;
ss << lPgmResult->_cmd << " > " << lPgmResult->_stdOutFile << " 2> " << lPgmResult->_stdErrFile;
std::string command = context->interpret(ss.str().c_str());
lPgmResult->_result = system(command.c_str());
lPgmResult->displayStdOut();
lPgmResult->displayStdErr();
return CSlimTools::traitReturnSystem(lPgmResult->_result);
}
/**
* @return Into a FitNesse TestCommandLine Script table this command return the content of the first argument if it is a file else the string "nothing to display".
*/
static char* readFile(void *fixture, SlimList *args) {
TestCommandLine* context = TestCommandLine::from(fixture);
std::string logFile = context->interpret(SlimList_GetStringAt(args, 0));
std::cerr << "Debug: interpret: " << logFile << std::endl;
delete [] context->_logToDisplay; context->_logToDisplay=nullptr;
if ( logFile[0] != '$') {
std::ifstream t(logFile.c_str()); // open input file
if (t) {
t.seekg(0, std::ios::end); // go to the end
int length = t.tellg(); // report location (this is the length)
t.seekg(0, std::ios::beg); // go back to the beginning
context->_logToDisplay = new char[length+1]; // allocate memory for a buffer of appropriate dimension
memset(context->_logToDisplay,0,sizeof(char)*(length+1));
t.read(context->_logToDisplay, length); // read the whole file into the buffer
t.close(); // close file handle
}
}
if (context->_logToDisplay) { return const_cast<char *>(context->_logToDisplay); }
return const_cast<char *>("nothing to display");
}
SLIM_CREATE_FIXTURE(TestCommandLine)
SLIM_FUNCTION(referenceIdTest)
SLIM_FUNCTION(executeCommandAlias)
SLIM_FUNCTION(readFile)
SLIM_END
} //extern "C"