[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System / Test / System.CodeDom.Compiler / ExecutorTest.cs
1 //
2 // ExecutorTest.cs 
3 //      - Unit tests for System.CodeDom.Compiler.Executor
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
30 using NUnit.Framework;
31
32 using System;
33 using System.CodeDom.Compiler;
34 using System.IO;
35 using System.Runtime.InteropServices;
36 using System.Security.Principal;
37
38 namespace MonoTests.System.CodeDom.Compiler
39 {
40         [TestFixture]
41         public class ExecutorTest
42         {
43                 private bool posix;
44                 private bool cmdNotFound;
45                 private int errcode;
46                 private string cmd;
47                 private string cd;
48                 private string temp;
49                 private TempFileCollection tfc;
50                 private WindowsIdentity winid;
51                 private IntPtr token;
52
53                 [TestFixtureSetUp]
54                 public void FixtureSetUp ()
55                 {
56                         cmd = "ping"; // available everywhere
57                         cd = Environment.CurrentDirectory;
58                         temp = Path.GetTempPath ();
59                         tfc = new TempFileCollection ();
60                         winid = WindowsIdentity.GetCurrent ();
61                         token = winid.Token;
62
63                         try {
64                                 string output = null;
65                                 string error = null;
66                                 errcode = Executor.ExecWaitWithCapture (cmd, tfc, ref output, ref error);
67                         }
68                         catch (Exception) {
69                                 // cmd might not be in the PATH
70                                 cmdNotFound = true;
71                         }
72                 }
73
74                 [TestFixtureTearDown]
75                 public void FixtureTearDown ()
76                 {
77                         winid.Dispose ();
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (ExternalException))]
82                 public void ExecWait_NullCmd ()
83                 {
84                         Executor.ExecWait (null, new TempFileCollection ());
85                 }
86
87                 [Test]
88                 [ExpectedException (typeof (NullReferenceException))]
89                 public void ExecWait_NullTempFileCollection ()
90                 {
91                         if (cmdNotFound)
92                                 Assert.Ignore ("ping command not found.");
93
94                         Executor.ExecWait (cmd, null);
95                 }
96
97                 [Test]
98                 [Category ("NotWorking")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=341293
99                 public void ExecWait ()
100                 {
101                         if (cmdNotFound)
102                                 Assert.Ignore ("ping command not found.");
103
104                         try {
105                                 Executor.ExecWait (cmd, new TempFileCollection ());
106                                 Assert.Fail ("#1");
107                         } catch (ExternalException ex) {
108                                 // Cannot execute a program. The command being executed was .
109                                 Assert.AreEqual (typeof (ExternalException), ex.GetType (), "#2");
110                                 Assert.AreEqual (2, ex.ErrorCode, "#3");
111                                 Assert.IsNull (ex.InnerException, "#4");
112                                 Assert.IsNotNull (ex.Message, "#5");
113                         }
114                 }
115
116                 [Test]
117                 public void ExecWaitWithCapture ()
118                 {
119                         if (cmdNotFound)
120                                 Assert.Ignore ("ping command not found.");
121
122                         string output = null;
123                         string error = null;
124                         TempFileCollection tfc = new TempFileCollection ();
125                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (cmd, tfc, ref output, ref error), "ErrorCode");
126                         Assert.IsTrue (File.Exists (output), "output");
127                         Assert.IsTrue (output.StartsWith (temp), "output-path");
128                         Assert.IsTrue (File.Exists (error), "error");
129                         Assert.IsTrue (error.StartsWith (temp), "error-path");
130                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
131                 }
132
133                 [Test]
134                 public void ExecWaitWithCapture_CurrentDir ()
135                 {
136                         if (cmdNotFound)
137                                 Assert.Ignore ("ping command not found.");
138
139                         string output = null;
140                         string error = null;
141                         TempFileCollection tfc = new TempFileCollection ();
142                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (cmd, cd, tfc, ref output, ref error), "ErrorCode");
143                         Assert.IsTrue (File.Exists (output), "output");
144                         Assert.IsTrue (output.StartsWith (temp), "output-path");
145                         Assert.IsTrue (File.Exists (error), "error");
146                         Assert.IsTrue (error.StartsWith (temp), "error-path");
147                         // output/error file are relative to temp path
148                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
149                 }
150
151                 [Test]
152                 public void ExecWaitWithCapture_Token ()
153                 {
154                         if (cmdNotFound)
155                                 Assert.Ignore ("ping command not found.");
156
157                         string output = null;
158                         string error = null;
159                         TempFileCollection tfc = new TempFileCollection ();
160                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (token, cmd, tfc, ref output, ref error), "ErrorCode");
161                         Assert.IsTrue (File.Exists (output), "output");
162                         Assert.IsTrue (output.StartsWith (temp), "output-path");
163                         Assert.IsTrue (File.Exists (error), "error");
164                         Assert.IsTrue (error.StartsWith (temp), "error-path");
165                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
166                 }
167
168                 [Test]
169                 public void ExecWaitWithCapture_Token_CurrentDir ()
170                 {
171                         if (cmdNotFound)
172                                 Assert.Ignore ("ping command not found.");
173
174                         string output = null;
175                         string error = null;
176                         TempFileCollection tfc = new TempFileCollection ();
177                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (token, cmd, cd, tfc, ref output, ref error), "ErrorCode");
178                         Assert.IsTrue (File.Exists (output), "output");
179                         Assert.IsTrue (output.StartsWith (temp), "output-path");
180                         Assert.IsTrue (File.Exists (error), "error");
181                         Assert.IsTrue (error.StartsWith (temp), "error-path");
182                         // output/error file are relative to temp path
183                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
184                 }
185         }
186 }