@@ -223,6 +223,26 @@ private static string GetApplicationWorkingDirectory(string testAssemblyLocation
223
223
) ;
224
224
}
225
225
226
+ /// <summary>
227
+ /// Builds the path to the process's directory
228
+ /// </summary>
229
+ /// <param name="testAssemblyLocation">The path to the test's directory</param>
230
+ /// <param name="appLocation">The path to the processes directory</param>
231
+ /// <returns>The path to the directory for the given app</returns>
232
+ private static string GetAppsettingsDirectory ( string testAssemblyLocation , string appLocation )
233
+ {
234
+ string testedAppLocation = Path . GetDirectoryName ( testAssemblyLocation ) ! ;
235
+ // e.g. microsoft-identity-web\tests\E2E Tests\WebAppUiTests\bin\Debug\net6.0
236
+ string [ ] segments = testedAppLocation . Split ( Path . DirectorySeparatorChar ) ;
237
+ int numberSegments = segments . Length ;
238
+ int startLastSegments = numberSegments - 3 ;
239
+ int endFirstSegments = startLastSegments - 2 ;
240
+ return Path . Combine (
241
+ Path . Combine ( segments . Take ( endFirstSegments ) . ToArray ( ) ) ,
242
+ appLocation
243
+ ) ;
244
+ }
245
+
226
246
/// <summary>
227
247
/// Creates absolute path for Playwright trace file
228
248
/// </summary>
@@ -428,6 +448,70 @@ public static string GetRunningProcessAsString(Dictionary<string, Process>? proc
428
448
}
429
449
return runningProcesses . ToString ( ) ;
430
450
}
451
+
452
+ /// <summary>
453
+ /// Takes two paths to existing files and swaps their names and locations effectively swapping the contents of the files.
454
+ /// </summary>
455
+ /// <param name="path1">The path of the first file to swap</param>
456
+ /// <param name="path2">The path of the file to swap it with</param>
457
+ public static void SwapFiles ( string path1 , string path2 )
458
+ {
459
+ string tempFile = Path . GetTempFileName ( ) ;
460
+ string file1Name = Path . GetFileName ( path1 ) ;
461
+ string file2Name = Path . GetFileName ( path2 ) ;
462
+ string file1Dir = Path . GetDirectoryName ( path1 ) ;
463
+ string file2Dir = Path . GetDirectoryName ( path2 ) ;
464
+
465
+ // Move file1 to tempFile
466
+ File . Move ( path1 , tempFile ) ;
467
+
468
+ // Move file2 to file1's original location and rename it to file1's name
469
+ File . Move ( path2 , Path . Combine ( file1Dir , file1Name ) ) ;
470
+
471
+ // Move tempFile (original file1) to file2's original location and rename it to file2's name
472
+ File . Move ( tempFile , Path . Combine ( file2Dir , file2Name ) ) ;
473
+
474
+ Console . WriteLine ( "Files swapped and renamed successfully." ) ;
475
+ }
476
+
477
+
478
+ public static void RebuildSolution ( string solutionPath )
479
+ {
480
+ ProcessStartInfo startInfo = new ProcessStartInfo
481
+ {
482
+ FileName = "dotnet" ,
483
+ Arguments = $ "build { solutionPath } --no-incremental",
484
+ RedirectStandardOutput = true ,
485
+ RedirectStandardError = true ,
486
+ UseShellExecute = false ,
487
+ CreateNoWindow = true
488
+ } ;
489
+
490
+ using ( Process process = new Process ( ) )
491
+ {
492
+ process . StartInfo = startInfo ;
493
+ process . OutputDataReceived += ( sender , e ) => Console . WriteLine ( e . Data ) ;
494
+ process . ErrorDataReceived += ( sender , e ) => Console . WriteLine ( e . Data ) ;
495
+
496
+ process . Start ( ) ;
497
+ process . BeginOutputReadLine ( ) ;
498
+ process . BeginErrorReadLine ( ) ;
499
+ process . WaitForExit ( ) ;
500
+ }
501
+
502
+ Console . WriteLine ( "Solution rebuild initiated." ) ;
503
+ }
504
+
505
+ public static void BuildSampleWithTestAppsettings ( string testAssemblyLocation , string appLocation , string testAppsettingsName )
506
+ {
507
+ string appsettingsDirectory = GetAppsettingsDirectory ( testAssemblyLocation , appLocation ) ;
508
+ string appsettingsPath = Path . Combine ( appsettingsDirectory , TestConstants . AppSetttingsDotJson ) ;
509
+ string testAppsettingsPath = Path . Combine ( appsettingsDirectory , testAppsettingsName ) ;
510
+ SwapFiles ( appsettingsPath , testAppsettingsPath ) ;
511
+ RebuildSolution ( appsettingsDirectory ) ;
512
+ SwapFiles ( appsettingsPath , testAppsettingsPath ) ;
513
+
514
+ }
431
515
}
432
516
433
517
/// <summary>
0 commit comments