Skip to content

Commit

Permalink
Cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UnstoppableMango committed Jan 19, 2024
1 parent 33a869b commit 531f4eb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
63 changes: 61 additions & 2 deletions src/CliWrap.FSharp.Tests/CliTests.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
module CliTests

open System.Collections.Generic
open System.Linq
open CliWrap
open CliWrap.Tests
open FsCheck
open FsCheck.Xunit
open UnMango.CliWrap.FSharp

[<Property>]
let ``Should create command`` target =
let expected = Command(target)

let actual = Cli.wrap target

expected.TargetFilePath = actual.TargetFilePath

[<Property>]
let ``Should configure a single argument`` (arg: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithArguments(arg.Get)
let actual = cmd |> Cli.arg arg.Get
expected.Arguments = actual.Arguments

[<Property>]
let ``Should configure arguments`` (arg: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithArguments([ arg.Get ])
let actual = cmd |> Cli.args [ arg.Get ]
expected.Arguments = actual.Arguments

[<Property>]
let ``Should configure arguments with escape`` (arg: NonNull<string>) escape =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithArguments([ arg.Get ], escape)
let actual = cmd |> Cli.argse [ arg.Get ] escape
expected.Arguments = actual.Arguments

[<Property>]
let ``Should configure arguments with builder`` (arg: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithArguments(fun b -> b.Add(arg.Get) |> ignore)
let actual = cmd |> Cli.argsf _.Add(arg.Get)
expected.Arguments = actual.Arguments

[<Property>]
let ``Should configure credentials`` (arg: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithCredentials(Credentials(arg.Get))
let actual = cmd |> Cli.creds (Credentials(arg.Get))
expected.Credentials.Domain = actual.Credentials.Domain

[<Property>]
let ``Should configure credentials with builder`` (arg: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithCredentials(fun b -> b.SetUserName(arg.Get) |> ignore)
let actual = cmd |> Cli.credsf _.SetUserName(arg.Get)
expected.Credentials.UserName = actual.Credentials.UserName

[<Property>]
let ``Should configure environment variables`` (key: NonNull<string>) (value: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithEnvironmentVariables((dict [key.Get, value.Get]).AsReadOnly())
let actual = cmd |> Cli.env [key.Get, value.Get]
expected.EnvironmentVariables.SequenceEqual(actual.EnvironmentVariables)

[<Property>]
let ``Should configure environment variables with builder`` (key: NonNull<string>) (value: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let expected = cmd.WithEnvironmentVariables(fun b -> b.Set(key.Get, value.Get) |> ignore)
let actual = cmd |> Cli.envf _.Set(key.Get, value.Get)
expected.EnvironmentVariables.SequenceEqual(actual.EnvironmentVariables)

12 changes: 7 additions & 5 deletions src/CliWrap.FSharp/Cli.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module UnMango.CliWrap.FSharp.Cli

open System
open System.Collections.Generic
open CliWrap
open CliWrap.Builders
Expand Down Expand Up @@ -40,7 +39,8 @@ let argse args escape (command: Command) = command.WithArguments(args, escape)
/// <param name="f">The function that builds the arguments using an ArgumentsBuilder.</param>
/// <param name="command">The command for which the arguments are being built.</param>
/// <returns>A new command object with the arguments added.</returns>
let argsf (f: ArgumentsBuilder -> unit) (command: Command) = command.WithArguments(f)
let argsf (f: ArgumentsBuilder -> 'a) (command: Command) =
command.WithArguments(fun b -> f b |> ignore)

/// <summary>
/// Creates a new command with the specified parameters.
Expand All @@ -56,7 +56,7 @@ let argsf (f: ArgumentsBuilder -> unit) (command: Command) = command.WithArgumen
/// <param name="stderr">The error pipe target for the command.</param>
/// <returns>A new Command instance.</returns>
/// <remarks>
/// v = verbose. Idk why you would use this, but its there if you want to
/// v = verbose. Idk why you would use this, but it's there if you want to
/// </remarks>
let commandv target args workDir creds env v stdin stdout stderr =
Command(target, args, workDir, creds, env, v, stdin, stdout, stderr)
Expand All @@ -75,7 +75,8 @@ let creds (credentials: Credentials) (command: Command) = command.WithCredential
/// <param name="f">The function that builds the credentials with a CredentialsBuilder.</param>
/// <param name="command">The command for which the credentials are being built.</param>
/// <returns>The modified command with the credentials applied.</returns>
let credsf (f: CredentialsBuilder -> unit) (command: Command) = command.WithCredentials(f)
let credsf (f: CredentialsBuilder -> 'a) (command: Command) =
command.WithCredentials(fun b -> f b |> ignore)

/// <summary>
/// Creates a copy of this command, setting the environment variables to the specified value.
Expand All @@ -92,7 +93,8 @@ let env (env: (string * string) seq) (command: Command) =
/// <param name="f">The function that builds environment variables using an EnvironmentVariablesBuilder.</param>
/// <param name="command">The command to which the environment variables should be applied.</param>
/// <returns>The modified command with the updated environment variables.</returns>
let envf (f: EnvironmentVariablesBuilder -> unit) (command: Command) = command.WithEnvironmentVariables(f)
let envf (f: EnvironmentVariablesBuilder -> 'a) (command: Command) =
command.WithEnvironmentVariables(fun b -> f b |> ignore)

/// <summary>
/// Executes the command asynchronously.
Expand Down

0 comments on commit 531f4eb

Please sign in to comment.