Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -219,4 +222,16 @@ internal class JavaProcessBuilderTest {
assertThat(result===previous).isTrue()
}
}
}
}

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
Comment on lines +229 to +233
} catch (e: java.io.IOException) {
false
}
}