|
| 1 | +package org.polystat.py2eo.parser |
| 2 | + |
| 3 | +import org.junit.jupiter.api.{AfterAll, Assertions, Test} |
| 4 | + |
| 5 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 6 | +import scala.concurrent.duration.Duration |
| 7 | +import scala.concurrent.{Await, Future} |
| 8 | +import scala.reflect.io.{Directory, File} |
| 9 | +import scala.sys.process.Process |
| 10 | +import scala.util.Properties |
| 11 | + |
| 12 | +object PyParserIT { |
| 13 | + |
| 14 | + /** Delete the directory by hand since Scala has some problems with it */ |
| 15 | + @AfterAll def cleanup(): Unit = this.directory.deleteRecursively |
| 16 | + |
| 17 | + /** Temporary directory where to conduct tests */ |
| 18 | + private val directory = Directory.makeTemp(prefix = "org.polystat.py2eo.").toAbsolute |
| 19 | + |
| 20 | + /** Repository with python tests */ |
| 21 | + private val repo = "https://github.com/python/cpython" |
| 22 | + |
| 23 | + /** Blacklisted test names; do not update them */ |
| 24 | + private val blacklisted = Set( |
| 25 | + // these are excluded because of some encoding problems in the lexer |
| 26 | + "test_unicode_identifiers.py", "test_source_encoding.py", |
| 27 | + "badsyntax_3131.py", "badsyntax_pep3120.py", |
| 28 | + "module_koi8_r.py", "module_iso_8859_1.py", |
| 29 | + // most of these are excluded because they do tests by comparing |
| 30 | + // stack traces as strings but code before parser-printer has different |
| 31 | + // line numbers than code after => traces are always different => |
| 32 | + // those tests cannot possibly pass |
| 33 | + "test_traceback.py", "test_dis.py", "test_zipfile.py", |
| 34 | + "test_multiprocessing_fork.py", "test_sys.py", |
| 35 | + "test_import.yaml", "test_strtod.py", "test_trace.py", |
| 36 | + "test_doctest.py", "test_concurrent_futures.py", "test_inspect.py", |
| 37 | + "test_tracemalloc.py", "test_multiprocessing_spawn.py", |
| 38 | + "test_sys_settrace.py", "test_multiprocessing_forkserver.py", |
| 39 | + "test_compileall.py", "test_asyncio.py", "test_unittest.py", |
| 40 | + "test_email.py", "test_tools.py", "test_atexit.py", "test_pdb.py", |
| 41 | + "test_logging.py", "test_coroutines.py", "test_tasks.py", |
| 42 | + |
| 43 | + "test_grammar.py", "test_headerregistry.py" |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +/** Parse and re-print tests from cpython repo and test them out */ |
| 48 | +final class PyParserIT { |
| 49 | + @Test def apply(): Unit = { |
| 50 | + val cpython = Directory(PyParserIT.directory / "cpython") |
| 51 | + Process(s"git clone --depth=1 --branch v3.8.10 ${PyParserIT.repo} $cpython").!! |
| 52 | + |
| 53 | + val tests = Directory(cpython / "Lib" / "test") |
| 54 | + .deepFiles |
| 55 | + .toList |
| 56 | + .filter(_.extension == "py") |
| 57 | + .filter(_.name.startsWith("test")) |
| 58 | + .filterNot(file => PyParserIT.blacklisted(file.name)) |
| 59 | + .map(reprint) |
| 60 | + |
| 61 | + tests.foreach { Await.result(_, Duration.Inf) } |
| 62 | + println(s"Total of ${tests.length} files transpiled") |
| 63 | + |
| 64 | + assume(Properties.isMac || Properties.isLinux) |
| 65 | + Process("./configure", cpython.jfile).!! |
| 66 | + Process(s"make -j ${sys.runtime.availableProcessors}", cpython.jfile).!! |
| 67 | + Process("make test", cpython.jfile).!! |
| 68 | + } |
| 69 | + |
| 70 | + /** Parses and reprints the given test and calls [[Assertions.fail]] if failed to parse it */ |
| 71 | + private def reprint(test: File): Future[Unit] = Future { |
| 72 | + Parse(test).map(PrintPython.print).fold(this.fail(test))(test writeAll _) |
| 73 | + } |
| 74 | + |
| 75 | + /** Prints the failed test name and calls [[Assertions.fail]] */ |
| 76 | + private def fail(test: File): Unit = { |
| 77 | + println(s"failed on ${test.name}") |
| 78 | + Assertions.fail() |
| 79 | + } |
| 80 | +} |
0 commit comments