public void GetEnvironmentValidPaths()
        {
            string pathVariable = string.Join(_separator, GetValidPaths().Concat(GetInvalidPaths()));

            _environment.GetEnvironmentVariable("PATH").Returns(pathVariable);

            var validPaths = _provider.GetEnvironmentValidPaths();

            CollectionAssert.AreEqual(GetValidPaths().ToArray(), validPaths.ToArray());
        }
示例#2
0
        public static bool TryFindShellPath(string shell, [NotNullWhen(returnValue: true)] out string?shellPath)
        {
            try
            {
                shellPath = Path.Combine(EnvironmentAbstraction.GetEnvironmentVariable("ProgramW6432"), "Git", shell);
                if (File.Exists(shellPath))
                {
                    return(true);
                }

                shellPath = Path.Combine(EnvironmentAbstraction.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Git", shell);
                if (File.Exists(shellPath))
                {
                    return(true);
                }

                shellPath = Path.Combine(AppSettings.GitBinDir, shell);
                if (File.Exists(shellPath))
                {
                    return(true);
                }

                if (TryFindFullPath(shell, out shellPath))
                {
                    return(true);
                }
            }
            catch
            {
                // do nothing
            }

            shellPath = null;
            return(false);
        }
        /// <summary>
        /// Sets <c>PATH</c>, <c>HOME</c>, <c>TERM</c> and <c>SSH_ASKPASS</c> environment variables
        /// for the current process.
        /// </summary>
        public static void SetEnvironmentVariables()
        {
            // PATH variable

            if (!string.IsNullOrEmpty(AppSettings.GitBinDir))
            {
                // Ensure the git binary dir is on the path
                string path = Env.GetEnvironmentVariable("PATH");

                if (path == null)
                {
                    Env.SetEnvironmentVariable("PATH", AppSettings.GitBinDir);
                }
                else if (!path.Contains(AppSettings.GitBinDir))
                {
                    Env.SetEnvironmentVariable("PATH", $"{path}{Path.PathSeparator}{AppSettings.GitBinDir}");
                }
            }

            // HOME variable
            Env.SetEnvironmentVariable("HOME", ComputeHomeLocation());

            // TERM variable

            // to prevent from leaking processes see issue #1092 for details
            Env.SetEnvironmentVariable("TERM", "msys");

            // SSH_ASKPASS variable

            if (EnvUtils.RunningOnWindows())
            {
                string sshAskPass = Path.Combine(AppSettings.GetInstallDir(), "GitExtSshAskPass.exe");

                if (File.Exists(sshAskPass))
                {
                    Env.SetEnvironmentVariable("SSH_ASKPASS", sshAskPass);
                }
            }
            else if (string.IsNullOrEmpty(Env.GetEnvironmentVariable("SSH_ASKPASS")))
            {
                Env.SetEnvironmentVariable("SSH_ASKPASS", "ssh-askpass");
            }

            return;

            string ComputeHomeLocation()
            {
                if (!string.IsNullOrEmpty(AppSettings.CustomHomeDir))
                {
                    return(AppSettings.CustomHomeDir);
                }

                if (AppSettings.UserProfileHomeDir)
                {
                    return(Env.GetEnvironmentVariable("USERPROFILE"));
                }

                return(GetDefaultHomeDir());
            }
        }
        public void Find_should_return_GIT_SSH_environment_variable_if_set()
        {
            const string path = @"C:\somedir\ssh.exe";

            _environment.GetEnvironmentVariable("GIT_SSH", EnvironmentVariableTarget.Process).Returns(path);
            var sshPathLocator = new SshPathLocator(_fileSystem, _environment);

            sshPathLocator.Find(@"c:\someotherdir").Should().Be(path);
        }
        /// <summary>
        /// Gets the git SSH command;
        /// If the environment variable is not set, will try to find ssh.exe in git installation directory;
        /// If not found, will return "".
        /// </summary>
        public string Find(string gitBinDirectory)
        {
            var ssh = _environment.GetEnvironmentVariable("GIT_SSH", EnvironmentVariableTarget.Process);

            if (string.IsNullOrEmpty(ssh))
            {
                ssh = GetSshFromGitDir(gitBinDirectory);
            }

            return(ssh ?? "");
        }
        /// <summary>
        /// Gets the git SSH command.
        /// If the environment variable is not set, will try to find ssh.exe in git installation directory.
        /// If not found, will return "".
        /// </summary>
        public string Find(string gitBinDirectory)
        {
            var ssh = _environment.GetEnvironmentVariable("GIT_SSH", EnvironmentVariableTarget.Process) ?? "";

            if (!string.IsNullOrEmpty(ssh))
            {
                // OpenSSH uses empty path, compatibility with path set in 3.4
                var path = GetSshFromGitDir(gitBinDirectory);
                if (path == ssh)
                {
                    ssh = "";
                }
            }

            return(ssh);
        }
        /// <summary>
        /// Gets the list of paths defined under %PATH% environment variable.
        /// </summary>
        /// <returns>The list of paths defined under %PATH% environment variable.</returns>
        public IEnumerable <string> GetEnvironmentPaths()
        {
            string pathVariable = _environment.GetEnvironmentVariable("PATH");

            if (string.IsNullOrWhiteSpace(pathVariable))
            {
                yield break;
            }

            foreach (string rawdir in pathVariable.Split(EnvUtils.EnvVariableSeparator))
            {
                string dir = rawdir;
                // Usually, paths with spaces are not quoted on %PATH%, but it's well possible, and .NET won't consume a quoted path
                // This does not handle the full grammar of the %PATH%, but at least prevents Illegal Characters in Path exceptions (see #2924)
                dir = dir.Trim(new char[] { ' ', '"', '\t' });
                if (dir.Length == 0)
                {
                    continue;
                }
                yield return(dir);
            }
        }