@@ -1674,8 +1674,9 @@ describe('e2e', function () {
16741674 } ) ;
16751675 } ) ;
16761676
1677- /** Helper to visualize the existence of files, 1 if it exists, 0 otherwise. */
1678- const existingFiles = async ( paths : string [ ] ) => {
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. */
1679+ const getFilesState = async ( paths : string [ ] ) => {
16791680 return (
16801681 await Promise . all (
16811682 paths . map ( ( path ) =>
@@ -1722,7 +1723,7 @@ describe('e2e', function () {
17221723 `mongosh:\n logLocation: "${ customLogDir . path } "\n logRetentionDays: ${ retentionDays } `
17231724 ) ;
17241725
1725- expect ( await existingFiles ( paths ) ) . equals ( '1111111111' ) ;
1726+ expect ( await getFilesState ( paths ) ) . equals ( '1111111111' ) ;
17261727
17271728 shell = this . startTestShell ( {
17281729 args : [ '--nodb' ] ,
@@ -1738,7 +1739,57 @@ describe('e2e', function () {
17381739 // Add the newly created log file
17391740 paths . push ( path . join ( customLogDir . path , `${ shell . logId } _log` ) ) ;
17401741 // Expect 6 files to be deleted and 5 to remain (including the new log file)
1741- expect ( await existingFiles ( paths ) ) . equals ( '00000011111' ) ;
1742+ expect ( await getFilesState ( paths ) ) . equals ( '00000011111' ) ;
1743+ } ) ;
1744+ } ) ;
1745+
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 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' ) ;
17421793 } ) ;
17431794 } ) ;
17441795
0 commit comments