def __init__(self, configuration): super(CoderUtil, self).__init__(configuration) self._projectUtil = ProjectUtil.ProjectUtil() self._genBatFile = refineFilePath(self._ascetInstPath + "/code-generation-cli.bat") self._logger.info("Generation bat file:\t" + self._genBatFile) self._systest = refineFilePath(self._workspace + "/SysTest") self._logger.info("System Test workspace:\t" + self._systest) self._cmdLine = self._config.get("common", "cmdlinepath") self._logger.info("Command line path:\t%s" % self._cmdLine) self._lst = refineFilePath(self._cmdLine + "/Lst") self._logger.info("Lst folder:\t" + self._lst) self._rs = refineFilePath(self._cmdLine + "/Res") self._logger.info("Res folder:\t" + self._rs) self._ref = refineFilePath(self._cmdLine + "/Ref") self._logger.info("Ref folder:\t" + self._ref) self._version = self.getVersion() self._logger.info("Version:\t" + self._version)
def testCmpCGenSource(self, projectName, switches="", relatedStories=[]): r""" This is a type of test case that comparing source code "src" after generating c code. Steps for this type: + Generate C Code + Copy "src" in gen to Res folder + Comparing src in Ref vs Res folder @param projectName: the name of ESDL project @return: boolean type """ refSrcFolder = refineFilePath(self._ref + "/" + projectName + "/src") resSrcFolder = refineFilePath(self._rs + "/" + projectName + "/src") projectPath = refineFilePath(self._systest + "/" + projectName) lstFilePath = refineFilePath(self._lst + "/" + projectName + ".lst") genSrcFolder = self._getCGenFolderPath(projectName) + "/src" err = self.generateCCode(projectPath, switches)[1] if "ERROR" in err: return False else: if exists(resSrcFolder): shutil.rmtree(resSrcFolder) shutil.copytree(genSrcFolder, resSrcFolder) rs = self._bc.quickCompareFilesInFolder(refSrcFolder, resSrcFolder, os.listdir(refSrcFolder)) self._createLstFile(lstFilePath, projectName, rs, self._version, relatedStories) return rs
def _appendLst(self, projectName): srcFile = refineFilePath(self._getCGenFolderPath(projectName) + "/%s.lst") % projectName desFile = refineFilePath(self._lst + "/%s.lst") % projectName try: with open(srcFile, 'r') as src: if not exists(desFile): copyfile(srcFile, desFile) else: with open(desFile, 'a') as des: for line in src.readlines(): if line.startswith("#"): des.write(line) except IOError as err: self._logger.error(str(err))
def _addMainC(self, projectName, startMethod=""): """ This method will add "main.c" file which contains main method e.g. int main { setVerion(1.2.0_4600) setFileName(UncontrainedArray,'src\\Project.txt', 'D:\\Workspace\\SysTest\\UnconstrainedArray\\Reference\UncontrainedArray.txt') UncontrainedArray() } @param projectName: Name of method @param startMethod: The method in ESDL project will be added to main c and it will be run firstly """ cgenProjectPath = self._getCGenFolderPath(projectName) if startMethod == "": startMethod = projectName refRs = self._findReferenceFile(self._systest + "/" + projectName).replace("\\", "\\\\") if refRs == "": refRs = refineFilePath(self._systest + "/{0}/{1}/Reference/{1}.txt").format(projectName,startMethod).replace(os.sep, os.sep + os.sep) content = r""" int main() { setVersion("%s"); setFileName("%s","%s","%s"); %s }""" % (self._version, projectName, refineFilePath("src//Project.txt"), refRs, (startMethod + "();") if startMethod != "" else "") mainFilePath = refineFilePath(cgenProjectPath + "/src/main.c") try: self._logger.info("Add main C to %s" % mainFilePath) with open(mainFilePath, 'w') as mainFile: mainFile.write(content) self._logger.info("Add main C to %s - SUCCEED" % mainFilePath) except IOError: self._logger.error("Add main.c to cgen project %s - FAILED" % cgenProjectPath) raise IOError
def _createSuiteDetails(self, suite): line = "ISOLAR CODER %s: <span style='color:%s'> %s </span> fail=%s pass=%s, time=%s<br>" refinedSuiteName = refineFilePath(suite.getSuiteName()) suitename = refinedSuiteName.rsplit(os.sep, 1)[1] npass = suite.countPass() nfail = suite.countFail() runtime = suite.getRuntime() line = line % (suitename.split(".")[0], "red" if nfail > 0 else "green", "failed" if nfail > 0 else "passed", nfail, npass, strftime("%H:%M:%S",time.gmtime(runtime))) return line
def testProject(self, projectName, startMethod, switches="", relatedStories=[]): r""" Starting point to run test. We have 2 types of test case: Test Positive + Generate C code + Compile and link + Run exe file (this will include comparing reference file in ESDL project Test Negative + Generate C code + Compared sorted GenErrLine.exe file (Ref vs Res folder) @param projectName: name of the ESDL project @param startMethod: the start method in ESDL file @param relatedStories: the list of stories associated with project name @return boolean type """ try: testRS = True; projectPath = refineFilePath(self._systest + "/" + projectName) rsfolder = self._rs + "/" + projectName if not exists(rsfolder): makedirs(rsfolder) refFile = refineFilePath(self._ref + "/" + projectName + "/GenErrLine.txt") resFile = refineFilePath(self._rs + "/" + projectName + "/GenErrLine.txt") lstFile = refineFilePath(self._lst + "/" + projectName + ".lst") # generate C code outGen, errGen,rsGen = self.generateCCode(projectPath, switches) if rsGen == True: # TEST POSITIVE TESTCASES # add main.c file to CGen folder self._addMainC(projectName, startMethod) # Compile and link CGen folder cgenPrjFolder = self._getCGenFolderPath(projectName) outCompile, errCompile, rsCompile = self._projectUtil.compileAndLink(self._make, cgenPrjFolder) with open(rsfolder + "/MakeStdErr.txt", "w") as makeErr: makeErr.write(errCompile) if rsCompile == False: makeErrFile = rsfolder + "/MakeErrLine.txt" with open(makeErrFile, "w") as data: data.write(errCompile) return rsCompile # Run exe runRs = self._runExe(cgenPrjFolder + "/" + projectName + "_CGen.exe") testRS = runRs[1] == "" with open(rsfolder + "/RunStdErr.txt", "w") as runStdErr, open(rsfolder + "/RunStdOut.txt", "w") as runStdOut: runStdOut.write(runRs[0]) runStdErr.write(runRs[1]) else: # TEST NEGATIVE TESTCASES # write errors to file and compare with reference sortedErrArr = self._sortErrorMsg(errGen.split(os.linesep)) self._rmCharAndSave(resFile, sortedErrArr, "ERROR - ") testRS = self._bc.quickCompareFile(refFile, resFile) return testRS except IOError as ex: self._logger.error(str(ex)) return False finally: self._createLstFile(lstFile, projectName, testRS , self._version, relatedStories)