| 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", "-C", cloneDir, "rm", "test/language/export/escaped-from.js"] |
| 56 |
) |
| 57 |
# Rename test/language/export/escaped-default.js |
| 58 |
subprocess.check_call( |
| 59 |
[ |
| 60 |
"git", |
| 61 |
"-C", |
| 62 |
cloneDir, |
| 63 |
"mv", |
| 64 |
"test/language/export/escaped-default.js", |
| 65 |
"test/language/export/escaped-foobarbaz.js", |
| 66 |
] |
| 67 |
) |
| 68 |
# Copy fixtures files |
| 69 |
fixturesDir = os.path.join(testDir, "fixtures", "import", "files") |
| 70 |
shutil.copytree(fixturesDir, os.path.join(cloneDir, "test", "temp42")) |
| 71 |
# Stage and Commit changes |
| 72 |
subprocess.check_call(["git", "-C", cloneDir, "add", "."]) |
| 73 |
subprocess.check_call( |
| 74 |
["git", "-C", cloneDir, "commit", "-m", '"local foo"'] |
| 75 |
) |
| 76 |
|
| 77 |
# Run import script |
| 78 |
print( |
| 79 |
"%s %s --local %s --out %s" |
| 80 |
% (sys.executable, importExec, cloneDir, OUT_DIR) |
| 81 |
) |
| 82 |
sp = subprocess.Popen( |
| 83 |
[sys.executable, importExec, "--local", cloneDir, "--out", OUT_DIR], |
| 84 |
stdout=subprocess.PIPE, |
| 85 |
stderr=subprocess.PIPE, |
| 86 |
cwd=os.path.join(testDir, ".."), |
| 87 |
) |
| 88 |
stdoutdata, stderrdata = sp.communicate() |
| 89 |
|
| 90 |
return stdoutdata, stderrdata, sp.returncode, cloneDir |
| 91 |
|
| 92 |
def isTestFile(self, filename: str): |
| 93 |
return not ( |
| 94 |
filename.startswith(".") |
| 95 |
or filename.startswith("#") |
| 96 |
or filename.endswith("~") |
| 97 |
) |
| 98 |
|
| 99 |
def getFiles(self, path: str): |
| 100 |
names: list[str] = [] |
| 101 |
for root, _, fileNames in os.walk(path): |
| 102 |
for fileName in filter(self.isTestFile, fileNames): |
| 103 |
names.append(os.path.join(root, fileName)) |
| 104 |
names.sort() |
| 105 |
return names |
| 106 |
|
| 107 |
def compareTrees(self, expectedPath: str, actualPath: str): |
| 108 |
expectedFiles = self.getFiles(expectedPath) |
| 109 |
actualFiles = self.getFiles(actualPath) |
| 110 |
|
| 111 |
self.assertListEqual( |
| 112 |
[os.path.relpath(x, expectedPath) for x in expectedFiles], |
| 113 |
[os.path.relpath(x, actualPath) for x in actualFiles], |
| 114 |
) |
| 115 |
|
| 116 |
for expectedFile, actualFile in zip(expectedFiles, actualFiles): |
| 117 |
with open(expectedFile) as expectedHandle: |
| 118 |
with open(actualFile) as actualHandle: |
| 119 |
self.assertMultiLineEqual( |
| 120 |
expectedHandle.read(), actualHandle.read(), expectedFile |
| 121 |
) |
| 122 |
|
| 123 |
def compareContents(self, output: bytes, filePath: str, folder: str): |
| 124 |
with open(filePath) as file: |
| 125 |
expected = file.read() |
| 126 |
|
| 127 |
expected = expected.replace("{{folder}}", folder) |
| 128 |
self.assertMultiLineEqual(output.decode("utf-8"), expected) |
| 129 |
|
| 130 |
def tearDown(self): |
| 131 |
shutil.rmtree(OUT_DIR, ignore_errors=True) |
| 132 |
|
| 133 |
def test_export(self): |
| 134 |
result = self.exportScript() |
| 135 |
self.assertEqual(result["returncode"], 0) |
| 136 |
expectedPath = os.path.join(EXPECTED_DIR, "export") |
| 137 |
actualPath = os.path.join(OUT_DIR, "tests", "export") |
| 138 |
self.compareTrees(expectedPath, actualPath) |
| 139 |
|
| 140 |
@pytest.mark.skip(reason="test fetches from github") |
| 141 |
def test_import_local(self): |
| 142 |
stdoutdata, stderrdata, returncode, folder = self.importLocal() |
| 143 |
self.assertEqual(stderrdata, b"") |
| 144 |
self.assertEqual(returncode, 0) |
| 145 |
self.compareTrees( |
| 146 |
os.path.join(EXPECTED_DIR, "import", "files", "local"), |
| 147 |
os.path.join(OUT_DIR, "local"), |
| 148 |
) |
| 149 |
self.compareContents( |
| 150 |
stdoutdata, |
| 151 |
os.path.join(testDir, "expected", "import", "output.txt"), |
| 152 |
folder, |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |