|
1 | 1 | using NUnit.Framework;
|
2 | 2 | using System;
|
3 | 3 | using System.IO;
|
| 4 | +using System.Text.RegularExpressions; |
4 | 5 |
|
5 | 6 | namespace OpenQA.Selenium.Internal.Logging
|
6 | 7 | {
|
@@ -34,5 +35,95 @@ public void ShouldHandleLogEvent()
|
34 | 35 | File.Delete(tempFile);
|
35 | 36 | }
|
36 | 37 | }
|
| 38 | + |
| 39 | + [Test] |
| 40 | + public void ShouldCreateFileIfDoesNotExist() |
| 41 | + { |
| 42 | + var tempFile = Path.GetTempFileName(); |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + using (var fileLogHandler = new FileLogHandler(tempFile)) |
| 47 | + { |
| 48 | + fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message")); |
| 49 | + } |
| 50 | + |
| 51 | + using (var fileLogHandler2 = new FileLogHandler(tempFile)) |
| 52 | + { |
| 53 | + fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message")); |
| 54 | + } |
| 55 | + |
| 56 | + Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1)); |
| 57 | + } |
| 58 | + finally |
| 59 | + { |
| 60 | + File.Delete(tempFile); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void ShouldAppendFileIfExists() |
| 66 | + { |
| 67 | + var tempFilePath = Path.GetTempPath() + "somefile.log"; |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + using (var fileLogHandler = new FileLogHandler(tempFilePath)) |
| 72 | + { |
| 73 | + fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message")); |
| 74 | + } |
| 75 | + |
| 76 | + using (var fileLogHandler2 = new FileLogHandler(tempFilePath, overwrite: false)) |
| 77 | + { |
| 78 | + fileLogHandler2.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message")); |
| 79 | + } |
| 80 | + |
| 81 | + Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(2)); |
| 82 | + } |
| 83 | + finally |
| 84 | + { |
| 85 | + File.Delete(tempFilePath); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + [Test] |
| 90 | + public void ShouldOverwriteFileIfExists() |
| 91 | + { |
| 92 | + var tempFile = Path.GetTempFileName(); |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + using (var fileLogHandler = new FileLogHandler(tempFile, overwrite: true)) |
| 97 | + { |
| 98 | + fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message")); |
| 99 | + } |
| 100 | + |
| 101 | + Assert.That(Regex.Matches(File.ReadAllText(tempFile), "test message").Count, Is.EqualTo(1)); |
| 102 | + } |
| 103 | + finally |
| 104 | + { |
| 105 | + File.Delete(tempFile); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void ShouldAppendFileIfDoesNotExist() |
| 111 | + { |
| 112 | + var tempFilePath = Path.GetTempPath() + "somefile.log"; |
| 113 | + |
| 114 | + try |
| 115 | + { |
| 116 | + using (var fileLogHandler = new FileLogHandler(tempFilePath, overwrite: true)) |
| 117 | + { |
| 118 | + fileLogHandler.Handle(new LogEvent(typeof(FileLogHandlerTest), DateTimeOffset.Now, LogEventLevel.Info, "test message")); |
| 119 | + } |
| 120 | + |
| 121 | + Assert.That(Regex.Matches(File.ReadAllText(tempFilePath), "test message").Count, Is.EqualTo(1)); |
| 122 | + } |
| 123 | + finally |
| 124 | + { |
| 125 | + File.Delete(tempFilePath); |
| 126 | + } |
| 127 | + } |
37 | 128 | }
|
38 | 129 | }
|
0 commit comments