run.py (5667B)
1 #!/usr/bin/env python3 2 # Adapted from https://github.com/tc39/test262/blob/main/tools/generation/test/run.py 3 4 import contextlib 5 import os 6 import shutil 7 import subprocess 8 import sys 9 import tempfile 10 import unittest 11 12 import pytest 13 from mozunit import main 14 15 testDir = os.path.dirname(os.path.abspath(__file__)) 16 OUT_DIR = os.path.abspath(os.path.join(testDir, "..", "out")) 17 EXPECTED_DIR = os.path.join(testDir, "expected") 18 ex = os.path.join(testDir, "..", "test262-export.py") 19 importExec = os.path.join(testDir, "..", "test262-update.py") 20 test262Url = "https://github.com/tc39/test262.git" 21 22 23 @contextlib.contextmanager 24 def TemporaryDirectory(): 25 tmpDir = tempfile.mkdtemp() 26 try: 27 yield tmpDir 28 finally: 29 shutil.rmtree(tmpDir) 30 31 32 class TestExport(unittest.TestCase): 33 maxDiff = None 34 35 def exportScript(self): 36 abspath = os.path.abspath(os.path.join(testDir, "fixtures", "export")) 37 sp = subprocess.Popen( 38 [sys.executable, os.path.abspath(ex), abspath, "--out", OUT_DIR], 39 stdout=subprocess.PIPE, 40 cwd=os.path.join(testDir, ".."), 41 ) 42 stdout, stderr = sp.communicate() 43 return dict(stdout=stdout, stderr=stderr, returncode=sp.returncode) 44 45 def importLocal(self): 46 with TemporaryDirectory() as cloneDir: 47 branch = "smTempBranch" 48 # Clone Test262 to a local branch 49 subprocess.check_call(["git", "clone", "--depth=1", test262Url, cloneDir]) 50 # Checkout to a new branch 51 subprocess.check_call(["git", "-C", cloneDir, "checkout", "-b", branch]) 52 # Make changes on the new branch 53 # Remove test/language/export/escaped-from.js 54 subprocess.check_call([ 55 "git", 56 "-C", 57 cloneDir, 58 "rm", 59 "test/language/export/escaped-from.js", 60 ]) 61 # Rename test/language/export/escaped-default.js 62 subprocess.check_call([ 63 "git", 64 "-C", 65 cloneDir, 66 "mv", 67 "test/language/export/escaped-default.js", 68 "test/language/export/escaped-foobarbaz.js", 69 ]) 70 # Copy fixtures files 71 fixturesDir = os.path.join(testDir, "fixtures", "import", "files") 72 shutil.copytree(fixturesDir, os.path.join(cloneDir, "test", "temp42")) 73 # Stage and Commit changes 74 subprocess.check_call(["git", "-C", cloneDir, "add", "."]) 75 subprocess.check_call([ 76 "git", 77 "-C", 78 cloneDir, 79 "commit", 80 "-m", 81 '"local foo"', 82 ]) 83 84 # Run import script 85 print( 86 "%s %s --local %s --out %s" 87 % (sys.executable, importExec, cloneDir, OUT_DIR) 88 ) 89 sp = subprocess.Popen( 90 [sys.executable, importExec, "--local", cloneDir, "--out", OUT_DIR], 91 stdout=subprocess.PIPE, 92 stderr=subprocess.PIPE, 93 cwd=os.path.join(testDir, ".."), 94 ) 95 stdoutdata, stderrdata = sp.communicate() 96 97 return stdoutdata, stderrdata, sp.returncode, cloneDir 98 99 def isTestFile(self, filename: str): 100 return not ( 101 filename.startswith(".") 102 or filename.startswith("#") 103 or filename.endswith("~") 104 ) 105 106 def getFiles(self, path: str): 107 names: list[str] = [] 108 for root, _, fileNames in os.walk(path): 109 for fileName in filter(self.isTestFile, fileNames): 110 names.append(os.path.join(root, fileName)) 111 names.sort() 112 return names 113 114 def compareTrees(self, expectedPath: str, actualPath: str): 115 expectedFiles = self.getFiles(expectedPath) 116 actualFiles = self.getFiles(actualPath) 117 118 self.assertListEqual( 119 [os.path.relpath(x, expectedPath) for x in expectedFiles], 120 [os.path.relpath(x, actualPath) for x in actualFiles], 121 ) 122 123 for expectedFile, actualFile in zip(expectedFiles, actualFiles): 124 with open(expectedFile) as expectedHandle: 125 with open(actualFile) as actualHandle: 126 self.assertMultiLineEqual( 127 expectedHandle.read(), actualHandle.read(), expectedFile 128 ) 129 130 def compareContents(self, output: bytes, filePath: str, folder: str): 131 with open(filePath) as file: 132 expected = file.read() 133 134 expected = expected.replace("{{folder}}", folder) 135 self.assertMultiLineEqual(output.decode("utf-8"), expected) 136 137 def tearDown(self): 138 shutil.rmtree(OUT_DIR, ignore_errors=True) 139 140 def test_export(self): 141 result = self.exportScript() 142 self.assertEqual(result["returncode"], 0) 143 expectedPath = os.path.join(EXPECTED_DIR, "export") 144 actualPath = os.path.join(OUT_DIR, "tests", "export") 145 self.compareTrees(expectedPath, actualPath) 146 147 @pytest.mark.skip(reason="test fetches from github") 148 def test_import_local(self): 149 stdoutdata, stderrdata, returncode, folder = self.importLocal() 150 self.assertEqual(stderrdata, b"") 151 self.assertEqual(returncode, 0) 152 self.compareTrees( 153 os.path.join(EXPECTED_DIR, "import", "files", "local"), 154 os.path.join(OUT_DIR, "local"), 155 ) 156 self.compareContents( 157 stdoutdata, 158 os.path.join(testDir, "expected", "import", "output.txt"), 159 folder, 160 ) 161 162 163 if __name__ == "__main__": 164 main()