Skip to content
This repository was archived by the owner on Sep 8, 2022. It is now read-only.

Commit 0141256

Browse files
committed
Remove FileUtil in favor of FileManager consolidation.
Methods independent of FileManager state are in the companion. While I was at it, also swapped the sense of diffing check and log, so that it's more natural (original = check, revised = log).
1 parent 95bf49f commit 0141256

File tree

4 files changed

+64
-65
lines changed

4 files changed

+64
-65
lines changed

src/main/scala/scala/tools/partest/IcodeTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package scala.tools.partest
77

8-
import scala.tools.partest.nest.FileUtil.compareContents
8+
import scala.tools.partest.nest.FileManager.compareContents
99

1010
/** A trait for testing icode. All you need is this in a
1111
* partest source file:

src/main/scala/scala/tools/partest/nest/DirectCompiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DirectCompiler(val fileManager: FileManager) {
5252

5353
val classPath: List[Path] = specializedOverride ++ codeLib ++ fileManager.testClassPath ++ List[Path](outDir)
5454

55-
val testSettings = new TestSettings(fileManager.joinPaths(classPath))
55+
val testSettings = new TestSettings(FileManager.joinPaths(classPath))
5656
val logWriter = new FileWriter(logFile)
5757
val srcDir = if (testFile.isDirectory) testFile else Path(testFile).parent.jfile
5858
val opts = fileManager.updatePluginPath(opts0, AbstractFile getDirectory outDir, AbstractFile getDirectory srcDir)

src/main/scala/scala/tools/partest/nest/FileManager.scala

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,61 @@ import scala.reflect.io.AbstractFile
2525
import scala.collection.mutable
2626
import scala.reflect.internal.util.ScalaClassLoader
2727

28-
trait FileUtil {
28+
object FileManager {
29+
def getLogFile(dir: File, fileBase: String, kind: String): File =
30+
new File(dir, fileBase + "-" + kind + ".log")
31+
32+
def getLogFile(file: File, kind: String): File = {
33+
val dir = file.getParentFile
34+
val fileBase = basename(file.getName)
35+
36+
getLogFile(dir, fileBase, kind)
37+
}
38+
39+
def logFileExists(file: File, kind: String) =
40+
getLogFile(file, kind).canRead
41+
42+
def overwriteFileWith(dest: File, file: File) =
43+
dest.isFile && copyFile(file, dest)
44+
45+
def copyFile(from: File, dest: File): Boolean = {
46+
if (from.isDirectory) {
47+
assert(dest.isDirectory, "cannot copy directory to file")
48+
val subDir: Directory = Path(dest) / Directory(from.getName)
49+
subDir.createDirectory()
50+
from.listFiles.toList forall (copyFile(_, subDir))
51+
} else {
52+
val to = if (dest.isDirectory) new File(dest, from.getName) else dest
53+
54+
try {
55+
SFile(to) writeAll SFile(from).slurp()
56+
true
57+
} catch { case _: IOException => false }
58+
}
59+
}
60+
61+
def mapFile(file: File, replace: String => String) {
62+
val f = SFile(file)
63+
64+
f.printlnAll(f.lines.toList map replace: _*)
65+
}
66+
67+
def jarsWithPrefix(dir: Directory, name: String): Iterator[SFile] =
68+
dir.files filter (f => (f hasExtension "jar") && (f.name startsWith name))
69+
70+
def dirsWithPrefix(dir: Directory, name: String): Iterator[Directory] =
71+
dir.dirs filter (_.name startsWith name)
72+
73+
def joinPaths(paths: List[Path]) = ClassPath.join(paths.map(_.getAbsolutePath).distinct: _*)
74+
2975
/** Compares two files using difflib to produce a unified diff.
3076
*
31-
* @param f1 the first file to be compared
32-
* @param f2 the second file to be compared
77+
* @param original the first file to be compared
78+
* @param revised the second file to be compared
3379
* @return the unified diff of the compared files or the empty string if they're equal
3480
*/
35-
def compareFiles(f1: File, f2: File): String = {
36-
compareContents(io.Source.fromFile(f1).getLines.toSeq, io.Source.fromFile(f2).getLines.toSeq, f1.getName, f2.getName)
81+
def compareFiles(original: File, revised: File): String = {
82+
compareContents(io.Source.fromFile(original).getLines.toSeq, io.Source.fromFile(revised).getLines.toSeq, original.getName, revised.getName)
3783
}
3884

3985
/** Compares two lists of lines using difflib to produce a unified diff.
@@ -44,26 +90,14 @@ trait FileUtil {
4490
* @param newName file name to be used in unified diff for `newLines`
4591
* @return the unified diff of the `origLines` and `newLines` or the empty string if they're equal
4692
*/
47-
def compareContents(origLines: Seq[String], newLines: Seq[String], origName: String = "a", newName: String = "b"): String = {
93+
def compareContents(original: Seq[String], revised: Seq[String], originalName: String = "a", revisedName: String = "b"): String = {
4894
import collection.JavaConverters._
4995

50-
val diff = difflib.DiffUtils.diff(origLines.asJava, newLines.asJava)
96+
val diff = difflib.DiffUtils.diff(original.asJava, revised.asJava)
5197
if (diff.getDeltas.isEmpty) ""
52-
else difflib.DiffUtils.generateUnifiedDiff(origName, newName, origLines.asJava, diff, 1).asScala.mkString("\n")
98+
else difflib.DiffUtils.generateUnifiedDiff(originalName, revisedName, original.asJava, diff, 1).asScala.mkString("\n")
5399
}
54100

55-
def jarsWithPrefix(dir: Directory, name: String): Iterator[SFile] =
56-
dir.files filter (f => (f hasExtension "jar") && (f.name startsWith name))
57-
58-
def dirsWithPrefix(dir: Directory, name: String): Iterator[Directory] =
59-
dir.dirs filter (_.name startsWith name)
60-
61-
def joinPaths(paths: List[Path]) = ClassPath.join(paths.map(_.getAbsolutePath).distinct: _*)
62-
}
63-
object FileUtil extends FileUtil {}
64-
65-
object FileManager {
66-
import FileUtil._
67101
def classPathFromTrifecta(library: Path, reflect: Path, compiler: Path) = {
68102
val usingJars = library.getAbsolutePath endsWith ".jar"
69103
// basedir for jars or classfiles on core classpath
@@ -107,7 +141,7 @@ class FileManager(val testClassPath: List[Path],
107141
val compilerUnderTest: Path,
108142
javaCmd: Option[String] = None,
109143
javacCmd: Option[String] = None,
110-
scalacOpts: Seq[String] = Seq.empty) extends FileUtil {
144+
scalacOpts: Seq[String] = Seq.empty) {
111145
def this(testClassPath: List[Path], javaCmd: Option[String] = None, javacCmd: Option[String] = None, scalacOpts: Seq[String] = Seq.empty) {
112146
this(testClassPath,
113147
FileManager.fromClassPath("library", testClassPath),
@@ -146,47 +180,10 @@ class FileManager(val testClassPath: List[Path],
146180
}
147181

148182
/** Only when --debug is given. */
149-
lazy val testTimings = new mutable.HashMap[String, Long]
183+
private[this] lazy val testTimings = new mutable.HashMap[String, Long]
150184
def recordTestTiming(name: String, milliseconds: Long) =
151185
synchronized { testTimings(name) = milliseconds }
152186

153-
def getLogFile(dir: File, fileBase: String, kind: String): File =
154-
new File(dir, fileBase + "-" + kind + ".log")
155-
156-
def getLogFile(file: File, kind: String): File = {
157-
val dir = file.getParentFile
158-
val fileBase = basename(file.getName)
159-
160-
getLogFile(dir, fileBase, kind)
161-
}
162-
163-
def logFileExists(file: File, kind: String) =
164-
getLogFile(file, kind).canRead
165-
166-
def overwriteFileWith(dest: File, file: File) =
167-
dest.isFile && copyFile(file, dest)
168-
169-
def copyFile(from: File, dest: File): Boolean = {
170-
if (from.isDirectory) {
171-
assert(dest.isDirectory, "cannot copy directory to file")
172-
val subDir: Directory = Path(dest) / Directory(from.getName)
173-
subDir.createDirectory()
174-
from.listFiles.toList forall (copyFile(_, subDir))
175-
} else {
176-
val to = if (dest.isDirectory) new File(dest, from.getName) else dest
177-
178-
try {
179-
SFile(to) writeAll SFile(from).slurp()
180-
true
181-
} catch { case _: IOException => false }
182-
}
183-
}
184-
185-
def mapFile(file: File, replace: String => String) {
186-
val f = SFile(file)
187-
188-
f.printlnAll(f.lines.toList map replace: _*)
189-
}
190187

191188
/** Massage args to merge plugins and fix paths.
192189
* Plugin path can be relative to test root, or cwd is out.

src/main/scala/scala/tools/partest/nest/Runner.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import ClassPath.{ join, split }
2727
import PartestDefaults.{ javaCmd, javacCmd }
2828
import TestState.{ Pass, Fail, Crash, Uninitialized, Updated }
2929

30+
import FileManager.{compareFiles, compareContents, joinPaths}
31+
3032
trait PartestRunSettings {
3133
def gitPath: Path
3234
def reportPath: Path
@@ -294,8 +296,8 @@ class Runner(val testFile: File, fileManager: FileManager, updateCheck: Boolean)
294296
}
295297

296298
def currentDiff = (
297-
if (checkFile.canRead) diffilter(compareFiles(logFile, checkFile))
298-
else compareContents(augmentString(file2String(logFile)).lines.toList, Nil)
299+
if (checkFile.canRead) diffilter(compareFiles(original = checkFile, revised = logFile))
300+
else compareContents(original = Nil, revised = augmentString(file2String(logFile)).lines.toList)
299301
)
300302

301303
val gitRunner = List("/usr/local/bin/git", "/usr/bin/git") map (f => new java.io.File(f)) find (_.canRead)
@@ -728,7 +730,7 @@ class Runner(val testFile: File, fileManager: FileManager, updateCheck: Boolean)
728730
}
729731

730732
/** Extended by Ant- and ConsoleRunner for running a set of tests. */
731-
trait DirectRunner {
733+
abstract class DirectRunner {
732734
def fileManager: FileManager
733735
def updateCheck: Boolean
734736
def failed: Boolean
@@ -746,7 +748,7 @@ trait DirectRunner {
746748
s"""|Compiler under test: ${relativize(fileManager.compilerUnderTest.getAbsolutePath)}
747749
|Scala version is: $versionMsg
748750
|Scalac options are: ${fileManager.SCALAC_OPTS mkString " "}
749-
|Compilation Path: ${relativize(fileManager.joinPaths(fileManager.testClassPath))}
751+
|Compilation Path: ${relativize(joinPaths(fileManager.testClassPath))}
750752
|Java binaries in: $vmBin
751753
|Java runtime is: $vmName
752754
|Java options are: $vmOpts

0 commit comments

Comments
 (0)