1 #region Copyright notice and license 2 3 // Copyright 2018 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System.Collections.Generic; 20 using System.Linq; 21 using System.Reflection; // UWYU: Object.GetType() extension. 22 using Microsoft.Build.Framework; 23 using Moq; 24 using NUnit.Framework; 25 26 namespace Grpc.Tools.Tests 27 { 28 public class ProtoCompileBasicTest 29 { 30 // Mock task class that stops right before invoking protoc. 31 public class ProtoCompileTestable : ProtoCompile 32 { 33 public string LastPathToTool { get; private set; } 34 public string[] LastResponseFile { get; private set; } 35 public List<string> StdErrMessages { get; } = new List<string>(); 36 ExecuteTool(string pathToTool, string response, string commandLine)37 protected override int ExecuteTool(string pathToTool, 38 string response, 39 string commandLine) 40 { 41 // We should never be using command line commands. 42 Assert.That(commandLine, Is.Null | Is.Empty); 43 44 // Must receive a path to tool 45 Assert.That(pathToTool, Is.Not.Null & Is.Not.Empty); 46 Assert.That(response, Is.Not.Null & Does.EndWith("\n")); 47 48 LastPathToTool = pathToTool; 49 LastResponseFile = response.Remove(response.Length - 1).Split('\n'); 50 51 foreach (string message in StdErrMessages) 52 { 53 LogEventsFromTextOutput(message, MessageImportance.High); 54 } 55 56 // Do not run the tool, but pretend it ran successfully. 57 return StdErrMessages.Any() ? -1 : 0; 58 } 59 }; 60 61 protected Mock<IBuildEngine> _mockEngine; 62 protected ProtoCompileTestable _task; 63 64 [SetUp] SetUp()65 public void SetUp() 66 { 67 _mockEngine = new Mock<IBuildEngine>(); 68 _task = new ProtoCompileTestable { 69 BuildEngine = _mockEngine.Object 70 }; 71 } 72 73 [TestCase("Protobuf")] 74 [TestCase("Generator")] 75 [TestCase("OutputDir")] 76 [Description("We trust MSBuild to initialize these properties.")] RequiredAttributePresentOnProperty(string prop)77 public void RequiredAttributePresentOnProperty(string prop) 78 { 79 var pinfo = _task.GetType()?.GetProperty(prop); 80 Assert.NotNull(pinfo); 81 Assert.That(pinfo, Has.Attribute<RequiredAttribute>()); 82 } 83 }; 84 } 85