13
13
namespace Medallion . Shell . Tests
14
14
{
15
15
using System . Collections ;
16
+ using System . Text . RegularExpressions ;
16
17
using static UnitTestHelpers ;
17
18
18
19
public class GeneralTest
@@ -145,45 +146,39 @@ public void TestZeroTimeout()
145
146
[ Test ]
146
147
public void TestCancellationAlreadyCanceled ( )
147
148
{
148
- using ( var alreadyCanceled = new CancellationTokenSource ( millisecondsDelay : 0 ) )
149
- {
150
- var command = TestShell . Run ( SampleCommand , new object [ ] { "sleep" , 1000000 } , o => o . CancellationToken ( alreadyCanceled . Token ) ) ;
151
- Assert . Throws < TaskCanceledException > ( ( ) => command . Wait ( ) ) ;
152
- Assert . Throws < TaskCanceledException > ( ( ) => command . Result . ToString ( ) ) ;
153
- command . Task . Status . ShouldEqual ( TaskStatus . Canceled ) ;
154
- Assert . DoesNotThrow ( ( ) => command . ProcessId . ToString ( ) , "still executes a command and gets a process ID" ) ;
155
- }
149
+ using var alreadyCanceled = new CancellationTokenSource ( millisecondsDelay : 0 ) ;
150
+ var command = TestShell . Run ( SampleCommand , new object [ ] { "sleep" , 1000000 } , o => o . CancellationToken ( alreadyCanceled . Token ) ) ;
151
+ Assert . Throws < TaskCanceledException > ( ( ) => command . Wait ( ) ) ;
152
+ Assert . Throws < TaskCanceledException > ( ( ) => command . Result . ToString ( ) ) ;
153
+ command . Task . Status . ShouldEqual ( TaskStatus . Canceled ) ;
154
+ Assert . DoesNotThrow ( ( ) => command . ProcessId . ToString ( ) , "still executes a command and gets a process ID" ) ;
156
155
}
157
156
158
157
[ Test ]
159
158
public void TestCancellationNotCanceled ( )
160
159
{
161
- using ( var notCanceled = new CancellationTokenSource ( ) )
162
- {
163
- var command = TestShell . Run ( SampleCommand , new object [ ] { "sleep" , 1000000 } , o => o . CancellationToken ( notCanceled . Token ) ) ;
164
- command . Task . Wait ( 50 ) . ShouldEqual ( false ) ;
165
- command . Kill ( ) ;
166
- command . Task . Wait ( 1000 ) . ShouldEqual ( true ) ;
167
- command . Result . Success . ShouldEqual ( false ) ;
168
- }
160
+ using var notCanceled = new CancellationTokenSource ( ) ;
161
+ var command = TestShell . Run ( SampleCommand , new object [ ] { "sleep" , 1000000 } , o => o . CancellationToken ( notCanceled . Token ) ) ;
162
+ command . Task . Wait ( 50 ) . ShouldEqual ( false ) ;
163
+ command . Kill ( ) ;
164
+ command . Task . Wait ( 1000 ) . ShouldEqual ( true ) ;
165
+ command . Result . Success . ShouldEqual ( false ) ;
169
166
}
170
167
171
168
[ Test ]
172
169
public void TestCancellationCanceledPartway ( )
173
170
{
174
- using ( var cancellationTokenSource = new CancellationTokenSource ( ) )
175
- {
176
- var results = new SyncCollection ( ) ;
177
- var command = TestShell . Run ( SampleCommand , new object [ ] { "echo" , "--per-char" } , o => o . CancellationToken ( cancellationTokenSource . Token ) ) > results ;
178
- command . StandardInput . WriteLine ( "hello" ) ;
179
- var timeout = Task . Delay ( TimeSpan . FromSeconds ( 10 ) ) ;
180
- while ( results . Count == 0 && ! timeout . IsCompleted ) { }
181
- results . Count . ShouldEqual ( 1 ) ;
182
- cancellationTokenSource . Cancel ( ) ;
183
- var aggregateException = Assert . Throws < AggregateException > ( ( ) => command . Task . Wait ( 1000 ) ) ;
184
- Assert . IsInstanceOf < TaskCanceledException > ( aggregateException . GetBaseException ( ) ) ;
185
- CollectionAssert . AreEqual ( results , new [ ] { "hello" } ) ;
186
- }
171
+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
172
+ var results = new SyncCollection ( ) ;
173
+ var command = TestShell . Run ( SampleCommand , new object [ ] { "echo" , "--per-char" } , o => o . CancellationToken ( cancellationTokenSource . Token ) ) > results ;
174
+ command . StandardInput . WriteLine ( "hello" ) ;
175
+ var timeout = Task . Delay ( TimeSpan . FromSeconds ( 10 ) ) ;
176
+ while ( results . Count == 0 && ! timeout . IsCompleted ) { }
177
+ results . Count . ShouldEqual ( 1 ) ;
178
+ cancellationTokenSource . Cancel ( ) ;
179
+ var aggregateException = Assert . Throws < AggregateException > ( ( ) => command . Task . Wait ( 1000 ) ) ;
180
+ Assert . IsInstanceOf < TaskCanceledException > ( aggregateException . GetBaseException ( ) ) ;
181
+ CollectionAssert . AreEqual ( results , new [ ] { "hello" } ) ;
187
182
}
188
183
189
184
private class SyncCollection : ICollection < string >
@@ -207,16 +202,14 @@ private class SyncCollection : ICollection<string>
207
202
[ Test ]
208
203
public void TestCancellationCanceledAfterCompletion ( )
209
204
{
210
- using ( var cancellationTokenSource = new CancellationTokenSource ( ) )
211
- {
212
- var results = new List < string > ( ) ;
213
- var command = TestShell . Run ( SampleCommand , new object [ ] { "echo" } , o => o . CancellationToken ( cancellationTokenSource . Token ) ) > results ;
214
- command . StandardInput . WriteLine ( "hello" ) ;
215
- command . StandardInput . Close ( ) ;
216
- command . Task . Wait ( 1000 ) . ShouldEqual ( true ) ;
217
- cancellationTokenSource . Cancel ( ) ;
218
- command . Result . Success . ShouldEqual ( true ) ;
219
- }
205
+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
206
+ var results = new List < string > ( ) ;
207
+ var command = TestShell . Run ( SampleCommand , new object [ ] { "echo" } , o => o . CancellationToken ( cancellationTokenSource . Token ) ) > results ;
208
+ command . StandardInput . WriteLine ( "hello" ) ;
209
+ command . StandardInput . Close ( ) ;
210
+ command . Task . Wait ( 1000 ) . ShouldEqual ( true ) ;
211
+ cancellationTokenSource . Cancel ( ) ;
212
+ command . Result . Success . ShouldEqual ( true ) ;
220
213
}
221
214
222
215
[ Test ]
@@ -357,7 +350,7 @@ public void TestVersioning()
357
350
var version = typeof ( Command ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Version . ToString ( ) ;
358
351
var informationalVersion = ( AssemblyInformationalVersionAttribute ) typeof ( Command ) . GetTypeInfo ( ) . Assembly . GetCustomAttribute ( typeof ( AssemblyInformationalVersionAttribute ) ) ;
359
352
Assert . IsNotNull ( informationalVersion ) ;
360
- version . ShouldEqual ( informationalVersion . InformationalVersion + ".0" ) ;
353
+ version . ShouldEqual ( Regex . Replace ( informationalVersion . InformationalVersion , "-.*$" , string . Empty ) + ".0" ) ;
361
354
}
362
355
363
356
[ Test ]
@@ -495,12 +488,10 @@ void TestHelper(bool disposeOnExit)
495
488
496
489
#if ! NETCOREAPP2_2
497
490
// https://stackoverflow.com/questions/2633628/can-i-get-command-line-arguments-of-other-processes-from-net-c
498
- string GetCommandLine ( int processId )
491
+ static string GetCommandLine ( int processId )
499
492
{
500
- using ( var searcher = new System . Management . ManagementObjectSearcher ( "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + processId ) )
501
- {
502
- return searcher . Get ( ) . Cast < System . Management . ManagementBaseObject > ( ) . Single ( ) [ "CommandLine" ] . ToString ( ) ;
503
- }
493
+ using var searcher = new System . Management . ManagementObjectSearcher ( "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + processId ) ;
494
+ return searcher . Get ( ) . Cast < System . Management . ManagementBaseObject > ( ) . Single ( ) [ "CommandLine" ] . ToString ( ) ;
504
495
}
505
496
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
506
497
{
0 commit comments