Esempio n. 1
0
        public void GitInstallation_CaseInsensitiveComparison()
        {
            List <Installation> list = new List <Installation>
            {
                new Installation(RuntimeContext.Default, @"C:\Program Files (x86)\Git", KnownDistribution.GitForWindows32v1),
                new Installation(RuntimeContext.Default, @"C:\Program Files (x86)\Git", KnownDistribution.GitForWindows32v2),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git", KnownDistribution.GitForWindows32v1),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git", KnownDistribution.GitForWindows32v2),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git", KnownDistribution.GitForWindows64v2),
                // ToLower versions
                new Installation(RuntimeContext.Default, @"C:\Program Files (x86)\Git".ToLower(), KnownDistribution.GitForWindows32v1),
                new Installation(RuntimeContext.Default, @"C:\Program Files (x86)\Git".ToLower(), KnownDistribution.GitForWindows32v2),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git".ToLower(), KnownDistribution.GitForWindows32v1),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git".ToLower(), KnownDistribution.GitForWindows32v2),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git".ToLower(), KnownDistribution.GitForWindows64v2),
                // ToUpper versions
                new Installation(RuntimeContext.Default, @"C:\Program Files (x86)\Git".ToUpper(), KnownDistribution.GitForWindows32v1),
                new Installation(RuntimeContext.Default, @"C:\Program Files (x86)\Git".ToUpper(), KnownDistribution.GitForWindows32v2),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git".ToUpper(), KnownDistribution.GitForWindows32v1),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git".ToUpper(), KnownDistribution.GitForWindows32v2),
                new Installation(RuntimeContext.Default, @"C:\Program Files\Git".ToUpper(), KnownDistribution.GitForWindows64v2),
            };

            HashSet <Installation> set = new HashSet <Installation>(list);

            Assert.Equal(15, list.Count);
            Assert.Equal(5, set.Count);

            Assert.Equal(6, list.Where(x => x.Version == KnownDistribution.GitForWindows32v1).Count());
            Assert.Equal(6, list.Where(x => x.Version == KnownDistribution.GitForWindows32v2).Count());
            Assert.Equal(3, list.Where(x => x.Version == KnownDistribution.GitForWindows64v2).Count());

            Assert.Equal(2, set.Where(x => x.Version == KnownDistribution.GitForWindows32v1).Count());
            Assert.Equal(2, set.Where(x => x.Version == KnownDistribution.GitForWindows32v2).Count());
            Assert.Single(set.Where(x => x.Version == KnownDistribution.GitForWindows64v2));

            foreach (var v in Enum.GetValues(typeof(KnownDistribution)))
            {
                KnownDistribution kgd = (KnownDistribution)v;

                var a = list.Where(x => x.Version == kgd);
                Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Cmd, a.First().Cmd)));
                Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Config, a.First().Config)));
                Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Git, a.First().Git)));
                Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Libexec, a.First().Libexec)));
                Assert.True(a.All(x => x != a.First() || Installation.PathComparer.Equals(x.Sh, a.First().Sh)));
            }
        }
Esempio n. 2
0
 public bool FindGitInstallation(string path, KnownDistribution distro, out Installation installation)
 {
     installation = new Installation(Context, path, distro);
     return(installation.IsValid());
 }
        internal Installation(string path, KnownDistribution version)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!CommonConfigPaths.ContainsKey(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version));
            }
            if (!CommonCmdPaths.ContainsKey(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version));
            }
            if (!CommonGitPaths.ContainsKey(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version));
            }
            if (!CommonLibexecPaths.ContainsKey(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version));
            }
            if (!CommonShPaths.ContainsKey(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version));
            }
            if (!CommonDocPaths.ContainsKey(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version));
            }

            path = path.TrimEnd('\\');

            // Make sure the GitExeName isn't included as a part of the path.
            if (path.EndsWith(AllVersionGitPath, StringComparison.OrdinalIgnoreCase))
            {
                path = path.Substring(0, path.Length - AllVersionGitPath.Length);
            }

            // Versions of git installation could have 2 binaries. One in the `bin` directory and the
            // other in the `cmd` directory. Handle both scenarios.

            if (path.EndsWith(AllVersionBinGitPath, StringComparison.OrdinalIgnoreCase))
            {
                path = path.Substring(0, path.Length - AllVersionBinGitPath.Length);
            }

            if (path.EndsWith(GitExeName, StringComparison.OrdinalIgnoreCase))
            {
                path = path.Substring(0, path.Length - GitExeName.Length);
            }

            // Trim off trailing '\' characters to increase compatibility.
            path = path.TrimEnd('\\');

            _path         = path;
            _distribution = version;
            _cmd          = null;
            _config       = null;
            _doc          = null;
            _git          = null;
            _libexec      = null;
            _sh           = null;
        }