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