Skip to content

Commit

Permalink
Merge pull request #53 from CaptnCodr/bugfix/bash-quotation
Browse files Browse the repository at this point in the history
No need to quote the bash command anymore
  • Loading branch information
CaptnCodr authored Jul 25, 2023
2 parents 9291a04 + 079d93f commit d225487
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Fli.Tests/ExecContext/ExecCommandConfigureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open System
open System.Collections.Generic
open System.Text


[<Test>]
let ``Check FileName in ProcessStartInfo Exec program`` () =
cli { Exec "cmd.exe" }
Expand Down
2 changes: 1 addition & 1 deletion src/Fli.Tests/ShellContext/ShellCommandConfigureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ let ``Check all possible values in ProcessStartInfo`` () =
|> Command.buildProcess

config.FileName |> should equal "bash"
config.Arguments |> should equal "-c echo Hello World! €"
config.Arguments |> should equal "-c \"echo Hello World! €\""
config.WorkingDirectory |> should equal @"C:\Users"
config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True
config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True
Expand Down
14 changes: 7 additions & 7 deletions src/Fli.Tests/ShellContext/ShellCommandExecuteTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ let ``Get output in StringBuilder`` () =
else
cli {
Shell BASH
Command "\"echo Test\""
Command "echo Test"
Output sb
}
|> Command.execute
Expand Down Expand Up @@ -196,7 +196,7 @@ let ``Hello World with BASH`` () =
if OperatingSystem.IsWindows() |> not then
cli {
Shell BASH
Command "\"echo Hello World!\""
Command "echo Hello World!"
}
|> Command.execute
|> Output.toText
Expand All @@ -209,8 +209,8 @@ let ``Input text in BASH`` () =
if OperatingSystem.IsWindows() |> not then
cli {
Shell BASH
Command "\"echo Hello World!\""
Input "\"echo Test\""
Command "echo Hello World!"
Input "echo Test"
}
|> Command.execute
|> Output.toText
Expand All @@ -225,7 +225,7 @@ let ``Hello World with BASH async`` () =
let! output =
cli {
Shell BASH
Command "\"echo Hello World!\""
Command "echo Hello World!"
}
|> Command.executeAsync

Expand All @@ -240,7 +240,7 @@ let ``BASH returning non zero ExitCode`` () =
if OperatingSystem.IsWindows() |> not then
cli {
Shell BASH
Command "\"echl Test\""
Command "echl Test"
}
|> Command.execute
|> Output.toExitCode
Expand All @@ -253,7 +253,7 @@ let ``BASH returning non zero process id`` () =
if OperatingSystem.IsWindows() |> not then
cli {
Shell BASH
Command "\"echo Test\""
Command "echo Test"
}
|> Command.execute
|> Output.toId
Expand Down
2 changes: 1 addition & 1 deletion src/Fli.Tests/ShellContext/ShellCommandToStringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let ``WSL command toString returns full line`` () =
let ``BASH command toString returns full line`` () =
cli {
Shell BASH
Command "\"echo Hello World!\""
Command "echo Hello World!"
}
|> Command.toString
|> should equal "bash -c \"echo Hello World!\""
11 changes: 9 additions & 2 deletions src/Fli/Command.fs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,17 @@ module Command =

cts.Token

let private quoteBashCommand (context: ShellContext) =
match context.config.Shell with
| Shells.BASH -> context.config.Command |> Option.defaultValue "" |> fun s -> $"\"{s}\""
| _ -> context.config.Command |> Option.defaultValue ""

type Command =
static member internal buildProcess(context: ShellContext) =
let (proc, flag) = (context.config.Shell, context.config.Input) ||> shellToProcess
let command = context |> quoteBashCommand

(createProcess proc $"""{flag} {context.config.Command |> Option.defaultValue ""}""")
(createProcess proc $"""{flag} {command}""")
.With(WorkingDirectory = (context.config.WorkingDirectory |> Option.defaultValue ""))
.With(StandardOutputEncoding = (context.config.Encoding |> Option.defaultValue null))
.With(StandardErrorEncoding = (context.config.Encoding |> Option.defaultValue null))
Expand All @@ -197,7 +203,8 @@ module Command =
/// Stringifies shell + opening flag and given command.
static member toString(context: ShellContext) =
let (proc, flag) = (context.config.Shell, context.config.Input) ||> shellToProcess
$"""{proc} {flag} {context.config.Command |> Option.defaultValue ""}"""
let command = context |> quoteBashCommand
$"""{proc} {flag} {command}"""

/// Stringifies executable + arguments.
static member toString(context: ExecContext) =
Expand Down

0 comments on commit d225487

Please sign in to comment.