Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Jul 18, 2024
1 parent 7908288 commit 912f1af
Show file tree
Hide file tree
Showing 35 changed files with 590 additions and 200 deletions.
226 changes: 170 additions & 56 deletions CSharpInteractive.Tests/README_TEMPLATE.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions CSharpInteractive.Tests/UsageScenarios/BaseScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class BaseScenario : IHost, IDisposable

public BaseScenario()
{
Composition.Shared.Root.TestEnvironment.ExitCode = default;
_tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()[..4]);
Directory.CreateDirectory(_tempDir);
_prevCurDir = Environment.CurrentDirectory;
Expand Down Expand Up @@ -81,5 +82,6 @@ void IDisposable.Dispose()
}

Environment.CurrentDirectory = _prevCurDir;
Composition.Shared.Root.TestEnvironment.ExitCode.ShouldBe(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public void Run()
// $tag=10 Command Line API
// $priority=06
// $description=Cancellation of asynchronous run
// $header=The cancellation will kill a related process.
// $header=Cancellation will destroy the process and its child processes.
// {
// Adds the namespace "HostApi" to use Command Line API
// ## using HostApi;

var cancellationTokenSource = new CancellationTokenSource();
var task = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120").RunAsync(default, cancellationTokenSource.Token);
var task = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120")
.RunAsync(default, cancellationTokenSource.Token);

cancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(100));
task.IsCompleted.ShouldBeFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ public async Task Run()
// Adds the namespace "HostApi" to use Command Line API
// ## using HostApi;

var task = await GetService<ICommandLineRunner>().RunAsync(new CommandLine("cmd", "/C", "DIR"));
await GetService<ICommandLineRunner>()
.RunAsync(new CommandLine("cmd", "/C", "DIR"))
.EnsureSuccess();

// or the same thing using the extension method
task = await new CommandLine("cmd", "/c", "DIR").RunAsync();
var result = await new CommandLine("cmd", "/c", "DIR")
.RunAsync()
.EnsureSuccess();
// }

task.ExitCode.HasValue.ShouldBeTrue();
result.ExitCode.HasValue.ShouldBeTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace CSharpInteractive.Tests.UsageScenarios;
public class CommandLineInParallelScenario : BaseScenario
{
[SkippableFact]
public void Run()
public async Task Run()
{
Skip.IfNot(Environment.OSVersion.Platform == PlatformID.Win32NT);
Skip.IfNot(string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TEAMCITY_VERSION")));
Expand All @@ -24,9 +24,15 @@ public void Run()
// Adds the namespace "HostApi" to use Command Line API
// ## using HostApi;

var task = new CommandLine("cmd", "/c", "DIR").RunAsync();
var result = new CommandLine("cmd", "/c", "SET").Run();
task.Wait();
var task = new CommandLine("cmd", "/c", "DIR")
.RunAsync()
.EnsureSuccess();

var result = new CommandLine("cmd", "/c", "SET")
.Run()
.EnsureSuccess();

await task;
// }

task.Result.ExitCode.HasValue.ShouldBeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ public void Run()
// ## using HostApi;

var lines = new List<string>();
int? exitCode = new CommandLine("cmd", "/c", "SET")
var result = new CommandLine("cmd", "/c", "SET")
.AddVars(("MyEnv", "MyVal"))
.Run(output => lines.Add(output.Line)).ExitCode;
.Run(output => lines.Add(output.Line))
.EnsureSuccess();

lines.ShouldContain("MyEnv=MyVal");
// }

exitCode.HasValue.ShouldBeTrue();
result.ExitCode.HasValue.ShouldBeTrue();
}
}
8 changes: 6 additions & 2 deletions CSharpInteractive.Tests/UsageScenarios/CommandLineScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ public void Run()
// Adds the namespace "HostApi" to use Command Line API
// ## using HostApi;

GetService<ICommandLineRunner>().Run(new CommandLine("cmd", "/c", "DIR")).EnsureSuccess();
GetService<ICommandLineRunner>()
.Run(new CommandLine("cmd", "/c", "DIR"))
.EnsureSuccess();

// or the same thing using the extension method
new CommandLine("cmd", "/c", "DIR").Run().EnsureSuccess();
new CommandLine("cmd", "/c", "DIR")
.Run()
.EnsureSuccess();

// using operator '+'
var cmd = new CommandLine("cmd") + "/c" + "DIR";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public void Run()
// Adds the namespace "HostApi" to use Command Line API
// ## using HostApi;

int? exitCode = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120").Run(default, TimeSpan.FromMilliseconds(1)).ExitCode;
int? exitCode = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120")
.Run(default, TimeSpan.FromMilliseconds(1))
.EnsureSuccess()
.ExitCode;

exitCode.HasValue.ShouldBeFalse();
// }
Expand Down
19 changes: 13 additions & 6 deletions CSharpInteractive.Tests/UsageScenarios/CommandLinesScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,31 @@ public void Run()
// ## using HostApi;

// Creates and run a simple command line
"whoami".AsCommandLine().Run();
"whoami".AsCommandLine().Run().EnsureSuccess();

// Creates and run a simple command line
new CommandLine("whoami").Run();
new CommandLine("whoami").Run().EnsureSuccess();

// Creates and run a command line with arguments
new CommandLine("cmd", "/c", "echo", "Hello").Run();

// Same as previous statement
new CommandLine("cmd", "/c")
.AddArgs("echo", "Hello")
.Run();
.Run()
.EnsureSuccess();

(new CommandLine("cmd") + "/c" + "echo" + "Hello").Run();
(new CommandLine("cmd") + "/c" + "echo" + "Hello")
.Run()
.EnsureSuccess();

"cmd".AsCommandLine("/c", "echo", "Hello").Run();
"cmd".AsCommandLine("/c", "echo", "Hello")
.Run()
.EnsureSuccess();

("cmd".AsCommandLine() + "/c" + "echo" + "Hello").Run();
("cmd".AsCommandLine() + "/c" + "echo" + "Hello")
.Run()
.EnsureSuccess();

// Just builds a command line with multiple environment variables
var cmd = new CommandLine("cmd", "/c", "echo", "Hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public void Run()
// Creates a new library project in a docker container
dockerRun
.WithCommandLine(new DotNetCustom("new", "classlib", "-n", "MyLib", "--force"))
.Run().EnsureSuccess();
.Run()
.EnsureSuccess();

// Builds the library project in a docker container
var result = dockerRun
.WithCommandLine(new DotNetBuild().WithProject("MyLib/MyLib.csproj"))
.Build();
.Build()
.EnsureSuccess();

// The "result" variable provides details about a build
result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse();
Expand Down
2 changes: 2 additions & 0 deletions CSharpInteractive.Tests/UsageScenarios/DockerRunScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ public void Run()
.Run()
.EnsureSuccess();
// }

result.ExitCode.ShouldBe(0);
}
}
16 changes: 12 additions & 4 deletions CSharpInteractive.Tests/UsageScenarios/DotNetBuildScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ public void Run()
// ## using HostApi;

// Creates a new library project, running a command like: "dotnet new classlib -n MyLib --force"
var result = new DotNetNew("xunit", "-n", "MyLib", "--force").Build();
result.ExitCode.ShouldBe(0);
new DotNetNew("xunit", "-n", "MyLib", "--force")
.Build()
.EnsureSuccess();

// Builds the library project, running a command like: "dotnet build" from the directory "MyLib"
result = new DotNetBuild().WithWorkingDirectory("MyLib").Build();
var result = new DotNetBuild()
.WithWorkingDirectory("MyLib")
.Build()
.EnsureSuccess();

// The "result" variable provides details about a build
result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse();
result.ExitCode.ShouldBe(0);

// Runs tests in docker
result = new DotNetTest().WithWorkingDirectory("MyLib").Build();
result = new DotNetTest()
.WithWorkingDirectory("MyLib")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);
result.Summary.Tests.ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public void Run()
// ## using HostApi;

// Shuts down all build servers that are started from dotnet.
new DotNetBuildServerShutdown().Run().EnsureSuccess();
new DotNetBuildServerShutdown()
.Run()
.EnsureSuccess();
// }
}
}
16 changes: 13 additions & 3 deletions CSharpInteractive.Tests/UsageScenarios/DotNetCleanScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@ public void Run()
// ## using HostApi;

// Creates a new library project, running a command like: "dotnet new classlib -n MyLib --force"
var result = new DotNetNew("classlib", "-n", "MyLib", "--force").Build();
var result = new DotNetNew("classlib", "-n", "MyLib", "--force")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Builds the library project, running a command like: "dotnet build" from the directory "MyLib"
result = new DotNetBuild().WithWorkingDirectory("MyLib").Build();
result = new DotNetBuild()
.WithWorkingDirectory("MyLib")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Clean the project, running a command like: "dotnet clean" from the directory "MyLib"
result = new DotNetClean().WithWorkingDirectory("MyLib").Build();
result = new DotNetClean()
.WithWorkingDirectory("MyLib")
.Build()
.EnsureSuccess();

// The "result" variable provides details about a build
result.ExitCode.ShouldBe(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Run()

// Gets the dotnet version, running a command like: "dotnet --version"
NuGetVersion? version = default;
var exitCode = new DotNetCustom("--version")
new DotNetCustom("--version")
.Run(message => NuGetVersion.TryParse(message.Line, out version))
.EnsureSuccess();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ public void Run()
// ## using HostApi;

// Creates a new test project, running a command like: "dotnet new mstest -n MyTests --force"
var result = new DotNetNew("mstest", "-n", "MyTests", "--force").Build();
var result = new DotNetNew("mstest", "-n", "MyTests", "--force")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Runs tests via a command like: "dotnet msbuild /t:VSTest" from the directory "MyTests"
result = new MSBuild()
.WithTarget("VSTest")
.WithWorkingDirectory("MyTests").Build();
.WithWorkingDirectory("MyTests")
.Build()
.EnsureSuccess();

// The "result" variable provides details about a build
result.ExitCode.ShouldBe(0);
Expand Down
8 changes: 6 additions & 2 deletions CSharpInteractive.Tests/UsageScenarios/DotNetPackScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ public void Run()
// ## using HostApi;

// Creates a new library project, running a command like: "dotnet new classlib -n MyLib --force"
var result = new DotNetNew("classlib", "-n", "MyLib", "--force").Build();
var result = new DotNetNew("classlib", "-n", "MyLib", "--force")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Creates a NuGet package of version 1.2.3 for the project, running a command like: "dotnet pack /p:version=1.2.3" from the directory "MyLib"
result = new DotNetPack()
.WithWorkingDirectory("MyLib")
.AddProps(("version", "1.2.3"))
.Build();
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);
// }
Expand Down
12 changes: 10 additions & 2 deletions CSharpInteractive.Tests/UsageScenarios/DotNetPublishScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ public void Run()
// ## using HostApi;

// Creates a new library project, running a command like: "dotnet new classlib -n MyLib --force"
var result = new DotNetNew("classlib", "-n", "MyLib", "--force", "-f", "net8.0").Build();
var result = new DotNetNew("classlib", "-n", "MyLib", "--force", "-f", "net8.0")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Publish the project, running a command like: "dotnet publish --framework net6.0" from the directory "MyLib"
result = new DotNetPublish().WithWorkingDirectory("MyLib").WithFramework("net8.0").Build();
result = new DotNetPublish()
.WithWorkingDirectory("MyLib")
.WithFramework("net8.0")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);
// }
}
Expand Down
11 changes: 9 additions & 2 deletions CSharpInteractive.Tests/UsageScenarios/DotNetRestoreScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ public void Run()
// ## using HostApi;

// Creates a new library project, running a command like: "dotnet new classlib -n MyLib --force"
var result = new DotNetNew("classlib", "-n", "MyLib", "--force").Build();
var result = new DotNetNew("classlib", "-n", "MyLib", "--force")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Restore the project, running a command like: "dotnet restore" from the directory "MyLib"
result = new DotNetRestore().WithWorkingDirectory("MyLib").Build();
result = new DotNetRestore()
.WithWorkingDirectory("MyLib")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);
// }
}
Expand Down
10 changes: 8 additions & 2 deletions CSharpInteractive.Tests/UsageScenarios/DotNetRunScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ public void Run()
// ## using HostApi;

// Creates a new console project, running a command like: "dotnet new console -n MyApp --force"
var result = new DotNetNew("console", "-n", "MyApp", "--force").Build();
var result = new DotNetNew("console", "-n", "MyApp", "--force")
.Build()
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Runs the console project using a command like: "dotnet run" from the directory "MyApp"
var stdOut = new List<string>();
result = new DotNetRun().WithWorkingDirectory("MyApp").Build(message => stdOut.Add(message.Text));
result = new DotNetRun().WithWorkingDirectory("MyApp")
.Build(message => stdOut.Add(message.Text))
.EnsureSuccess();

result.ExitCode.ShouldBe(0);

// Checks StdOut
Expand Down
Loading

0 comments on commit 912f1af

Please sign in to comment.