|
| 1 | +package dotty.tools |
| 2 | +package repl |
| 3 | + |
| 4 | +import java.io.File |
| 5 | +import java.nio.file.{Path, Files} |
| 6 | +import java.util.Comparator |
| 7 | + |
| 8 | +import org.junit.{Test, Ignore, BeforeClass, AfterClass} |
| 9 | + |
| 10 | +import dotc.Driver |
| 11 | +import dotc.reporting.TestReporter |
| 12 | +import dotc.interfaces.Diagnostic.ERROR |
| 13 | +import vulpix.{TestConfiguration, TestFlags} |
| 14 | + |
| 15 | +/** Test that the REPL can shadow artifacts in the local filesystem on the classpath. |
| 16 | + * Since the REPL launches with the current directory on the classpath, stray .class |
| 17 | + * files containing definitions in the empty package will be in scope in the REPL. |
| 18 | + * Additionally, any subdirectories will be treated as package names in scope. |
| 19 | + * As this may come as a surprise to an unsuspecting user, we would like definitions |
| 20 | + * from the REPL session to shadow these names. |
| 21 | + * |
| 22 | + * Provided here is a framework for creating the filesystem artifacts to be shadowed |
| 23 | + * and running scripted REPL tests with them on the claspath. |
| 24 | + */ |
| 25 | +object ShadowingTests: |
| 26 | + def classpath = TestConfiguration.basicClasspath + File.pathSeparator + shadowDir |
| 27 | + def options = ReplTest.commonOptions ++ Array("-classpath", classpath) |
| 28 | + def shadowDir = dir.toAbsolutePath.toString |
| 29 | + |
| 30 | + def createSubDir(name: String): Path = |
| 31 | + val subdir = dir.resolve(name) |
| 32 | + try Files.createDirectory(subdir) |
| 33 | + catch case _: java.nio.file.FileAlreadyExistsException => |
| 34 | + assert(Files.isDirectory(subdir), s"failed to create shadowed subdirectory $subdir") |
| 35 | + subdir |
| 36 | + |
| 37 | + // The directory on the classpath containing artifacts to be shadowed |
| 38 | + private var dir: Path = null |
| 39 | + |
| 40 | + @BeforeClass def setupDir: Unit = |
| 41 | + dir = Files.createTempDirectory("repl-shadow") |
| 42 | + |
| 43 | + @AfterClass def tearDownDir: Unit = |
| 44 | + Files.walk(dir).sorted(Comparator.reverseOrder).forEach(Files.delete) |
| 45 | + dir = null |
| 46 | + |
| 47 | +class ShadowingTests extends ReplTest(options = ShadowingTests.options): |
| 48 | + // delete contents of shadowDir after each test |
| 49 | + override def cleanup: Unit = |
| 50 | + super.cleanup |
| 51 | + val dir = ShadowingTests.dir |
| 52 | + Files.walk(dir) |
| 53 | + .filter(_ != dir) |
| 54 | + .sorted(Comparator.reverseOrder) |
| 55 | + .forEach(Files.delete) |
| 56 | + |
| 57 | + /** Run a scripted REPL test with the compilation artifacts of `shadowed` on the classpath */ |
| 58 | + def shadowedScriptedTest(name: String, shadowed: String, script: String): Unit = |
| 59 | + compileShadowed(shadowed) |
| 60 | + testScript(name, script.linesIterator.toList) |
| 61 | + |
| 62 | + /** Compile the given source text and output to the shadow dir on the classpath */ |
| 63 | + private def compileShadowed(src: String): Unit = |
| 64 | + val file: Path = Files.createTempFile("repl-shadow-test", ".scala") |
| 65 | + Files.write(file, src.getBytes) |
| 66 | + |
| 67 | + val flags = |
| 68 | + TestFlags(TestConfiguration.basicClasspath, TestConfiguration.noCheckOptions) |
| 69 | + .and("-d", ShadowingTests.shadowDir) |
| 70 | + val driver = new Driver |
| 71 | + val reporter = TestReporter.reporter(System.out, logLevel = ERROR) |
| 72 | + driver.process(flags.all :+ file.toString, reporter) |
| 73 | + assert(!reporter.hasErrors, s"compilation of $file failed") |
| 74 | + Files.delete(file) |
| 75 | + end compileShadowed |
| 76 | + |
| 77 | + @Test def i7635 = shadowedScriptedTest(name = "<i7635>", |
| 78 | + shadowed = "class C(val c: Int)", |
| 79 | + script = |
| 80 | + """|scala> new C().c |
| 81 | + |1 | new C().c |
| 82 | + | | ^^^^^^^ |
| 83 | + | | missing argument for parameter c of constructor C in class C: (c: Int): C |
| 84 | + | |
| 85 | + |scala> new C(13).c |
| 86 | + |val res0: Int = 13 |
| 87 | + | |
| 88 | + |scala> class C { val c = 42 } |
| 89 | + |// defined class C |
| 90 | + | |
| 91 | + |scala> new C().c |
| 92 | + |val res1: Int = 42 |
| 93 | + |""".stripMargin |
| 94 | + ) |
| 95 | + |
| 96 | + @Ignore("not yet fixed") |
| 97 | + @Test def `shadow subdirectories on classpath` = |
| 98 | + // NB: Tests of shadowing of subdirectories on the classpath are only valid |
| 99 | + // when the subdirectories exist prior to initialization of the REPL driver. |
| 100 | + // In the tests below this is enforced by the call to `testScript` which |
| 101 | + // in turn invokes `ReplDriver#resetToInitial`. When testing interactively, |
| 102 | + // the subdirectories may be created before launching the REPL, or during |
| 103 | + // an existing session followed by the `:reset` command. |
| 104 | + |
| 105 | + ShadowingTests.createSubDir("foo") |
| 106 | + testScript(name = "<shadow-subdir-foo>", |
| 107 | + """|scala> val foo = 3 |
| 108 | + |val foo: Int = 3 |
| 109 | + | |
| 110 | + |scala> foo |
| 111 | + |val res0: Int = 3 |
| 112 | + |""".stripMargin.linesIterator.toList |
| 113 | + ) |
| 114 | + |
| 115 | + ShadowingTests.createSubDir("x") |
| 116 | + testScript(name = "<shadow-subdir-x>", |
| 117 | + """|scala> val (x, y) = (42, "foo") |
| 118 | + |val x: Int = 42 |
| 119 | + |val y: String = foo |
| 120 | + | |
| 121 | + |scala> if (true) x else y |
| 122 | + |val res0: Matchable = 42 |
| 123 | + |""".stripMargin.linesIterator.toList |
| 124 | + ) |
| 125 | + |
| 126 | + ShadowingTests.createSubDir("util") |
| 127 | + testScript(name = "<shadow-subdir-util>", |
| 128 | + """|scala> import util.Try |
| 129 | + |1 | import util.Try |
| 130 | + | | ^^^ |
| 131 | + | | value Try is not a member of util |
| 132 | + | |
| 133 | + |scala> object util { class Try { override def toString = "you've gotta try!" } } |
| 134 | + |// defined object util |
| 135 | + | |
| 136 | + |scala> import util.Try |
| 137 | + |scala> new Try |
| 138 | + |val res0: util.Try = you've gotta try! |
| 139 | + |""".stripMargin.linesIterator.toList |
| 140 | + ) |
| 141 | +end ShadowingTests |
0 commit comments