Skip to content

Commit 346ad4f

Browse files
committed
Using FileLock to distinguish between test and partest mode
1 parent 8d08915 commit 346ad4f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

project/Build.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sbt.Keys._
22
import sbt._
3-
3+
import java.io.{ RandomAccessFile, File }
4+
import java.nio.channels.{ FileLock, OverlappingFileLockException }
45
object DottyBuild extends Build {
56

67
val TRAVIS_BUILD = "dotty.travis.build"
@@ -10,6 +11,7 @@ object DottyBuild extends Build {
1011
// "-agentpath:/home/dark/opt/yjp-2013-build-13072/bin/linux-x86-64/libyjpagent.so"
1112
)
1213

14+
var partestLock: FileLock = null
1315

1416
val defaults = Defaults.defaultSettings ++ Seq(
1517
// set sources to src/, tests to test/ and resources to resources/
@@ -48,11 +50,14 @@ object DottyBuild extends Build {
4850

4951
// enable verbose exception messages for JUnit
5052
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "--run-listener=test.ContextEscapeDetector"),
51-
// when this file is present, running test generates the files for partest
53+
testOptions in Test += Tests.Cleanup({ () => if (partestLock != null) partestLock.release }),
54+
// when this file is locked, running test generates the files for partest
5255
// otherwise it just executes the tests directly
53-
createPartestFile := { new java.io.File("./tests", "runPartest.flag").createNewFile },
56+
lockPartestFile := {
57+
val partestLockFile = "." + File.separator + "tests" + File.separator + "partest.lock"
58+
partestLock = new RandomAccessFile(partestLockFile, "rw").getChannel.tryLock
59+
},
5460
runPartestRunner <<= runTask(Test, "dotty.partest.DPConsoleRunner", "") dependsOn (test in Test),
55-
deletePartestFile := { new java.io.File("./tests", "runPartest.flag").delete },
5661

5762
// Adjust classpath for running dotty
5863
mainClass in (Compile, run) := Some("dotty.tools.dotc.Main"),
@@ -86,7 +91,7 @@ object DottyBuild extends Build {
8691

8792
tuning ::: agentOptions ::: travis_build ::: fullpath
8893
}
89-
) ++ addCommandAlias("partest", ";createPartestFile;runPartestRunner;deletePartestFile")
94+
) ++ addCommandAlias("partest", ";lockPartestFile;runPartestRunner")
9095

9196
lazy val dotty = Project(id = "dotty", base = file("."), settings = defaults)
9297

@@ -137,8 +142,7 @@ object DottyBuild extends Build {
137142
lazy val benchmarks = Project(id = "dotty-bench", settings = benchmarkSettings,
138143
base = file("bench")) dependsOn(dotty % "compile->test")
139144

140-
lazy val createPartestFile = TaskKey[Unit]("createPartestFile", "Creates the tests/runPartest.flag file")
145+
lazy val lockPartestFile = TaskKey[Unit]("lockPartestFile", "Creates the file lock on ./tests/partest.lock")
141146
lazy val runPartestRunner = TaskKey[Unit]("runPartestRunner", "Runs partests")
142-
lazy val deletePartestFile = TaskKey[Unit]("deletePartestFile", "Deletes the tests/runPartest.flag file")
143147

144148
}

test/test/CompilerTest.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import dotty.tools.dotc.reporting.Reporter
66
import scala.collection.mutable.ListBuffer
77
import scala.reflect.io.{ Path, Directory, File => SFile }
88
import scala.tools.partest.nest.FileManager
9-
import java.io.{ File => JFile }
9+
import java.io.{ RandomAccessFile, File => JFile }
10+
1011
import org.junit.Test
1112

1213

@@ -39,7 +40,14 @@ abstract class CompilerTest extends DottyTest {
3940
def partestableDir(prefix: String, dirName: String, args: List[String], xerrors: Int) = true
4041
def partestableList(testName: String, files: List[String], args: List[String], xerrors: Int) = true
4142

42-
val generatePartestFiles = new JFile("tests", "runPartest.flag").exists
43+
val generatePartestFiles = {
44+
val partestLockFile = "." + JFile.separator + "tests" + JFile.separator + "partest.lock"
45+
val partestLock = new RandomAccessFile(partestLockFile, "rw").getChannel.tryLock
46+
if (partestLock != null) { // file not locked by sbt -> don't generate partest
47+
partestLock.release
48+
false
49+
} else true
50+
}
4351

4452
// Delete generated files from previous run
4553
if (generatePartestFiles)

tests/partest.lock

Whitespace-only changes.

0 commit comments

Comments
 (0)