TestFileCleanupVerifier.cs
2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace UnityEditor.TestTools.TestRunner
{
[Serializable]
internal class TestFileCleanupVerifier
{
const string k_Indent = " ";
[SerializeField]
List<string> m_ExistingFiles;
[SerializeField]
bool m_ExistingFilesScanned;
public Action<object> logAction = Debug.LogWarning;
private Func<string[]> getAllAssetPathsAction;
public Func<string[]> GetAllAssetPathsAction
{
get
{
if (getAllAssetPathsAction != null)
{
return getAllAssetPathsAction;
}
return AssetDatabase.GetAllAssetPaths;
}
set
{
getAllAssetPathsAction = value;
}
}
public void RegisterExistingFiles()
{
if (m_ExistingFilesScanned)
{
return;
}
m_ExistingFiles = GetAllFilesInAssetsDirectory().ToList();
m_ExistingFilesScanned = true;
}
public void VerifyNoNewFilesAdded()
{
var currentFiles = GetAllFilesInAssetsDirectory().ToList();
//Expect that if its the same amount of files, there havent been any changes
//This is to optimize if there are many files
if (currentFiles.Count != m_ExistingFiles.Count)
{
LogWarningForFilesIfAny(currentFiles.Except(m_ExistingFiles));
}
}
void LogWarningForFilesIfAny(IEnumerable<string> filePaths)
{
if (!filePaths.Any())
{
return;
}
var stringWriter = new StringWriter();
stringWriter.WriteLine("Files generated by test without cleanup.");
stringWriter.WriteLine(k_Indent + "Found {0} new files.", filePaths.Count());
foreach (var filePath in filePaths)
{
stringWriter.WriteLine(k_Indent + filePath);
}
LogAction(stringWriter.ToString());
}
private void LogAction(object obj)
{
if (this.logAction != null)
{
this.logAction(obj);
}
else
{
Debug.LogWarning(obj);
}
}
private IEnumerable<string> GetAllFilesInAssetsDirectory()
{
return GetAllAssetPathsAction();
}
}
}