Last active
January 24, 2017 11:14
-
-
Save shijinkui/cc0286690d29e06e6160f28207434eb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import java.io.File | |
import com.google.common.base.Charsets | |
import com.google.common.io.Files | |
import scala.io.Source | |
/** | |
* main | |
* Created by sjk on 1/24/17. | |
*/ | |
object StatisticUT { | |
def main(args: Array[String]): Unit = { | |
val dir = new File("/Users/sjk/g/flink/") | |
val ret = listFiles(dir) | |
showTestFiles(ret, dir) | |
println(countLine(ret)) | |
} | |
def showTestFiles(list: Set[File], home: File): Unit = { | |
val abs = home.getAbsolutePath | |
val test = list | |
.filter(_.getAbsolutePath.contains("src/test")) | |
.toArray | |
.sortBy(_.getAbsolutePath) | |
val suffix = test.map(_.getName).groupBy(f => { | |
if (f.contains("ITCase")) { | |
"ITCase" | |
} else if (f.contains("ITSuite")) { | |
"ITSuite" | |
} else if (f.contains("Test")) { | |
"Test" | |
} else if (f.contains("Suite")) { | |
"Suite" | |
} else "other" | |
}) | |
val scalaNum = test.count(_.getName.endsWith(".scala")) | |
val javaNum = test.count(_.getName.endsWith(".java")) | |
val st = s"count scala test file: $scalaNum\ncount java test file:$javaNum\n" + suffix.map(f => { | |
s""" | |
|## ${f._1} total: ${f._2.length} | |
|--------------------------------- | |
|${f._2.mkString("\n")} | |
""".stripMargin | |
}).mkString("\n") | |
println(st) | |
val lines = test.map(f => { | |
val p = f.getAbsolutePath.replace(abs + "/", "") | |
p.substring(0, p.indexOf("/")) + " " + f.getAbsolutePath | |
}) | |
val txt = | |
s""" | |
|total files: ${test.length} | |
|${lines.mkString("\n")} | |
""".stripMargin | |
println(txt) | |
Files.write(st, new File(home.getParent, "test_suffix_files.txt"), Charsets.UTF_8) | |
Files.write(txt, new File(home.getParent, "test_all_files.txt"), Charsets.UTF_8) | |
} | |
def countLine(list: Set[File]): Int = { | |
list.map(f => Source.fromFile(f).getLines().length).sum | |
} | |
def listFiles(dir: File, suffix: Array[String] = Array("java", "scala")): Set[File] = { | |
dir.isDirectory match { | |
case false => | |
suffix.find(sf => dir.getName.toLowerCase.endsWith(sf)) match { | |
case Some(_) => Set(dir) | |
case None => Set.empty[File] | |
} | |
case true => dir.listFiles.flatMap(f => listFiles(f, suffix)).toSet | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment