Skip to content

add zsh shell path option #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 17 additions & 6 deletions Sources/ShellOut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ import Dispatch
at path: String = ".",
process: Process = .init(),
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil
errorHandle: FileHandle? = nil,
shellType: ShellType = .bashPath
) throws -> String {
let command = "cd \(path.escapingSpaces) && \(command) \(arguments.joined(separator: " "))"

return try process.launchBash(
with: command,
outputHandle: outputHandle,
errorHandle: errorHandle
errorHandle: errorHandle,
shellType: shellType
)
}

Expand Down Expand Up @@ -101,14 +103,16 @@ import Dispatch
at path: String = ".",
process: Process = .init(),
outputHandle: FileHandle? = nil,
errorHandle: FileHandle? = nil
errorHandle: FileHandle? = nil,
shellType: ShellType = .bashPath
) throws -> String {
return try shellOut(
to: command.string,
at: path,
process: process,
outputHandle: outputHandle,
errorHandle: errorHandle
errorHandle: errorHandle,
shellType: shellType
)
}

Expand All @@ -123,6 +127,12 @@ public struct ShellOutCommand {
}
}

/// Used to specify the path of which shell to use. Default is set to Bash.
public enum ShellType: String {
case bashPath = "/bin/bash"
case zshPath = "/bin/zsh"
}

/// Git commands
public extension ShellOutCommand {
/// Initialize a git repository
Expand Down Expand Up @@ -379,8 +389,9 @@ extension ShellOutError: LocalizedError {
// MARK: - Private

private extension Process {
@discardableResult func launchBash(with command: String, outputHandle: FileHandle? = nil, errorHandle: FileHandle? = nil) throws -> String {
launchPath = "/bin/bash"
@discardableResult func launchBash(with command: String, outputHandle: FileHandle? = nil, errorHandle: FileHandle? = nil, shellType: ShellType = .bashPath) throws -> String {
// Default shell path is set to Bash
launchPath = shellType.rawValue
arguments = ["-c", command]

// Because FileHandle's readabilityHandler might be called from a
Expand Down
3 changes: 2 additions & 1 deletion Tests/ShellOutTests/ShellOutTests+Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ extension ShellOutTests {
("testSeriesOfCommandsAtPath", testSeriesOfCommandsAtPath),
("testThrowingError", testThrowingError),
("testGitCommands", testGitCommands),
("testSwiftPackageManagerCommands", testSwiftPackageManagerCommands)
("testSwiftPackageManagerCommands", testSwiftPackageManagerCommands),
("testZshShellCommands", testZshShellCommands)
]
}
#endif
20 changes: 20 additions & 0 deletions Tests/ShellOutTests/ShellOutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,24 @@ class ShellOutTests: XCTestCase {
try shellOut(to: .generateSwiftPackageXcodeProject(), at: packagePath)
XCTAssertTrue(try shellOut(to: "ls -a", at: packagePath).contains("SwiftPackageManagerTest.xcodeproj"))
}

func testZshShellCommands() throws {
// Check current shell type
let bashNameEcho = try shellOut(to: "echo $0")
XCTAssertEqual(bashNameEcho, "/bin/bash")

let zshNameEcho = try shellOut(to: "echo $0", shellType: .zshPath)
XCTAssertEqual(zshNameEcho, "/bin/zsh")

// Test with arguments
let echo = try shellOut(to: "echo", arguments: ["Hello world"], shellType: .zshPath)
XCTAssertEqual(echo, "Hello world")

// Test with single command at path
try shellOut(to: "echo \"Hello\" > \(NSTemporaryDirectory())ShellOutTests-SingleCommand.txt", shellType: .zshPath)
let textFileContent = try shellOut(to: "cat ShellOutTests-SingleCommand.txt",
at: NSTemporaryDirectory(), shellType: .zshPath)

XCTAssertEqual(textFileContent, "Hello")
}
}