|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using Neo.Compiler; |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace Neo.Compiler.CSharp.UnitTests; |
| 9 | + |
| 10 | +[TestClass] |
| 11 | +public class UnitTest_AnalyzerExecution |
| 12 | +{ |
| 13 | + [TestMethod] |
| 14 | + public void CompileProject_ReportsNeoAnalyzerErrorsBeforeLowering() |
| 15 | + { |
| 16 | + using var project = TempContractProject.Create(""" |
| 17 | +using Neo.SmartContract.Framework; |
| 18 | +using System.Diagnostics; |
| 19 | +
|
| 20 | +public class Contract : SmartContract |
| 21 | +{ |
| 22 | + public static void Main() |
| 23 | + { |
| 24 | + Debug.WriteLine("unsupported"); |
| 25 | + } |
| 26 | +} |
| 27 | +"""); |
| 28 | + |
| 29 | + var result = CreateEngine().CompileProject(project.ProjectFile).Single(); |
| 30 | + var diagnostics = result.Diagnostics.Where(item => item.Id == "NC4028").ToArray(); |
| 31 | + |
| 32 | + Assert.IsFalse(result.Success); |
| 33 | + Assert.IsTrue(diagnostics.Length > 0); |
| 34 | + Assert.IsTrue(diagnostics.All(item => item.Severity == DiagnosticSeverity.Error)); |
| 35 | + Assert.IsTrue(diagnostics.All(item => item.Location.SourceTree?.FilePath == project.SourceFile)); |
| 36 | + Assert.IsTrue(diagnostics.Any(item => item.Location.GetLineSpan().StartLinePosition.Line + 1 == 8)); |
| 37 | + Assert.IsFalse(result.Diagnostics.Any(item => item.Id == "NC1002")); |
| 38 | + } |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void CompileProject_PreservesSupportedContracts() |
| 42 | + { |
| 43 | + using var project = TempContractProject.Create(""" |
| 44 | +using Neo.SmartContract.Framework; |
| 45 | +
|
| 46 | +public class Contract : SmartContract |
| 47 | +{ |
| 48 | + public static int Main(int value) => value + 1; |
| 49 | +} |
| 50 | +"""); |
| 51 | + |
| 52 | + var result = CreateEngine().CompileProject(project.ProjectFile).Single(); |
| 53 | + |
| 54 | + Assert.IsTrue(result.Success, string.Join(Environment.NewLine, result.Diagnostics)); |
| 55 | + Assert.IsFalse(result.Diagnostics.Any(item => item.Id.StartsWith("NC4", StringComparison.Ordinal))); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void CompileProject_ReportsAnalyzerWarningsWithoutBlockingOutput() |
| 60 | + { |
| 61 | + using var project = TempContractProject.Create(""" |
| 62 | +using Neo.SmartContract.Framework; |
| 63 | +
|
| 64 | +public class Contract : SmartContract |
| 65 | +{ |
| 66 | + public static int Main(int value) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + return value + 1; |
| 71 | + } |
| 72 | + catch (System.InvalidOperationException) |
| 73 | + { |
| 74 | + return 0; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +"""); |
| 79 | + |
| 80 | + var result = CreateEngine().CompileProject(project.ProjectFile).Single(); |
| 81 | + var diagnostic = result.Diagnostics.Single(item => item.Id == "NC4027"); |
| 82 | + |
| 83 | + Assert.IsTrue(result.Success, string.Join(Environment.NewLine, result.Diagnostics)); |
| 84 | + Assert.AreEqual(DiagnosticSeverity.Warning, diagnostic.Severity); |
| 85 | + } |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + public void CompileProject_DoesNotChangeLibraryBehaviorUnlessEnabled() |
| 89 | + { |
| 90 | + using var project = TempContractProject.Create(""" |
| 91 | +using Neo.SmartContract.Framework; |
| 92 | +using System.Diagnostics; |
| 93 | +
|
| 94 | +public class Contract : SmartContract |
| 95 | +{ |
| 96 | + public static void Main() |
| 97 | + { |
| 98 | + Debug.WriteLine("unsupported"); |
| 99 | + } |
| 100 | +} |
| 101 | +"""); |
| 102 | + |
| 103 | + var result = new CompilationEngine(new CompilationOptions |
| 104 | + { |
| 105 | + SkipRestoreIfAssetsPresent = true |
| 106 | + }).CompileProject(project.ProjectFile).Single(); |
| 107 | + |
| 108 | + Assert.IsFalse(result.Success); |
| 109 | + Assert.IsFalse(result.Diagnostics.Any(item => item.Id == "NC4028")); |
| 110 | + Assert.IsTrue(result.Diagnostics.Any(item => item.Id == "NC1002")); |
| 111 | + } |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + public void ProgramMain_RunsNeoAnalyzersByDefault() |
| 115 | + { |
| 116 | + using var project = TempContractProject.Create(""" |
| 117 | +using Neo.SmartContract.Framework; |
| 118 | +using System.Diagnostics; |
| 119 | +
|
| 120 | +public class Contract : SmartContract |
| 121 | +{ |
| 122 | + public static void Main() |
| 123 | + { |
| 124 | + Debug.WriteLine("unsupported"); |
| 125 | + } |
| 126 | +} |
| 127 | +"""); |
| 128 | + using var output = new StringWriter(); |
| 129 | + using var error = new StringWriter(); |
| 130 | + var originalOut = Console.Out; |
| 131 | + var originalError = Console.Error; |
| 132 | + int exitCode; |
| 133 | + |
| 134 | + try |
| 135 | + { |
| 136 | + Console.SetOut(output); |
| 137 | + Console.SetError(error); |
| 138 | + exitCode = Program.Main([project.ProjectFile]); |
| 139 | + } |
| 140 | + finally |
| 141 | + { |
| 142 | + Console.SetOut(originalOut); |
| 143 | + Console.SetError(originalError); |
| 144 | + } |
| 145 | + |
| 146 | + Assert.AreEqual(1, exitCode); |
| 147 | + StringAssert.Contains(error.ToString(), "error NC4028"); |
| 148 | + Assert.IsFalse(error.ToString().Contains("NC1002", StringComparison.Ordinal)); |
| 149 | + } |
| 150 | + |
| 151 | + private static CompilationEngine CreateEngine() => new(new CompilationOptions |
| 152 | + { |
| 153 | + SkipRestoreIfAssetsPresent = true, |
| 154 | + RunAnalyzers = true |
| 155 | + }); |
| 156 | + |
| 157 | + private sealed class TempContractProject : IDisposable |
| 158 | + { |
| 159 | + private readonly string _projectDirectory; |
| 160 | + |
| 161 | + public string ProjectFile { get; } |
| 162 | + public string SourceFile { get; } |
| 163 | + |
| 164 | + private TempContractProject(string projectDirectory, string projectFile, string sourceFile) |
| 165 | + { |
| 166 | + _projectDirectory = projectDirectory; |
| 167 | + ProjectFile = projectFile; |
| 168 | + SourceFile = sourceFile; |
| 169 | + } |
| 170 | + |
| 171 | + public static TempContractProject Create(string source) |
| 172 | + { |
| 173 | + var projectDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); |
| 174 | + Directory.CreateDirectory(projectDirectory); |
| 175 | + var projectFile = Path.Combine(projectDirectory, "Contract.csproj"); |
| 176 | + var sourceFile = Path.Combine(projectDirectory, "Contract.cs"); |
| 177 | + var repoRoot = Syntax.SyntaxProbeLoader.GetRepositoryRoot(); |
| 178 | + var frameworkProject = Path.Combine(repoRoot, "src", "Neo.SmartContract.Framework", "Neo.SmartContract.Framework.csproj"); |
| 179 | + |
| 180 | + File.WriteAllText(projectFile, $$""" |
| 181 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 182 | + <PropertyGroup> |
| 183 | + <TargetFramework>net10.0</TargetFramework> |
| 184 | + <Nullable>enable</Nullable> |
| 185 | + </PropertyGroup> |
| 186 | + <ItemGroup> |
| 187 | + <ProjectReference Include="{{frameworkProject}}" /> |
| 188 | + </ItemGroup> |
| 189 | +</Project> |
| 190 | +"""); |
| 191 | + File.WriteAllText(sourceFile, source); |
| 192 | + |
| 193 | + return new TempContractProject(projectDirectory, projectFile, sourceFile); |
| 194 | + } |
| 195 | + |
| 196 | + public void Dispose() |
| 197 | + { |
| 198 | + Directory.Delete(_projectDirectory, recursive: true); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments