public void WorkingDirectoryDoesNotExistFailsWithInformativeMessage()
    {
        var workingDirectory = PathHelper.Combine(ExecutableHelper.GetCurrentDirectory(), Guid.NewGuid().ToString("N"));
        var executable       = ExecutableHelper.GetDotNetExecutable();

        var output = new StringBuilder();
        var args   = ExecutableHelper.GetExecutableArgs($" /targetpath {workingDirectory} ");

        var exitCode = ProcessHelper.Run(
            s => output.AppendLine(s),
            s => output.AppendLine(s),
            null,
            executable,
            args,
            ExecutableHelper.GetCurrentDirectory());

        exitCode.ShouldNotBe(0);
        var outputString = output.ToString();

        outputString.ShouldContain($"The working directory '{workingDirectory}' does not exist.", Case.Insensitive, outputString);
    }
Ejemplo n.º 2
0
    private static ExecutionResults ExecuteIn(ArgumentBuilder arguments,
                                              params KeyValuePair <string, string?>[] environments
                                              )
    {
        var executable = ExecutableHelper.GetExecutable();
        var output     = new StringBuilder();

        var environmentalVariables = new Dictionary <string, string?>
        {
            { TeamCity.EnvironmentVariableName, null },
            { AppVeyor.EnvironmentVariableName, null },
            { TravisCi.EnvironmentVariableName, null },
            { Jenkins.EnvironmentVariableName, null },
            { AzurePipelines.EnvironmentVariableName, null },
            { GitHubActions.EnvironmentVariableName, null },
            { SpaceAutomation.EnvironmentVariableName, null }
        };

        foreach (var(key, value) in environments)
        {
            if (environmentalVariables.ContainsKey(key))
            {
                environmentalVariables[key] = value;
            }
            else
            {
                environmentalVariables.Add(key, value);
            }
        }

        var exitCode = -1;

        try
        {
            var args = ExecutableHelper.GetExecutableArgs(arguments.ToString());

            Console.WriteLine("Executing: {0} {1}", executable, args);
            Console.WriteLine();

            exitCode = ProcessHelper.Run(
                s => output.AppendLine(s),
                s => output.AppendLine(s),
                null,
                executable,
                args,
                arguments.WorkingDirectory,
                environmentalVariables.ToArray());
        }
        catch (Exception exception)
        {
            // NOTE: It's the exit code and output from the process we want to test,
            //       not the internals of the ProcessHelper. That's why we're catching
            //       any exceptions here, because problems in the process being executed
            //       should be visible in the output or exit code. @asbjornu
            Console.WriteLine(exception);
        }

        Console.WriteLine("Output from gitversion tool");
        Console.WriteLine("-------------------------------------------------------");
        Console.WriteLine(output.ToString());
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("-------------------------------------------------------");

        if (arguments.LogFile.IsNullOrWhiteSpace() || !File.Exists(arguments.LogFile))
        {
            return(new ExecutionResults(exitCode, output.ToString(), null));
        }

        var logContents = File.ReadAllText(arguments.LogFile);

        Console.WriteLine("Log from gitversion tool");
        Console.WriteLine("-------------------------------------------------------");
        Console.WriteLine(logContents);
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("-------------------------------------------------------");

        return(new ExecutionResults(exitCode, output.ToString(), logContents));
    }