-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- do not rely on exercise slug or .config - scan entire test output folder for "TEST-*.xml" files
- Loading branch information
1 parent
2b84335
commit 929d5fe
Showing
12 changed files
with
149 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,80 @@ | ||
import scala.io.Source | ||
import org.json.{JSONArray, JSONObject, XML} | ||
import org.json.{JSONArray, JSONObject, XML} | ||
|
||
import java.io.{File, FileWriter} | ||
import java.io.{File, FileFilter, FileWriter} | ||
import scala.io.Source | ||
|
||
object Application extends App { | ||
require(args.length == 3, "Invalid number of arguments. Expected: <build-log-file-path> <test-results-file-path> <results-json-file-path>") | ||
val buildLogFilePath = args(0) | ||
val testResultsFilePath = args(1) | ||
val resultsJsonFilePath = args(2) | ||
object Application extends App { | ||
require(args.length == 3, s"Invalid number of arguments. Expected: <build-log-file-path> <test-results-folder-path> <results-json-file-path>, got: ${args.mkString("", ", ", "")}") | ||
val buildLogFilePath = args(0) | ||
val testResultsFolderPath = args(1) | ||
val resultsJsonFilePath = args(2) | ||
|
||
writeResultsJSON(buildLogFilePath, testResultsFilePath, resultsJsonFilePath) | ||
|
||
def writeResultsJSON(buildLogFilePath: String, testResultsFilePath: String, resultsJsonFilePath: String): Unit = { | ||
val resultsJsonFile = new File(resultsJsonFilePath) | ||
val resultsJsonFileWriter = new FileWriter(resultsJsonFile) | ||
val testResultsFolder = new File(testResultsFolderPath) | ||
if (!testResultsFolder.isDirectory) { | ||
throw new RuntimeException(s"Expected $testResultsFolderPath to be a folder") | ||
} | ||
val testResultFiles = testResultsFolder.listFiles(new FileFilter() { | ||
override def accept(file: File): Boolean = file.getName.matches("TEST-.*\\.xml") | ||
}).toList | ||
writeResultsJSON(buildLogFilePath, testResultFiles, resultsJsonFilePath) | ||
|
||
val json = toExercismJSON(buildLogFilePath, testResultsFilePath) | ||
json.write(resultsJsonFileWriter) | ||
resultsJsonFileWriter.close() | ||
} | ||
def writeResultsJSON(buildLogFilePath: String, testResultsFiles: List[File], resultsJsonFilePath: String): Unit = { | ||
val resultsJsonFile = new File(resultsJsonFilePath) | ||
val resultsJsonFileWriter = new FileWriter(resultsJsonFile) | ||
|
||
def getTestSuiteObject(testResultsFilePath: String): JSONObject = { | ||
val bufferedSource = Source.fromFile(testResultsFilePath) | ||
val xml = bufferedSource.mkString | ||
bufferedSource.close | ||
XML.toJSONObject(xml).getJSONObject("testsuite") | ||
} | ||
val json = toExercismJSON(buildLogFilePath, testResultsFiles) | ||
json.write(resultsJsonFileWriter) | ||
resultsJsonFileWriter.close() | ||
} | ||
|
||
def getTestCasesJSON(testResultsFilePath: String): JSONArray = { | ||
getTestSuiteObject(testResultsFilePath).getJSONArray("testcase") | ||
} | ||
def getTestSuiteObject(testResultsFile: File): JSONObject = { | ||
val bufferedSource = Source.fromFile(testResultsFile) | ||
val xml = bufferedSource.mkString | ||
bufferedSource.close | ||
XML.toJSONObject(xml).getJSONObject("testsuite") | ||
} | ||
|
||
// log, not xml | ||
def findErrorsInLog(buildLogFilePath: String): String = { | ||
val fileSource = Source.fromFile(buildLogFilePath) | ||
val rawContent = fileSource.mkString | ||
fileSource.close | ||
if (rawContent.contains("error: ")) rawContent else "" | ||
} | ||
// log, not xml | ||
def findErrorsInLog(buildLogFilePath: String): String = { | ||
val fileSource = Source.fromFile(buildLogFilePath) | ||
val rawContent = fileSource.mkString | ||
fileSource.close | ||
if (rawContent.contains("error: ")) rawContent else "" | ||
} | ||
|
||
def toTestCaseJSON(testCase: JSONObject): JSONObject = { | ||
val fail = testCase.optJSONObject("failure") | ||
new JSONObject() | ||
def toTestCaseJSON(testCase: JSONObject): JSONObject = { | ||
val fail = testCase.optJSONObject("failure") | ||
new JSONObject() | ||
.put("name", testCase.get("name").toString) | ||
.put("status", if(fail != null) "fail" else "pass" ) | ||
.put("message", if(fail != null) fail.getString("message") else JSONObject.NULL) | ||
.put("status", if (fail != null) "fail" else "pass") | ||
.put("message", if (fail != null) fail.getString("message") else JSONObject.NULL) | ||
.put("output", JSONObject.NULL) | ||
.put("test_code", JSONObject.NULL) | ||
} | ||
} | ||
|
||
def toExercismJSON(buildLogFilePath: String, testResultsFilePath: String): JSONObject = { | ||
val baseObject = new JSONObject().put("version", 2) | ||
val errorMessage = findErrorsInLog(buildLogFilePath) | ||
if(errorMessage.nonEmpty) { | ||
baseObject | ||
def toExercismJSON(buildLogFilePath: String, testResultsFiles: List[File]): JSONObject = { | ||
val baseObject = new JSONObject().put("version", 2) | ||
val errorMessage = findErrorsInLog(buildLogFilePath) | ||
if (errorMessage.nonEmpty) { | ||
baseObject | ||
.put("status", "error") | ||
.put("message", errorMessage) | ||
} else { | ||
val testSuite = getTestSuiteObject(testResultsFilePath) | ||
val failuresNum = testSuite.getInt("failures") | ||
val testcase = testSuite.get("testcase") | ||
val testCases: Array[JSONObject] = testcase match { | ||
case arr: JSONArray => (0 until arr.length).map(idx => toTestCaseJSON(arr.getJSONObject(idx))).toArray | ||
case obj: JSONObject => Array(toTestCaseJSON(obj)) | ||
} | ||
|
||
baseObject | ||
.put("status", if(failuresNum > 0) "fail" else "pass") | ||
} else { | ||
val (failuresCount, testCases) = testResultsFiles.map(getTestSuiteObject) | ||
.filter(testSuite => testSuite.has("testcase")) | ||
.map(testSuite => { | ||
val failuresCount = testSuite.getInt("failures") | ||
val testcase = testSuite.get("testcase") | ||
val testCases: Array[JSONObject] = testcase match { | ||
case arr: JSONArray => (0 until arr.length).map(idx => toTestCaseJSON(arr.getJSONObject(idx))).toArray | ||
case obj: JSONObject => Array(toTestCaseJSON(obj)) | ||
} | ||
(failuresCount, testCases) | ||
}).reduce((a, b) => (a._1 + b._1, Array.concat(a._2, b._2))) | ||
baseObject | ||
.put("status", if (failuresCount > 0) "fail" else "pass") | ||
.put("message", JSONObject.NULL) | ||
.put("tests", testCases) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"blurb": "Verify that expected test class names can be extracted from .meta/config.json", | ||
"files": { | ||
"test": [ | ||
"src/test/scala/ExampleMultipleFilesTest.scala", | ||
"src/test/scala/ExampleMultipleClassesTest.scala" | ||
] | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
scalaVersion := "2.13.6" | ||
|
||
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % "test" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"tests": [ | ||
{ | ||
"output": null, | ||
"name": "two-one", | ||
"test_code": null, | ||
"message": null, | ||
"status": "pass" | ||
}, | ||
{ | ||
"output": null, | ||
"name": "one-one", | ||
"test_code": null, | ||
"message": null, | ||
"status": "pass" | ||
}, | ||
{ | ||
"output": null, | ||
"name": "one-two", | ||
"test_code": null, | ||
"message": null, | ||
"status": "pass" | ||
} | ||
], | ||
"message": null, | ||
"version": 2, | ||
"status": "pass" | ||
} |
Empty file.
8 changes: 8 additions & 0 deletions
8
tests/example-multiple-tests/src/test/scala/ExampleMulitpleFilesTest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class ExampleMultipleFilesTest extends AnyFunSuite with Matchers { | ||
test("two-one") { | ||
true should be (true) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/example-multiple-tests/src/test/scala/ExampleMultipleClassesTest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
/** @version 1.3.0 */ | ||
class ExampleMultipleTestsTest extends AnyFunSuite with Matchers { | ||
test("one-one") { | ||
true should be (true) | ||
} | ||
} | ||
|
||
class ExampleAnotherMultipleTestsTest extends AnyFunSuite with Matchers { | ||
test("one-two") { | ||
true should be (true) | ||
} | ||
} |