Skip to content

Commit 00c945b

Browse files
committed
Refactored the cpython integration test
1 parent 9245a3c commit 00c945b

1 file changed

Lines changed: 44 additions & 31 deletions

File tree

parser/src/test/scala/org/polystat/py2eo/parser/ParserPrinterIT.scala

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
package org.polystat.py2eo.parser
22

3-
import org.junit.jupiter.api.Assertions.fail
4-
import org.junit.jupiter.api.{AfterEach, Test}
3+
import org.junit.jupiter.api.{AfterAll, Assertions, Test}
4+
import org.polystat.py2eo.parser.ParserPrinterIT.blacklisted
55

66
import scala.concurrent.ExecutionContext.Implicits.global
77
import scala.concurrent.duration.Duration
88
import scala.concurrent.{Await, Awaitable, Future}
9-
import scala.reflect.io.Directory
9+
import scala.reflect.io.{Directory, File}
1010
import scala.sys.process.Process
1111
import scala.util.Properties
1212

13-
/** Parse and re-print tests from cpython repo and test them out */
14-
final class ParserPrinterIT {
13+
object ParserPrinterIT {
14+
15+
/** Temporary directory where to conduct tests */
16+
private val directory = Directory.makeTemp(prefix = "org.polystat.py2eo.").toAbsolute
17+
18+
/** Delete the directory by hand since Scala has some problems with it */
19+
@AfterAll def cleanup(): Unit = ParserPrinterIT.directory.deleteRecursively
1520

16-
private val cpythonLink = "https://github.com/python/cpython"
17-
private val directory = Directory.makeTemp(prefix = "org.polystat.py2eo.")
18-
private val availableProcessors = sys.runtime.availableProcessors
21+
/** Repository with python tests */
22+
private val repo = "https://github.com/python/cpython"
23+
24+
/** Blacklisted test names; do not update them */
1925
private val blacklisted = Set(
2026
// these are excluded because of some encoding problems in the lexer
2127
"test_unicode_identifiers.py", "test_source_encoding.py",
2228
"badsyntax_3131.py", "badsyntax_pep3120.py",
2329
"module_koi8_r.py", "module_iso_8859_1.py",
24-
// most of these are excluded because they do tests by comparing stack traces as strings
25-
// but code before parser-printer has different line numbers than code after => traces are always different =>
30+
// most of these are excluded because they do tests by comparing
31+
// stack traces as strings but code before parser-printer has different
32+
// line numbers than code after => traces are always different =>
2633
// those tests cannot possibly pass
2734
"test_traceback.py", "test_dis.py", "test_zipfile.py",
2835
"test_multiprocessing_fork.py", "test_sys.py",
@@ -36,40 +43,46 @@ final class ParserPrinterIT {
3643

3744
"test_grammar.py", "test_headerregistry.py"
3845
)
46+
}
3947

40-
@Test def apply(): Unit = {
41-
val cpython = Directory(directory / "cpython")
48+
/** Parse and re-print tests from cpython repo and test them out */
49+
final class ParserPrinterIT {
4250

43-
Process(s"git clone $cpythonLink ${cpython.name}", directory.jfile).!!
44-
Process("git checkout v3.8.10", cpython.jfile).!!
51+
@Test def apply(): Unit = {
52+
val cpython = Directory(ParserPrinterIT.directory / "cpython")
4553

46-
val testsDirectory = Directory(cpython / "Lib" / "test")
47-
val tests = testsDirectory.deepFiles.filter(file => (file.extension == "py") && (file.name.startsWith("test")))
54+
Process(s"git clone --depth=1 --branch v3.8.10 ${ParserPrinterIT.repo} $cpython").!!
4855

49-
val futures = for {test <- tests if !blacklisted(test.name)} yield Future {
50-
println(s"parsed ${test.name}")
51-
Parse(test).map(PrintPython.print).fold(fail())(test writeAll _)
52-
}
56+
val tests = Directory(cpython / "Lib" / "test")
57+
.deepFiles
58+
.toList
59+
.filter(_.extension == "py")
60+
.filter(_.name.startsWith("test"))
61+
.filterNot(file => blacklisted(file.name))
62+
.map(reprint)
5363

54-
futures foreach await
64+
tests foreach await
5565

56-
println(s"Total of ${futures.length} files transpiled")
66+
println(s"Total of ${tests.length} files transpiled")
5767

5868
assume(Properties.isMac || Properties.isLinux)
5969
Process("./configure", cpython.jfile).!!
60-
Process(s"make -j ${availableProcessors + 2}", cpython.jfile).!!
70+
Process(s"make -j ${sys.runtime.availableProcessors}", cpython.jfile).!!
6171
Process("make test", cpython.jfile).!!
6272
}
6373

64-
// @AfterEach def cleanup(): Unit = {
65-
// directory.deleteRecursively
66-
// }
74+
/** Parses and reprints the given test and calls [[Assertions.fail]] if failed to parse it */
75+
private def reprint(test: File): Future[Unit] = Future {
76+
Parse(test).map(PrintPython.print).fold(fail(test))(test writeAll _)
77+
}
78+
79+
/** Prints the failed test name and calls [[Assertions.fail]] */
80+
private def fail(test: File): Unit = {
81+
println(s"failed on ${test.name}")
82+
Assertions.fail()
83+
}
6784

68-
/**
69-
* Await and return the result (of type `T`) of an [[Awaitable]]
70-
*
71-
* @param awaitable the [[Awaitable]] to be awaited
72-
*/
85+
/** Awaits and returns the result of an [[Awaitable]] */
7386
private def await[T](awaitable: Awaitable[T]): T = {
7487
Await.result(awaitable, Duration.Inf)
7588
}

0 commit comments

Comments
 (0)