Skip to content

Commit 8bffcc4

Browse files
Update HtmlValidator to delete temporary files safely
1 parent 1e38704 commit 8bffcc4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Atata.HtmlValidation/HtmlValidator.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ public HtmlValidationResult Validate(string html)
120120
finally
121121
{
122122
if (File.Exists(tempOutputFilePath))
123-
File.Delete(tempOutputFilePath);
123+
DeleteTempFileSafely(tempOutputFilePath);
124124
}
125125

126126
if (!ShouldSaveHtmlFile(result.IsSuccessful, _options.SaveHtmlToFile))
127127
{
128-
File.Delete(htmlFilePath);
128+
DeleteTempFileSafely(htmlFilePath);
129129
htmlFilePath = null;
130130
}
131131

@@ -215,6 +215,18 @@ private void WriteToFile(string path, string contents)
215215
File.WriteAllText(path, contents, _options.Encoding);
216216
}
217217

218+
private void DeleteTempFileSafely(string filePath)
219+
{
220+
try
221+
{
222+
File.Delete(filePath);
223+
}
224+
catch (Exception exception)
225+
{
226+
_atataContext?.Log.Warn(exception, $"Failed to delete temporary file \"{Path.GetFileName(filePath)}\".");
227+
}
228+
}
229+
218230
private HtmlValidateResult ExecuteCliCommand(string workingDirectory, string htmlFileName, string formatter, string outputFilePath)
219231
{
220232
var cliOptions = CreateCliOptions(formatter, outputFilePath);

0 commit comments

Comments
 (0)