diff --git a/kommand/src/main/kotlin/io/github/manamiproject/kommand/JavaProcessBuilder.kt b/kommand/src/main/kotlin/io/github/manamiproject/kommand/JavaProcessBuilder.kt index b2699028..6b80c8b8 100644 --- a/kommand/src/main/kotlin/io/github/manamiproject/kommand/JavaProcessBuilder.kt +++ b/kommand/src/main/kotlin/io/github/manamiproject/kommand/JavaProcessBuilder.kt @@ -98,7 +98,8 @@ public class JavaProcessBuilder( } when { - e.cause?.message == "error=2, No such file or directory" -> throw CommandNotFoundException(commandName) + // The wording depends on the JDK, e.g. "error=2, No such file or directory" or "Exec failed, error: 2 (No such file or directory)" + e.cause?.message?.contains("No such file or directory") == true -> throw CommandNotFoundException(commandName) e.cause?.message?.contains("permission denied", false) == true -> throw PermissionDeniedException(commandName) else -> throw e } diff --git a/kommand/src/test/kotlin/io/github/manamiproject/kommand/JavaProcessBuilderTest.kt b/kommand/src/test/kotlin/io/github/manamiproject/kommand/JavaProcessBuilderTest.kt index 3edc0682..29ba8562 100644 --- a/kommand/src/test/kotlin/io/github/manamiproject/kommand/JavaProcessBuilderTest.kt +++ b/kommand/src/test/kotlin/io/github/manamiproject/kommand/JavaProcessBuilderTest.kt @@ -5,6 +5,7 @@ import io.github.manamiproject.kommand.exceptions.IncorrectPasswordAttemptExcept import io.github.manamiproject.kommand.exceptions.UnexpectedUsageOfSudo import io.github.manamiproject.kommand.exceptions.UnsupportedOperatingSystemException import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Assumptions.assumeTrue import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows @@ -186,6 +187,8 @@ internal class JavaProcessBuilderTest { @Test fun `throws exception if command with sudo is given an incorrect password`() { + assumeTrue(sudoRequiresPassword(), "sudo doesn't ask for a password on this system, so an incorrect password cannot be detected.") + // given val javaProcessBuilder = JavaProcessBuilder { useSudo = true @@ -219,4 +222,16 @@ internal class JavaProcessBuilderTest { assertThat(result===previous).isTrue() } } -} \ No newline at end of file +} + +private fun sudoRequiresPassword(): Boolean { + return try { + // -k ignores cached credentials, -n fails instead of asking if a password is required + ProcessBuilder("sudo", "-k", "-n", "true") + .redirectErrorStream(true) + .start() + .waitFor() != 0 + } catch (e: java.io.IOException) { + false + } +}