@@ -25,15 +25,61 @@ import scala.reflect.io.AbstractFile
25
25
import scala .collection .mutable
26
26
import scala .reflect .internal .util .ScalaClassLoader
27
27
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
+
29
75
/** Compares two files using difflib to produce a unified diff.
30
76
*
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
33
79
* @return the unified diff of the compared files or the empty string if they're equal
34
80
*/
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)
37
83
}
38
84
39
85
/** Compares two lists of lines using difflib to produce a unified diff.
@@ -44,26 +90,14 @@ trait FileUtil {
44
90
* @param newName file name to be used in unified diff for `newLines`
45
91
* @return the unified diff of the `origLines` and `newLines` or the empty string if they're equal
46
92
*/
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 = {
48
94
import collection .JavaConverters ._
49
95
50
- val diff = difflib.DiffUtils .diff(origLines .asJava, newLines .asJava)
96
+ val diff = difflib.DiffUtils .diff(original .asJava, revised .asJava)
51
97
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 " )
53
99
}
54
100
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 ._
67
101
def classPathFromTrifecta (library : Path , reflect : Path , compiler : Path ) = {
68
102
val usingJars = library.getAbsolutePath endsWith " .jar"
69
103
// basedir for jars or classfiles on core classpath
@@ -107,7 +141,7 @@ class FileManager(val testClassPath: List[Path],
107
141
val compilerUnderTest : Path ,
108
142
javaCmd : Option [String ] = None ,
109
143
javacCmd : Option [String ] = None ,
110
- scalacOpts : Seq [String ] = Seq .empty) extends FileUtil {
144
+ scalacOpts : Seq [String ] = Seq .empty) {
111
145
def this (testClassPath : List [Path ], javaCmd : Option [String ] = None , javacCmd : Option [String ] = None , scalacOpts : Seq [String ] = Seq .empty) {
112
146
this (testClassPath,
113
147
FileManager .fromClassPath(" library" , testClassPath),
@@ -146,47 +180,10 @@ class FileManager(val testClassPath: List[Path],
146
180
}
147
181
148
182
/** 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 ]
150
184
def recordTestTiming (name : String , milliseconds : Long ) =
151
185
synchronized { testTimings(name) = milliseconds }
152
186
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
- }
190
187
191
188
/** Massage args to merge plugins and fix paths.
192
189
* Plugin path can be relative to test root, or cwd is out.
0 commit comments