Exemplo n.º 1
0
 private static string PrepareResult(bool allowinvstate, ConsoleApplicationResult result)
 {
     if (null != result.Exception)
     {
         throw result.Exception;
     }
     if (!allowinvstate && 0 != result.State)
     {
         throw new Exception("Invalid State " + result.State + "\r\n" + result.Output + result.Error);
     }
     return((result.Output ?? "").Trim(new[] { '\r', '\n', ' ' }));
 }
	    private static ConsoleApplicationResult DoDeleteCommand(ProcessStartInfo startinfo, ConsoleApplicationResult result) {
	        var fullpath = Path.Combine(startinfo.WorkingDirectory, startinfo.Arguments.Replace("\"", "").Trim());
	        var mask = Path.GetFileName(fullpath);
	        var dir = Path.GetDirectoryName(fullpath);
	        try {
	            if (Directory.Exists(dir)) {
	                var files = Directory.GetFiles(dir, mask);
	                foreach (var file in files) {
	                    try {
	                        File.Delete(file);
	                    }
	                    catch {
	                        try {
	                            Thread.Sleep(10);
	                            File.Delete(file);
	                        }
	                        catch {
	                        }
	                    }
	                }
	            }
	            result.State = 0;
	        }
	        catch (Exception ex) {
	            result.State = -1;
	            result.Exception = ex;
	        }
	        return result;
	    }
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public ConsoleApplicationResult RunSync(){
			if (IsStub){
				var res = new ConsoleApplicationResult();
				if (null != OnStub) OnStub.Invoke(this, new ConsoleApplicationResult());
				return res;
			}
			Interlocked.Increment(ref Calls);
			var result = new ConsoleApplicationResult();
			var startinfo = PrepareStrartInfo();
			result.StartInfo = startinfo;

			if (ExePath == "del"){
				return DoDeleteCommand(startinfo, result);
			}

			var process = new Process{StartInfo = startinfo};
			try{
				var output = new StringBuilder();
				var error = new StringBuilder();

				process.OutputDataReceived += (s, e) =>{
					output.AppendLine(e.Data);
					if (null != Listener){
						Listener.EmitOutput(e.Data);
						CheckMessage(process);
					}
				};
				process.ErrorDataReceived += (s, e) =>{
					error.AppendLine(e.Data);
					if (null != Listener){
						Listener.EmitOutput(e.Data);
						CheckMessage(process);
					}
				};

				try{
					var wellFinish = false;
					process.Start();
					process.BeginOutputReadLine();
					process.BeginErrorReadLine();
				    if (null != SendLines && 0 != SendLines.Length) {
				        foreach (var sendLine in SendLines) {
				            process.StandardInput.WriteLine(sendLine);
				        }
				    }
					CheckMessage(process);
					if (0 != Timeout){
						wellFinish = process.WaitForExit(Timeout);
						result.Timeouted = !wellFinish;
					}
					else{
						process.WaitForExit();
						wellFinish = true;
					}
					if (!wellFinish){
						if (!process.HasExited){
							process.Kill();
						}
					}
					result.State = process.ExitCode;
					result.Output = output.ToString();
					result.Error = error.ToString();
				}
				catch (Exception ex){
					result.Exception = ex;
				}

				return result;
			}
			finally{
				if (!process.HasExited){
					process.Kill();
				}
			}
		}
Exemplo n.º 4
0
		private static string PrepareResult(bool allowinvstate, ConsoleApplicationResult result){
			if (null != result.Exception) throw result.Exception;
			if (!allowinvstate && 0 != result.State){
				throw new Exception("Invalid State " + result.State + "\r\n" + result.Output + result.Error);
			}
			return (result.Output??"").Trim(new[]{'\r', '\n', ' '});
		}
Exemplo n.º 5
0
        public override void Specify()
        {
            given("a psake script which writes the current process id to output", delegate
            {
                string scriptPath = GetVerifiedPathOfTestScript("task_writes_process_id.ps1");

                when("that script is invoked interactively", delegate
                {
                    ConsoleApplicationResult invocation = arrange(() =>
                    {
                        string moduleLocation = GetModuleLocation();

                        return(ProcessHandling.RunNoninteractiveConsoleProcess(PsakeUtil.GetPowershellCommand(moduleLocation, scriptPath, "default")));
                    });

                    then("the script succeeds", delegate
                    {
                        expect(() => invocation.ExitCode == 0);
                    });

                    then("the output file has a different process id than the current", delegate
                    {
                        var allLines = invocation.ConsoleOutput;

                        int?processId =
                            allLines.Select(l => Regex.Match(l, @"Process ID: (\d*)")).Where(m => m.Success).Select(
                                m => int.Parse(m.Groups[1].Value)).Single();

                        expect(() => processId.Value > 0);
                        expect(() => processId.Value != Process.GetCurrentProcess().Id);
                    });
                });
            });

            given("a psake script that fails", delegate
            {
                var scriptPath = GetVerifiedPathOfTestScript("task_fails_assert.ps1");

                when("that script is invoked interactively", delegate
                {
                    ConsoleApplicationResult invocation = arrange(() =>
                    {
                        string moduleLocation = GetModuleLocation();
                        return(ProcessHandling.RunNoninteractiveConsoleProcess(PsakeUtil.GetPowershellCommand(moduleLocation, scriptPath, "Fails")));
                    });

                    then("the exit code indicates failure", delegate
                    {
                        expect(() => invocation.ExitCode == 1);
                    });
                });
            });

            given("a non-default psake script", delegate
            {
                var scriptPath = GetVerifiedPathOfTestScript("task_writes_process_id.ps1");

                when("that script is invoked interactively", delegate
                {
                    ConsoleApplicationResult invocation = arrange(() =>
                    {
                        string moduleLocation = GetModuleLocation();
                        return(ProcessHandling.RunNoninteractiveConsoleProcess(PsakeUtil.GetPowershellCommand(moduleLocation, scriptPath, "Other")));
                    });

                    then("the other tasks output is seen", delegate
                    {
                        expect(() => invocation.ConsoleOutput.Any(l => l.Equals("Another task")));
                    });
                });
            });

            given("a different user account", delegate
            {
                given("a script that prints the accounts username", delegate
                {
                    when("that script is invoked interactively", delegate
                    {
                        then("the script output is the user account's username");

                        then("the script can interact with a desktop");
                    });
                });
            });

            given("a vhd image (64bit?  32bit?)", delegate
            {
                given("a psake script that writes its IP address and process id", delegate
                {
                    when("the script is invoked remotely", delegate
                    {
                        then("the reported ip address and process id are different");
                    });
                });
            });
        }