@@ -1407,7 +1407,7 @@ describe('e2e', function () {
14071407 }
14081408 const logPath = path . join (
14091409 customBasePath ?? logBasePath ,
1410- `${ shell . logId } _log`
1410+ `${ customBasePath ? 'mongosh_' : '' } ${ shell . logId } _log`
14111411 ) ;
14121412 return readReplLogFile < T > ( logPath ) ;
14131413 } ;
@@ -1691,6 +1691,43 @@ describe('e2e', function () {
16911691 ) . join ( '' ) ;
16921692 } ;
16931693
1694+ const getLogName = (
1695+ logId : string | null ,
1696+ { isCompressed = false , prefix = 'mongosh_' } = { }
1697+ ) : string => {
1698+ if ( ! logId ) throw new Error ( 'logId is not set' ) ;
1699+ return `${ prefix } ${ logId } _log${ isCompressed ? '.gz' : '' } ` ;
1700+ } ;
1701+
1702+ /** Creates fake log files for testing. */
1703+ const createFakeLogFiles = async ( {
1704+ count,
1705+ prefix = 'mongosh_' ,
1706+ size = 0 ,
1707+ offset,
1708+ basePath,
1709+ } : {
1710+ count : number ;
1711+ offset ?: number ;
1712+ prefix ?: string ;
1713+ basePath : string | null ;
1714+ size ?: number ;
1715+ } ) : Promise < string [ ] > => {
1716+ const paths : string [ ] = [ ] ;
1717+ offset ??= Math . floor ( Date . now ( ) / 1000 ) ;
1718+ for ( let i = count - 1 ; i >= 0 ; i -- ) {
1719+ const logPath = path . join (
1720+ basePath ?? logBasePath ,
1721+ getLogName ( ObjectId . createFromTime ( offset - i ) . toHexString ( ) , {
1722+ prefix,
1723+ } )
1724+ ) ;
1725+ paths . push ( logPath ) ;
1726+ await fs . writeFile ( logPath , '0' . repeat ( size ) ) ;
1727+ }
1728+ return paths ;
1729+ } ;
1730+
16941731 describe ( 'with custom log compression' , function ( ) {
16951732 const customLogDir = useTmpdir ( ) ;
16961733
@@ -1716,11 +1753,11 @@ describe('e2e', function () {
17161753
17171754 const logFile = path . join (
17181755 customLogDir . path ,
1719- ` ${ shell . logId as string } _log`
1756+ getLogName ( shell . logId )
17201757 ) ;
17211758 const logFileGzip = path . join (
17221759 customLogDir . path ,
1723- ` ${ shell . logId as string } _log.gz`
1760+ getLogName ( shell . logId , { isCompressed : true } )
17241761 ) ;
17251762
17261763 // Only the gzipped file should exist
@@ -1741,27 +1778,27 @@ describe('e2e', function () {
17411778 const paths : string [ ] = [ ] ;
17421779 const today = Math . floor ( Date . now ( ) / 1000 ) ;
17431780 const tenDaysAgo = today - 10 * 24 * 60 * 60 ;
1744- // Create 6 files older than 7 days
1745- for ( let i = 5 ; i >= 0 ; i -- ) {
1746- const filename = path . join (
1747- customLogDir . path ,
1748- ObjectId . createFromTime ( tenDaysAgo - i ) . toHexString ( ) + '_log'
1749- ) ;
1750- await fs . writeFile ( filename , '' ) ;
1751- paths . push ( filename ) ;
1752- }
1753- // Create 4 files newer than 10 days
1754- for ( let i = 3 ; i >= 0 ; i -- ) {
1755- const filename = path . join (
1756- customLogDir . path ,
1757- ObjectId . createFromTime ( today - i ) . toHexString ( ) + '_log'
1758- ) ;
1759- await fs . writeFile ( filename , '' ) ;
1760- paths . push ( filename ) ;
1761- }
17621781
17631782 const retentionDays = 7 ;
17641783
1784+ // Create 6 files older than 7 days
1785+ paths . push (
1786+ ...( await createFakeLogFiles ( {
1787+ count : 6 ,
1788+ offset : tenDaysAgo ,
1789+ basePath : customLogDir . path ,
1790+ } ) )
1791+ ) ;
1792+
1793+ // Create 4 files newer than 7 days
1794+ paths . push (
1795+ ...( await createFakeLogFiles ( {
1796+ count : 4 ,
1797+ offset : today ,
1798+ basePath : customLogDir . path ,
1799+ } ) )
1800+ ) ;
1801+
17651802 const globalConfig = path . join ( homedir , 'globalconfig.conf' ) ;
17661803 await fs . writeFile (
17671804 globalConfig ,
@@ -1784,7 +1821,7 @@ describe('e2e', function () {
17841821 await shell . waitForPrompt ( ) ;
17851822
17861823 // Add the newly created log file
1787- paths . push ( path . join ( customLogDir . path , ` ${ shell . logId } _log` ) ) ;
1824+ paths . push ( path . join ( customLogDir . path , getLogName ( shell . logId ) ) ) ;
17881825 // Expect 6 files to be deleted and 5 to remain (including the new log file)
17891826 expect ( await getFilesState ( paths ) ) . equals ( '00000011111' ) ;
17901827 } ) ;
@@ -1793,6 +1830,56 @@ describe('e2e', function () {
17931830 describe ( 'with custom log retention max file count' , function ( ) {
17941831 const customLogDir = useTmpdir ( ) ;
17951832
1833+ it ( 'should only delete files with mongosh_ prefix in a custom location' , async function ( ) {
1834+ const globalConfig = path . join ( homedir , 'globalconfig.conf' ) ;
1835+ await fs . writeFile (
1836+ globalConfig ,
1837+ `mongosh:\n logLocation: ${ JSON . stringify (
1838+ customLogDir . path
1839+ ) } \n logMaxFileCount: 2`
1840+ ) ;
1841+
1842+ const paths : string [ ] = [ ] ;
1843+
1844+ // Create 3 log files without mongosh_ prefix
1845+ paths . push (
1846+ ...( await createFakeLogFiles ( {
1847+ count : 3 ,
1848+ prefix : '' ,
1849+ basePath : customLogDir . path ,
1850+ } ) )
1851+ ) ;
1852+
1853+ // Create 4 log files with mongosh_ prefix
1854+ paths . push (
1855+ ...( await createFakeLogFiles ( {
1856+ count : 3 ,
1857+ prefix : 'mongosh_' ,
1858+ basePath : customLogDir . path ,
1859+ } ) )
1860+ ) ;
1861+
1862+ // All 7 existing log files exist.
1863+ expect ( await getFilesState ( paths ) ) . to . equal ( '111111' ) ;
1864+
1865+ shell = this . startTestShell ( {
1866+ args : [ '--nodb' ] ,
1867+ env : {
1868+ ...env ,
1869+ MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING : globalConfig ,
1870+ } ,
1871+ forceTerminal : true ,
1872+ } ) ;
1873+
1874+ await shell . waitForPrompt ( ) ;
1875+
1876+ paths . push ( path . join ( customLogDir . path , getLogName ( shell . logId ) ) ) ;
1877+ // 3 log files without mongosh_ prefix should remain
1878+ // 2 log file with mongosh_ prefix should be deleted
1879+ // 2 log files with mongosh_ prefix should remain (including the new log)
1880+ expect ( await getFilesState ( paths ) ) . to . equal ( '1110011' ) ;
1881+ } ) ;
1882+
17961883 it ( 'should delete files once it is above the max file count limit' , async function ( ) {
17971884 const globalConfig = path . join ( homedir , 'globalconfig.conf' ) ;
17981885 await fs . writeFile (
@@ -1801,18 +1888,12 @@ describe('e2e', function () {
18011888 customLogDir . path
18021889 ) } \n logMaxFileCount: 4`
18031890 ) ;
1804- const paths : string [ ] = [ ] ;
1805- const offset = Math . floor ( Date . now ( ) / 1000 ) ;
18061891
18071892 // Create 10 log files
1808- for ( let i = 9 ; i >= 0 ; i -- ) {
1809- const filename = path . join (
1810- customLogDir . path ,
1811- ObjectId . createFromTime ( offset - i ) . toHexString ( ) + '_log'
1812- ) ;
1813- await fs . writeFile ( filename , '' ) ;
1814- paths . push ( filename ) ;
1815- }
1893+ const paths = await createFakeLogFiles ( {
1894+ count : 10 ,
1895+ basePath : customLogDir . path ,
1896+ } ) ;
18161897
18171898 // All 10 existing log files exist.
18181899 expect ( await getFilesState ( paths ) ) . to . equal ( '1111111111' ) ;
@@ -1829,9 +1910,7 @@ describe('e2e', function () {
18291910 await shell . waitForPrompt ( ) ;
18301911
18311912 // Add the newly created log to the file list.
1832- paths . push (
1833- path . join ( customLogDir . path , `${ shell . logId as string } _log` )
1834- ) ;
1913+ paths . push ( path . join ( customLogDir . path , getLogName ( shell . logId ) ) ) ;
18351914
18361915 expect (
18371916 await shell . executeLine ( 'config.get("logMaxFileCount")' )
0 commit comments