0.2.0-dev Download ZIP
71 lines ·2.2 KB Raw
| 1 | namespace EvilEngine.Host.Tests; |
| 2 | |
| 3 | public sealed class ReferenceCheckTests : IDisposable |
| 4 | { |
| 5 | private readonly string _folder = Path.Combine(Path.GetTempPath(), $"evil-references-{Guid.NewGuid():N}"); |
| 6 | |
| 7 | public void Dispose() |
| 8 | { |
| 9 | if (Directory.Exists(_folder)) |
| 10 | Directory.Delete(_folder, true); |
| 11 | } |
| 12 | |
| 13 | private static int Handles(string path) |
| 14 | { |
| 15 | var count = 0; |
| 16 | foreach (var fd in Directory.EnumerateFileSystemEntries("/proc/self/fd")) |
| 17 | try |
| 18 | { |
| 19 | if (File.ResolveLinkTarget(fd, false)?.FullName == path) |
| 20 | count++; |
| 21 | } |
| 22 | catch (IOException) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | return count; |
| 27 | } |
| 28 | |
| 29 | [Fact] |
| 30 | public void ReadsReferencesWithoutLoading() |
| 31 | { |
| 32 | var dll = typeof(EngineHost).Assembly.Location; |
| 33 | |
| 34 | Assert.Equal("EvilEngine.Host", ReferenceCheck.AssemblyName(dll)); |
| 35 | Assert.Contains("EvilEngine.Mounts", ReferenceCheck.References(dll)); |
| 36 | } |
| 37 | |
| 38 | [Fact] |
| 39 | public void NamesEachRefusedReference() |
| 40 | { |
| 41 | var folder = Path.GetDirectoryName(typeof(EngineHost).Assembly.Location)!; |
| 42 | |
| 43 | var refusals = ReferenceCheck.Refusals(folder, reference => reference == "Tomlyn" ? "for the test." : null); |
| 44 | |
| 45 | Assert.Contains("EvilEngine.Host.dll references Tomlyn, for the test.", refusals); |
| 46 | } |
| 47 | |
| 48 | [Fact] |
| 49 | public void LeavesNoDllOpenThatIsntAnAssembly() |
| 50 | { |
| 51 | Assert.SkipUnless(Directory.Exists("/proc/self/fd"), "Open files are counted through Linux's /proc/self/fd."); |
| 52 | Directory.CreateDirectory(_folder); |
| 53 | var junk = Path.GetFullPath(Path.Combine(_folder, "junk.dll")); |
| 54 | File.WriteAllText(junk, string.Concat(Enumerable.Repeat("not a portable executable ", 40))); |
| 55 | |
| 56 | var refusals = ReferenceCheck.Refusals(_folder, _ => "for the test."); |
| 57 | |
| 58 | Assert.Empty(refusals); |
| 59 | Assert.Equal(0, Handles(junk)); |
| 60 | } |
| 61 | |
| 62 | [Theory] |
| 63 | [InlineData("GodotSharp", true)] |
| 64 | [InlineData("GodotSharpEditor", true)] |
| 65 | [InlineData("Godot.SourceGenerators", true)] |
| 66 | [InlineData("EvilEngine.Mounts", false)] |
| 67 | public void KnowsGodotAssemblies(string name, bool expected) |
| 68 | { |
| 69 | Assert.Equal(expected, ReferenceCheck.IsGodot(name)); |
| 70 | } |
| 71 | } |