Skip to content
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

Change how the dummy program is vendored #26

Closed
wants to merge 11 commits into from
23 changes: 15 additions & 8 deletions CliWrap.FSharp.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
Expand All @@ -20,6 +20,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{BE2AB152
global.json = global.json
.config\dotnet-tools.json = .config\dotnet-tools.json
Directory.Build.props = Directory.Build.props
scripts\update-vendored-code.sh = scripts\update-vendored-code.sh
patches\0001-CliWrap.Tests.Dummy.csproj.patch = patches\0001-CliWrap.Tests.Dummy.csproj.patch
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "github", "github", "{FE346152-5716-4224-9B25-C6B3815C70CA}"
Expand All @@ -40,9 +42,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{BC5059
.gitmodules = .gitmodules
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "vendor", "vendor", "{711E438A-D184-4513-921C-C4D7AE85E2B9}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliWrap.Tests.Dummy", "src\CliWrap.Tests.Dummy\CliWrap.Tests.Dummy.csproj", "{26F29434-8760-4387-BF01-4FB1FA65426E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliWrap.Tests.Dummy", "vendor\CliWrap\CliWrap.Tests.Dummy\CliWrap.Tests.Dummy.csproj", "{C06FC79C-B308-4078-8C36-AB2F42C583E0}"
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "CliWrap.FSharp.Tests.Dummy", "src\CliWrap.FSharp.Tests.Dummy\CliWrap.FSharp.Tests.Dummy.fsproj", "{47479E30-792D-4B63-A1A9-D5981968D21E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -61,14 +63,19 @@ Global
{12BFD7AA-776D-4B58-8AC3-241AF39EED84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12BFD7AA-776D-4B58-8AC3-241AF39EED84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12BFD7AA-776D-4B58-8AC3-241AF39EED84}.Release|Any CPU.Build.0 = Release|Any CPU
{C06FC79C-B308-4078-8C36-AB2F42C583E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C06FC79C-B308-4078-8C36-AB2F42C583E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C06FC79C-B308-4078-8C36-AB2F42C583E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C06FC79C-B308-4078-8C36-AB2F42C583E0}.Release|Any CPU.Build.0 = Release|Any CPU
{26F29434-8760-4387-BF01-4FB1FA65426E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26F29434-8760-4387-BF01-4FB1FA65426E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26F29434-8760-4387-BF01-4FB1FA65426E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26F29434-8760-4387-BF01-4FB1FA65426E}.Release|Any CPU.Build.0 = Release|Any CPU
{47479E30-792D-4B63-A1A9-D5981968D21E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47479E30-792D-4B63-A1A9-D5981968D21E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47479E30-792D-4B63-A1A9-D5981968D21E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47479E30-792D-4B63-A1A9-D5981968D21E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2E2FBB43-30C0-45A1-8DAC-D7A48411EC7A} = {DE85DD40-1128-4648-A2EB-9A1FE73C28A4}
{12BFD7AA-776D-4B58-8AC3-241AF39EED84} = {DE85DD40-1128-4648-A2EB-9A1FE73C28A4}
{C06FC79C-B308-4078-8C36-AB2F42C583E0} = {711E438A-D184-4513-921C-C4D7AE85E2B9}
{26F29434-8760-4387-BF01-4FB1FA65426E} = {DE85DD40-1128-4648-A2EB-9A1FE73C28A4}
{47479E30-792D-4B63-A1A9-D5981968D21E} = {DE85DD40-1128-4648-A2EB-9A1FE73C28A4}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions patches/0001-CliWrap.Tests.Dummy.csproj.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
5,6c5,6
< <TargetFrameworks>net8.0</TargetFrameworks>
< <TargetFrameworks Condition="$([MSBuild]::IsOsPlatform('Windows'))">$(TargetFrameworks);net48</TargetFrameworks>
---
> <TargetFramework>net8.0</TargetFramework>
> <RestorePackagesWithLockFile>false</RestorePackagesWithLockFile>
11d10
< <PackageReference Include="CSharpier.MsBuild" Version="0.27.0" PrivateAssets="all" />
16c15
< </Project>
\ No newline at end of file
---
> </Project>
15 changes: 15 additions & 0 deletions scripts/update-vendored-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -eum

root="$(git rev-parse --show-toplevel)"
patch="$root/patches/0001-CliWrap.Tests.Dummy.csproj.patch"
vendorDir="$root/vendor/CliWrap"
targetDir="$root/src/CliWrap.Tests.Dummy"

git submodule foreach --recursive git clean -xfd
rm -rf "$targetDir" || echo 'Target is clean'

[ '--clean' == "${1:-}" ] && exit 0

cp -r "$vendorDir/CliWrap.Tests.Dummy" "$targetDir"
patch "$targetDir/CliWrap.Tests.Dummy.csproj" <"$patch"
17 changes: 17 additions & 0 deletions src/CliWrap.FSharp.Tests.Dummy/CliWrap.FSharp.Tests.Dummy.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>UnMango.CliWrap.FSharp.Tests.Dummy</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1"/>
</ItemGroup>

</Project>
81 changes: 81 additions & 0 deletions src/CliWrap.FSharp.Tests.Dummy/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace UnMango.CliWrap.FSharp.Tests.Dummy

open System
open System.Buffers
open System.CommandLine
open System.CommandLine.Invocation
open System.IO
open System.Reflection
open System.Runtime.InteropServices
open System.Threading.Tasks

module Opt =
let add o (r: Command) =
r.AddOption(o)
r

module Cmd =
let add c (r: Command) =
r.AddCommand(c)
r

let handler (f: InvocationContext -> Task) (c: Command) =
c.SetHandler(f)
c

module Commands =
let streams =
function
| "stdout" -> [ Console.OpenStandardOutput() ]
| "stderr" -> [ Console.OpenStandardError() ]
| "all" -> [ Console.OpenStandardOutput(); Console.OpenStandardError() ]
| _ -> failwith "unsupported target"

let rec generateBytes (rand: Random) (buf: IMemoryOwner<byte>) (streams: Stream list) len =
function
| total when total >= len -> ()
| total ->
rand.NextBytes(buf.Memory.Span)
let wanted = Math.Min(int64 buf.Memory.Length, len - total)

for stream in streams do
stream.Write(buf.Memory.Slice(0, int wanted).Span)

generateBytes rand buf streams len (total + wanted)

let generate =
let targetOpt = Option<string>("--target", (fun () -> "stdout"))
let lengthOpt = Option<int64>("--length", (fun () -> 100_000L))
let bufferOpt = Option<int>("--buffer", (fun () -> 1024))
let rand = Random(1234567)

Command("generate")
|> Cmd.add (
Command("binary")
|> Opt.add targetOpt
|> Opt.add lengthOpt
|> Opt.add bufferOpt
|> Cmd.handler (fun c -> task {
let streams = c.ParseResult.GetValueForOption(targetOpt) |> streams
let len = c.ParseResult.GetValueForOption(lengthOpt)
let size = c.ParseResult.GetValueForOption(bufferOpt)
use buf = MemoryPool<byte>.Shared.Rent(size)
generateBytes rand buf streams len 0L
})
)

type Program =
static member FilePath =
Path.ChangeExtension(
Assembly.GetExecutingAssembly().Location,
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
"exe"
else
null
)

module Program =
let command = RootCommand("Dummy program for testing") |> Cmd.add Commands.generate

[<EntryPoint>]
let main args = command.Invoke args
19 changes: 19 additions & 0 deletions src/CliWrap.FSharp.Tests.Dummy/packages.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": 1,
"dependencies": {
"net8.0": {
"FSharp.Core": {
"type": "Direct",
"requested": "[8.0.300, )",
"resolved": "8.0.300",
"contentHash": "Jv44fV7TNglyMku89lQcA4Q6mFKLyHb2bs1Yb72nvSVc+cHplEnoZ4XQUaaTLJGUTx/iMqcrkYGtaLzkkIhpaA=="
},
"System.CommandLine": {
"type": "Direct",
"requested": "[2.0.0-beta4.22272.1, )",
"resolved": "2.0.0-beta4.22272.1",
"contentHash": "1uqED/q2H0kKoLJ4+hI2iPSBSEdTuhfCYADeJrAqERmiGQ2NNacYKRNEQ+gFbU4glgVyK8rxI+ZOe1onEtr/Pg=="
}
}
}
}
37 changes: 18 additions & 19 deletions src/CliWrap.FSharp.Tests/CliTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ open System.Linq
open System.Text
open System.Threading
open CliWrap
open CliWrap.Tests
open Xunit
open FsCheck
open FsCheck.Xunit
Expand Down Expand Up @@ -40,7 +39,7 @@ let ``Should create command the long way``

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

let actual = cmd |> Cli.arg arg.Get
Expand All @@ -49,7 +48,7 @@ let ``Should configure a single argument`` (arg: NonNull<string>) =

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

let actual = cmd |> Cli.args [ arg.Get ]
Expand All @@ -58,7 +57,7 @@ let ``Should configure arguments`` (arg: NonNull<string>) =

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

let actual = cmd |> Cli.argse [ arg.Get ] escape
Expand All @@ -67,7 +66,7 @@ let ``Should configure arguments with escape`` (arg: NonNull<string>) escape =

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

let actual = cmd |> Cli.argsf _.Add(arg.Get)
Expand All @@ -76,7 +75,7 @@ let ``Should configure arguments with builder`` (arg: NonNull<string>) =

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

let actual = cmd |> Cli.creds (Credentials(arg.Get))
Expand All @@ -85,7 +84,7 @@ let ``Should configure credentials`` (arg: NonNull<string>) =

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

let actual = cmd |> Cli.credsf _.SetUserName(arg.Get)
Expand All @@ -94,7 +93,7 @@ let ``Should configure credentials with builder`` (arg: NonNull<string>) =

[<Property>]
let ``Should configure environment variables`` (key: NonNull<string>) (value: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)

let expected =
cmd.WithEnvironmentVariables((dict [ key.Get, value.Get ]).AsReadOnly())
Expand All @@ -104,7 +103,7 @@ let ``Should configure environment variables`` (key: NonNull<string>) (value: No

[<Property>]
let ``Should configure environment variables with builder`` (key: NonNull<string>) (value: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)

let expected =
cmd.WithEnvironmentVariables(fun b -> b.Set(key.Get, value.Get) |> ignore)
Expand All @@ -114,7 +113,7 @@ let ``Should configure environment variables with builder`` (key: NonNull<string

[<Fact>]
let ``Should execute asynchronously`` () = task {
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let! expected = cmd.ExecuteAsync()

let! actual = cmd |> Cli.exec
Expand All @@ -125,7 +124,7 @@ let ``Should execute asynchronously`` () = task {
[<Fact>]
let ``Should execute asynchronously with cancellation`` () = task {
use cts = new CancellationTokenSource()
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let! expected = cmd.ExecuteAsync(cts.Token)

let! actual = cmd |> Cli.Task.exec cts.Token
Expand All @@ -137,7 +136,7 @@ let ``Should execute asynchronously with cancellation`` () = task {
let ``Should execute asynchronously with forceful and graceful tokens`` () = task {
use forceful = new CancellationTokenSource()
use graceful = new CancellationTokenSource()
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let! expected = cmd.ExecuteAsync(forceful.Token, graceful.Token)

let! actual = cmd |> Cli.Task.execf forceful.Token graceful.Token
Expand All @@ -148,7 +147,7 @@ let ``Should execute asynchronously with forceful and graceful tokens`` () = tas
[<Property>]
let ``Should configure stdin`` (value: NonNull<string>) =
let input = PipeSource.FromString value.Get
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let expected = cmd.WithStandardInputPipe(input)

let actual = cmd |> Cli.stdin input
Expand All @@ -158,7 +157,7 @@ let ``Should configure stdin`` (value: NonNull<string>) =
[<Property>]
let ``Should configure stdout`` () =
let pipe = PipeTarget.ToStringBuilder(StringBuilder())
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let expected = cmd.WithStandardOutputPipe(pipe)

let actual = cmd |> Cli.stdout pipe
Expand All @@ -168,7 +167,7 @@ let ``Should configure stdout`` () =
[<Property>]
let ``Should configure stderr`` () =
let pipe = PipeTarget.ToStringBuilder(StringBuilder())
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let expected = cmd.WithStandardErrorPipe(pipe)

let actual = cmd |> Cli.stderr pipe
Expand All @@ -177,7 +176,7 @@ let ``Should configure stderr`` () =

[<Property>]
let ``Should configure target file`` (file: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let expected = cmd.WithTargetFile(file.Get)

let actual = cmd |> Cli.target file.Get
Expand All @@ -186,7 +185,7 @@ let ``Should configure target file`` (file: NonNull<string>) =

[<Property>]
let ``Should configure validation`` () =
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let expected = cmd.WithValidation(CommandResultValidation.None)

let actual = cmd |> Cli.validation CommandResultValidation.None
Expand All @@ -195,7 +194,7 @@ let ``Should configure validation`` () =

[<Property>]
let ``Should configure working directory`` (dir: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath)
let cmd = Command(Tests.Dummy.Program.FilePath)
let expected = cmd.WithWorkingDirectory(dir.Get)

let actual = cmd |> Cli.workDir dir.Get
Expand All @@ -204,7 +203,7 @@ let ``Should configure working directory`` (dir: NonNull<string>) =

[<Property>]
let ``Should convert to string`` (arg: NonNull<string>) =
let cmd = Command(Dummy.Program.FilePath).WithArguments([ arg.Get ])
let cmd = Command(Tests.Dummy.Program.FilePath).WithArguments([ arg.Get ])
let expected = cmd.ToString()

let actual = cmd |> Cli.toString
Expand Down
2 changes: 1 addition & 1 deletion src/CliWrap.FSharp.Tests/CliWrap.FSharp.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\vendor\CliWrap\CliWrap.Tests.Dummy\CliWrap.Tests.Dummy.csproj" />
<ProjectReference Include="..\CliWrap.FSharp.Tests.Dummy\CliWrap.FSharp.Tests.Dummy.fsproj" />
<ProjectReference Include="..\CliWrap.FSharp\CliWrap.FSharp.fsproj"/>
</ItemGroup>

Expand Down
Loading
Loading