private void CloneEmptyRepositoryWithCredentials(GitInstance git)
        {
            var result = RunGit(git, String.Format(String.Format("clone {0}", RepositoryUrlWithCredentials), RepositoryName), WorkingDirectory);

            Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryOutput], result.StdOut);
            Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryError], result.StdErr);
        }
        private static bool AnyCredentialHelperExists(GitInstance git)
        {
            IEnumerable <string> urls = new List <string>
            {
                string.Format(RepositoryUrlTemplate, string.Empty, string.Empty, string.Empty),
                string.Format(RepositoryUrlTemplate, Credentials, string.Empty, string.Empty),
            };

            foreach (var url in urls)
            {
                var exists = RunGit(git, string.Format("config --get-urlmatch credential.helper {0}", url), WorkingDirectory);

                /* Credential.helper is a multi-valued configuration setting. This means it you cannot rely on the value returned by any of the calls
                 * as it might return an empty value, which is a valid(! altough pointless) value where there is a second configured value
                 * in the same config file. But you can rely on the exit code. 0 means it found this key. */
                /* There seems to be a bug in git config --get-urlmatch where it will retun 0 eventhough there was no match.
                 * So we need to check the value. The good part is that if it is not set anywhere we get an empty string. If
                 * it is set somewhere the string contains at least "\r\n".
                 * See the bug report here http://article.gmane.org/gmane.comp.version-control.git/287740 */
                if (exists.ExitCode == 0 && exists.StdOut != "")
                {
                    Console.Write(string.Format("Stdout: {0}", exists.StdOut));
                    Console.Write(string.Format("Stderr: {0}", exists.StdErr));
                    Debug.Write(string.Format("Stdout: {0}", exists.StdOut));
                    Debug.Write(string.Format("Stderr: {0}", exists.StdErr));
                    return(true);
                }
            }

            return(false);
        }
        private void PushTag(GitInstance git)
        {
            RunGitOnRepo(git, "tag -a v1.4 -m \"my version 1.4\"").ExpectSuccess();
            var result = RunGitOnRepo(git, "push --tags origin");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PushTagError], RepositoryUrlWithCredentials), result.StdErr);
        }
        private void PushBranch(GitInstance git)
        {
            RunGitOnRepo(git, "checkout -b \"TestBranch\"").ExpectSuccess();
            var result = RunGitOnRepo(git, "push origin TestBranch");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PushBranchError], RepositoryUrlWithCredentials), result.StdErr);
        }
        private void PullBranch(GitInstance git)
        {
            var result = RunGitOnRepo(git, "pull origin TestBranch");

            Assert.AreEqual("Already up-to-date.\r\n", result.StdOut);
            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PullBranchError], RepositoryUrlWithoutCredentials), result.StdErr);
        }
        private void CreateAndPushFiles(GitInstance git)
        {
            CreateAndAddFiles(git);
            var result = RunGitOnRepo(git, "push origin master");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PushFilesSuccessError], RepositoryUrlWithCredentials), result.StdErr);
        }
        private void InitAndPushRepository(GitInstance git)
        {
            RunGitOnRepo(git, "init").ExpectSuccess();
            RunGitOnRepo(git, String.Format("remote add origin {0}", RepositoryUrlWithCredentials)).ExpectSuccess();
            var result = RunGitOnRepo(git, "push origin master");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PullRepositoryError], RepositoryUrlWithoutCredentials), result.StdErr);
        }
 private void CreateAndAddTestFiles(GitInstance git, int count)
 {
     foreach (var i in 0.To(count - 1))
     {
         CreateRandomFile(Path.Combine(RepositoryDirectory, "file" + i), 0);
     }
     RunGitOnRepo(git, "add .").ExpectSuccess();
     RunGitOnRepo(git, "commit -m \"Commit me! For linikfy tests: #1234\"").ExpectSuccess();
 }
        private void CreateAndAddFiles(GitInstance git)
        {
            CreateRandomFile(Path.Combine(RepositoryDirectory, "1.dat"), 10);
            CreateRandomFile(Path.Combine(RepositoryDirectory, "2.dat"), 1);
            Directory.CreateDirectory(Path.Combine(RepositoryDirectory, "SubDirectory"));
            CreateRandomFile(Path.Combine(RepositoryDirectory, "Subdirectory", "3.dat"), 20);
            CreateRandomFile(Path.Combine(RepositoryDirectory, "Subdirectory", "4.dat"), 15);

            RunGitOnRepo(git, "add .").ExpectSuccess();
            RunGitOnRepo(git, "commit -m \"Test Files Added\"").ExpectSuccess();
        }
        private void SetGlobalAnonPush(GitInstance git, bool allowAnonymousPush)
        {
            app.NavigateTo <SettingsController>(c => c.Index());
            var form  = app.FindFormFor <GlobalSettingsModel>();
            var field = form.Field(f => f.AllowAnonymousPush);

            ITH.SetCheckbox(field.Field, allowAnonymousPush);
            var languages = new SelectElement(form.Field(f => f.DefaultLanguage).Field);

            languages.SelectByValue("en-US");
            form.Submit();
            ITH.AssertThatNoValidationErrorOccurred();
        }
        private void PushFiles(GitInstance git, bool success)
        {
            var res = RunGitOnRepo(git, "push origin master");

            if (success)
            {
                Assert.AreEqual(string.Format(git.Resources[MsysgitResources.Definition.PushFilesSuccessError], RepositoryUrlWithoutCredentials + ".git"), res.StdErr);
            }
            else
            {
                Assert.AreEqual(string.Format(git.Resources[MsysgitResources.Definition.PushFilesFailError], BareUrl), res.StdErr);
            }
        }
        private void CloneRepoAnon(GitInstance git, bool success)
        {
            var result = RunGit(git, string.Format("clone {0}.git", RepositoryUrlWithoutCredentials), WorkingDirectory);

            if (success)
            {
                Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryOutput], result.StdOut);
                Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryError], result.StdErr);
            }
            else
            {
                Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryOutput], result.StdOut);
                Assert.AreEqual(string.Format(git.Resources[MsysgitResources.Definition.CloneRepositoryFailRequiresAuthError], BareUrl), result.StdErr);
            }
        }
        private void CreateAndPushFiles(GitInstance git)
        {
            CreateAndAddFiles(git);
            var result = RunGitOnRepo(git, "push origin master");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PushFilesSuccessError], RepositoryUrlWithCredentials), result.StdErr);
        }
 private void CreateAndAddTestFiles(GitInstance git, int count)
 {
     foreach (var i in 0.To(count - 1))
     {
         CreateRandomFile(Path.Combine(RepositoryDirectory, "file" + i), 0);
     }
     RunGitOnRepo(git, "add .").ExpectSuccess();
     RunGitOnRepo(git, "commit -m \"Commit me! For linikfy tests: #1234\"").ExpectSuccess();
 }
        private void CreateAndAddFiles(GitInstance git)
        {
            CreateRandomFile(Path.Combine(RepositoryDirectory, "1.dat"), 10);
            CreateRandomFile(Path.Combine(RepositoryDirectory, "2.dat"), 1);
            Directory.CreateDirectory(Path.Combine(RepositoryDirectory, "SubDirectory"));
            CreateRandomFile(Path.Combine(RepositoryDirectory, "Subdirectory", "3.dat"), 20);
            CreateRandomFile(Path.Combine(RepositoryDirectory, "Subdirectory", "4.dat"), 15);

            RunGitOnRepo(git, "add .").ExpectSuccess();
            RunGitOnRepo(git, "commit -m \"Test Files Added\"").ExpectSuccess();
        }
        private void CloneRepository(GitInstance git)
        {
            var result = RunGit(git, String.Format(String.Format("clone {0}", RepositoryUrlWithCredentials), RepositoryName), WorkingDirectory);

            Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneRepositoryOutput], result.StdOut);
            Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneRepositoryError], result.StdErr);
        }
 private void CloneRepoAnon(GitInstance git, bool success)
 {
     var result = RunGit(git, string.Format("clone {0}.git", RepositoryUrlWithoutCredentials), WorkingDirectory);
     if (success)
     {
         Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryOutput], result.StdOut);
         Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryError], result.StdErr);
     }
     else
     {
         Assert.AreEqual(git.Resources[MsysgitResources.Definition.CloneEmptyRepositoryOutput], result.StdOut);
         Assert.AreEqual(string.Format(git.Resources[MsysgitResources.Definition.CloneRepositoryFailRequiresAuthError], BareUrl), result.StdErr);
     }
 }
        private static GitResult RunGit(GitInstance git, string arguments, string workingDirectory, int timeout = 30000 /* milliseconds */)
        {
            // When a git version supports overwriting multi-value config files values this should be uncommented to make
            // all tests runnable on systems that have a credential.helper configured.
            // arguments = "-c credential.helper=\"store --file=bonobo.randomstring.credentials.txt\" " + arguments;
            Console.WriteLine("About to run '{0}' with args '{1}' in '{2}'", git, arguments, workingDirectory);
            Debug.WriteLine("About to run '{0}' with args '{1}' in '{2}'", git, arguments, workingDirectory);

            // http://stackoverflow.com/a/7608823/551045 and http://stackoverflow.com/a/22956924/551045
            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
            using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
            {
                using (var process = new Process())
                {
                    process.StartInfo.FileName = git.GitExe;
                    process.StartInfo.WorkingDirectory = workingDirectory;
                    process.StartInfo.Arguments = arguments;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;

                    StringBuilder output = new StringBuilder();
                    StringBuilder error = new StringBuilder();

                    process.OutputDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            outputWaitHandle.Set();
                        }
                        else
                        {
                            output.AppendLine(e.Data);
                        }
                    };
                    process.ErrorDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            errorWaitHandle.Set();
                        }
                        else
                        {
                            error.AppendLine(e.Data);
                        }
                    };

                    process.Start();

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    if (process.WaitForExit(timeout) &&
                        outputWaitHandle.WaitOne(timeout) &&
                        errorWaitHandle.WaitOne(timeout))
                    {
                        var strout = output.ToString();
                        var strerr = error.ToString();

                        Console.WriteLine("Stdout: {0}", output);
                        Console.WriteLine("Stderr: {0}", error);

                        return new GitResult {StdErr = strerr, StdOut = strout, ExitCode = process.ExitCode, Resources = git.Resources};
                    }
                    else
                    {
                        Assert.Fail(string.Format("Runing command '{0} {1}' timed out! Timeout {2} seconds.", git, arguments, timeout));
                        return new GitResult() { StdErr = null, StdOut = null, ExitCode = -1 };
                    }
                }
            }
        }
 private GitResult RunGitOnRepo(GitInstance git, string arguments, int timeout = 30000 /* milliseconds */)
 {
     return RunGit(git, arguments, RepositoryDirectory, timeout);
 }
 private void InitRepository(GitInstance git)
 {
     RunGitOnRepo(git, "init").ExpectSuccess();
     RunGitOnRepo(git, String.Format("remote add origin {0}", RepositoryUrlWithCredentials)).ExpectSuccess();
 }
 private void CreateIdentity(GitInstance git)
 {
     RunGitOnRepo(git, "config user.name \"McFlono McFloonyloo\"").ExpectSuccess();
     RunGitOnRepo(git, "config user.email \"[email protected]\"").ExpectSuccess();
 }
 private void CreateIdentity(GitInstance git)
 {
     RunGitOnRepo(git, "config user.name \"McFlono McFloonyloo\"").ExpectSuccess();
     RunGitOnRepo(git, "config user.email \"[email protected]\"").ExpectSuccess();
 }
        private void PullTag(GitInstance git)
        {
            var result = RunGitOnRepo(git, "fetch");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PullTagError], RepositoryUrlWithoutCredentials), result.StdErr);
        }
 private void InitRepository(GitInstance git)
 {
     RunGitOnRepo(git, "init").ExpectSuccess();
     RunGitOnRepo(git, String.Format("remote add origin {0}", RepositoryUrlWithCredentials)).ExpectSuccess();
 }
        private void InitAndPushRepository(GitInstance git)
        {
            RunGitOnRepo(git, "init").ExpectSuccess();
            RunGitOnRepo(git, String.Format("remote add origin {0}", RepositoryUrlWithCredentials)).ExpectSuccess();
            var result = RunGitOnRepo(git, "push origin master");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PullRepositoryError], RepositoryUrlWithoutCredentials), result.StdErr);
        }
        private void PullTag(GitInstance git)
        {
            var result = RunGitOnRepo(git, "fetch");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PullTagError], RepositoryUrlWithoutCredentials), result.StdErr);
        }
        private void PullBranch(GitInstance git)
        {
            var result = RunGitOnRepo(git, "pull origin TestBranch");

            Assert.AreEqual("Already up-to-date.\r\n", result.StdOut);
            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PullBranchError], RepositoryUrlWithoutCredentials), result.StdErr);
        }
 private void PushFiles(GitInstance git, bool success)
 {
     var res = RunGitOnRepo(git, "push origin master");
     if (success)
     {
         Assert.AreEqual(string.Format(git.Resources[MsysgitResources.Definition.PushFilesSuccessError], RepositoryUrlWithoutCredentials + ".git"), res.StdErr);
     }
     else
     {
         Assert.AreEqual(string.Format(git.Resources[MsysgitResources.Definition.PushFilesFailError], BareUrl), res.StdErr);
     }
 }
        private void PushBranch(GitInstance git)
        {
            RunGitOnRepo(git, "checkout -b \"TestBranch\"").ExpectSuccess();
            var result = RunGitOnRepo(git, "push origin TestBranch");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PushBranchError], RepositoryUrlWithCredentials), result.StdErr);
        }
 private GitResult RunGitOnRepo(GitInstance git, string arguments, int timeout = 30000 /* milliseconds */)
 {
     return(RunGit(git, arguments, RepositoryDirectory, timeout));
 }
        private void PushTag(GitInstance git)
        {
            RunGitOnRepo(git, "tag -a v1.4 -m \"my version 1.4\"").ExpectSuccess();
            var result = RunGitOnRepo(git, "push --tags origin");

            Assert.AreEqual(String.Format(git.Resources[MsysgitResources.Definition.PushTagError], RepositoryUrlWithCredentials), result.StdErr);
        }
        private static GitResult RunGit(GitInstance git, string arguments, string workingDirectory, int timeout = 30000 /* milliseconds */)
        {
            // When a git version supports overwriting multi-value config files values this should be uncommented to make
            // all tests runnable on systems that have a credential.helper configured.
            // arguments = "-c credential.helper=\"store --file=bonobo.randomstring.credentials.txt\" " + arguments;
            Console.WriteLine("About to run '{0}' with args '{1}' in '{2}'", git, arguments, workingDirectory);
            Debug.WriteLine("About to run '{0}' with args '{1}' in '{2}'", git, arguments, workingDirectory);

            // http://stackoverflow.com/a/7608823/551045 and http://stackoverflow.com/a/22956924/551045
            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                {
                    using (var process = new Process())
                    {
                        process.StartInfo.FileName               = git.GitExe;
                        process.StartInfo.WorkingDirectory       = workingDirectory;
                        process.StartInfo.Arguments              = arguments;
                        process.StartInfo.UseShellExecute        = false;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardError  = true;

                        StringBuilder output = new StringBuilder();
                        StringBuilder error  = new StringBuilder();

                        process.OutputDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                outputWaitHandle.Set();
                            }
                            else
                            {
                                output.AppendLine(e.Data);
                            }
                        };
                        process.ErrorDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                errorWaitHandle.Set();
                            }
                            else
                            {
                                error.AppendLine(e.Data);
                            }
                        };

                        process.Start();

                        process.BeginOutputReadLine();
                        process.BeginErrorReadLine();

                        if (process.WaitForExit(timeout) &&
                            outputWaitHandle.WaitOne(timeout) &&
                            errorWaitHandle.WaitOne(timeout))
                        {
                            var strout = output.ToString();
                            var strerr = error.ToString();

                            Console.WriteLine("Stdout: {0}", output);
                            Console.WriteLine("Stderr: {0}", error);

                            return(new GitResult {
                                StdErr = strerr, StdOut = strout, ExitCode = process.ExitCode, Resources = git.Resources
                            });
                        }
                        else
                        {
                            Assert.Fail(string.Format("Runing command '{0} {1}' timed out! Timeout {2} seconds.", git, arguments, timeout));
                            return(new GitResult()
                            {
                                StdErr = null, StdOut = null, ExitCode = -1
                            });
                        }
                    }
                }
        }
 private void SetGlobalAnonPush(GitInstance git, bool allowAnonymousPush)
 {
     app.NavigateTo<SettingsController>(c => c.Index());
     var form = app.FindFormFor<GlobalSettingsModel>();
     var field =  form.Field(f => f.AllowAnonymousPush);
     ITH.SetCheckbox(field.Field, allowAnonymousPush);
     var languages = new SelectElement(form.Field(f => f.DefaultLanguage).Field);
     languages.SelectByValue("en-US");
     form.Submit();
     ITH.AssertThatNoValidationErrorOccurred();
 }
        private static bool AnyCredentialHelperExists(GitInstance git)
        {
            IEnumerable<string> urls = new List<string>
            {
                string.Format(RepositoryUrlTemplate, string.Empty, string.Empty, string.Empty),
                string.Format(RepositoryUrlTemplate, AdminCredentials, string.Empty, string.Empty),
            };

            foreach (var url in urls)
            {
                var exists = RunGit(git, string.Format("config --get-urlmatch credential.helper {0}", url), WorkingDirectory);
                /* Credential.helper is a multi-valued configuration setting. This means it you cannot rely on the value returned by any of the calls
                 * as it might return an empty value, which is a valid(! altough pointless) value where there is a second configured value
                 * in the same config file. But you can rely on the exit code. 0 means it found this key. */
                /* There seems to be a bug in git config --get-urlmatch where it will retun 0 eventhough there was no match.
                 * So we need to check the value. The good part is that if it is not set anywhere we get an empty string. If
                 * it is set somewhere the string contains at least "\r\n".
                 * See the bug report here http://article.gmane.org/gmane.comp.version-control.git/287740 */
                if (exists.ExitCode == 0 && exists.StdOut != "")
                {
                    Console.Write(string.Format("Stdout: {0}", exists.StdOut));
                    Console.Write(string.Format("Stderr: {0}", exists.StdErr));
                    Debug.Write(string.Format("Stdout: {0}", exists.StdOut));
                    Debug.Write(string.Format("Stderr: {0}", exists.StdErr));
                    return true;
                }
            }

            return false;
        }