diff --git a/src/ApiBuilderTests/SanityChecks.cs b/src/ApiBuilderTests/SanityChecks.cs deleted file mode 100644 index 27a96695..00000000 --- a/src/ApiBuilderTests/SanityChecks.cs +++ /dev/null @@ -1,125 +0,0 @@ -#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(); - 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[] - { - "// ", - "#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(); - // foreach (var type in typeof(SanityChecks).Assembly.GetTypes()) - // { - // if (type.Namespace != null && !type.Namespace.StartsWith("System")) - // { - // continue; - // } - // - // if (type.IsNested) - // { - // continue; - // } - // - // if (HasAttribute(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(type)) - // { - // errors.Add($"{name} must have ExcludeFromCodeCoverageAttribute"); - // } - // - // if (!HasAttribute(type)) - // { - // errors.Add($"{name} must have DebuggerNonUserCodeAttribute"); - // } - // } - // } - // - // if (errors.Count > 0) - // { - // throw new(string.Join("\n", errors)); - // } - // } - - static bool HasAttribute(Type type) - { - try - { - return type.GetCustomAttribute(typeof(T)) != null; - } - catch (Exception e) - { - throw new($"Failed to get {typeof(T).Name} from {type.Name}", e); - } - } -} \ No newline at end of file diff --git a/src/Tests/SanityChecks.cs b/src/Tests/SanityChecks.cs new file mode 100644 index 00000000..874ce239 --- /dev/null +++ b/src/Tests/SanityChecks.cs @@ -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(); + 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[] + { + "// ", + "#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(); + foreach (var type in typeof(SanityChecks).Assembly.GetTypes()) + { + if (type.Namespace != null && !type.Namespace.StartsWith("System")) + { + continue; + } + + if (type.IsNested) + { + continue; + } + + if (HasAttribute(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(type)) + { + errors.Add($"{name} must have ExcludeFromCodeCoverageAttribute"); + } + + if (!HasAttribute(type)) + { + errors.Add($"{name} must have DebuggerNonUserCodeAttribute"); + } + } + } + + if (errors.Count > 0) + { + throw new(string.Join("\n", errors)); + } + } + + static bool HasAttribute(Type type) + { + try + { + return type.GetCustomAttribute(typeof(T)) != null; + } + catch (Exception e) + { + throw new($"Failed to get {typeof(T).Name} from {type.Name}", e); + } + } +} \ No newline at end of file