DepFileUtilTest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #region Copyright notice and license
  2. // Copyright 2018 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System.IO;
  17. using Microsoft.Build.Framework;
  18. using Microsoft.Build.Utilities;
  19. using NUnit.Framework;
  20. namespace Grpc.Tools.Tests
  21. {
  22. public class DepFileUtilTest
  23. {
  24. [Test]
  25. public void HashString64Hex_IsSane()
  26. {
  27. string hashFoo1 = DepFileUtil.HashString64Hex("foo");
  28. string hashEmpty = DepFileUtil.HashString64Hex("");
  29. string hashFoo2 = DepFileUtil.HashString64Hex("foo");
  30. StringAssert.IsMatch("^[a-f0-9]{16}$", hashFoo1);
  31. Assert.AreEqual(hashFoo1, hashFoo2);
  32. Assert.AreNotEqual(hashFoo1, hashEmpty);
  33. }
  34. [Test]
  35. public void GetDepFilenameForProto_IsSane()
  36. {
  37. StringAssert.IsMatch(@"^out[\\/][a-f0-9]{16}_foo.protodep$",
  38. DepFileUtil.GetDepFilenameForProto("out", "foo.proto"));
  39. StringAssert.IsMatch(@"^[a-f0-9]{16}_foo.protodep$",
  40. DepFileUtil.GetDepFilenameForProto("", "foo.proto"));
  41. }
  42. [Test]
  43. public void GetDepFilenameForProto_HashesDir()
  44. {
  45. string PickHash(string fname) =>
  46. DepFileUtil.GetDepFilenameForProto("", fname).Substring(0, 16);
  47. string same1 = PickHash("dir1/dir2/foo.proto");
  48. string same2 = PickHash("dir1/dir2/proto.foo");
  49. string same3 = PickHash("dir1/dir2/proto");
  50. string same4 = PickHash("dir1/dir2/.proto");
  51. string unsame1 = PickHash("dir2/foo.proto");
  52. string unsame2 = PickHash("/dir2/foo.proto");
  53. Assert.AreEqual(same1, same2);
  54. Assert.AreEqual(same1, same3);
  55. Assert.AreEqual(same1, same4);
  56. Assert.AreNotEqual(same1, unsame1);
  57. Assert.AreNotEqual(unsame1, unsame2);
  58. }
  59. [Test]
  60. public void GetOutputDirWithHash_IsSane()
  61. {
  62. StringAssert.IsMatch(@"^out[\\/][a-f0-9]{16}$",
  63. DepFileUtil.GetOutputDirWithHash("out", "foo.proto"));
  64. StringAssert.IsMatch(@"^[a-f0-9]{16}$",
  65. DepFileUtil.GetOutputDirWithHash("", "foo.proto"));
  66. }
  67. [Test]
  68. public void GetOutputDirWithHash_HashesDir()
  69. {
  70. string PickHash(string fname) => DepFileUtil.GetOutputDirWithHash("", fname);
  71. string same1 = PickHash("dir1/dir2/foo.proto");
  72. string same2 = PickHash("dir1/dir2/proto.foo");
  73. string same3 = PickHash("dir1/dir2/proto");
  74. string same4 = PickHash("dir1/dir2/.proto");
  75. string unsame1 = PickHash("dir2/foo.proto");
  76. string unsame2 = PickHash("/dir2/foo.proto");
  77. Assert.AreEqual(same1, same2);
  78. Assert.AreEqual(same1, same3);
  79. Assert.AreEqual(same1, same4);
  80. Assert.AreNotEqual(same1, unsame1);
  81. Assert.AreNotEqual(unsame1, unsame2);
  82. }
  83. //////////////////////////////////////////////////////////////////////////
  84. // Full file reading tests
  85. // Generated by protoc on Windows. Slashes vary.
  86. const string depFile1 =
  87. @"C:\projects\foo\src\./foo.grpc.pb.cc \
  88. C:\projects\foo\src\./foo.grpc.pb.h \
  89. C:\projects\foo\src\./foo.pb.cc \
  90. C:\projects\foo\src\./foo.pb.h: C:/usr/include/google/protobuf/wrappers.proto\
  91. C:/usr/include/google/protobuf/any.proto\
  92. C:/usr/include/google/protobuf/source_context.proto\
  93. C:/usr/include/google/protobuf/type.proto\
  94. foo.proto";
  95. // This has a nasty output directory with a space.
  96. const string depFile2 =
  97. @"obj\Release x64\net45\/Foo.cs \
  98. obj\Release x64\net45\/FooGrpc.cs: C:/usr/include/google/protobuf/wrappers.proto\
  99. C:/projects/foo/src//foo.proto";
  100. [Test]
  101. public void ReadDependencyInput_FullFile1()
  102. {
  103. string[] deps = ReadDependencyInputFromFileData(depFile1, "foo.proto");
  104. Assert.NotNull(deps);
  105. Assert.That(deps, Has.Length.InRange(4, 5)); // foo.proto may or may not be listed.
  106. Assert.That(deps, Has.One.EndsWith("wrappers.proto"));
  107. Assert.That(deps, Has.One.EndsWith("type.proto"));
  108. Assert.That(deps, Has.None.StartWith(" "));
  109. }
  110. [Test]
  111. public void ReadDependencyInput_FullFile2()
  112. {
  113. string[] deps = ReadDependencyInputFromFileData(depFile2, "C:/projects/foo/src/foo.proto");
  114. Assert.NotNull(deps);
  115. Assert.That(deps, Has.Length.InRange(1, 2));
  116. Assert.That(deps, Has.One.EndsWith("wrappers.proto"));
  117. Assert.That(deps, Has.None.StartWith(" "));
  118. }
  119. [Test]
  120. public void ReadDependencyInput_FullFileUnparsable()
  121. {
  122. string[] deps = ReadDependencyInputFromFileData("a:/foo.proto", "/foo.proto");
  123. Assert.NotNull(deps);
  124. Assert.Zero(deps.Length);
  125. }
  126. // NB in our tests files are put into the temp directory but all have
  127. // different names. Avoid adding files with the same directory path and
  128. // name, or add reasonable handling for it if required. Tests are run in
  129. // parallel and will collide otherwise.
  130. private string[] ReadDependencyInputFromFileData(string fileData, string protoName)
  131. {
  132. string tempPath = Path.GetTempPath();
  133. string tempfile = DepFileUtil.GetDepFilenameForProto(tempPath, protoName);
  134. try
  135. {
  136. File.WriteAllText(tempfile, fileData);
  137. var mockEng = new Moq.Mock<IBuildEngine>();
  138. var log = new TaskLoggingHelper(mockEng.Object, "x");
  139. return DepFileUtil.ReadDependencyInputs(tempPath, protoName, log);
  140. }
  141. finally
  142. {
  143. try
  144. {
  145. File.Delete(tempfile);
  146. }
  147. catch { }
  148. }
  149. }
  150. };
  151. }