|
| 1 | +// Copyright (C) 2015-2026 The Neo Project. |
| 2 | +// |
| 3 | +// ContractTemplateToolManifestTests.cs file belongs to the neo project and is free |
| 4 | +// software distributed under the MIT software license, see the |
| 5 | +// accompanying file LICENSE in the main directory of the |
| 6 | +// repository or http://www.opensource.org/licenses/mit-license.php |
| 7 | +// for more details. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms with or without |
| 10 | +// modifications are permitted. |
| 11 | + |
| 12 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 | +using System; |
| 14 | +using System.IO; |
| 15 | +using System.Linq; |
| 16 | +using System.Text.Json; |
| 17 | +using System.Xml.Linq; |
| 18 | + |
| 19 | +namespace Neo.SmartContract.Template.UnitTests.templates; |
| 20 | + |
| 21 | +[TestClass] |
| 22 | +public class ContractTemplateToolManifestTests |
| 23 | +{ |
| 24 | + private static readonly string TemplateRoot = Path.GetFullPath( |
| 25 | + "../../../../../src/Neo.SmartContract.Template/templates"); |
| 26 | + |
| 27 | + [DataTestMethod] |
| 28 | + [DataRow("neocontractnep11")] |
| 29 | + [DataRow("neocontractnep17")] |
| 30 | + [DataRow("neocontractoracle")] |
| 31 | + [DataRow("neocontractowner")] |
| 32 | + [DataRow("neocontractsolution")] |
| 33 | + public void ContractTemplate_IncludesCompilerToolManifest(string templateName) |
| 34 | + { |
| 35 | + var manifestPath = Path.Combine(TemplateRoot, templateName, ".config", "dotnet-tools.json"); |
| 36 | + Assert.IsTrue(File.Exists(manifestPath), $"{templateName} is missing .config/dotnet-tools.json."); |
| 37 | + |
| 38 | + using var document = JsonDocument.Parse(File.ReadAllText(manifestPath)); |
| 39 | + var root = document.RootElement; |
| 40 | + var compiler = root.GetProperty("tools").GetProperty("neo.compiler.csharp"); |
| 41 | + |
| 42 | + Assert.AreEqual(1, root.GetProperty("version").GetInt32()); |
| 43 | + Assert.IsTrue(root.GetProperty("isRoot").GetBoolean()); |
| 44 | + Assert.AreEqual("TemplateNeoVersion", compiler.GetProperty("version").GetString()); |
| 45 | + |
| 46 | + var commands = compiler.GetProperty("commands"); |
| 47 | + Assert.AreEqual(1, commands.GetArrayLength()); |
| 48 | + Assert.AreEqual("nccs", commands[0].GetString()); |
| 49 | + } |
| 50 | + |
| 51 | + [DataTestMethod] |
| 52 | + [DataRow("neocontractnep11", "Nep11Contract.csproj")] |
| 53 | + [DataRow("neocontractnep17", "Nep17Contract.csproj")] |
| 54 | + [DataRow("neocontractoracle", "OracleRequest.csproj")] |
| 55 | + [DataRow("neocontractowner", "Ownable.csproj")] |
| 56 | + public void StandaloneContractTemplate_UsesLocalCompilerTool( |
| 57 | + string templateName, |
| 58 | + string projectName) |
| 59 | + { |
| 60 | + var project = XDocument.Load(Path.Combine(TemplateRoot, templateName, projectName)); |
| 61 | + var commands = project.Descendants("Exec") |
| 62 | + .Select(element => (string?)element.Attribute("Command")) |
| 63 | + .Where(command => command is not null) |
| 64 | + .ToArray(); |
| 65 | + |
| 66 | + CollectionAssert.Contains(commands, "dotnet tool restore"); |
| 67 | + Assert.IsTrue( |
| 68 | + commands.Any(command => command!.StartsWith("dotnet tool run nccs ", StringComparison.Ordinal)), |
| 69 | + $"{projectName} must invoke nccs through the local tool manifest."); |
| 70 | + } |
| 71 | +} |
0 commit comments