public void ClientIsNotCachedIfExceptionOccursWhen()
        {
            // Arrange
            var uri         = new Uri(@"http://nuget.org");
            var webResponse = new Mock <WebResponse>(MockBehavior.Strict);

            webResponse.Setup(s => s.ResponseUri).Returns(uri)
            .Verifiable();
            var client = new Mock <HttpClient>(MockBehavior.Strict, uri);

            client.Setup(s => s.GetResponse()).Returns(webResponse.Object);
            var memoryCache = new MemoryCache();

            var redirectedClient1 = new Mock <RedirectedHttpClient>(uri, memoryCache)
            {
                CallBase = true
            };

            redirectedClient1.Setup(r => r.EnsureClient()).Throws(new Exception("Na na na na na")).Verifiable();

            var redirectedClient2 = new Mock <RedirectedHttpClient>(uri, memoryCache)
            {
                CallBase = true
            };

            redirectedClient2.Setup(r => r.EnsureClient()).Returns(client.Object);

            // Act and Assert
            ExceptionAssert.Throws <Exception>(() => redirectedClient1.Object.CachedClient.ToString(), "Na na na na na");
            var result2 = redirectedClient2.Object.CachedClient;

            // Assert
            Assert.Same(client.Object, result2);
            redirectedClient1.Verify(r => r.EnsureClient(), Times.Once());
            redirectedClient2.Verify(r => r.EnsureClient(), Times.Once());
        }
Example #2
0
 public void UserSettings_CallingCtroWithNullFileSystemWithThrowException()
 {
     // Act & Assert
     ExceptionAssert.Throws <ArgumentNullException>(() => new UserSettings(null));
 }
Example #3
0
        public void ReThrowWithMeaningfulErrorMessageWhenXdtFileHasSyntaxError()
        {
            // Arrange
            var mockProjectSystem = new MockProjectSystem();
            var mockRepository    = new MockPackageRepository();

            mockProjectSystem.AddFile("web.config",
                                      @"<configuration>
    <system.web>
        <compilation debug=""true"" />
    </system.web>
</configuration>
".AsStream());
            var projectManager = new ProjectManager(mockRepository, new DefaultPackagePathResolver(new MockProjectSystem()), mockProjectSystem, new MockPackageRepository());

            var package = new Mock <IPackage>();

            package.Setup(m => m.Id).Returns("A");
            package.Setup(m => m.Version).Returns(new SemanticVersion("1.0"));
            package.Setup(m => m.Listed).Returns(true);

            var file = new Mock <IPackageFile>();

            file.Setup(m => m.Path).Returns(@"content\web.config.install.xdt");
            file.Setup(m => m.EffectivePath).Returns("web.config.install.xdt");
            file.Setup(m => m.GetStream()).Returns(() =>
                                                   @"<configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
    <system.web>
        <compilation xd:Locator=""Condition('@debug=true')"" debug=""false"" xdt:Transform=""Replace"" />
    </system.web>
</configuration>".AsStream());

            var file2 = new Mock <IPackageFile>();

            file2.Setup(m => m.Path).Returns(@"content\web.config.uninstall.xdt");
            file2.Setup(m => m.EffectivePath).Returns("web.config.uninstall.xdt");
            file2.Setup(m => m.GetStream()).Returns(() =>
                                                    @"<configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform"">
    <system.web>
        <compilation xdt:Locator=""Match(debug)"" debug=""false"" xdt:Transform=""Remove"" />
    </system.web>
</configuration>".AsStream());

            package.Setup(m => m.GetFiles()).Returns(new[] { file.Object, file2.Object });
            mockRepository.AddPackage(package.Object);

            // Act
            ExceptionAssert.Throws <InvalidDataException>(
                () => projectManager.AddPackageReference("A"),
                @"An error occurred while applying transformation to 'web.config' in project 'x:\MockFileSystem': 'xd' is an undeclared prefix. Line 3, position 22.");

            // Assert
            Assert.False(mockProjectSystem.FileExists("web.config.install.xdt"));
            Assert.False(mockProjectSystem.FileExists("web.config.uninstall.xdt"));
            Assert.True(mockProjectSystem.FileExists("web.config"));
            Assert.Equal(
                @"<configuration>
    <system.web>
        <compilation debug=""true"" />
    </system.web>
</configuration>
", mockProjectSystem.OpenFile("web.config").ReadToEnd());
        }
 public void ResolveUnknownPackageThrows()
 {
     ExceptionAssert.Throws <InvalidOperationException>(
         () => PackageHelper.ResolvePackage(new MockPackageRepository(), new MockPackageRepository(), "elmah", null, allowPrereleaseVersions: false),
         "Unable to find package 'elmah'.");
 }
 public void ResolveSpecificVersionOfUnknownPackageThrows()
 {
     ExceptionAssert.Throws <InvalidOperationException>(
         () => PackageHelper.ResolvePackage(new MockPackageRepository(), new MockPackageRepository(), "elmah", new SemanticVersion("1.1"), allowPrereleaseVersions: false),
         "Unable to find version '1.1' of package 'elmah'.");
 }
 public void NullSettingsThrowsIfWriteOperationMethodsAreInvoked(Assert.ThrowsDelegate throwsDelegate, string methodName)
 {
     // Act and Assert
     ExceptionAssert.Throws <InvalidOperationException>(throwsDelegate,
                                                        String.Format("\"{0}\" cannot be called on a NullSettings. This may be caused on account of insufficient permissions to read or write to \"%AppData%\\NuGet\\NuGet.config\".", methodName));
 }