Skip to content

Commit

Permalink
move SanityChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Oct 24, 2024
1 parent 483ca82 commit 236b331
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 125 deletions.
125 changes: 0 additions & 125 deletions src/ApiBuilderTests/SanityChecks.cs

This file was deleted.

125 changes: 125 additions & 0 deletions src/Tests/SanityChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#pragma warning disable IL2026

[TestFixture]
public class SanityChecks
{
[Test]
public void NoPublicTypes()
{
var visibleTypes = typeof(SanityChecks).Assembly
.GetExportedTypes()
.Where(type => type.Namespace?.StartsWith("System") == true);
#if PolyPublic
#if !NET7_0_OR_GREATER
Assert.That(visibleTypes, Is.Not.Empty);
#endif
#else
Assert.That(visibleTypes, Is.Empty);
#endif
}
#if DEBUG
[Test]
public void CodeChecks()
{
var dir = Path.Combine(SolutionDirectoryFinder.Find(), "Polyfill");
var errors = new List<string>();
foreach (var file in Directory.EnumerateFiles(dir, "*.cs", SearchOption.AllDirectories))
{
var directoryName = Path.GetDirectoryName(file)!;
if (directoryName.Contains("bin") || directoryName.Contains("obj"))
{
continue;
}

var content = File.ReadAllText(file);
var requiredText = new[]
{
"// <auto-generated />",
"#pragma warning disable"
};
foreach (var required in requiredText)
{
if (!content.Contains(required))
{
errors.Add($"{file} must contain '{required}'");
}
}
}

if (errors.Count > 0)
{
throw new(string.Join("\n", errors));
}
}
#endif
[Test]
public void ReflectionChecks()
{
var errors = new List<string>();
foreach (var type in typeof(SanityChecks).Assembly.GetTypes())
{
if (type.Namespace != null && !type.Namespace.StartsWith("System"))
{
continue;
}

if (type.IsNested)
{
continue;
}

if (HasAttribute<TestFixtureAttribute>(type))
{
continue;
}

var name = type.Name;
if (name.EndsWith("Usage") ||
name.Contains("<") ||
name.Contains("Sample") ||
name == "SolutionDirectoryFinder" ||
name == "AutoGeneratedProgram" ||
name == "IsReadOnlyAttribute" ||
name == "IsByRefLikeAttribute" ||
name == "NullableAttribute" ||
name == "NullableContextAttribute" ||
name == "ScopedRefAttribute" ||
name == "RefSafetyRulesAttribute" ||
name == "ParamCollectionAttribute" ||
name == "FileUtil")
{
continue;
}

if (type is {IsInterface: false, IsEnum: false})
{
if (!HasAttribute<ExcludeFromCodeCoverageAttribute>(type))
{
errors.Add($"{name} must have ExcludeFromCodeCoverageAttribute");
}

if (!HasAttribute<DebuggerNonUserCodeAttribute>(type))
{
errors.Add($"{name} must have DebuggerNonUserCodeAttribute");
}
}
}

if (errors.Count > 0)
{
throw new(string.Join("\n", errors));
}
}

static bool HasAttribute<T>(Type type)
{
try
{
return type.GetCustomAttribute(typeof(T)) != null;
}
catch (Exception e)
{
throw new($"Failed to get {typeof(T).Name} from {type.Name}", e);
}
}
}

0 comments on commit 236b331

Please sign in to comment.