New test.
[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 int errcode;
45                 private string cmd;
46                 private string cd;
47                 private string temp;
48                 private TempFileCollection tfc;
49                 private WindowsIdentity winid;
50                 private IntPtr token;
51
52                 [TestFixtureSetUp]
53                 public void FixtureSetUp ()
54                 {
55                         int platform = (int) Environment.OSVersion.Platform;
56                         posix = (platform == 4) || (platform == 128);
57                         errcode = (posix ? 2 : 1);
58                         cmd = "ping"; // available everywhere
59                         cd = Environment.CurrentDirectory;
60                         temp = Path.GetTempPath ();
61                         tfc = new TempFileCollection ();
62                         winid = WindowsIdentity.GetCurrent ();
63                         token = winid.Token;
64                 }
65
66                 [TestFixtureTearDown]
67                 public void FixtureTearDown ()
68                 {
69 #if NET_2_0
70                         winid.Dispose ();
71 #else
72                         GC.KeepAlive (winid);
73 #endif
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (ExternalException))]
78                 public void ExecWait_NullCmd ()
79                 {
80                         Executor.ExecWait (null, new TempFileCollection ());
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (NullReferenceException))]
85                 public void ExecWait_NullTempFileCollection ()
86                 {
87                         Executor.ExecWait (cmd, null);
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ExternalException))]
92                 [Category ("NotWorking")]
93                 public void ExecWait ()
94                 {
95                         // why does it fail ? any case that works ?
96                         Executor.ExecWait (cmd, tfc);
97                 }
98
99                 [Test]
100                 public void ExecWaitWithCapture ()
101                 {
102                         string output = null;
103                         string error = null;
104                         TempFileCollection tfc = new TempFileCollection ();
105                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (cmd, tfc, ref output, ref error), "ErrorCode");
106                         Assert.IsTrue (File.Exists (output), "output");
107                         Assert.IsTrue (output.StartsWith (temp), "output-path");
108                         Assert.IsTrue (File.Exists (error), "error");
109                         Assert.IsTrue (error.StartsWith (temp), "error-path");
110                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
111                 }
112
113                 [Test]
114                 public void ExecWaitWithCapture_CurrentDir ()
115                 {
116                         string output = null;
117                         string error = null;
118                         TempFileCollection tfc = new TempFileCollection ();
119                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (cmd, cd, tfc, ref output, ref error), "ErrorCode");
120                         Assert.IsTrue (File.Exists (output), "output");
121                         Assert.IsTrue (output.StartsWith (temp), "output-path");
122                         Assert.IsTrue (File.Exists (error), "error");
123                         Assert.IsTrue (error.StartsWith (temp), "error-path");
124                         // output/error file are relative to temp path
125                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
126                 }
127
128                 [Test]
129                 public void ExecWaitWithCapture_Token ()
130                 {
131                         string output = null;
132                         string error = null;
133                         TempFileCollection tfc = new TempFileCollection ();
134                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (token, cmd, 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                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
140                 }
141
142                 [Test]
143                 public void ExecWaitWithCapture_Token_CurrentDir ()
144                 {
145                         string output = null;
146                         string error = null;
147                         TempFileCollection tfc = new TempFileCollection ();
148                         Assert.AreEqual (errcode, Executor.ExecWaitWithCapture (token, cmd, cd, tfc, ref output, ref error), "ErrorCode");
149                         Assert.IsTrue (File.Exists (output), "output");
150                         Assert.IsTrue (output.StartsWith (temp), "output-path");
151                         Assert.IsTrue (File.Exists (error), "error");
152                         Assert.IsTrue (error.StartsWith (temp), "error-path");
153                         // output/error file are relative to temp path
154                         Assert.IsTrue (tfc.Count >= 2, "TempFileCollection");
155                 }
156         }
157 }