@@ -1743,6 +1743,56 @@ describe('e2e', function () {
17431743 } ) ;
17441744 } ) ;
17451745
1746+ describe ( 'with custom log retention max file count' , function ( ) {
1747+ const customLogDir = useTmpdir ( ) ;
1748+
1749+ it ( 'should delete files once it is above the max file count limit' , async function ( ) {
1750+ const globalConfig = path . join ( homedir , 'globalconfig.conf' ) ;
1751+ await fs . writeFile (
1752+ globalConfig ,
1753+ `mongosh:\n logLocation: "${ customLogDir . path } "\n logMaxFileCount: 4`
1754+ ) ;
1755+ const paths : string [ ] = [ ] ;
1756+ const offset = Math . floor ( Date . now ( ) / 1000 ) ;
1757+
1758+ // Create 10 log files
1759+ for ( let i = 9 ; i >= 0 ; i -- ) {
1760+ const filename = path . join (
1761+ customLogDir . path ,
1762+ ObjectId . createFromTime ( offset - i ) . toHexString ( ) + '_log'
1763+ ) ;
1764+ await fs . writeFile ( filename , '' ) ;
1765+ paths . push ( filename ) ;
1766+ }
1767+
1768+ // All 10 existing log files exist.
1769+ expect ( await getFilesState ( paths ) ) . to . equal ( '1111111111' ) ;
1770+ shell = this . startTestShell ( {
1771+ args : [ '--nodb' ] ,
1772+ env : {
1773+ ...env ,
1774+ MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT : '' ,
1775+ MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING : globalConfig ,
1776+ } ,
1777+ forceTerminal : true ,
1778+ } ) ;
1779+
1780+ await shell . waitForPrompt ( ) ;
1781+
1782+ // Add the newly created log to the file list.
1783+ paths . push (
1784+ path . join ( customLogDir . path , `${ shell . logId as string } _log` )
1785+ ) ;
1786+
1787+ expect (
1788+ await shell . executeLine ( 'config.get("logMaxFileCount")' )
1789+ ) . contains ( '4' ) ;
1790+
1791+ // Expect 7 files to be deleted and 4 to remain (including the new log file)
1792+ expect ( await getFilesState ( paths ) ) . to . equal ( '00000001111' ) ;
1793+ } ) ;
1794+ } ) ;
1795+
17461796 it ( 'creates a log file that keeps track of session events' , async function ( ) {
17471797 expect ( await shell . executeLine ( 'print(123 + 456)' ) ) . to . include ( '579' ) ;
17481798 const log = await readLogFile ( ) ;
0 commit comments