|
| 1 | +package dartzee.utils |
| 2 | + |
| 3 | +import dartzee.core.helper.verifyNotCalled |
| 4 | +import dartzee.helper.AbstractTest |
| 5 | +import dartzee.logging.CODE_EXEC_ERROR |
| 6 | +import dartzee.logging.Severity |
| 7 | +import dartzee.`object`.DartsClient |
| 8 | +import io.kotest.matchers.shouldBe |
| 9 | +import io.mockk.every |
| 10 | +import io.mockk.mockk |
| 11 | +import io.mockk.verify |
| 12 | +import java.io.IOException |
| 13 | +import org.junit.jupiter.api.Test |
| 14 | + |
| 15 | +class CommandUtilTest : AbstractTest() { |
| 16 | + @Test |
| 17 | + fun `launchUrl should execute the expected command on linux`() { |
| 18 | + DartsClient.operatingSystem = "linux" |
| 19 | + val runtime = mockk<Runtime>(relaxed = true) |
| 20 | + launchUrl("foo.bar", runtime) |
| 21 | + |
| 22 | + verify { runtime.exec(arrayOf("xdg-open", "foo.bar")) } |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun `launchUrl should execute the expected command on Windows`() { |
| 27 | + DartsClient.operatingSystem = "windows" |
| 28 | + val runtime = mockk<Runtime>(relaxed = true) |
| 29 | + launchUrl("google.com", runtime) |
| 30 | + |
| 31 | + verify { runtime.exec(arrayOf("cmd", "/c", "start", "google.com")) } |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `launchUrl should log an appropriate error if launching the URL fails`() { |
| 36 | + DartsClient.operatingSystem = "linux" |
| 37 | + val error = IOException("Oops") |
| 38 | + |
| 39 | + val runtime = mockk<Runtime>() |
| 40 | + every { runtime.exec(any<Array<String>>()) }.throws(error) |
| 41 | + |
| 42 | + launchUrl("foo.bar", runtime) |
| 43 | + |
| 44 | + val log = verifyLog(CODE_EXEC_ERROR, Severity.ERROR) |
| 45 | + log.message shouldBe "Command failed: xdg-open foo.bar" |
| 46 | + log.errorObject shouldBe error |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `runCommand should error for unsupported OS`() { |
| 51 | + DartsClient.operatingSystem = "apple" |
| 52 | + val runtime = mockk<Runtime>() |
| 53 | + |
| 54 | + runCommand(windows = arrayOf(), linux = arrayOf()) |
| 55 | + |
| 56 | + val log = verifyLog(CODE_EXEC_ERROR, Severity.ERROR) |
| 57 | + log.message shouldBe "Operating system unsupported: apple" |
| 58 | + |
| 59 | + verifyNotCalled { runtime.exec(any<Array<String>>()) } |
| 60 | + } |
| 61 | +} |
0 commit comments