|
| 1 | +package test |
| 2 | + |
| 3 | +import org.junit.Assert._ |
| 4 | +import org.junit.{Before, After, Test} |
| 5 | + |
| 6 | +import scala.io.Source |
| 7 | +import scala.sys.process.{Process, ProcessLogger} |
| 8 | +import java.io.{File => JFile, FileNotFoundException} |
| 9 | + |
| 10 | +class TestDotc { |
| 11 | + private val lineSep = util.Properties.lineSeparator |
| 12 | + private def doUnlessWindows(op: => Unit) = |
| 13 | + if (!System.getProperty("os.name").toLowerCase.contains("windows")) |
| 14 | + op |
| 15 | + else |
| 16 | + Console.err.println("[warn] Could not perform test, windows batch-scripts not available") |
| 17 | + |
| 18 | + private def executeScript(script: String): (Int, String) = { |
| 19 | + val sb = new StringBuilder |
| 20 | + val ret = Process(script) ! ProcessLogger(sb append _) |
| 21 | + (ret, sb.toString) |
| 22 | + } |
| 23 | + |
| 24 | + private def deletePackages: Unit = { |
| 25 | + def delete(path: String) = { |
| 26 | + val file = new JFile(path) |
| 27 | + if (file.exists) file.delete() |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + for (jar <- Source.fromFile(".packages").getLines()) |
| 32 | + delete(jar) |
| 33 | + |
| 34 | + delete(".packages") |
| 35 | + delete("src/dotty/tools/dotc/Dummy.scala") |
| 36 | + } catch { |
| 37 | + case _: FileNotFoundException => () |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Before def buildUp = deletePackages |
| 42 | + @After def tearDown = deletePackages |
| 43 | + |
| 44 | + /** bin/dotc script should be able to build hello world and successfully |
| 45 | + * execute it using dotr |
| 46 | + */ |
| 47 | + @Test def buildAndRunHelloWorld = doUnlessWindows { |
| 48 | + val (retDotc, dotcOutput) = executeScript("bin/dotc tests/pos/HelloWorld.scala") |
| 49 | + |
| 50 | + // Check correct output of building and running dotc |
| 51 | + assert( |
| 52 | + retDotc == 0, |
| 53 | + s"bin/dotc script did not run properly. Output:$lineSep$dotcOutput" |
| 54 | + ) |
| 55 | + |
| 56 | + val (retDotr, dotrOutput) = executeScript("bin/dotr Test") |
| 57 | + assert( |
| 58 | + retDotr == 0 && dotrOutput == "Hello, world!", |
| 59 | + s"Running hello world exited with status: $retDotr and output: $dotrOutput" |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + /** bin/dotc script should be able to detect changes in dotty sources and |
| 64 | + * rebuild dotty if needed |
| 65 | + */ |
| 66 | + @Test def rebuildIfNecessary = doUnlessWindows { |
| 67 | + val (retFirstBuild, _) = executeScript("bin/dotc tests/pos/HelloWorld.scala") |
| 68 | + assert(retFirstBuild == 0, "building dotc failed") |
| 69 | + |
| 70 | + // Create a new file |
| 71 | + new JFile("src/dotty/tools/dotc/Dummy.scala").createNewFile() |
| 72 | + |
| 73 | + val (retSecondBuild, output) = executeScript("bin/dotc tests/pos/HelloWorld.scala") |
| 74 | + assert( |
| 75 | + retSecondBuild == 0 && output.contains("rebuilding"), |
| 76 | + s"Rebuilding the tool should result in jar files being rebuilt. Status: $retSecondBuild, output:$lineSep$output") |
| 77 | + } |
| 78 | + |
| 79 | + /** if no changes to dotty, dotc script should be fast */ |
| 80 | + @Test def beFastOnNoChanges = doUnlessWindows { |
| 81 | + val (retFirstBuild, _) = executeScript("bin/dotc tests/pos/HelloWorld.scala") |
| 82 | + assert(retFirstBuild == 0, "building dotc failed") |
| 83 | + |
| 84 | + val (ret, output) = executeScript("bin/dotc tests/pos/HelloWorld.scala") |
| 85 | + assert( |
| 86 | + ret == 0 && !output.contains("rebuilding"), |
| 87 | + s"Project recompiled when it didn't need to be. Status $ret, output:$lineSep$output") |
| 88 | + } |
| 89 | +} |
0 commit comments