public void TestStdOutput()
        {
            using (ProcessRunner runner = new ProcessRunner("cmd.exe"))
            {
                StringWriter wtr = new StringWriter();
                StringWriter err = new StringWriter();
                ProcessOutputEventHandler handler =
                        delegate(object o, ProcessOutputEventArgs e)
                        { if (e.Error) err.WriteLine(e.Data); else wtr.WriteLine(e.Data); };

                runner.OutputReceived += handler;

                Assert.AreEqual(0, runner.Run("/C", "dir", "/b", "/on", "/ad-h-s", "c:\\"));
                Assert.AreEqual(String.Empty, err.ToString());

                Assert.AreNotEqual(0, runner.PID);

                StringReader rdr = new StringReader(wtr.ToString());
                List<DirectoryInfo> rootdirs = new List<DirectoryInfo>(new DirectoryInfo("C:\\").GetDirectories());
                rootdirs.Sort(delegate(DirectoryInfo x, DirectoryInfo y) { return StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name); });
                foreach (DirectoryInfo di in rootdirs)
                {
                    if ((di.Attributes & (FileAttributes.Hidden | FileAttributes.System)) != 0)
                        continue;
                    Assert.AreEqual(di.Name, rdr.ReadLine());
                }

                Assert.AreEqual(null, rdr.ReadLine());
            }
        }
        /// <summary>
        /// Creates a runnable script with the specified engine parameters
        /// </summary>
        public ScriptRunner(ScriptEngine engine, string script)
        {
			_engine = engine;
			_scriptFile = _engine.Compile(script);

			_arguments = ArgumentList.Parse(engine.ArgumentFormat.Replace("{SCRIPT}", _scriptFile.TempPath));
			_runner = new ProcessRunner(engine.Executable, _arguments);
        }
        private void shell(string exePath, List <string> args)
        {
            CSharpTest.Net.Processes.ProcessRunner procRunner =
                new CSharpTest.Net.Processes.ProcessRunner(isTrueString(_autoEscape), exePath, args.ToArray());

            _cmdLine = procRunner.ToString(); // What will be spawned

            procRunner.OutputReceived += new ProcessOutputEventHandler(run_OutputReceived);
            _retCode = procRunner.Run().ToString();
        }
		public void TestStdError()
		{
			ProcessRunner runner = new ProcessRunner("cmd.exe");

			StringWriter wtr = new StringWriter();
			StringWriter err = new StringWriter();
			runner.OutputReceived += delegate(object o, ProcessOutputEventArgs e)
			{ if (e.Error) err.WriteLine(e.Data); else wtr.WriteLine(e.Data); };

			Assert.AreNotEqual(0, runner.Run("/C", "dir", "c:\\I truely hope for this tests sake that this directory doesn't exist\\"));
			Assert.AreEqual(String.Empty, wtr.ToString());
			Assert.AreEqual("The system cannot find the file specified.", err.ToString().Trim());
		}
Esempio n. 5
0
        private TempFile SetExePath(string script)
        {
            _executable = script.Trim();
            _executable = Environment.ExpandEnvironmentVariables(_executable);
            if (!Path.IsPathRooted(_executable))
            {
                if (File.Exists(_executable))
                {
                    _executable = Path.GetFullPath(_executable);
                }
                else
                {
                    _executable = ProcessRunner.FindFullPath(_executable);
                }
            }

            _fileExtension = Path.GetExtension(script.Trim());

            TempFile temp = new TempFile();

            temp.Delete();
            return(temp);
        }
        public void CreateResFile(string output, out string resFile)
        {
            int code;
            string mcexe = "mc.exe";
            if (!String.IsNullOrEmpty(_toolsBin)) mcexe = Path.Combine(_toolsBin, mcexe);
            using (ProcessRunner mc = new ProcessRunner(mcexe, "-U", "{0}", "-r", "{1}", "-h", "{1}"))
            using (StringWriter stdio = new StringWriter())
            {
                mc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { stdio.WriteLine(e.Data); };
                if (0 != (code = mc.RunFormatArgs(_mcFile, IntermediateFiles)))
                {
                    Trace.WriteLine(stdio.ToString());
                    throw new ApplicationException(String.Format("mc.exe failed ({0:x}):\r\n{1}", code, stdio));
                }
            }

            string rcFile = Path.Combine(IntermediateFiles, Path.ChangeExtension(Path.GetFileName(_mcFile), ".rc"));
            VersionInfo.AppendToRc(Path.GetFileName(output), rcFile);
            if (!String.IsNullOrEmpty(IconFile) && File.Exists(IconFile))
                File.AppendAllText(rcFile, String.Format("\r\n1 ICON \"{0}\"\r\n", IconFile.Replace(@"\", @"\\")));
            if (!String.IsNullOrEmpty(ManifestFile) && File.Exists(ManifestFile))
                File.AppendAllText(rcFile, String.Format("\r\n1 24 \"{0}\"\r\n", ManifestFile.Replace(@"\", @"\\")));
            if (!String.IsNullOrEmpty(ResourceScript))
                File.AppendAllText(rcFile, "\r\n" + ResourceScript + "\r\n");

            string rcexe = "rc.exe";
            if (!String.IsNullOrEmpty(_toolsBin)) rcexe = Path.Combine(_toolsBin, rcexe);
            using (ProcessRunner rc = new ProcessRunner(rcexe, "{0}"))
            using (StringWriter stdio = new StringWriter())
            {
                rc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { stdio.WriteLine(e.Data); Trace.WriteLine(e.Data, rcexe); };
                if (0 != (code = rc.RunFormatArgs(rcFile)))
                    throw new ApplicationException(String.Format("mc.exe failed ({0:x}):\r\n{1}", code, stdio));
            }

            resFile = Path.ChangeExtension(rcFile, ".res");
        }
Esempio n. 7
0
        public void TestBuildMcFromResX()
        {
            TestResourceBuilder builder1 = new TestResourceBuilder("TestNamespace", "TestResXClass1");
            builder1.Add("Testing", "test value 1", "#MessageId=42");

            using (TempDirectory intermediateFiles = new TempDirectory())
            using (TempFile mctxt = TempFile.FromExtension(".mc"))
            {
                using (TempFile resx1 = TempFile.FromExtension(".resx"))
                {
                    builder1.BuildResX(resx1.TempPath);
                    Commands.ResXtoMc(mctxt.TempPath, new string[] { resx1.TempPath });
                }

                string mcexe = TestResourceBuilder.FindExe("mc.exe");

                using (ProcessRunner mc = new ProcessRunner(mcexe, "-U", "{0}", "-r", "{1}", "-h", "{1}"))
                {
                    mc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { Trace.WriteLine(e.Data, mcexe); };
                    Assert.AreEqual(0, mc.RunFormatArgs(mctxt.TempPath, intermediateFiles.TempPath), "mc.exe failed.");
                }

                string rcfile = Path.Combine(intermediateFiles.TempPath, Path.GetFileNameWithoutExtension(mctxt.TempPath) + ".rc");
                Assert.IsTrue(File.Exists(rcfile));
                Assert.IsTrue(File.Exists(Path.ChangeExtension(rcfile, ".h")));
                Assert.IsTrue(File.Exists(Path.Combine(intermediateFiles.TempPath, "MSG00409.bin")));

                string rcexe = Path.Combine(Path.GetDirectoryName(mcexe), "rc.exe");
                if (!File.Exists(rcexe))
                    rcexe = TestResourceBuilder.FindExe("rc.exe");

                using (ProcessRunner rc = new ProcessRunner(rcexe, "{0}"))
                {
                    rc.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { Trace.WriteLine(e.Data, rcexe); };
                    Assert.AreEqual(0, rc.RunFormatArgs(rcfile), "rc.exe failed.");
                }

                string resfile = Path.ChangeExtension(rcfile, ".res");
                Assert.IsTrue(File.Exists(resfile));
                Assert.IsTrue(File.ReadAllText(resfile).Contains("\0t\0e\0s\0t\0 \0v\0a\0l\0u\0e\0 \01"));
            }
        }
		public void TestOutputEventUnsubscribe()
		{
			ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "echo");

			bool outputReceived = false;
			ProcessOutputEventHandler handler =
					delegate(object o, ProcessOutputEventArgs e)
					{ outputReceived = true; };

			runner.OutputReceived += handler;
			runner.OutputReceived -= handler;

			Assert.AreEqual(0, runner.Run());
			Assert.IsFalse(outputReceived);
        }
		public void TestToString()
		{
			ProcessRunner runner = new ProcessRunner("cmd.exe", "/c", "echo hi");
			string target = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"cmd.exe");
			Assert.AreEqual(target + " /c \"echo hi\"", runner.ToString());
		}
		public void TestKill()
		{
			string tempfile = Path.GetTempFileName();
			try
			{
				Environment.CurrentDirectory = Path.GetDirectoryName(FileUtils.FindFullPath("cmd.exe"));
				ProcessRunner runner = new ProcessRunner("cmd.exe");
				Assert.IsFalse(runner.IsRunning);
				runner.Kill(); // always safe to call

				runner.Start("/K", "ECHO Hello > " + tempfile);
				Assert.IsTrue(runner.IsRunning);

				try //make sure we can't start twice.
				{
					runner.Start("/C", "ECHO", "HELO");
					Assert.Fail();
				}
				catch (InvalidOperationException)
				{ }

				Assert.IsFalse(runner.WaitForExit(TimeSpan.FromSeconds(1), false));
				runner.Kill();
				string output = File.ReadAllText(tempfile).Trim();
				Assert.AreEqual("Hello", output);
			}
			finally
			{ File.Delete(tempfile); }
		}
		public void TestFormatArgs()
		{
			string tempfile = Path.GetTempFileName();
			try
			{
				ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "ECHO", "Hello", ">{0}");

				runner.StartFormatArgs(tempfile);
				Assert.AreEqual(0, runner.ExitCode);

				string output = File.ReadAllText(tempfile).Trim();
				Assert.AreEqual("Hello", output);

				File.Delete(tempfile);
				Assert.AreEqual(0, runner.RunFormatArgs(tempfile));

				output = File.ReadAllText(tempfile).Trim();
				Assert.AreEqual("Hello", output);
			}
			finally
			{ File.Delete(tempfile); }
		}
		public void TestStdInput()
		{
			string tempfile = Path.GetTempFileName();
			try
			{
				ProcessRunner runner = new ProcessRunner("cmd.exe");

				runner.Start();
				runner.StandardInput.WriteLine("ECHO Hello > " + tempfile);
				runner.StandardInput.WriteLine("EXIT");

				Assert.IsTrue(runner.WaitForExit(TimeSpan.FromMinutes(1)));
				Assert.AreEqual(0, runner.ExitCode);

				string output = File.ReadAllText(tempfile).Trim();
				Assert.AreEqual("Hello", output);
			}
			finally
			{ File.Delete(tempfile); }
		}
		public void TestExitedEventUnsubscribe()
		{
			ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "echo");

			int exitCode = -1;
			bool receivedExit = false;
			ProcessExitedEventHandler handler =
					delegate(object o, ProcessExitedEventArgs e)
					{ receivedExit = true; exitCode = e.ExitCode; };

			runner.ProcessExited += handler;
			runner.ProcessExited -= handler;

			Assert.AreEqual(0, runner.Run());
			Assert.IsFalse(receivedExit);
			Assert.AreEqual(-1, exitCode);
		}
        public void TestRunWithWorkingDirectory()
        {
            using (TempDirectory dir = new TempDirectory())
            using (ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "echo %CD%"))
            {
                List<string> lines = new List<string>();
                runner.OutputReceived += delegate(Object o, ProcessOutputEventArgs e) { lines.Add(e.Data); };

                Assert.AreNotEqual(dir.TempPath, runner.WorkingDirectory);
                runner.WorkingDirectory = dir.TempPath;
                Assert.AreEqual(dir.TempPath, runner.WorkingDirectory);
                
                int exitCode = runner.Run();
                Assert.AreEqual(0, exitCode);
                Assert.AreEqual(dir.TempPath.TrimEnd('\\', '/'), lines[0].TrimEnd('\\', '/'));
            }
        }
 public void TestRunWithInput()
 {
     using (ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "sort"))
     {
         List<string> lines = new List<string>();
         runner.OutputReceived += delegate(Object o, ProcessOutputEventArgs e) { lines.Add(e.Data); };
         int exitCode = runner.Run(new StringReader("Hello World\r\nWhatever!\r\nA line that goes first."));
         Assert.AreEqual(0, exitCode);
         Assert.AreEqual("A line that goes first.", lines[0]);
         Assert.AreEqual("Hello World", lines[1]);
         Assert.AreEqual("Whatever!", lines[2]);
     }
 }