mirror of https://github.com/sbt/sbt.git
[2.x] refactor: Use `IO.withTemporaryDirectory` instead of `File.createTempFile` (#9084)
* Use IO.withTemporaryDirectory instead of File.createTempFile * fix JUnitXmlTestsListenerSpec
This commit is contained in:
parent
840887820c
commit
8bcc6ae420
|
|
@ -13,16 +13,14 @@ import java.util.concurrent.atomic.AtomicReference
|
|||
|
||||
import testing.{ Event as TEvent, OptionalThrowable, Status as TStatus, TestSelector }
|
||||
import util.{ AbstractLogger, Level, ControlEvent, LogEvent }
|
||||
import sbt.io.IO
|
||||
import sbt.protocol.testing.TestResult
|
||||
import verify.BasicTestSuite
|
||||
|
||||
object JUnitXmlTestsListenerSpec extends BasicTestSuite:
|
||||
|
||||
test("JUnitXmlTestsListener should log debug message when writing test report"):
|
||||
val tempDir = File.createTempFile("junit-test", "")
|
||||
tempDir.delete()
|
||||
tempDir.mkdirs()
|
||||
try
|
||||
IO.withTemporaryDirectory: tempDir =>
|
||||
val loggedMessages = new AtomicReference[List[String]](Nil)
|
||||
val mockLogger = new AbstractLogger:
|
||||
def getLevel: Level.Value = Level.Debug
|
||||
|
|
@ -66,17 +64,9 @@ object JUnitXmlTestsListenerSpec extends BasicTestSuite:
|
|||
messages.exists(_.contains("TEST-TestSuite.xml")),
|
||||
s"Expected log message containing 'TEST-TestSuite.xml', but got: $messages"
|
||||
)
|
||||
finally
|
||||
// Cleanup
|
||||
if tempDir.exists() then
|
||||
tempDir.listFiles().foreach(_.delete())
|
||||
tempDir.delete()
|
||||
|
||||
test("JUnitXmlTestsListener should handle null logger gracefully"):
|
||||
val tempDir = File.createTempFile("junit-test", "")
|
||||
tempDir.delete()
|
||||
tempDir.mkdirs()
|
||||
try
|
||||
IO.withTemporaryDirectory: tempDir =>
|
||||
val listener = new JUnitXmlTestsListener(tempDir, false, null)
|
||||
listener.doInit()
|
||||
listener.startGroup("TestSuite")
|
||||
|
|
@ -97,9 +87,5 @@ object JUnitXmlTestsListenerSpec extends BasicTestSuite:
|
|||
// Verify XML file was still created
|
||||
val xmlFile = new File(tempDir, "TEST-TestSuite.xml")
|
||||
assert(xmlFile.exists(), "XML file should be created even when logger is null")
|
||||
finally
|
||||
if tempDir.exists() then
|
||||
tempDir.listFiles().foreach(_.delete())
|
||||
tempDir.delete()
|
||||
|
||||
end JUnitXmlTestsListenerSpec
|
||||
|
|
|
|||
Loading…
Reference in New Issue