@@ -1674,7 +1674,8 @@ describe('e2e', function () {
16741674 } ) ;
16751675 } ) ;
16761676
1677- /** Helper to visualize the existence of files, 1 if it exists, 0 otherwise. */
1677+ /** Helper to visualize and compare the existence of files in a specific order.
1678+ * Returns a string comprised of: 1 if a given file exists, 0 otherwise. */
16781679 const existingFiles = async ( paths : string [ ] ) => {
16791680 return (
16801681 await Promise . all (
@@ -1723,22 +1724,56 @@ describe('e2e', function () {
17231724 ) ;
17241725
17251726 expect ( await existingFiles ( paths ) ) . equals ( '1111111111' ) ;
1727+ } ) ;
1728+ } ) ;
1729+
1730+ describe ( 'with custom log retention max file count' , function ( ) {
1731+ const customLogDir = useTmpdir ( ) ;
17261732
1733+ it ( 'should delete files once it is above the max file limit' , async function ( ) {
1734+ const globalConfig = path . join ( homedir , 'globalconfig.conf' ) ;
1735+ await fs . writeFile (
1736+ globalConfig ,
1737+ `mongosh:\n logLocation: "${ customLogDir . path } "\n logMaxFileCount: 4`
1738+ ) ;
1739+ const paths : string [ ] = [ ] ;
1740+ const offset = Math . floor ( Date . now ( ) / 1000 ) ;
1741+
1742+ // Create 10 log files
1743+ for ( let i = 9 ; i >= 0 ; i -- ) {
1744+ const filename = path . join (
1745+ customLogDir . path ,
1746+ ObjectId . createFromTime ( offset - i ) . toHexString ( ) + '_log'
1747+ ) ;
1748+ await fs . writeFile ( filename , '' ) ;
1749+ paths . push ( filename ) ;
1750+ }
1751+
1752+ // All 10 existing log files exist.
1753+ expect ( await existingFiles ( paths ) ) . to . equal ( '1111111111' ) ;
17271754 shell = this . startTestShell ( {
17281755 args : [ '--nodb' ] ,
17291756 env : {
17301757 ...env ,
1758+ MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT : '' ,
17311759 MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING : globalConfig ,
17321760 } ,
17331761 forceTerminal : true ,
17341762 } ) ;
17351763
17361764 await shell . waitForPrompt ( ) ;
17371765
1738- // Add the newly created log file
1739- paths . push ( path . join ( customLogDir . path , `${ shell . logId } _log` ) ) ;
1740- // Expect 6 files to be deleted and 5 to remain (including the new log file)
1741- expect ( await existingFiles ( paths ) ) . equals ( '00000011111' ) ;
1766+ // Add the newly created log to the file list.
1767+ paths . push (
1768+ path . join ( customLogDir . path , `${ shell . logId as string } _log` )
1769+ ) ;
1770+
1771+ expect (
1772+ await shell . executeLine ( 'config.get("logMaxFileCount")' )
1773+ ) . contains ( '4' ) ;
1774+
1775+ // Expect 7 files to be deleted and 4 to remain (including the new log file)
1776+ expect ( await existingFiles ( paths ) ) . to . equal ( '00000001111' ) ;
17421777 } ) ;
17431778 } ) ;
17441779
0 commit comments