|
| 1 | +namespace BlueDotBrigade.Weevil.Integration |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.IO; |
| 5 | + using System.IO.Compression; |
| 6 | + using BlueDotBrigade.Weevil; |
| 7 | + using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | + |
| 9 | + /// <summary> |
| 10 | + /// Integration test to verify that opening a log from a zip file and saving metadata |
| 11 | + /// does not throw an exception when the temporary directory is deleted. |
| 12 | + /// </summary> |
| 13 | + [TestClass] |
| 14 | + public class CompressedFileIntegrationTests |
| 15 | + { |
| 16 | + [TestMethod] |
| 17 | + public void OpenLogFromZip_SaveMetadata_SkipsSaveInTempDirectory() |
| 18 | + { |
| 19 | + // Arrange - Create a test log file in a zip |
| 20 | + var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 21 | + Directory.CreateDirectory(tempDir); |
| 22 | + |
| 23 | + var logDir = Path.Combine(tempDir, "logs"); |
| 24 | + Directory.CreateDirectory(logDir); |
| 25 | + |
| 26 | + var logFilePath = Path.Combine(logDir, "test.log"); |
| 27 | + System.IO.File.WriteAllLines(logFilePath, new[] |
| 28 | + { |
| 29 | + "2024-01-01 10:00:00 [INFO] Test log entry 1", |
| 30 | + "2024-01-01 10:00:01 [INFO] Test log entry 2", |
| 31 | + "2024-01-01 10:00:02 [ERROR] Test error entry 3", |
| 32 | + "2024-01-01 10:00:03 [INFO] Test log entry 4", |
| 33 | + "2024-01-01 10:00:04 [INFO] Test log entry 5" |
| 34 | + }); |
| 35 | + |
| 36 | + var zipFilePath = Path.Combine(tempDir, "test.zip"); |
| 37 | + ZipFile.CreateFromDirectory(logDir, zipFilePath, CompressionLevel.Optimal, false); |
| 38 | + |
| 39 | + // Simulate extracting to a temp directory (like Weevil does) |
| 40 | + var extractDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 41 | + ZipFile.ExtractToDirectory(zipFilePath, extractDir); |
| 42 | + var extractedLogPath = Path.Combine(extractDir, "test.log"); |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + // Act - Open the log file and add a comment |
| 47 | + var engine = Engine |
| 48 | + .UsingPath(extractedLogPath) |
| 49 | + .Open(); |
| 50 | + |
| 51 | + // Add a comment to the first record |
| 52 | + engine.Records[0].Metadata.Comment = "Test comment added"; |
| 53 | + |
| 54 | + // Try to save - this should NOT throw an exception |
| 55 | + // and should skip the save since it's in a temp directory |
| 56 | + engine.Save(); |
| 57 | + |
| 58 | + // Assert - no sidecar should be created in temp directory |
| 59 | + var sidecarPath = $"{extractedLogPath}.xml"; |
| 60 | + Assert.IsFalse(System.IO.File.Exists(sidecarPath), "Sidecar should not be created in temp directory"); |
| 61 | + } |
| 62 | + finally |
| 63 | + { |
| 64 | + // Cleanup |
| 65 | + try |
| 66 | + { |
| 67 | + if (Directory.Exists(tempDir)) |
| 68 | + { |
| 69 | + Directory.Delete(tempDir, true); |
| 70 | + } |
| 71 | + if (Directory.Exists(extractDir)) |
| 72 | + { |
| 73 | + Directory.Delete(extractDir, true); |
| 74 | + } |
| 75 | + } |
| 76 | + catch |
| 77 | + { |
| 78 | + // Ignore cleanup errors |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public void OpenLogFromNonTempDirectory_SuccessfulSave_CreatesMetadata() |
| 85 | + { |
| 86 | + // Arrange - Create a test log file in current directory (not temp) |
| 87 | + var currentDir = Directory.GetCurrentDirectory(); |
| 88 | + var testDir = Path.Combine(currentDir, $"test_{Guid.NewGuid()}"); |
| 89 | + Directory.CreateDirectory(testDir); |
| 90 | + |
| 91 | + var logFilePath = Path.Combine(testDir, "test.log"); |
| 92 | + System.IO.File.WriteAllLines(logFilePath, new[] |
| 93 | + { |
| 94 | + "2024-01-01 10:00:00 [INFO] Test log entry 1", |
| 95 | + "2024-01-01 10:00:01 [INFO] Test log entry 2", |
| 96 | + "2024-01-01 10:00:02 [ERROR] Test error entry 3" |
| 97 | + }); |
| 98 | + |
| 99 | + try |
| 100 | + { |
| 101 | + // Act - Open the log file and add a comment |
| 102 | + var engine = Engine |
| 103 | + .UsingPath(logFilePath) |
| 104 | + .Open(); |
| 105 | + |
| 106 | + // Add a comment to the first record |
| 107 | + engine.Records[0].Metadata.Comment = "Test comment"; |
| 108 | + |
| 109 | + // Save - this should succeed since it's not in temp directory |
| 110 | + engine.Save(); |
| 111 | + |
| 112 | + // Assert |
| 113 | + var sidecarPath = $"{logFilePath}.xml"; |
| 114 | + Assert.IsTrue(System.IO.File.Exists(sidecarPath), "Sidecar file should be created in non-temp directory"); |
| 115 | + |
| 116 | + // Verify the comment was saved |
| 117 | + var engine2 = Engine |
| 118 | + .UsingPath(logFilePath) |
| 119 | + .Open(); |
| 120 | + |
| 121 | + Assert.AreEqual("Test comment", engine2.Records[0].Metadata.Comment, "Comment should be loaded from sidecar"); |
| 122 | + } |
| 123 | + finally |
| 124 | + { |
| 125 | + // Cleanup |
| 126 | + try |
| 127 | + { |
| 128 | + if (Directory.Exists(testDir)) |
| 129 | + { |
| 130 | + Directory.Delete(testDir, true); |
| 131 | + } |
| 132 | + } |
| 133 | + catch |
| 134 | + { |
| 135 | + // Ignore cleanup errors |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments