@@ -1388,22 +1388,19 @@ public async Task InitializeAsync_WhenInitializationFails_DisposesSession()
13881388 }
13891389
13901390 /// <summary>
1391- /// Verifies that disposing the session compresses the CDB log file and deletes the original log.
1391+ /// Verifies that disposing the session requests GZip compression of the CDB log file and deletes the original log via the file system abstraction .
13921392 /// </summary>
13931393 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
13941394 [ Fact ]
13951395 public async Task DisposeAsync_CompressesCdbLog_AndDeletesOriginalLog ( )
13961396 {
13971397 // Arrange
13981398 var originalConfig = LogManager . Configuration ;
1399- var tempRoot = Path . Combine ( Path . GetTempPath ( ) , "MCPNexusCdbCompressionTests" , Guid . NewGuid ( ) . ToString ( "N" ) ) ;
1400- var logsDirectory = Path . Combine ( tempRoot , "Logs" ) ;
1401- _ = Directory . CreateDirectory ( logsDirectory ) ;
14021399
14031400 try
14041401 {
14051402 var config = new LoggingConfiguration ( ) ;
1406- var mainLogPath = Path . Combine ( logsDirectory , " main.log") ;
1403+ const string mainLogPath = "C: \\ logs \\ main.log";
14071404 var fileTarget = new FileTarget ( "mainFile" )
14081405 {
14091406 FileName = mainLogPath ,
@@ -1412,42 +1409,39 @@ public async Task DisposeAsync_CompressesCdbLog_AndDeletesOriginalLog()
14121409 config . AddRuleForAllLevels ( fileTarget ) ;
14131410 LogManager . Configuration = config ;
14141411
1415- var accessor = new CdbSessionTestAccessor ( m_Settings . Object , m_MockFileSystem . Object , m_MockProcessManager . Object ) ;
1412+ var fileSystem = new Mock < IFileSystem > ( ) ;
1413+ var processManager = m_MockProcessManager ;
1414+
1415+ var accessor = new CdbSessionTestAccessor ( m_Settings . Object , fileSystem . Object , processManager . Object ) ;
14161416
14171417 var sessionId = "session-compress" ;
14181418 var sessionIdProperty = typeof ( CdbSession ) . GetProperty ( "SessionId" , System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Public ) ;
14191419 sessionIdProperty ! . SetValue ( accessor , sessionId ) ;
14201420
14211421 var getLogPathMethod = typeof ( CdbSession ) . GetMethod ( "GetCdbSessionBasedLogPath" , System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . NonPublic ) ;
14221422 var logPath = ( string ) getLogPathMethod ! . Invoke ( accessor , new object [ ] { sessionId } ) ! ;
1423+ var compressedPath = $ "{ logPath } .gz";
14231424
1424- var logDirectory = Path . GetDirectoryName ( logPath ) ;
1425- if ( ! string . IsNullOrEmpty ( logDirectory ) )
1426- {
1427- _ = Directory . CreateDirectory ( logDirectory ) ;
1428- }
1425+ _ = fileSystem . Setup ( fs => fs . FileExists ( logPath ) )
1426+ . Returns ( true ) ;
1427+ _ = fileSystem . Setup ( fs => fs . FileExists ( compressedPath ) )
1428+ . Returns ( false ) ;
1429+
1430+ _ = fileSystem . Setup ( fs => fs . CompressToGZipAsync ( logPath , compressedPath , It . IsAny < CancellationToken > ( ) ) )
1431+ . Returns ( Task . CompletedTask ) ;
14291432
1430- const string logContent = "cdb log content" ;
1431- File . WriteAllText ( logPath , logContent ) ;
1433+ _ = fileSystem . Setup ( fs => fs . DeleteFile ( logPath ) ) ;
14321434
14331435 // Act
14341436 await accessor . DisposeAsync ( ) ;
14351437
14361438 // Assert
1437- var compressedPath = $ "{ logPath } .gz";
1438- _ = File . Exists ( logPath ) . Should ( ) . BeFalse ( ) ;
1439- _ = File . Exists ( compressedPath ) . Should ( ) . BeTrue ( ) ;
1440- var compressedFileInfo = new FileInfo ( compressedPath ) ;
1441- _ = compressedFileInfo . Length . Should ( ) . BeGreaterThan ( 0 ) ;
1439+ fileSystem . Verify ( fs => fs . CompressToGZipAsync ( logPath , compressedPath , It . IsAny < CancellationToken > ( ) ) , Times . Once ) ;
1440+ fileSystem . Verify ( fs => fs . DeleteFile ( logPath ) , Times . Once ) ;
14421441 }
14431442 finally
14441443 {
14451444 LogManager . Configuration = originalConfig ;
1446-
1447- if ( Directory . Exists ( tempRoot ) )
1448- {
1449- Directory . Delete ( tempRoot , true ) ;
1450- }
14511445 }
14521446 }
14531447}
0 commit comments