Skip to content

Commit faaa681

Browse files
committed
Insert bash -c parameter as needed
1 parent 9182123 commit faaa681

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

Sources/Argument.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33

4-
public enum Argument {
4+
public enum Argument: Equatable {
55
case quoted(QuotedString)
66
case verbatim(String)
77

Sources/QuotedString.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ShellQuote
22

33

4-
public struct QuotedString {
4+
public struct QuotedString: Equatable {
55
public var unquoted: String
66
public var quoted: String
77

Sources/ShellOut.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ extension ShellOutCommand: CustomStringConvertible {
129129

130130
public extension ShellOutCommand {
131131
static func bash(arguments: [Argument]) -> Self {
132-
.init(command: "bash", arguments: ["-c", arguments.map(\.string).joined(separator: " ")])
132+
let arguments = arguments.first == "-c" ? Array(arguments.dropFirst()) : arguments
133+
return .init(command: "bash", arguments: ["-c", arguments.map(\.string).joined(separator: " ")])
133134
}
134135
}
135136

Tests/ShellOutTests/ShellOutTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ class ShellOutTests: XCTestCase {
191191
await XCTAssertEqualAsync(try await shellOut(to: .readFile(at: filePath), logger: .init(label: "test")).stdout, "Hello again")
192192
}
193193

194+
func testBash() async throws {
195+
// Without explicit -c parameter
196+
await XCTAssertEqualAsync(try await shellOut(to: .bash(arguments: ["echo", "foo"])).stdout,
197+
"foo")
198+
// With explicit -c parameter
199+
await XCTAssertEqualAsync(try await shellOut(to: .bash(arguments: ["-c", "echo", "foo"])).stdout,
200+
"foo")
201+
}
202+
194203
func testBashArgumentQuoting() async throws {
195204
await XCTAssertEqualAsync(try await shellOut(to: .bash(arguments: ["echo",
196205
"foo ; echo bar".quoted])).stdout,

0 commit comments

Comments
 (0)