fix(tstest-logging): Improve log file handling with log rotation and diff reporting
This commit is contained in:
		| @@ -38,6 +38,11 @@ export class TsTest { | ||||
|   } | ||||
|  | ||||
|   async run() { | ||||
|     // Move previous log files if --logfile option is used | ||||
|     if (this.logger.options.logFile) { | ||||
|       await this.movePreviousLogFiles(); | ||||
|     } | ||||
|      | ||||
|     const testGroups = await this.testDir.getTestFileGroups(); | ||||
|     const allFiles = [...testGroups.serial, ...Object.values(testGroups.parallelGroups).flat()]; | ||||
|      | ||||
| @@ -167,7 +172,7 @@ export class TsTest { | ||||
|     }); | ||||
|     server.addRoute( | ||||
|       '/test', | ||||
|       new plugins.typedserver.servertools.Handler('GET', async (req, res) => { | ||||
|       new plugins.typedserver.servertools.Handler('GET', async (_req, res) => { | ||||
|         res.type('.html'); | ||||
|         res.write(` | ||||
|         <html> | ||||
| @@ -202,7 +207,7 @@ export class TsTest { | ||||
|  | ||||
|     // lets do the browser bit | ||||
|     await this.smartbrowserInstance.start(); | ||||
|     const evaluation = await this.smartbrowserInstance.evaluateOnPage( | ||||
|     await this.smartbrowserInstance.evaluateOnPage( | ||||
|       `http://localhost:3007/test?bundleName=${bundleFileName}`, | ||||
|       async () => { | ||||
|         // lets enable real time comms | ||||
| @@ -215,12 +220,12 @@ export class TsTest { | ||||
|         const originalError = console.error; | ||||
|  | ||||
|         // Override console methods to capture the logs | ||||
|         console.log = (...args) => { | ||||
|         console.log = (...args: any[]) => { | ||||
|           logStore.push(args.join(' ')); | ||||
|           ws.send(args.join(' ')); | ||||
|           originalLog(...args); | ||||
|         }; | ||||
|         console.error = (...args) => { | ||||
|         console.error = (...args: any[]) => { | ||||
|           logStore.push(args.join(' ')); | ||||
|           ws.send(args.join(' ')); | ||||
|           originalError(...args); | ||||
| @@ -271,4 +276,40 @@ export class TsTest { | ||||
|   } | ||||
|  | ||||
|   public async runInDeno() {} | ||||
|    | ||||
|   private async movePreviousLogFiles() { | ||||
|     const logDir = plugins.path.join('.nogit', 'testlogs'); | ||||
|     const previousDir = plugins.path.join('.nogit', 'testlogs', 'previous'); | ||||
|      | ||||
|     try { | ||||
|       // Get all files in log directory | ||||
|       const files = await plugins.smartfile.fs.listFileTree(logDir, '*.log'); | ||||
|       if (files.length === 0) { | ||||
|         return; | ||||
|       } | ||||
|        | ||||
|       // Ensure previous directory exists | ||||
|       await plugins.smartfile.fs.ensureDir(previousDir); | ||||
|        | ||||
|       // Move each file to previous directory | ||||
|       for (const file of files) { | ||||
|         const filename = plugins.path.basename(file); | ||||
|         const sourcePath = plugins.path.join(logDir, filename); | ||||
|         const destPath = plugins.path.join(previousDir, filename); | ||||
|          | ||||
|         try { | ||||
|           // Read file content and write to new location | ||||
|           const content = await plugins.smartfile.fs.toStringSync(sourcePath); | ||||
|           await plugins.smartfile.fs.toFs(content, destPath); | ||||
|           // Remove original file | ||||
|           await plugins.smartfile.fs.remove(sourcePath); | ||||
|         } catch (error) { | ||||
|           // Silently continue if a file can't be moved | ||||
|         } | ||||
|       } | ||||
|     } catch (error) { | ||||
|       // Directory might not exist, which is fine | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user