public async void RunsEvenIfExactVersionDirectoryExistsIfForced()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, DefaultTestRelease);

            parms.Force = true;
            await InstallsMocked(parms, forcedOverwrite : true);
        }
        public async void ThrowsIfExtractDoesNotComplete()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, DefaultTestRelease);
            var exc   = await Assert.ThrowsAsync <DotNetCoreInstallerException>(async() => await InstallsMocked(parms, failExtract: true));

            Assert.Contains("failed to install", exc.Message);
        }
        public async void RunsEvenIfCompatibleVersionDirectoryExistsIfForced()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, "2.0");

            parms.Force = true;
            await InstallsMocked(parms, forcedOverwrite : true, expectsLatestVersionLookup : true);
        }
        public async void ThrowsWithBadVersionNumber(string badVersion)
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, badVersion);
            var exc   = await Assert.ThrowsAsync <DotNetCoreInstallerException>(async() => await InstallsMocked(parms, failExtract: true));

            Assert.Contains("version", exc.Message);
        }
        public async void InstallsAspNetCoreExplicitly()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x64, DefaultTestRelease)
            {
                Runtime = DotNetRuntime.AspNetCore
            };

            await InstallsMocked(parms);
        }
        public async void ExitsEarlyIfDirectoryExistsWithAnyCompatibleVersion()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, "2.0");

            var logMock = new Mock <Action <string> >();

            parms.Log = logMock.Object;

            await InstallsMocked(parms, earlyExit : true);

            logMock.Verify(l => l(It.Is <string>(s => s.Contains("is already installed"))));
        }
        public async void InstallsForRealWithLatestVersion()
        {
            var platform = DotNetPlatform.Windows;
            var arch     = DotNetArchitecture.x64;
            var version  = "2.1";

            var parms     = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x64, version);
            var installer = new DotNetCoreInstaller();
            await installer.InstallStandalone(parms);

            var matchingDirectories = Directory.GetDirectories(Path.Combine(InstallDir, "shared", "Microsoft.NETCore.App"), version + "*");

            Assert.True(matchingDirectories.Length == 1 && Directory.Exists(matchingDirectories[0]));
        }
        public async void InstallsForRealWithSpecificVersion()
        {
            var platform = DotNetPlatform.Windows;
            var arch     = DotNetArchitecture.x64;
            var version  = "2.1.4";

            var parms     = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x64, version);
            var installer = new DotNetCoreInstaller();
            await installer.InstallStandalone(parms);

            var expectedInstallPath = Path.Combine(InstallDir, "shared", "Microsoft.NETCore.App", version);

            Assert.True(Directory.Exists(expectedInstallPath));
        }
        public async void HonorsArchitecture()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, DefaultTestRelease);

            await InstallsMocked(parms);
        }
        private async Task InstallsMocked(
            DotNetDistributionParameters parms,
            bool earlyExit = false, bool forcedOverwrite = false, bool failExtract = false, bool expectsLatestVersionLookup = false)
        {
            var platform = parms.Platform;
            var arch     = parms.Architecture;
            var version  = parms.Version;

            var mockDownloader = CreateMockDownloader();
            var mockExtractor  = CreateMockExtractor();

            var mockFilesystem = CreateMockFilesystem();

            var mockFile      = Mock.Get(mockFilesystem.Object.File);
            var mockDirectory = Mock.Get(mockFilesystem.Object.Directory);

            var installer = new DotNetCoreInstaller(mockDownloader.Object, mockExtractor.Object, mockFilesystem.Object);

            var archiveExt = (parms.Platform == DotNetPlatform.Windows) ? "zip" : "tar.gz";

            var assetDir            = (parms.Runtime == DotNetRuntime.NETCore) ? "Microsoft.NETCore.App" : "Microsoft.AspNetCore.App";
            var expectedPackageRoot = Path.Combine(InstallDir, "shared", assetDir);

            mockDirectory.Setup(d => d.Exists(expectedPackageRoot)).Returns(true); // Just test the true case; the false case is uninteresting and the code is covered by other tests anyway.
            mockDirectory.Setup(d => d.GetDirectories(expectedPackageRoot, parms.Version + "*")).Returns(
                earlyExit ? new [] { version + ".3" } : new string[0]
                );

            if (expectsLatestVersionLookup)
            {
                var expectedLatestUri = (parms.Runtime == DotNetRuntime.NETCore)
                    ? new Uri($"{DotNetDistributionParameters.DefaultUncachedFeed}/Runtime/{version}/latest.version")
                    : new Uri($"{DotNetDistributionParameters.DefaultUncachedFeed}/aspnetcore/Runtime/{version}/latest.version");

                mockDownloader.Setup(d => d(expectedLatestUri, It.IsAny <string>())).Returns(Task.CompletedTask); // Download latest.version File

                version = version + ".3";                                                                         // Pretend that the latest of, e.g. 2.2, is 2.2.3.

                mockFile.Setup(f => f.ReadAllText(It.IsAny <string>())).Returns($"some-hash\n{version}");
            }

            var expectedUri = (parms.Runtime == DotNetRuntime.NETCore)
                ? new Uri($"{DotNetDistributionParameters.DefaultCachedFeed}/Runtime/{version}/dotnet-runtime-{version}-{platform}-{arch}.{archiveExt}")
                : new Uri($"{DotNetDistributionParameters.DefaultCachedFeed}/aspnetcore/Runtime/{version}/aspnetcore-runtime-{version}-{platform}-{arch}.{archiveExt}");
            var expectedInstallPath = Path.Combine(InstallDir, "shared", assetDir, version);

            if (earlyExit)
            {
                mockDirectory.Setup(d => d.Exists(expectedInstallPath)).ReturnsInOrder(true);                               // Checking for version directory.
            }
            else
            {
                bool initiallyPresent = forcedOverwrite ? true : false;
                bool finallyPresent   = failExtract ? false : true;

                mockDirectory.Setup(d => d.Exists(expectedInstallPath)).ReturnsInOrder(initiallyPresent, finallyPresent);   // Checking for version directory.
                mockDirectory.Setup(d => d.CreateDirectory(parms.InstallDir)).Returns(value: null);                         // Creating the root install dir.
                mockDownloader.Setup(d => d(expectedUri, It.IsAny <string>())).Returns(Task.CompletedTask);                 // Download
                mockExtractor.Setup(e => e(It.IsAny <string>(), parms.InstallDir, forcedOverwrite))                         // Extract
                .Returns(Task.CompletedTask);
                mockFile.Setup(f => f.Delete(It.IsAny <string>()));                                                         // Deleting the temporary zip file.
            }

            await installer.InstallStandalone(parms);

            mockDownloader.VerifyAll();
            mockExtractor.VerifyAll();
        }
        public async void LooksUpLatestVersionInSeriesWhenRequested()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, "2.0");

            await InstallsMocked(parms, expectsLatestVersionLookup : true);
        }
        public async void HonorsVersion()
        {
            var parms = new DotNetDistributionParameters(InstallDir, DotNetPlatform.Windows, DotNetArchitecture.x86, "2.0.4");

            await InstallsMocked(parms);
        }