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