@@ -204,6 +204,7 @@ public AppTester(
204204 timeout ,
205205 null ,
206206 ( level , message ) => _mainLog . WriteLine ( message ) ) ;
207+ IResultFileHandler resultFileHandler = new ResultFileHandler ( _processManager , _mainLog ) ;
207208
208209 deviceListener . ConnectedTask
209210 . TimeoutAfter ( testLaunchTimeout )
@@ -252,6 +253,7 @@ await RunSimulatorTests(
252253 mlaunchArguments ,
253254 crashReporter ,
254255 testReporter ,
256+ resultFileHandler ,
255257 deviceListener ,
256258 ( ISimulatorDevice ) device ,
257259 companionDevice as ISimulatorDevice ,
@@ -289,6 +291,7 @@ await RunDeviceTests(
289291 mlaunchArguments ,
290292 crashReporter ,
291293 testReporter ,
294+ resultFileHandler ,
292295 deviceListener ,
293296 device ,
294297 appOutputLog ,
@@ -309,6 +312,7 @@ private async Task RunSimulatorTests(
309312 MlaunchArguments mlaunchArguments ,
310313 ICrashSnapshotReporter crashReporter ,
311314 ITestReporter testReporter ,
315+ IResultFileHandler resultFileHandler ,
312316 ISimpleListener deviceListener ,
313317 ISimulatorDevice simulator ,
314318 ISimulatorDevice ? companionSimulator ,
@@ -327,58 +331,20 @@ private async Task RunSimulatorTests(
327331 cancellationToken ) ;
328332
329333 await testReporter . CollectSimulatorResult ( result ) ;
330- if ( simulator == null || simulator . OSVersion == null )
331- {
332- _mainLog . WriteLine ( "Simulator OS version is not set, skipping result copying." ) ;
333- return ;
334- }
335-
336- var osVersionParts = simulator . OSVersion . Split ( ' ' , StringSplitOptions . RemoveEmptyEntries ) ;
337- if ( osVersionParts . Length < 2 )
338- {
339- _mainLog . WriteLine ( "Simulator OS version is not in the expected format, skipping result copying." ) ;
340- return ;
341- }
342-
343- bool versionParsed = Version . TryParse ( osVersionParts [ 1 ] , out var osVersion ) ;
344334
345335 // On iOS 18 and later, transferring results over a TCP tunnel isn’t supported.
346336 // Instead, copy the results file from the device to the host machine.
347- _mainLog . WriteLine ( $ "Simulator OS version: { osVersion } ") ;
348- if ( versionParsed && osVersion ! . Major >= 18 )
337+ if ( deviceListener . TestLog != null &&
338+ ! await resultFileHandler . CopyResultsAsync (
339+ runMode ,
340+ true ,
341+ simulator . OSVersion ,
342+ simulator . UDID ,
343+ appInformation . BundleIdentifier ,
344+ deviceListener . TestLog . FullPath ,
345+ cancellationToken ) )
349346 {
350- try
351- {
352- var resultsFilePathOnDevice = runMode == RunMode . iOS
353- ? "/Documents/test-results.xml"
354- : "/Library/Caches/Documents/test-results.xml" ;
355- var resultsFilePathOnHost = deviceListener . TestLog . FullPath ;
356- var simCtlCmd = $ "cp \" $(xcrun simctl get_app_container { simulator . UDID } { appInformation . BundleIdentifier } data){ resultsFilePathOnDevice } \" \" { resultsFilePathOnHost } \" ";
357-
358- var _ = await _processManager . ExecuteCommandAsync (
359- "/bin/bash" ,
360- new [ ] { "-c" , simCtlCmd } ,
361- _mainLog ,
362- _mainLog ,
363- _mainLog ,
364- TimeSpan . FromMinutes ( 1 ) ,
365- null ,
366- cancellationToken : cancellationToken ) ;
367-
368- if ( File . Exists ( resultsFilePathOnHost ) )
369- {
370- _mainLog . WriteLine ( $ "Test results copied from simulator to { resultsFilePathOnHost } ") ;
371- }
372- else
373- {
374- _mainLog . WriteLine ( $ "Failed to copy test results from simulator") ;
375- }
376- }
377- catch ( Exception ex )
378- {
379- _mainLog . WriteLine ( $ "Exception while copying test results from simulator: { ex } ") ;
380- throw ;
381- }
347+ throw new InvalidOperationException ( "Failed to copy test results from simulator to host." ) ;
382348 }
383349 }
384350
@@ -387,6 +353,7 @@ private async Task RunDeviceTests(
387353 MlaunchArguments mlaunchArguments ,
388354 ICrashSnapshotReporter crashReporter ,
389355 ITestReporter testReporter ,
356+ IResultFileHandler resultFileHandler ,
390357 ISimpleListener deviceListener ,
391358 IDevice device ,
392359 ILog appOutputLog ,
@@ -401,8 +368,6 @@ private async Task RunDeviceTests(
401368 var deviceLogCapturer = _deviceLogCapturerFactory . Create ( _mainLog , deviceSystemLog , device . Name ) ;
402369 deviceLogCapturer . StartCapture ( ) ;
403370
404- bool versionParsed = Version . TryParse ( device . OSVersion , out var osVersion ) ;
405-
406371 try
407372 {
408373 await crashReporter . StartCaptureAsync ( ) ;
@@ -457,39 +422,17 @@ private async Task RunDeviceTests(
457422
458423 // On iOS 18 and later, transferring results over a TCP tunnel isn’t supported.
459424 // Instead, copy the results file from the device to the host machine.
460- _mainLog . WriteLine ( $ "Device OS version: { osVersion } ") ;
461- if ( versionParsed && osVersion ! . Major >= 18 )
425+ if ( deviceListener . TestLog != null &&
426+ ! await resultFileHandler . CopyResultsAsync (
427+ runMode ,
428+ false ,
429+ device . OSVersion ,
430+ device . UDID ,
431+ appInformation . BundleIdentifier ,
432+ deviceListener . TestLog . FullPath ,
433+ cancellationToken ) )
462434 {
463- var resultsFilePathOnDevice = runMode == RunMode . iOS
464- ? "/Documents/test-results.xml"
465- : "/Library/Caches/Documents/test-results.xml" ;
466- var resultsFilePathOnHost = deviceListener . TestLog . FullPath ;
467- var devicectlCmd = $ "xcrun devicectl device copy from --device { device . UDID } --source { resultsFilePathOnDevice } --destination { resultsFilePathOnHost } --domain-type appDataContainer --domain-identifier { appInformation . BundleIdentifier } ";
468- try
469- {
470- var result = await _processManager . ExecuteCommandAsync (
471- "/bin/bash" ,
472- new List < string > { "-c" , devicectlCmd } ,
473- _mainLog ,
474- _mainLog ,
475- _mainLog ,
476- TimeSpan . FromMinutes ( 1 ) ,
477- null ,
478- cancellationToken : cancellationToken ) ;
479- if ( File . Exists ( resultsFilePathOnHost ) )
480- {
481- _mainLog . WriteLine ( $ "Test results copied from device to { resultsFilePathOnHost } ") ;
482- }
483- else
484- {
485- _mainLog . WriteLine ( $ "Failed to copy test results from device") ;
486- }
487- }
488- catch ( Exception ex )
489- {
490- _mainLog . WriteLine ( $ "Exception while copying test results from device: { ex } ") ;
491- throw ;
492- }
435+ throw new InvalidOperationException ( "Failed to copy test results from device to host." ) ;
493436 }
494437 }
495438
0 commit comments