core/builtins/ciftest.cpp
changeset 66 2a78c4ff2eab
equal deleted inserted replaced
63:6a2083f7eeb8 66:2a78c4ff2eab
       
     1 // ciftest.cpp
       
     2 //
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "ciftest.h"
       
    14 #include "fshell.h"
       
    15 #include "command_factory.h"
       
    16 
       
    17 CCommandBase* CCmdCifTest::NewLC()
       
    18 	{
       
    19 	CCmdCifTest* self = new(ELeave) CCmdCifTest();
       
    20 	CleanupStack::PushL(self);
       
    21 	self->BaseConstructL();
       
    22 	return self;
       
    23 	}
       
    24 
       
    25 CCmdCifTest::~CCmdCifTest()
       
    26 	{
       
    27 	delete iCmd;
       
    28 	delete iParser;
       
    29 	delete iEnvForScript;
       
    30 	delete iCurrentCif;
       
    31 	iCifFiles.ResetAndDestroy();
       
    32 	}
       
    33 
       
    34 CCmdCifTest::CCmdCifTest()
       
    35 	: CCommandBase(EManualComplete | EReportAllErrors)
       
    36 	{
       
    37 	}
       
    38 
       
    39 const TDesC& CCmdCifTest::Name() const
       
    40 	{
       
    41 	_LIT(KName, "ciftest");	
       
    42 	return KName;
       
    43 	}
       
    44 
       
    45 void CCmdCifTest::ArgumentsL(RCommandArgumentList& aArguments)
       
    46 	{
       
    47 	aArguments.AppendStringL(iCmd, _L("command"));
       
    48 	}
       
    49 
       
    50 void CCmdCifTest::OptionsL(RCommandOptionList& aOptions)
       
    51 	{
       
    52 	aOptions.AppendBoolL(iVerbose, _L("verbose"));
       
    53 	aOptions.AppendBoolL(iKeepGoing, _L("keep-going"));
       
    54 	}
       
    55 
       
    56 void CCmdCifTest::DoRunL()
       
    57 	{
       
    58 	if (iCmd)
       
    59 		{
       
    60 		CCommandInfoFile* cif = CCommandInfoFile::NewL(FsL(), Env(), *iCmd);
       
    61 		TestCifL(cif); // Takes ownership
       
    62 		}
       
    63 	else
       
    64 		{
       
    65 		_LIT(KCifDir, "y:\\resource\\cif\\fshell\\");
       
    66 		TFindFile find(FsL());
       
    67 		CDir* dir = NULL;
       
    68 		TInt found = find.FindWildByDir(_L("*.cif"), KCifDir, dir);
       
    69 		while (found == KErrNone)
       
    70 			{
       
    71 			for (TInt i = 0; i < dir->Count(); i++)
       
    72 				{
       
    73 				iFileName.Copy(TParsePtrC(find.File()).DriveAndPath()); // The docs for TFindFile state you shouldn't need the extra TParsePtrC::DriveAndPath(). Sigh.
       
    74 				iFileName.Append((*dir)[i].iName);
       
    75 				iCifFiles.AppendL(iFileName.AllocLC());
       
    76 				CleanupStack::Pop();
       
    77 				}
       
    78 			delete dir;
       
    79 			dir = NULL;
       
    80 			found = find.FindWild(dir);
       
    81 			}
       
    82 		NextCif();
       
    83 		}
       
    84 	}
       
    85 
       
    86 void CCmdCifTest::NextCif()
       
    87 	{
       
    88 	if (iNextCif == iCifFiles.Count())
       
    89 		{
       
    90 		if (iVerbose)
       
    91 			{
       
    92 			Printf(_L("%d tests run, %d passes %d failures."), iPasses + iFailures, iPasses, iFailures);
       
    93 			if (iCifFiles.Count()) Printf(_L(" %d commands have no tests defined."), iCifFiles.Count() - iPasses - iFailures);
       
    94 			Printf(_L("\r\n"));
       
    95 			}
       
    96 		Complete(KErrNone);
       
    97 		}
       
    98 	else
       
    99 		{
       
   100 		CCommandInfoFile* cif = NULL;
       
   101 		TRAPD(err, cif = CCommandInfoFile::NewL(FsL(), *iCifFiles[iNextCif]));
       
   102 		if (!err)
       
   103 			{
       
   104 			TRAP(err, TestCifL(cif));
       
   105 			if (err) PrintError(err, _L("Error setting up test for CIF %S"), iCifFiles[iNextCif]);
       
   106 			}
       
   107 		iNextCif++;
       
   108 		
       
   109 		if (err)
       
   110 			{
       
   111 			iFailures++;
       
   112 			TestCompleted(err);
       
   113 			}
       
   114 		}
       
   115 	}
       
   116 
       
   117 void CCmdCifTest::TestCifL(CCommandInfoFile* aCif)
       
   118 	{
       
   119 	iCurrentCif = aCif;
       
   120 	if (iVerbose) Printf(_L("Checking %S\r\n"), &aCif->CifFileName());
       
   121 
       
   122 	const TDesC& scriptData = aCif->SmokeTest();
       
   123 	if (scriptData.Length() == 0)
       
   124 		{
       
   125 		if (iVerbose) Printf(_L("Cif has no smoketest section\r\n"));
       
   126 		TestCompleted(KErrNone);
       
   127 		return;
       
   128 		}
       
   129 
       
   130 	iEnvForScript = CEnvironment::NewL(Env());
       
   131 	iEnvForScript->SetL(_L("Error"), _L("fshell -e 'echo \"Test failed, env is:\" && env && error'"));
       
   132 	iEnvForScript->SetL(_L("Quiet"), _L(">/dev/null"));
       
   133 	iEnvForScript->SetL(_L("Silent"), _L("2>&1 >/dev/null"));
       
   134 	iEnvForScript->Remove(_L("Verbose")); // In case it's ended up in our parent env
       
   135 	if (iVerbose) iEnvForScript->SetL(_L("Verbose"), 1);
       
   136 	iFileName.Copy(aCif->CifFileName());
       
   137 	iFileName.Append(_L(":smoke-test"));
       
   138 	TParsePtrC parse(iFileName);
       
   139 	iEnvForScript->SetL(KScriptName, parse.NameAndExt());
       
   140 	iEnvForScript->SetL(KScriptPath, parse.DriveAndPath());
       
   141 	iEnvForScript->SetL(_L("0"), iFileName);
       
   142 
       
   143 	iParser = CParser::NewL(CParser::EExportLineNumbers, scriptData, IoSession(), Stdin(), Stdout(), Stderr(), *iEnvForScript, gShell->CommandFactory(), this, aCif->GetSmokeTestStartingLineNumber());
       
   144 	iParser->Start();
       
   145 	}
       
   146 
       
   147 void CCmdCifTest::HandleParserComplete(CParser& /*aParser*/, const TError& aError)
       
   148 	{
       
   149 	TInt err = aError.Error();
       
   150 	if (err)
       
   151 		{
       
   152 		iFailures++;
       
   153 		PrintError(err, _L("%S failed at line %d"), &aError.ScriptFileName(), aError.ScriptLineNumber());
       
   154 		}
       
   155 	else
       
   156 		{
       
   157 		if (iVerbose)
       
   158 			{
       
   159 			Printf(_L("Smoketest for %S completed ok.\r\n"), &iCurrentCif->Name());
       
   160 			}
       
   161 		iPasses++;
       
   162 		}
       
   163 	TestCompleted(err);
       
   164 	}
       
   165 
       
   166 void CCmdCifTest::TestCompleted(TInt aError)
       
   167 	{
       
   168 	// Delete interim data
       
   169 	delete iEnvForScript;
       
   170 	iEnvForScript = NULL;
       
   171 	delete iParser;
       
   172 	iParser = NULL;
       
   173 	delete iCurrentCif;
       
   174 	iCurrentCif = NULL;
       
   175 
       
   176 	if (aError == KErrNone || iKeepGoing)
       
   177 		{
       
   178 		// Async call NextCif()
       
   179 		TRequestStatus* stat = &iStatus;
       
   180 		User::RequestComplete(stat, KErrNone);
       
   181 		SetActive();
       
   182 		}
       
   183 	else
       
   184 		{
       
   185 		Complete(aError);
       
   186 		}
       
   187 	}
       
   188 
       
   189 void CCmdCifTest::RunL()
       
   190 	{
       
   191 	NextCif();
       
   192 	}