diff --git a/src/Fli.Tests/ExecContext/ExecCommandConfigureTests.fs b/src/Fli.Tests/ExecContext/ExecCommandConfigureTests.fs deleted file mode 100644 index 0452eb5..0000000 --- a/src/Fli.Tests/ExecContext/ExecCommandConfigureTests.fs +++ /dev/null @@ -1,174 +0,0 @@ -module Fli.Tests.ExecContext.ExecCommandConfigureTests - -open NUnit.Framework -open FsUnit -open Fli -open System -open System.Collections.Generic -open System.Text - - -[] -let ``Check FileName in ProcessStartInfo Exec program`` () = - cli { Exec "cmd.exe" } - |> Command.buildProcess - |> (fun p -> p.FileName) - |> should equal "cmd.exe" - -[] -let ``Check Arguments in ProcessStartInfo with Arguments`` () = - cli { - Exec "cmd.exe" - Arguments "-c echo Hello World!" - } - |> Command.buildProcess - |> _.Arguments - |> should equal "-c echo Hello World!" - -[] -let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () = - cli { - Exec "cmd.exe" - WorkingDirectory @"C:\Users" - } - |> Command.buildProcess - |> _.WorkingDirectory - |> should equal @"C:\Users" - -[] -let ``Check WindowStyle in ProcessStartInfo with WorkingDirectory`` () = - cli { - Exec "cmd.exe" - WindowStyle Normal - } - |> Command.buildProcess - |> _.WindowStyle - |> should equal Diagnostics.ProcessWindowStyle.Normal - -[] -let ``Check Verb in ProcessStartInfo with Verb`` () = - if OperatingSystem.IsWindows() then - cli { - Exec "cmd.exe" - Verb "open" - } - |> Command.buildProcess - |> _.Verb - |> should equal "open" - else - Assert.Pass() - -[] -let ``Check UserName in ProcessStartInfo with Username`` () = - cli { - Exec "cmd.exe" - Username "admin" - } - |> Command.buildProcess - |> _.UserName - |> should equal "admin" - -[] -let ``Check Domain, UserName, Password in ProcessStartInfo with Credentials for windows (overwrites Username)`` () = - if OperatingSystem.IsWindows() then - let config = - cli { - Exec "cmd.exe" - Username "admin" - Credentials("domain", "user", "password") - } - |> Command.buildProcess - - config.Domain |> should equal "domain" - config.UserName |> should equal "user" - config.Password |> should not' (equal "password") // stored as SecureString - -[] -let ``Check Environment in ProcessStartInfo with single environment variable`` () = - cli { - Exec "cmd.exe" - EnvironmentVariable("Fli", "test") - } - |> Command.buildProcess - |> _.Environment.Contains(KeyValuePair("Fli", "test")) - |> should be True - -[] -let ``Check Environment in ProcessStartInfo with multiple environment variables`` () = - let config = - cli { - Exec "cmd.exe" - EnvironmentVariables [ ("Fli", "test"); ("Fli.Test", "test") ] - } - |> Command.buildProcess - - config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True - config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True - -[] -let ``Check StandardOutputEncoding & StandardErrorEncoding with setting Encoding`` () = - let config = - cli { - Exec "cmd.exe" - Encoding Encoding.UTF8 - } - |> Command.buildProcess - - config.StandardOutputEncoding |> should equal Encoding.UTF8 - config.StandardErrorEncoding |> should equal Encoding.UTF8 - -[] -let ``Check all possible values in ProcessStartInfo for windows`` () = - if OperatingSystem.IsWindows() then - let config = - cli { - Exec "cmd.exe" - Arguments "--help" - Input "Test" - Output @"C:\Users\test.txt" - WorkingDirectory @"C:\Users" - Verb "open" - Username "admin" - Credentials("domain", "admin", "password") - EnvironmentVariable("Fli", "test") - EnvironmentVariables [ ("Fli.Test", "test") ] - Encoding Encoding.UTF8 - WindowStyle Normal - } - |> Command.buildProcess - - config.FileName |> should equal "cmd.exe" - config.Arguments |> should equal "--help" - config.WorkingDirectory |> should equal @"C:\Users" - config.Verb |> should equal "open" - config.Domain |> should equal "domain" - config.UserName |> should equal "admin" - config.Password |> should not' (equal "password") - config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True - config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True - config.StandardOutputEncoding |> should equal Encoding.UTF8 - config.StandardErrorEncoding |> should equal Encoding.UTF8 - config.WindowStyle |> should equal Diagnostics.ProcessWindowStyle.Normal - else - let config = - cli { - Exec "bash" - Arguments "--help" - Output "./Users/test.txt" - WorkingDirectory "./Users" - Username "admin" - EnvironmentVariable("Fli", "test") - EnvironmentVariables [ ("Fli.Test", "test") ] - Encoding Encoding.UTF8 - } - |> Command.buildProcess - - config.FileName |> should equal "bash" - config.Arguments |> should equal "--help" - config.WorkingDirectory |> should equal "./Users" - config.Verb |> should equal String.Empty - config.UserName |> should equal "admin" - config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True - config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True - config.StandardOutputEncoding |> should equal Encoding.UTF8 - config.StandardErrorEncoding |> should equal Encoding.UTF8 diff --git a/src/Fli.Tests/ExecContext/ExecCommandConfigureUnixTests.fs b/src/Fli.Tests/ExecContext/ExecCommandConfigureUnixTests.fs new file mode 100644 index 0000000..a50b6e4 --- /dev/null +++ b/src/Fli.Tests/ExecContext/ExecCommandConfigureUnixTests.fs @@ -0,0 +1,123 @@ +module Fli.Tests.ExecContext.ExecCommandConfigureUnixTests + +open NUnit.Framework +open FsUnit +open Fli +open System +open System.Collections.Generic +open System.Text + +[] +[] +let ``Check FileName in ProcessStartInfo Exec program`` () = + cli { Exec "bash" } + |> Command.buildProcess + |> (fun p -> p.FileName) + |> should equal "bash" + +[] +[] +let ``Check Arguments in ProcessStartInfo with Arguments`` () = + cli { + Exec "bash" + Arguments "-c echo Hello World!" + } + |> Command.buildProcess + |> _.Arguments + |> should equal "-c echo Hello World!" + +[] +[] +let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () = + cli { + Exec "bash" + WorkingDirectory @"/etc/" + } + |> Command.buildProcess + |> _.WorkingDirectory + |> should equal @"/etc/" + +[] +[] +let ``Check WindowStyle in ProcessStartInfo with WorkingDirectory`` () = + cli { + Exec "bash" + WindowStyle Normal + } + |> Command.buildProcess + |> _.WindowStyle + |> should equal Diagnostics.ProcessWindowStyle.Normal + +[] +[] +let ``Check UserName in ProcessStartInfo with Username`` () = + cli { + Exec "bash" + Username "root" + } + |> Command.buildProcess + |> _.UserName + |> should equal "root" + +[] +[] +let ``Check Environment in ProcessStartInfo with single environment variable`` () = + cli { + Exec "bash" + EnvironmentVariable("Fli", "test") + } + |> Command.buildProcess + |> _.Environment.Contains(KeyValuePair("Fli", "test")) + |> should be True + +[] +[] +let ``Check Environment in ProcessStartInfo with multiple environment variables`` () = + let config = + cli { + Exec "bash" + EnvironmentVariables [ ("Fli", "test"); ("Fli.Test", "test") ] + } + |> Command.buildProcess + + config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True + config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True + +[] +[] +let ``Check StandardOutputEncoding & StandardErrorEncoding with setting Encoding`` () = + let config = + cli { + Exec "bash" + Encoding Encoding.UTF8 + } + |> Command.buildProcess + + config.StandardOutputEncoding |> should equal Encoding.UTF8 + config.StandardErrorEncoding |> should equal Encoding.UTF8 + +[] +[] +let ``Check all possible values in ProcessStartInfo`` () = + let config = + cli { + Exec "bash" + Arguments "--help" + Output "./Users/test.txt" + WorkingDirectory "./Users" + Username "admin" + EnvironmentVariable("Fli", "test") + EnvironmentVariables [ ("Fli.Test", "test") ] + Encoding Encoding.UTF8 + } + |> Command.buildProcess + + config.FileName |> should equal "bash" + config.Arguments |> should equal "--help" + config.WorkingDirectory |> should equal "./Users" + config.Verb |> should equal String.Empty + config.UserName |> should equal "admin" + config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True + config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True + config.StandardOutputEncoding |> should equal Encoding.UTF8 + config.StandardErrorEncoding |> should equal Encoding.UTF8 \ No newline at end of file diff --git a/src/Fli.Tests/ExecContext/ExecCommandConfigureWindowsTests.fs b/src/Fli.Tests/ExecContext/ExecCommandConfigureWindowsTests.fs new file mode 100644 index 0000000..8087d26 --- /dev/null +++ b/src/Fli.Tests/ExecContext/ExecCommandConfigureWindowsTests.fs @@ -0,0 +1,156 @@ +module Fli.Tests.ExecContext.ExecCommandConfigureWindowsTests + +open NUnit.Framework +open FsUnit +open Fli +open System +open System.Collections.Generic +open System.Text + +[] +[] +let ``Check FileName in ProcessStartInfo Exec program`` () = + cli { Exec "cmd.exe" } + |> Command.buildProcess + |> (fun p -> p.FileName) + |> should equal "cmd.exe" + +[] +[] +let ``Check Arguments in ProcessStartInfo with Arguments`` () = + cli { + Exec "cmd.exe" + Arguments "-c echo Hello World!" + } + |> Command.buildProcess + |> _.Arguments + |> should equal "-c echo Hello World!" + +[] +[] +let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () = + cli { + Exec "cmd.exe" + WorkingDirectory @"C:\Users" + } + |> Command.buildProcess + |> _.WorkingDirectory + |> should equal @"C:\Users" + +[] +[] +let ``Check WindowStyle in ProcessStartInfo with WorkingDirectory`` () = + cli { + Exec "cmd.exe" + WindowStyle Normal + } + |> Command.buildProcess + |> _.WindowStyle + |> should equal Diagnostics.ProcessWindowStyle.Normal + +[] +[] +let ``Check Verb in ProcessStartInfo with Verb`` () = + cli { + Exec "cmd.exe" + Verb "open" + } + |> Command.buildProcess + |> _.Verb + |> should equal "open" + +[] +[] +let ``Check UserName in ProcessStartInfo with Username`` () = + cli { + Exec "cmd.exe" + Username "admin" + } + |> Command.buildProcess + |> _.UserName + |> should equal "admin" + +[] +[] +let ``Check Domain, UserName, Password in ProcessStartInfo with Credentials for windows (overwrites Username)`` () = + let config = + cli { + Exec "cmd.exe" + Username "admin" + Credentials("domain", "user", "password") + } + |> Command.buildProcess + + config.Domain |> should equal "domain" + config.UserName |> should equal "user" + config.Password |> should not' (equal "password") // stored as SecureString + +[] +[] +let ``Check Environment in ProcessStartInfo with single environment variable`` () = + cli { + Exec "cmd.exe" + EnvironmentVariable("Fli", "test") + } + |> Command.buildProcess + |> _.Environment.Contains(KeyValuePair("Fli", "test")) + |> should be True + +[] +[] +let ``Check Environment in ProcessStartInfo with multiple environment variables`` () = + let config = + cli { + Exec "cmd.exe" + EnvironmentVariables [ ("Fli", "test"); ("Fli.Test", "test") ] + } + |> Command.buildProcess + + config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True + config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True + +[] +[] +let ``Check StandardOutputEncoding & StandardErrorEncoding with setting Encoding`` () = + let config = + cli { + Exec "cmd.exe" + Encoding Encoding.UTF8 + } + |> Command.buildProcess + + config.StandardOutputEncoding |> should equal Encoding.UTF8 + config.StandardErrorEncoding |> should equal Encoding.UTF8 + +[] +[] +let ``Check all possible values in ProcessStartInfo for windows`` () = + let config = + cli { + Exec "cmd.exe" + Arguments "--help" + Input "Test" + Output @"C:\Users\test.txt" + WorkingDirectory @"C:\Users" + Verb "open" + Username "admin" + Credentials("domain", "admin", "password") + EnvironmentVariable("Fli", "test") + EnvironmentVariables [ ("Fli.Test", "test") ] + Encoding Encoding.UTF8 + WindowStyle Normal + } + |> Command.buildProcess + + config.FileName |> should equal "cmd.exe" + config.Arguments |> should equal "--help" + config.WorkingDirectory |> should equal @"C:\Users" + config.Verb |> should equal "open" + config.Domain |> should equal "domain" + config.UserName |> should equal "admin" + config.Password |> should not' (equal "password") + config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True + config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True + config.StandardOutputEncoding |> should equal Encoding.UTF8 + config.StandardErrorEncoding |> should equal Encoding.UTF8 + config.WindowStyle |> should equal Diagnostics.ProcessWindowStyle.Normal \ No newline at end of file diff --git a/src/Fli.Tests/ExecContext/ExecCommandExecuteTests.fs b/src/Fli.Tests/ExecContext/ExecCommandExecuteTests.fs deleted file mode 100644 index 2e96d0a..0000000 --- a/src/Fli.Tests/ExecContext/ExecCommandExecuteTests.fs +++ /dev/null @@ -1,141 +0,0 @@ -module Fli.Tests.ExecContext.ExecCommandExecuteTests - -open NUnit.Framework -open FsUnit -open Fli -open System -open System.Text - - -[] -let ``Hello World with executing program`` () = - if OperatingSystem.IsWindows() then - cli { - Exec "cmd.exe" - Arguments "/C echo Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Get process Id`` () = - if OperatingSystem.IsWindows() then - cli { - Exec "cmd.exe" - Arguments "/C echo Test" - } - |> Command.execute - |> Output.toId - |> should not' (equal 0) - else - Assert.Pass() - -[] -let ``print text with Input with executing program`` () = - if OperatingSystem.IsWindows() then - cli { - Exec "cmd.exe" - Arguments "/k echo Test" - Input "echo Hello World!" - WorkingDirectory @"C:\" - } - |> Command.execute - |> Output.toText - |> should equal "Test\r\n\r\nC:\\>echo Hello World!\r\nHello World!\r\n\r\nC:\\>" - else - Assert.Pass() - -[] -let ``Get output in StringBuilder`` () = - let sb = StringBuilder() - - if OperatingSystem.IsWindows() then - cli { - Exec "cmd.exe" - Arguments "/c echo Test" - Output sb - } - |> Command.execute - |> ignore - - sb.ToString() |> should equal "Test\r\n" - else - cli { - Exec "bash" - Arguments "-c \"echo Test\"" - Output sb - } - |> Command.execute - |> ignore - - sb.ToString() |> should equal "Test\n" - -[] -let ``Call custom function in output`` () = - let testFunc (test: string) (s: string) = s |> should equal test - - if OperatingSystem.IsWindows() then - cli { - Exec "cmd.exe" - Arguments "/c echo Test" - Output(testFunc "Test\r\n") - } - |> Command.execute - |> ignore - else - cli { - Exec "bash" - Arguments "-c \"echo Test\"" - Output(testFunc "Test\n") - } - |> Command.execute - |> ignore - -[] -let ``Hello World with executing program async`` () = - if OperatingSystem.IsWindows() then - async { - let! output = - cli { - Exec "cmd.exe" - Arguments "/C echo Hello World!" - } - |> Command.executeAsync - - output |> Output.toText |> should equal "Hello World!" - } - else - Assert.Pass() |> async.Return - -[] -let ``Hello World with executing program with Verb`` () = - if OperatingSystem.IsWindows() then - cli { - Exec "cmd.exe" - Verb "open" - Arguments "/C echo Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Hello World with executing program throws exception with unknown Verb`` () = - if OperatingSystem.IsWindows() then - try - cli { - Exec "cmd.exe" - Verb "print" - } - |> Command.execute - |> ignore - with :? ArgumentException as ex -> - ex.Message - |> should equal ("Unknown verb 'print'. Possible verbs on 'cmd.exe': open, runas, runasuser") - else - Assert.Pass() diff --git a/src/Fli.Tests/ExecContext/ExecCommandExecuteUnixTests.fs b/src/Fli.Tests/ExecContext/ExecCommandExecuteUnixTests.fs new file mode 100644 index 0000000..250cd25 --- /dev/null +++ b/src/Fli.Tests/ExecContext/ExecCommandExecuteUnixTests.fs @@ -0,0 +1,95 @@ +module Fli.Tests.ExecContext.ExecCommandExecuteLinuxTests + +open NUnit.Framework +open FsUnit +open Fli +open System +open System.Text + +[] +[] +let ``Hello World with executing program`` () = + cli { + Exec "bash" + Arguments "-c \"echo Hello World!\"" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Get process Id`` () = + cli { + Exec "bash" + Arguments "-c \"echo Test\"" + } + |> Command.execute + |> Output.toId + |> should not' (equal 0) + +[] +[] +let ``print text with Input with executing program`` () = + cli { + Exec "bash" + Arguments "-c \"echo Test\"" + Input "echo Hello World!" + WorkingDirectory @"/etc/" + } + |> Command.execute + |> Output.toText + |> should equal "Test" + +[] +[] +let ``Get output in StringBuilder`` () = + let sb = StringBuilder() + + cli { + Exec "bash" + Arguments "-c \"echo Test\"" + Output sb + } + |> Command.execute + |> ignore + + sb.ToString() |> should equal "Test\n" + +[] +[] +let ``Call custom function in output`` () = + let testFunc (test: string) (s: string) = s |> should equal test + + cli { + Exec "bash" + Arguments "-c \"echo Test\"" + Output(testFunc "Test\n") + } + |> Command.execute + |> ignore + +[] +[] +let ``Hello World with executing program async`` () = + async { + let! output = + cli { + Exec "bash" + Arguments "-c \"echo Hello World!\"" + } + |> Command.executeAsync + + output |> Output.toText |> should equal "Hello World!" + } + +[] +[] +let ``Passing data to a progrma on stdin`` () = + cli { + Exec "cat" + Input "Test" + } + |> Command.execute + |> Output.toText + |> should equal "Test" \ No newline at end of file diff --git a/src/Fli.Tests/ExecContext/ExecCommandExecuteWindowsTests.fs b/src/Fli.Tests/ExecContext/ExecCommandExecuteWindowsTests.fs new file mode 100644 index 0000000..ae59428 --- /dev/null +++ b/src/Fli.Tests/ExecContext/ExecCommandExecuteWindowsTests.fs @@ -0,0 +1,113 @@ +module Fli.Tests.ExecContext.ExecCommandExecuteWindowsTests + +open NUnit.Framework +open FsUnit +open Fli +open System +open System.Text + + +[] +[] +let ``Hello World with executing program`` () = + cli { + Exec "cmd.exe" + Arguments "/C echo Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Get process Id`` () = + cli { + Exec "cmd.exe" + Arguments "/C echo Test" + } + |> Command.execute + |> Output.toId + |> should not' (equal 0) + +[] +[] +let ``print text with Input with executing program`` () = + cli { + Exec "cmd.exe" + Arguments "/k echo Test" + Input "echo Hello World!" + WorkingDirectory @"C:\" + } + |> Command.execute + |> Output.toText + |> should equal "Test\r\n\r\nC:\\>echo Hello World!\r\nHello World!\r\n\r\nC:\\>" + +[] +[] +let ``Get output in StringBuilder`` () = + let sb = StringBuilder() + + cli { + Exec "cmd.exe" + Arguments "/c echo Test" + Output sb + } + |> Command.execute + |> ignore + + sb.ToString() |> should equal "Test\r\n" + +[] +[] +let ``Call custom function in output`` () = + let testFunc (test: string) (s: string) = s |> should equal test + + cli { + Exec "cmd.exe" + Arguments "/c echo Test" + Output(testFunc "Test\r\n") + } + |> Command.execute + |> ignore + +[] +[] +let ``Hello World with executing program async`` () = + async { + let! output = + cli { + Exec "cmd.exe" + Arguments "/C echo Hello World!" + } + |> Command.executeAsync + + output |> Output.toText |> should equal "Hello World!" + } + + +[] +[] +let ``Hello World with executing program with Verb`` () = + cli { + Exec "cmd.exe" + Verb "open" + Arguments "/C echo Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + + +[] +[] +let ``Hello World with executing program throws exception with unknown Verb`` () = + try + cli { + Exec "cmd.exe" + Verb "print" + } + |> Command.execute + |> ignore + with :? ArgumentException as ex -> + ex.Message + |> should equal ("Unknown verb 'print'. Possible verbs on 'cmd.exe': open, runas, runasuser") \ No newline at end of file diff --git a/src/Fli.Tests/Fli.Tests.fsproj b/src/Fli.Tests/Fli.Tests.fsproj index 21e4302..ca7dd88 100644 --- a/src/Fli.Tests/Fli.Tests.fsproj +++ b/src/Fli.Tests/Fli.Tests.fsproj @@ -1,4 +1,4 @@ - + Library @@ -6,13 +6,18 @@ false - - + + + + - - - + + + + + + diff --git a/src/Fli.Tests/ShellContext/ShellCommandConfigureUnixTests.fs b/src/Fli.Tests/ShellContext/ShellCommandConfigureUnixTests.fs new file mode 100644 index 0000000..f96cf98 --- /dev/null +++ b/src/Fli.Tests/ShellContext/ShellCommandConfigureUnixTests.fs @@ -0,0 +1,98 @@ +module Fli.Tests.ShellContext.ShellCommandConfigureUnixTests + +open Fli +open NUnit.Framework +open FsUnit +open System.Collections.Generic +open System.Text + + +[] +[] +let ``Check FileName in ProcessStartInfo with CMD Shell`` () = + cli { Shell BASH } + |> Command.buildProcess + |> (fun p -> p.FileName) + |> should equal "bash" + +[] +[] +let ``Check Argument in ProcessStartInfo with Command`` () = + cli { + Shell BASH + Command "echo Hello World!" + } + |> Command.buildProcess + |> _.Arguments + |> should equal "-c \"echo Hello World!\"" + +[] +[] +let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () = + cli { + Shell BASH + WorkingDirectory @"/etc/" + } + |> Command.buildProcess + |> _.WorkingDirectory + |> should equal @"/etc/" + +[] +[] +let ``Check Environment in ProcessStartInfo with single environment variable`` () = + cli { + Shell BASH + EnvironmentVariable("Fli", "test") + } + |> Command.buildProcess + |> _.Environment.Contains(KeyValuePair("Fli", "test")) + |> should be True + +[] +[] +let ``Check Environment in ProcessStartInfo with multiple environment variables`` () = + let config = + cli { + Shell BASH + EnvironmentVariables [ ("Fli", "test"); ("Fli.Test", "test") ] + } + |> Command.buildProcess + + config.Environment.Contains(KeyValuePair("Fli", "test")) |> should be True + config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True + +[] +[] +let ``Check StandardOutputEncoding & StandardErrorEncoding with setting Encoding`` () = + let config = + cli { + Shell BASH + Encoding Encoding.UTF8 + } + |> Command.buildProcess + + config.StandardOutputEncoding |> should equal Encoding.UTF8 + config.StandardErrorEncoding |> should equal Encoding.UTF8 + +[] +[] +let ``Check all possible values in ProcessStartInfo`` () = + let config = + cli { + Shell BASH + Command "echo Hello World! €" + Input "echo TestInput" + WorkingDirectory @"C:\Users" + EnvironmentVariable("Fli", "test") + EnvironmentVariables [ ("Fli.Test", "test") ] + Encoding Encoding.UTF8 + } + |> Command.buildProcess + + config.FileName |> should equal "bash" + 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 + config.StandardOutputEncoding |> should equal Encoding.UTF8 + config.StandardErrorEncoding |> should equal Encoding.UTF8 diff --git a/src/Fli.Tests/ShellContext/ShellCommandConfigureTests.fs b/src/Fli.Tests/ShellContext/ShellCommandConfigureWindowsTests.fs similarity index 88% rename from src/Fli.Tests/ShellContext/ShellCommandConfigureTests.fs rename to src/Fli.Tests/ShellContext/ShellCommandConfigureWindowsTests.fs index 0125c9a..8abdd41 100644 --- a/src/Fli.Tests/ShellContext/ShellCommandConfigureTests.fs +++ b/src/Fli.Tests/ShellContext/ShellCommandConfigureWindowsTests.fs @@ -1,4 +1,4 @@ -module Fli.Tests.ShellContext.ShellCommandConfigureTests +module Fli.Tests.ShellContext.ShellCommandConfigureWindowsTests open Fli open NUnit.Framework @@ -8,6 +8,7 @@ open System.Text [] +[] let ``Check FileName in ProcessStartInfo with CMD Shell`` () = cli { Shell CMD } |> Command.buildProcess @@ -15,6 +16,7 @@ let ``Check FileName in ProcessStartInfo with CMD Shell`` () = |> should equal "cmd.exe" [] +[] let ``Check Argument in ProcessStartInfo with Command`` () = cli { Shell PS @@ -25,6 +27,7 @@ let ``Check Argument in ProcessStartInfo with Command`` () = |> should equal "-Command echo Hello World!" [] +[] let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () = cli { Shell CMD @@ -35,6 +38,7 @@ let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () = |> should equal @"C:\Users" [] +[] let ``Check Environment in ProcessStartInfo with single environment variable`` () = cli { Shell CMD @@ -45,6 +49,7 @@ let ``Check Environment in ProcessStartInfo with single environment variable`` ( |> should be True [] +[] let ``Check Environment in ProcessStartInfo with multiple environment variables`` () = let config = cli { @@ -57,6 +62,7 @@ let ``Check Environment in ProcessStartInfo with multiple environment variables` config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True [] +[] let ``Check StandardOutputEncoding & StandardErrorEncoding with setting Encoding`` () = let config = cli { @@ -69,10 +75,11 @@ let ``Check StandardOutputEncoding & StandardErrorEncoding with setting Encoding config.StandardErrorEncoding |> should equal Encoding.UTF8 [] +[] let ``Check all possible values in ProcessStartInfo`` () = let config = cli { - Shell BASH + Shell CMD Command "echo Hello World! €" Input "echo TestInput" WorkingDirectory @"C:\Users" @@ -82,8 +89,8 @@ let ``Check all possible values in ProcessStartInfo`` () = } |> Command.buildProcess - config.FileName |> should equal "bash" - config.Arguments |> should equal "-c \"echo Hello World! €\"" + config.FileName |> should equal "cmd.exe" + config.Arguments |> should equal "/k 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 diff --git a/src/Fli.Tests/ShellContext/ShellCommandExecuteTests.fs b/src/Fli.Tests/ShellContext/ShellCommandExecuteTests.fs deleted file mode 100644 index a883c09..0000000 --- a/src/Fli.Tests/ShellContext/ShellCommandExecuteTests.fs +++ /dev/null @@ -1,283 +0,0 @@ -module Fli.Tests.ShellContext.ShellCommandExecuteTests - -open NUnit.Framework -open FsUnit -open Fli -open System -open System.Text -open System.Diagnostics - - -[] -let ``Hello World with CMD`` () = - if OperatingSystem.IsWindows() then - let operation = - cli { - Shell CMD - Command "echo Hello World!" - } - |> Command.execute - - operation |> Output.toText |> should equal "Hello World!" - operation |> Output.toError |> should equal "" - else - Assert.Pass() - -[] -let ``Hello World with CMD waiting async`` () = - if OperatingSystem.IsWindows() then - async { - let stopwatch = new Stopwatch() - stopwatch.Start() - - try - let! operation = - cli { - Shell(CUSTOM("cmd.exe", "/K")) - Command "Hello World!" - CancelAfter 3000 - } - |> Command.executeAsync - - () - with :? AggregateException as e -> - e.GetType() |> should equal typeof - - stopwatch.Stop() - stopwatch.Elapsed.TotalSeconds |> should be (inRange 2.9 3.2) - } - else - Assert.Pass() |> async.Return - -[] -let ``Hello World with CUSTOM shell`` () = - if OperatingSystem.IsWindows() then - cli { - Shell(CUSTOM("cmd.exe", "/c")) - Command "echo Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - cli { - Shell(CUSTOM("bash", "-c")) - Command "echo Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - -[] -let ``CMD returning non zero ExitCode`` () = - if OperatingSystem.IsWindows() then - cli { - Shell CMD - Command "echl Test" - } - |> Command.execute - |> Output.toExitCode - |> should equal 1 - else - Assert.Pass() - -[] -let ``Get output in StringBuilder`` () = - let sb = StringBuilder() - - if OperatingSystem.IsWindows() then - cli { - Shell CMD - Command "echo Test" - Output sb - } - |> Command.execute - |> ignore - - sb.ToString() |> should equal "Test\r\n" - else - cli { - Shell BASH - Command "echo Test" - Output sb - } - |> Command.execute - |> ignore - - sb.ToString() |> should equal "Test\n" - -[] -let ``CMD returning non zero process id`` () = - if OperatingSystem.IsWindows() then - cli { - Shell CMD - Command "echo Test" - } - |> Command.execute - |> Output.toId - |> should not' (equal 0) - else - Assert.Pass() - -[] -let ``Text in Input with CMD`` () = - if OperatingSystem.IsWindows() then - cli { - Shell CMD - Input "echo 123\r\necho 345" - WorkingDirectory @"C:\" - } - |> Command.execute - |> Output.toText - |> should equal "C:\\>echo 123\r\n123\r\n\r\nC:\\>echo 345\r\n345\r\n\r\nC:\\>" - else - Assert.Pass() - -[] -let ``CMD returning error message`` () = - if OperatingSystem.IsWindows() then - cli { - Shell CMD - Command "echl Test" - } - |> Command.execute - |> Output.toError - |> should not' (equal None) - else - Assert.Pass() - -[] -let ``CMD returning error message without text`` () = - if OperatingSystem.IsWindows() then - let operation = - cli { - Shell CMD - Command "echl Test" - } - |> Command.execute - - operation |> Output.toError |> should not' (equal None) - operation |> Output.toText |> should equal "" - else - Assert.Pass() - -[] -let ``Hello World with PS`` () = - if OperatingSystem.IsWindows() then - cli { - Shell PS - Command "Write-Host Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Hello World with PWSH`` () = - if OperatingSystem.IsWindows() then - cli { - Shell PWSH - Command "Write-Host Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Hello World with SH`` () = - if OperatingSystem.IsWindows() |> not then - cli { - Shell SH - Command "echo Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Hello World with BASH`` () = - if OperatingSystem.IsWindows() |> not then - cli { - Shell BASH - Command "echo Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Hello World with ZSH`` () = - if OperatingSystem.IsMacOS() then - cli { - Shell ZSH - Command "echo Hello World!" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Input text in BASH`` () = - if OperatingSystem.IsWindows() |> not then - cli { - Shell BASH - Command "echo Hello World!" - Input "echo Test" - } - |> Command.execute - |> Output.toText - |> should equal "Hello World!" - else - Assert.Pass() - -[] -let ``Hello World with BASH async`` () = - if OperatingSystem.IsWindows() |> not then - async { - let! output = - cli { - Shell BASH - Command "echo Hello World!" - } - |> Command.executeAsync - - output |> Output.toText |> should equal "Hello World!" - } - else - Assert.Pass() |> async.Return - -[] -let ``BASH returning non zero ExitCode`` () = - if OperatingSystem.IsWindows() |> not then - cli { - Shell BASH - Command "echl Test" - } - |> Command.execute - |> Output.toExitCode - |> should equal 127 - else - Assert.Pass() - -[] -let ``BASH returning non zero process id`` () = - if OperatingSystem.IsWindows() |> not then - cli { - Shell BASH - Command "echo Test" - } - |> Command.execute - |> Output.toId - |> should not' (equal 0) - else - Assert.Pass() diff --git a/src/Fli.Tests/ShellContext/ShellCommandExecuteUnixTests.fs b/src/Fli.Tests/ShellContext/ShellCommandExecuteUnixTests.fs new file mode 100644 index 0000000..f4d49fe --- /dev/null +++ b/src/Fli.Tests/ShellContext/ShellCommandExecuteUnixTests.fs @@ -0,0 +1,115 @@ +module Fli.Tests.ShellContext.ShellCommandExecuteUnixTests + +open NUnit.Framework +open FsUnit +open Fli +open System +open System.Text +open System.Diagnostics + +[] +[] +let ``Hello World with CUSTOM shell`` () = + cli { + Shell(CUSTOM("bash", "-c")) + Command "echo Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Get output in StringBuilder`` () = + let sb = StringBuilder() + + cli { + Shell BASH + Command "echo Test" + Output sb + } + |> Command.execute + |> ignore + + sb.ToString() |> should equal "Test\n" + +[] +[] +let ``Hello World with SH`` () = + cli { + Shell SH + Command "echo Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Hello World with BASH`` () = + cli { + Shell BASH + Command "echo Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Hello World with ZSH`` () = + cli { + Shell ZSH + Command "echo Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Input text in BASH`` () = + cli { + Shell BASH + Command "echo Hello World!" + Input "echo Test" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Hello World with BASH async`` () = + async { + let! output = + cli { + Shell BASH + Command "echo Hello World!" + } + |> Command.executeAsync + + output |> Output.toText |> should equal "Hello World!" + } + +[] +[] +let ``BASH returning non zero ExitCode`` () = + cli { + Shell BASH + Command "echl Test" + } + |> Command.execute + |> Output.toExitCode + |> should equal 127 + +[] +[] +let ``BASH returning non zero process id`` () = + cli { + Shell BASH + Command "echo Test" + } + |> Command.execute + |> Output.toId + |> should not' (equal 0) diff --git a/src/Fli.Tests/ShellContext/ShellCommandExecuteWindowsTests.fs b/src/Fli.Tests/ShellContext/ShellCommandExecuteWindowsTests.fs new file mode 100644 index 0000000..b2876ad --- /dev/null +++ b/src/Fli.Tests/ShellContext/ShellCommandExecuteWindowsTests.fs @@ -0,0 +1,152 @@ +module Fli.Tests.ShellContext.ShellCommandExecuteWindowsTests + +open NUnit.Framework +open FsUnit +open Fli +open System +open System.Text +open System.Diagnostics + + +[] +[] +let ``Hello World with CMD`` () = + let operation = + cli { + Shell CMD + Command "echo Hello World!" + } + |> Command.execute + + operation |> Output.toText |> should equal "Hello World!" + operation |> Output.toError |> should equal "" + +[] +[] +let ``Hello World with CMD waiting async`` () = + async { + let stopwatch = new Stopwatch() + stopwatch.Start() + + try + let! operation = + cli { + Shell(CUSTOM("cmd.exe", "/K")) + Command "Hello World!" + CancelAfter 3000 + } + |> Command.executeAsync + + () + with :? AggregateException as e -> + e.GetType() |> should equal typeof + + stopwatch.Stop() + stopwatch.Elapsed.TotalSeconds |> should be (inRange 2.9 3.2) + } + +[] +[] +let ``Hello World with CUSTOM shell`` () = + cli { + Shell(CUSTOM("cmd.exe", "/c")) + Command "echo Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``CMD returning non zero ExitCode`` () = + cli { + Shell CMD + Command "echl Test" + } + |> Command.execute + |> Output.toExitCode + |> should equal 1 + +[] +[] +let ``Get output in StringBuilder`` () = + let sb = StringBuilder() + + cli { + Shell CMD + Command "echo Test" + Output sb + } + |> Command.execute + |> ignore + + sb.ToString() |> should equal "Test\r\n" + +[] +[] +let ``CMD returning non zero process id`` () = + cli { + Shell CMD + Command "echo Test" + } + |> Command.execute + |> Output.toId + |> should not' (equal 0) + +[] +[] +let ``Text in Input with CMD`` () = + cli { + Shell CMD + Input "echo 123\r\necho 345" + WorkingDirectory @"C:\" + } + |> Command.execute + |> Output.toText + |> should equal "C:\\>echo 123\r\n123\r\n\r\nC:\\>echo 345\r\n345\r\n\r\nC:\\>" + +[] +[] +let ``CMD returning error message`` () = + cli { + Shell CMD + Command "echl Test" + } + |> Command.execute + |> Output.toError + |> should not' (equal None) + +[] +[] +let ``CMD returning error message without text`` () = + let operation = + cli { + Shell CMD + Command "echl Test" + } + |> Command.execute + + operation |> Output.toError |> should not' (equal None) + operation |> Output.toText |> should equal "" + +[] +[] +let ``Hello World with PS`` () = + cli { + Shell PS + Command "Write-Host Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" + +[] +[] +let ``Hello World with PWSH`` () = + cli { + Shell PWSH + Command "Write-Host Hello World!" + } + |> Command.execute + |> Output.toText + |> should equal "Hello World!" diff --git a/src/Fli.Tests/ShellContext/ShellCommandToStringUnixTests.fs b/src/Fli.Tests/ShellContext/ShellCommandToStringUnixTests.fs new file mode 100644 index 0000000..960e7be --- /dev/null +++ b/src/Fli.Tests/ShellContext/ShellCommandToStringUnixTests.fs @@ -0,0 +1,26 @@ +module Fli.Tests.ShellContext.ShellCommandToStringUnixTests + +open NUnit.Framework +open FsUnit +open Fli +open System + +[] +[] +let ``PWSH command toString returns full line`` () = + cli { + Shell PWSH + Command "Write-Host Hello World!" + } + |> Command.toString + |> should equal "pwsh -Command Write-Host Hello World!" + +[] +[] +let ``BASH command toString returns full line`` () = + cli { + Shell BASH + Command "echo Hello World!" + } + |> Command.toString + |> should equal "bash -c \"echo Hello World!\"" diff --git a/src/Fli.Tests/ShellContext/ShellCommandToStringTests.fs b/src/Fli.Tests/ShellContext/ShellCommandToStringWindowsTests.fs similarity index 62% rename from src/Fli.Tests/ShellContext/ShellCommandToStringTests.fs rename to src/Fli.Tests/ShellContext/ShellCommandToStringWindowsTests.fs index 3e90750..c78ef6b 100644 --- a/src/Fli.Tests/ShellContext/ShellCommandToStringTests.fs +++ b/src/Fli.Tests/ShellContext/ShellCommandToStringWindowsTests.fs @@ -1,4 +1,4 @@ -module Fli.Tests.ShellContext.ShellCommandToStringTests +module Fli.Tests.ShellContext.ShellCommandToStringWindowsTests open NUnit.Framework open FsUnit @@ -7,6 +7,7 @@ open System [] +[] let ``CMD command toString returns full line`` () = cli { Shell CMD @@ -16,6 +17,7 @@ let ``CMD command toString returns full line`` () = |> should equal "cmd.exe /c echo Hello World!" [] +[] let ``CMD command toString returns full line in interactive mode`` () = cli { Shell CMD @@ -26,6 +28,7 @@ let ``CMD command toString returns full line in interactive mode`` () = |> should equal "cmd.exe /k echo Hello World!" [] +[] let ``PS command toString returns full line`` () = cli { Shell PS @@ -35,36 +38,21 @@ let ``PS command toString returns full line`` () = |> should equal "powershell.exe -Command Write-Host Hello World!" [] +[] let ``PWSH command toString returns full line`` () = - if OperatingSystem.IsWindows() then - cli { - Shell PWSH - Command "Write-Host Hello World!" - } - |> Command.toString - |> should equal "pwsh.exe -Command Write-Host Hello World!" - else - cli { - Shell PWSH - Command "Write-Host Hello World!" - } - |> Command.toString - |> should equal "pwsh -Command Write-Host Hello World!" - -[] -let ``WSL command toString returns full line`` () = cli { - Shell WSL - Command "echo Hello World!" + Shell PWSH + Command "Write-Host Hello World!" } |> Command.toString - |> should equal "wsl.exe -- echo Hello World!" + |> should equal "pwsh.exe -Command Write-Host Hello World!" [] -let ``BASH command toString returns full line`` () = +[] +let ``WSL command toString returns full line`` () = cli { - Shell BASH + Shell WSL Command "echo Hello World!" } |> Command.toString - |> should equal "bash -c \"echo Hello World!\"" + |> should equal "wsl.exe -- echo Hello World!" diff --git a/src/Fli/Command.fs b/src/Fli/Command.fs index 06ff25a..755130e 100644 --- a/src/Fli/Command.fs +++ b/src/Fli/Command.fs @@ -242,12 +242,11 @@ module Command = checkVerb context.config.Verb context.config.Program let arguments = (context.config.Arguments |> Option.defaultValue "") - - let openDefault = - if arguments = "" && context.config.EnvironmentVariables.IsNone then - true - else - false + + let openDefault = + context.config.Arguments.IsNone + && context.config.EnvironmentVariables.IsNone + && context.config.Input.IsNone (createProcess context.config.Program arguments openDefault) .With(Verb = (context.config.Verb |> Option.defaultValue null))