@@ -123,9 +123,8 @@ public void Run()
123
123
124
124
private class MyTask (ICommandLineRunner runner )
125
125
{
126
-
127
126
public int ? Run () =>
128
- runner .Run (new CommandLine (" whoami" ));
127
+ runner .Run (new CommandLine (" whoami" )). ExitCode ;
129
128
}
130
129
131
130
```
@@ -256,22 +255,18 @@ cmd = new CommandLine("cmd", "/c", "echo", "Hello")
256
255
// Adds the namespace "HostApi" to use Command Line API
257
256
using HostApi ;
258
257
259
- var exitCode = GetService <ICommandLineRunner >().Run (new CommandLine (" cmd" , " /c" , " DIR" ));
260
- exitCode .ShouldBe (0 );
258
+ GetService <ICommandLineRunner >().Run (new CommandLine (" cmd" , " /c" , " DIR" )).EnsureSuccess ();
261
259
262
260
// or the same thing using the extension method
263
- exitCode = new CommandLine (" cmd" , " /c" , " DIR" ).Run ();
264
- exitCode .ShouldBe (0 );
261
+ new CommandLine (" cmd" , " /c" , " DIR" ).Run ().EnsureSuccess ();
265
262
266
263
// using operator '+'
267
264
var cmd = new CommandLine (" cmd" ) + " /c" + " DIR" ;
268
- exitCode = cmd .Run ();
269
- exitCode .ShouldBe (0 );
265
+ cmd .Run ().EnsureSuccess ();
270
266
271
267
// with environment variables
272
268
cmd = new CommandLine (" cmd" ) + " /c" + " DIR" + (" MyEnvVar" , " Some Value" );
273
- exitCode = cmd .Run ();
274
- exitCode .ShouldBe (0 );
269
+ cmd .Run ().EnsureSuccess ();
275
270
```
276
271
277
272
@@ -284,10 +279,10 @@ exitCode.ShouldBe(0);
284
279
// Adds the namespace "HostApi" to use Command Line API
285
280
using HostApi ;
286
281
287
- int ? exitCode = await GetService <ICommandLineRunner >().RunAsync (new CommandLine (" cmd" , " /C" , " DIR" ));
282
+ var task = await GetService <ICommandLineRunner >().RunAsync (new CommandLine (" cmd" , " /C" , " DIR" ));
288
283
289
284
// or the same thing using the extension method
290
- exitCode = await new CommandLine (" cmd" , " /c" , " DIR" ).RunAsync ();
285
+ task = await new CommandLine (" cmd" , " /c" , " DIR" ).RunAsync ();
291
286
```
292
287
293
288
@@ -303,7 +298,7 @@ using HostApi;
303
298
var lines = new List <string >();
304
299
int ? exitCode = new CommandLine (" cmd" , " /c" , " SET" )
305
300
.AddVars ((" MyEnv" , " MyVal" ))
306
- .Run (output => lines .Add (output .Line ));
301
+ .Run (output => lines .Add (output .Line )). ExitCode ;
307
302
308
303
lines .ShouldContain (" MyEnv=MyVal" );
309
304
```
@@ -318,8 +313,8 @@ lines.ShouldContain("MyEnv=MyVal");
318
313
// Adds the namespace "HostApi" to use Command Line API
319
314
using HostApi ;
320
315
321
- Task < int ? > task = new CommandLine (" cmd" , " /c" , " DIR" ).RunAsync ();
322
- int ? exitCode = new CommandLine (" cmd" , " /c" , " SET" ).Run ();
316
+ var task = new CommandLine (" cmd" , " /c" , " DIR" ).RunAsync ();
317
+ var result = new CommandLine (" cmd" , " /c" , " SET" ).Run ();
323
318
task .Wait ();
324
319
```
325
320
@@ -334,7 +329,7 @@ The cancellation will kill a related process.
334
329
using HostApi ;
335
330
336
331
var cancellationTokenSource = new CancellationTokenSource ();
337
- Task < int ? > task = new CommandLine (" cmd" , " /c" , " TIMEOUT" , " /T" , " 120" ).RunAsync (default , cancellationTokenSource .Token );
332
+ var task = new CommandLine (" cmd" , " /c" , " TIMEOUT" , " /T" , " 120" ).RunAsync (default , cancellationTokenSource .Token );
338
333
339
334
cancellationTokenSource .CancelAfter (TimeSpan .FromMilliseconds (100 ));
340
335
task .IsCompleted .ShouldBeFalse ();
@@ -350,7 +345,7 @@ If timeout expired a process will be killed.
350
345
// Adds the namespace "HostApi" to use Command Line API
351
346
using HostApi ;
352
347
353
- int ? exitCode = new CommandLine (" cmd" , " /c" , " TIMEOUT" , " /T" , " 120" ).Run (default , TimeSpan .FromMilliseconds (1 ));
348
+ int ? exitCode = new CommandLine (" cmd" , " /c" , " TIMEOUT" , " /T" , " 120" ).Run (default , TimeSpan .FromMilliseconds (1 )). ExitCode ;
354
349
355
350
exitCode .HasValue .ShouldBeFalse ();
356
351
```
@@ -420,9 +415,10 @@ using HostApi;
420
415
421
416
// Gets the dotnet version, running a command like: "dotnet --version"
422
417
NuGetVersion ? version = default ;
423
- var exitCode = new DotNetCustom (" --version" ).Run (message => NuGetVersion .TryParse (message .Line , out version ));
418
+ var exitCode = new DotNetCustom (" --version" )
419
+ .Run (message => NuGetVersion .TryParse (message .Line , out version ))
420
+ .EnsureSuccess ();
424
421
425
- exitCode .ShouldBe (0 );
426
422
version .ShouldNotBeNull ();
427
423
```
428
424
@@ -569,17 +565,16 @@ result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
569
565
using HostApi ;
570
566
571
567
// Creates a new test project, running a command like: "dotnet new mstest -n MyTests --force"
572
- var exitCode = new DotNetNew (" mstest" , " -n" , " MyTests" , " --force" ). Run ();
573
- exitCode . ShouldBe ( 0 );
568
+ new DotNetNew (" mstest" , " -n" , " MyTests" , " --force" )
569
+ . Run (). EnsureSuccess ( );
574
570
575
571
// Creates the tool manifest and installs the dotCover tool locally
576
572
// It is better to run the following 2 commands manually
577
573
// and commit these changes to a source control
578
- exitCode = new DotNetNew (" tool-manifest" ).Run ();
579
- exitCode .ShouldBe (0 );
574
+ new DotNetNew (" tool-manifest" ).Run ().EnsureSuccess ();
580
575
581
- exitCode = new DotNetCustom (" tool" , " install" , " --local" , " JetBrains.dotCover.GlobalTool" ). Run ();
582
- exitCode . ShouldBe ( 0 );
576
+ new DotNetCustom (" tool" , " install" , " --local" , " JetBrains.dotCover.GlobalTool" )
577
+ . Run (). EnsureSuccess ( );
583
578
584
579
// Creates a test command
585
580
var test = new DotNetTest ().WithProject (" MyTests" );
@@ -605,8 +600,8 @@ result.ExitCode.ShouldBe(0);
605
600
result .Tests .Count (i => i .State == TestState .Finished ).ShouldBe (1 );
606
601
607
602
// Generates a HTML code coverage report.
608
- exitCode = new DotNetCustom (" dotCover" , " report" , $" --source={dotCoverSnapshot }" , $" --output={dotCoverReport }" , " --reportType=HTML" ). Run ();
609
- exitCode . ShouldBe ( 0 );
603
+ new DotNetCustom (" dotCover" , " report" , $" --source={dotCoverSnapshot }" , $" --output={dotCoverReport }" , " --reportType=HTML" )
604
+ . Run (). EnsureSuccess ( );
610
605
611
606
// Check for a dotCover report
612
607
File .Exists (dotCoverReport ).ShouldBeTrue ();
@@ -626,12 +621,10 @@ var projectDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()[..4]
626
621
Directory .CreateDirectory (projectDir );
627
622
628
623
// Creates a local tool manifest
629
- var exitCode = new DotNetNew (" tool-manifest" ).WithWorkingDirectory (projectDir ).Run ();
630
- exitCode .ShouldBe (0 );
624
+ new DotNetNew (" tool-manifest" ).WithWorkingDirectory (projectDir ).Run ().EnsureSuccess ();
631
625
632
626
// Restore local tools
633
- exitCode = new DotNetToolRestore ().WithWorkingDirectory (projectDir ).Run ();
634
- exitCode .ShouldBe (0 );
627
+ new DotNetToolRestore ().WithWorkingDirectory (projectDir ).Run ().EnsureSuccess ();
635
628
```
636
629
637
630
@@ -703,9 +696,7 @@ result.ExitCode.ShouldBe(0);
703
696
using HostApi ;
704
697
705
698
// Shuts down all build servers that are started from dotnet.
706
- var exitCode = new DotNetBuildServerShutdown ().Run ();
707
-
708
- exitCode .ShouldBe (0 );
699
+ new DotNetBuildServerShutdown ().Run ().EnsureSuccess ();
709
700
```
710
701
711
702
@@ -764,11 +755,9 @@ var dockerRun = new DockerRun()
764
755
765
756
766
757
// Creates a new library project in a docker container
767
- var exitCode = dockerRun
758
+ dockerRun
768
759
.WithCommandLine (new DotNetCustom (" new" , " classlib" , " -n" , " MyLib" , " --force" ))
769
- .Run ();
770
-
771
- exitCode .ShouldBe (0 );
760
+ .Run ().EnsureSuccess ();
772
761
773
762
// Builds the library project in a docker container
774
763
var result = dockerRun
@@ -799,9 +788,8 @@ var cmd = new CommandLine("whoami");
799
788
// Runs the command line in a docker container
800
789
var result = new DockerRun (cmd , " mcr.microsoft.com/dotnet/sdk" )
801
790
.WithAutoRemove (true )
802
- .Run ();
803
-
804
- result .ShouldBe (0 );
791
+ .Run ()
792
+ .EnsureSuccess ();
805
793
```
806
794
807
795
0 commit comments