1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16from ast import Return 17import os 18import sys 19from unittest import suite 20 21G_SUITE = "" 22G_MAXCASECNT = 5 23G_MUSTPASSPATH = "mustpass/" 24G_MUSTPASSFILE = ".txt" 25G_TMPPATH = "template/" 26G_TMPDIRPATH = G_TMPPATH + "ace_ets_template" 27G_HAPDIRPATH = "../act_etc_component_" 28G_IMPORTTEST = "import {}Jsunit from \'./{}.test.ets\';" 29G_IMPORTTESTCASE = "{}Jsunit()" 30G_PATHDIRPATH = "/entry/src/main/ets/MainAbility/pages/" 31G_PAGETEMPPATH = "/pagetemplate.ets" 32G_PAGEATTRTEMPPATH = "/pageattrtemplate.ets" 33G_TESTDIRPATH = "/entry/src/main/ets/test/" 34G_TESTCASETEMPPATH = "/testcasetemplate.ets" 35G_TESTTEMPPATH = "/List.test.ets" 36G_LISTTESTETSPATH = "/entry/src/main/ets/test/List.test.ets" 37G_PAGENAMEPATTERN = "##PAGENAME" 38G_ATTRNAMEPATTERN = "##ATTRNAME" 39G_LOWATTRNAMEPATTERN = "##LOWATTRNAME" 40G_PAGENAMETESETPATTERN = "##PAGENAMETEST" 41G_PAGEFILENAMEPATTERN = "##PAGEFILENAME" 42G_SUITENAMEPATTERN = "##SUITENAME" 43G_PAGENAMETESTPATTERN = "##PAGENAMETEXT" 44G_CONFIGJSONPATH = "/entry/src/main/config.json" 45G_BUILDGNPATH = "/BUILD.gn" 46G_TESTJSONPATH = "/Test.json" 47G_INDEXETSPATH = "/entry/src/main/ets/MainAbility/pages/index.ets" 48 49def writemultestcase(): 50 print("writemultestcase") 51 writemultestcasecpp() 52 print("writemulbuildgn") 53 writemulbuildgn() 54 55def writemulbuildgn(): 56 #生成buildgn 57 for suiteitem in G_SUITE: 58 subcasecnt = 0 59 subattcnt = 0 60 subgncnt = 0 61 suiteitemcap = "".join(suiteitem[:1].upper() + suiteitem[1:]) 62 print("suite:", suiteitem, "----------------------") 63 #创建一个hap目录 64 hapdirpath = G_HAPDIRPATH + suiteitem.lower() 65 if os.path.exists(hapdirpath): 66 os.system(r"rm -rf {}".format(hapdirpath)) 67 os.system(r"cp {} {} -r".format(G_TMPDIRPATH, hapdirpath)) 68 else: 69 os.system(r"cp {} {} -r".format(G_TMPDIRPATH, hapdirpath)) 70 71 mustpass = G_MUSTPASSPATH + suiteitem + G_MUSTPASSFILE 72 with open(mustpass) as mastpassfile: 73 compdict = {"":1} 74 75 importtestheads = [] 76 importtests = [] 77 for line in mastpassfile: 78 if line[0] == "#": 79 continue 80 line = line.replace("\n", "") 81 linelist = line.split(",") 82 suitename = linelist[0] 83 casename = linelist[1] 84 85 if suitename in compdict: 86 #同文件里加属性 87 ind = compdict[suitename] 88 subattcnt += 1 89 #在page文件里追加attr内容 90 suitenamecap = "".join(suitename[:1].upper() + suitename[1:]) 91 casenamecap = "".join(casename[:1].upper() + casename[1:]) 92 pagename = suitenamecap + casenamecap 93 pagenametext = suitename + "-" + casenamecap 94 pagefilename = hapdirpath + G_PATHDIRPATH + suitename + ".ets" 95 attrtmp = G_TMPPATH + G_PAGEATTRTEMPPATH 96 with open(attrtmp, encoding="utf-8", mode="r") as attrtmpfile: 97 for attrtempline in attrtmpfile: 98 attrtempline = attrtempline.replace(" ", "\ ") 99 attrtempline = attrtempline.replace(G_PAGENAMETESTPATTERN, pagenametext) 100 attrtempline = attrtempline.replace(G_ATTRNAMEPATTERN, casename) 101 os.system(r"sed -i '{}a {}' {}".format(ind, attrtempline, pagefilename)) 102 ind += 1 103 compdict[suitename] = ind 104 105 else: 106 #创建不同页面 107 subgncnt += 1 108 subattcnt = 1 109 ind = 29 110 compdict[suitename] = ind 111 postfix = '%04d' % subgncnt 112 dirpath = suitename + postfix 113 temppagefilename = G_TMPPATH + G_PAGETEMPPATH 114 suitenamecap = "".join(suitename[:1].upper() + suitename[1:]) 115 casenamecap = "".join(casename[:1].upper() + casename[1:]) 116 pagename = suitename + casenamecap 117 capagename = suitenamecap + casenamecap 118 testname = pagename 119 pagefilename = hapdirpath + G_PATHDIRPATH + suitename + ".ets" 120 os.system(r"cp {} {}".format(temppagefilename, pagefilename)) 121 #替换page里的名称 122 os.system(r"sed -i 's/{}/{}/g' {}".format(G_PAGENAMEPATTERN, capagename, pagefilename)) 123 #追加page里的attr 124 pagenametext = suitename + "-" + casenamecap 125 attrtmp = G_TMPPATH + G_PAGEATTRTEMPPATH 126 with open(attrtmp, encoding="utf-8", mode="r") as attrtmpfile: 127 for attrtempline in attrtmpfile: 128 attrtempline = attrtempline.replace(" ", "\ ") 129 attrtempline = attrtempline.replace(G_PAGENAMETESTPATTERN, pagenametext) 130 attrtempline = attrtempline.replace(G_ATTRNAMEPATTERN, casename) 131 os.system(r"sed -i '{}a {}' {}".format(ind, attrtempline, pagefilename)) 132 ind += 1 133 compdict[suitename] = ind 134 135 temptestfilename = G_TMPPATH + G_TESTCASETEMPPATH 136 testfilename = hapdirpath + G_TESTDIRPATH + suitename + ".test.ets" 137 os.system(r"cp {} {}".format(temptestfilename, testfilename)) 138 #替换test里的名称 139 os.system(r"sed -i 's/{}/{}/g' {}".format(G_ATTRNAMEPATTERN, testname, testfilename)) 140 os.system(r"sed -i 's/{}/{}/g' {}".format(G_PAGENAMETESETPATTERN, testname+"Test", testfilename)) 141 os.system(r"sed -i 's/{}/{}/g' {}".format(G_PAGEFILENAMEPATTERN, pagename, testfilename)) 142 os.system(r"sed -i 's/{}/{}/g' {}".format(G_LOWATTRNAMEPATTERN, casename, testfilename)) 143 #替换config.json里的名称 144 configjsonname = hapdirpath + G_CONFIGJSONPATH 145 os.system(r"sed -i 's/{}/{}/g' {}".format(G_SUITENAMEPATTERN, suiteitemcap, configjsonname)) 146 os.system(r"sed -i 's/{}/{}/g' {}".format(G_PAGEFILENAMEPATTERN, casenamecap, configjsonname)) 147 #替换BUILD.gn里的名称 148 buildgnname = hapdirpath + G_BUILDGNPATH 149 os.system(r"sed -i 's/{}/{}/g' {}".format(G_SUITENAMEPATTERN, suiteitemcap, buildgnname)) 150 # formate gn 151 formatcmd = "cat "+buildgnname+" | ~/workspace/oh31/prebuilts/build-tools/linux-x86/bin/gn format --stdin > FORMAT_RESULT.gn;cp -f FORMAT_RESULT.gn "+buildgnname+";rm FORMAT_RESULT.gn" 152 os.system(formatcmd) 153 #替换index.ets里的名称 154 indexetsname = hapdirpath + G_INDEXETSPATH 155 os.system(r"sed -i 's/{}/{}/g' {}".format(G_SUITENAMEPATTERN, suiteitemcap, indexetsname)) 156 157 #替换Test.json里的名称 158 testjsonname = hapdirpath + G_TESTJSONPATH 159 os.system(r"sed -i 's/{}/{}/g' {}".format(G_SUITENAMEPATTERN, suiteitemcap, testjsonname)) 160 161 importtestheads.append(G_IMPORTTEST.format(testname, suitename)) 162 importtests.append(G_IMPORTTESTCASE.format(testname)) 163 subcasecnt += 1 164 #插入List.test.ets,装载多个testcase 165 ind = 16 166 for importtestsline in importtests: 167 os.system(r"sed -i '{}a \ \ {}' {}".format(ind, importtestsline, hapdirpath + G_LISTTESTETSPATH)) 168 ind += 1 169 ind = 14 170 for importtestheadsline in importtestheads: 171 os.system(r"sed -i '{}a {}' {}".format(ind, importtestheadsline, hapdirpath + G_LISTTESTETSPATH)) 172 ind += 1 173 174def writemultestcasecpp(): 175 #生成testcase 176 for suiteitem in G_SUITE: 177 print("suite:", suiteitem) 178 179def removedir(rootdir): 180 for root,dirs,files in os.walk(rootdir, topdown=False): 181 for name in files: 182 os.remove(os.path.join(rootdir,name)) 183 for name in dirs: 184 os.rmdir(os.path.join(rootdir,name)) 185 os.rmdir(rootdir) 186 187 188def printhelp(): 189 print("Need testsuite and codepattern:\n") 190 print("For example\n") 191 print("python3 .\GenerateTestCase.py apilack\n") 192 print("codepattern contains: apilack, attrlack\n") 193 194def new_report(bakdir, str): 195 files = os.listdir(bakdir) 196 lists = [] #列出目录的下所有文件和文件夹保存到lists 197 for f in files: 198 if "latest" in f: 199 continue 200 lists.append(f) 201 202 lists.sort(key=lambda fn:os.path.getmtime(bakdir + "/" + fn)) # 按时间排序 203 file_new = os.path.join(bakdir,lists[-1]) # 获取最新的文件保存到file_new 204 print("latest file:", file_new) 205 return file_new 206 207 208if __name__ == '__main__': 209 latestpath = new_report("reports", "") 210 tmpfile = "tmptestsuite.xml" 211 putfile = "/result/ActsLibuvTest.xml" 212 tasklogfile = "/log/task_log.log" 213 putdir = latestpath+putfile 214 tasklogpath = latestpath+tasklogfile 215 216 timelist = latestpath.split("/") 217 curtime = timelist[1].replace("\n", "") 218 testcaselist = [] 219 total = 0 220 passcnt = 0 221 failcnt = 0 222 suitename = "" 223 #读取最近的tasklog文件 224 with open(tasklogpath) as tasklogbuf: 225 #从tasklog文件中获取运行的testcase的信息 226 for tasklogline in tasklogbuf: 227 if "[Start test suite [" in tasklogline: 228 suitelist = tasklogline.split("[Start test suite [") 229 suiteitem = suitelist[1].split("]") 230 suitename = suiteitem[0] 231 if "[ok " in tasklogline: 232 freslist = tasklogline.split("ok") 233 numcase = freslist[1] 234 numcase = numcase.replace(" ","") 235 numcase = numcase.replace("]","") 236 caseline = numcase.split("-") 237 total +=1 238 passcnt += 1 239 testcaselist.append(caseline[1]+"-true") 240 if "[not ok " in tasklogline: 241 freslist = tasklogline.split("not ok") 242 numcase = freslist[1] 243 numcase = numcase.replace(" ","") 244 numcase = numcase.replace("]","") 245 caseline = numcase.split("-") 246 total +=1 247 failcnt += 1 248 testcaselist.append(caseline[1]+"-false") 249 #将testcase信息生成文件 250 xmlfile = open(tmpfile, mode='w+') 251 xmlfile.write("<?xml version='1.0' encoding='UTF-8'?>\n") 252 xmlfile.write("<testsuites name=\"{}\" timestamp=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" tests=\"{}\" ignored=\"0\" unavailable=\"0\" productinfo=\"{}\">\n".format(suitename, curtime, failcnt, total, "{}")) 253 xmlfile.write(" <testsuite name=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" ignored=\"0\" tests=\"{}\" message=\"\">\n".format(suitename, failcnt, total)) 254 for casename in testcaselist: 255 casename = casename.replace("\n","") 256 loccasename = casename.split("-") 257 recasename = loccasename[0] 258 casestate = loccasename[1] 259 xmlfile.write(" <testcase name=\"{}\" status=\"run\" time=\"0.0\" classname=\"{}\" result=\"{}\" level=\"1\" message=\"\" />\n".format(recasename, suitename, casestate)) 260 xmlfile.write(" </testsuite>\n") 261 xmlfile.write("</testsuites>\n") 262 xmlfile.close() 263 #将tmp文件替换xts框架的result 264 os.system(r"cp {} {}".format(tmpfile, putdir)) 265