ExecutorTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // ExecutorTest.cs
  3. // - Unit tests for System.CodeDom.Compiler.Executor
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.CodeDom.Compiler;
  32. using System.IO;
  33. using System.Runtime.InteropServices;
  34. using System.Security.Principal;
  35. namespace MonoTests.System.CodeDom.Compiler {
  36. [TestFixture]
  37. public class ExecutorTest {
  38. private bool posix;
  39. private int errcode;
  40. private string cmd;
  41. private string cd;
  42. private string temp;
  43. private TempFileCollection tfc;
  44. private WindowsIdentity winid;
  45. private IntPtr token;
  46. [TestFixtureSetUp]
  47. public void FixtureSetUp ()
  48. {
  49. int platform = (int) Environment.OSVersion.Platform;
  50. posix = (platform == 4) || (platform == 128);
  51. errcode = (posix ? 2 : 1);
  52. cmd = "ping"; // available everywhere
  53. cd = Environment.CurrentDirectory;
  54. temp = Path.GetTempPath ();
  55. tfc = new TempFileCollection ();
  56. winid = WindowsIdentity.GetCurrent ();
  57. token = winid.Token;
  58. }
  59. [TestFixtureTearDown]
  60. public void FixtureTearDown ()
  61. {
  62. #if NET_2_0
  63. winid.Dispose ();
  64. #else
  65. GC.KeepAlive (winid);
  66. #endif
  67. }
  68. [Test]
  69. [ExpectedException (typeof (ExternalException))]
  70. public void ExecWait_NullCmd ()
  71. {
  72. Executor.ExecWait (null, new TempFileCollection ());
  73. }
  74. [Test]
  75. [ExpectedException (typeof (NullReferenceException))]
  76. public void ExecWait_NullTempFileCollection ()
  77. {
  78. Executor.ExecWait (cmd, null);
  79. }
  80. [Test]
  81. [ExpectedException (typeof (ExternalException))]
  82. [Category ("NotWorking")]
  83. public void ExecWait ()
  84. {
  85. // why does it fail ? any case that works ?
  86. Executor.ExecWait (cmd, tfc);
  87. }
  88. [Test]
  89. public void ExecWaitWithCapture ()
  90. {
  91. string output = null;
  92. string error = null;
  93. TempFileCollection tfc = new TempFileCollection ();
  94. Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (cmd, tfc, ref output, ref error), "ErrorCode");
  95. Assert.IsTrue (File.Exists (output), "output");
  96. Assert.IsTrue (output.StartsWith (temp), "output-path");
  97. Assert.IsTrue (File.Exists (error), "error");
  98. Assert.IsTrue (error.StartsWith (temp), "error-path");
  99. Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
  100. }
  101. [Test]
  102. public void ExecWaitWithCapture_CurrentDir ()
  103. {
  104. string output = null;
  105. string error = null;
  106. TempFileCollection tfc = new TempFileCollection ();
  107. Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (cmd, cd, tfc, ref output, ref error), "ErrorCode");
  108. Assert.IsTrue (File.Exists (output), "output");
  109. Assert.IsTrue (output.StartsWith (temp), "output-path");
  110. Assert.IsTrue (File.Exists (error), "error");
  111. Assert.IsTrue (error.StartsWith (temp), "error-path");
  112. // output/error file are relative to temp path
  113. Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
  114. }
  115. [Test]
  116. public void ExecWaitWithCapture_Token ()
  117. {
  118. string output = null;
  119. string error = null;
  120. TempFileCollection tfc = new TempFileCollection ();
  121. Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (token, cmd, tfc, ref output, ref error), "ErrorCode");
  122. Assert.IsTrue (File.Exists (output), "output");
  123. Assert.IsTrue (output.StartsWith (temp), "output-path");
  124. Assert.IsTrue (File.Exists (error), "error");
  125. Assert.IsTrue (error.StartsWith (temp), "error-path");
  126. Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
  127. }
  128. [Test]
  129. public void ExecWaitWithCapture_Token_CurrentDir ()
  130. {
  131. string output = null;
  132. string error = null;
  133. TempFileCollection tfc = new TempFileCollection ();
  134. Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (token, cmd, cd, tfc, ref output, ref error), "ErrorCode");
  135. Assert.IsTrue (File.Exists (output), "output");
  136. Assert.IsTrue (output.StartsWith (temp), "output-path");
  137. Assert.IsTrue (File.Exists (error), "error");
  138. Assert.IsTrue (error.StartsWith (temp), "error-path");
  139. // output/error file are relative to temp path
  140. Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
  141. }
  142. }
  143. }