Beispiel #1
0
        /// <summary>
        ///     Get the <see cref="Range"/> represented by the <see cref="InvalidProjectFileException"/>.
        /// </summary>
        /// <param name="invalidProjectFileException">
        ///     The <see cref="InvalidProjectFileException"/>.
        /// </param>
        /// <param name="xmlLocator">
        ///     The XML locator API (if available).
        /// </param>
        /// <returns>
        ///     The <see cref="Range"/>.
        /// </returns>
        public static Range GetRange(this InvalidProjectFileException invalidProjectFileException, XmlLocator xmlLocator)
        {
            if (invalidProjectFileException == null)
            {
                throw new ArgumentNullException(nameof(invalidProjectFileException));
            }

            Position startPosition = new Position(
                invalidProjectFileException.LineNumber,
                invalidProjectFileException.ColumnNumber
                );

            // Attempt to use the range of the actual XML that the exception refers to.
            XmlLocation location = xmlLocator?.Inspect(startPosition);

            if (location != null)
            {
                return(location.Node.Range);
            }

            // Otherwise, fall back to using the exception's declared end position...
            Position endPosition = new Position(
                invalidProjectFileException.EndLineNumber,
                invalidProjectFileException.EndColumnNumber
                );

            // ...although it's sometimes less reliable.
            if (endPosition == Position.Zero)
            {
                endPosition = startPosition;
            }

            return(new Range(startPosition, endPosition));
        }
        public void VerifyThrowsWhenResolverFailsToLoad()
        {
            SdkResolverLoader sdkResolverLoader = new MockSdkResolverLoader
            {
                LoadResolverAssemblyFunc      = (resolverPath, loggingContext, location) => typeof(SdkResolverLoader_Tests).GetTypeInfo().Assembly,
                FindPotentialSdkResolversFunc = rootFolder => new List <string>
                {
                    "myresolver.dll"
                },
                GetResolverTypesFunc = assembly => new[] { typeof(MockSdkResolverThatDoesNotLoad) }
            };

            InvalidProjectFileException exception = Should.Throw <InvalidProjectFileException>(() =>
            {
                sdkResolverLoader.LoadResolvers(_loggingContext, ElementLocation.EmptyLocation);
            });

            exception.Message.ShouldBe($"The SDK resolver type \"{nameof(MockSdkResolverThatDoesNotLoad)}\" failed to load. A8BB8B3131D3475D881ACD3AF8D75BD6");

            Exception innerException = exception.InnerException.ShouldBeOfType <Exception>();

            innerException.Message.ShouldBe(MockSdkResolverThatDoesNotLoad.ExpectedMessage);

            _logger.WarningCount.ShouldBe(0);
            _logger.ErrorCount.ShouldBe(0);
        }
Beispiel #3
0
        public void TestCtorProjectFile()
        {
            InvalidProjectFileException ipfe;
            string projectFile      = "projectFile";
            int    lineNumber       = 1;
            int    columnNumber     = 2;
            int    endLineNumber    = 3;
            int    endColumnNumber  = 4;
            string message          = "message";
            string errorSubcategory = "errorSubcategory";
            string errorCode        = "CS0000";
            string helpKeyword      = "helpKeyword";

            ipfe = new InvalidProjectFileException(projectFile, lineNumber, columnNumber, endLineNumber, endColumnNumber,
                                                   message, errorSubcategory, errorCode, helpKeyword);

            Assert.AreEqual(projectFile, ipfe.ProjectFile, "A1");
            Assert.AreEqual(lineNumber, ipfe.LineNumber, "A2");
            Assert.AreEqual(columnNumber, ipfe.ColumnNumber, "A3");
            Assert.AreEqual(endLineNumber, ipfe.EndLineNumber, "A4");
            Assert.AreEqual(endColumnNumber, ipfe.EndColumnNumber, "A5");
            Assert.AreEqual(message, ipfe.BaseMessage, "A6");
            Assert.AreEqual(message + "  " + projectFile, ipfe.Message, "A7");
            Assert.AreEqual(errorSubcategory, ipfe.ErrorSubcategory, "A8");
            Assert.AreEqual(errorCode, ipfe.ErrorCode, "A9");
            Assert.AreEqual(helpKeyword, ipfe.HelpKeyword, "A10");
        }
Beispiel #4
0
        public void TestGetObjectData2()
        {
            StreamingContext            sc = new StreamingContext();
            SerializationInfo           si = new SerializationInfo(typeof(InvalidProjectFileException), new FormatterConverter());
            InvalidProjectFileException ipfe;
            string projectFile      = "projectFile";
            int    lineNumber       = 1;
            int    columnNumber     = 2;
            int    endLineNumber    = 3;
            int    endColumnNumber  = 4;
            string message          = "message";
            string errorSubcategory = "errorSubcategory";
            string errorCode        = "CS0000";
            string helpKeyword      = "helpKeyword";

            ipfe = new InvalidProjectFileException(projectFile, lineNumber, columnNumber, endLineNumber, endColumnNumber,
                                                   message, errorSubcategory, errorCode, helpKeyword);
            ipfe.GetObjectData(si, sc);

            Assert.AreEqual(projectFile, si.GetString("projectFile"), "A1");
            Assert.AreEqual(lineNumber, si.GetInt32("lineNumber"), "A2");
            Assert.AreEqual(columnNumber, si.GetInt32("columnNumber"), "A3");
            Assert.AreEqual(endLineNumber, si.GetInt32("endLineNumber"), "A4");
            Assert.AreEqual(endColumnNumber, si.GetInt32("endColumnNumber"), "A5");
            Assert.AreEqual(message, si.GetString("Message"), "A6");
            Assert.AreEqual(errorSubcategory, si.GetString("errorSubcategory"), "A7");
            Assert.AreEqual(errorCode, si.GetString("errorCode"), "A8");
            Assert.AreEqual(helpKeyword, si.GetString("helpKeyword"), "A9");
        }
        public void VerifyWarningLoggedWhenResolverAssemblyCannotBeLoaded()
        {
            const string assemblyPath    = @"C:\foo\bar\myresolver.dll";
            const string expectedMessage = "91BF077D4E9646819DE7AB2CBA2637B6";

            SdkResolverLoader sdkResolverLoader = new MockSdkResolverLoader
            {
                LoadResolverAssemblyFunc = (resolverPath, loggingContext, location) =>
                {
                    throw new Exception(expectedMessage);
                },
                FindPotentialSdkResolversFunc = rootFolder => new List <string>
                {
                    assemblyPath,
                }
            };

            InvalidProjectFileException exception = Should.Throw <InvalidProjectFileException>(() =>
            {
                sdkResolverLoader.LoadResolvers(_loggingContext, ElementLocation.EmptyLocation);
            });

            exception.Message.ShouldBe($"The SDK resolver assembly \"{assemblyPath}\" could not be loaded. {expectedMessage}");

            Exception innerException = exception.InnerException.ShouldBeOfType <Exception>();

            innerException.Message.ShouldBe(expectedMessage);

            _logger.WarningCount.ShouldBe(0);
            _logger.ErrorCount.ShouldBe(0);
        }
Beispiel #6
0
        public void SerializeDeserialize()
        {
            InvalidProjectFileException e = new InvalidProjectFileException(
                "projectFile",
                1, 2, 3, 4,
                "message",
                "errorSubcategory",
                "errorCode",
                "helpKeyword");

            using (MemoryStream memstr = new MemoryStream())
            {
                BinaryFormatter frm = new BinaryFormatter();

                frm.Serialize(memstr, e);
                memstr.Position = 0;

                InvalidProjectFileException e2 = (InvalidProjectFileException)frm.Deserialize(memstr);

                Assert.Equal(e.ColumnNumber, e2.ColumnNumber);
                Assert.Equal(e.EndColumnNumber, e2.EndColumnNumber);
                Assert.Equal(e.EndLineNumber, e2.EndLineNumber);
                Assert.Equal(e.ErrorCode, e2.ErrorCode);
                Assert.Equal(e.ErrorSubcategory, e2.ErrorSubcategory);
                Assert.Equal(e.HasBeenLogged, e2.HasBeenLogged);
                Assert.Equal(e.HelpKeyword, e2.HelpKeyword);
                Assert.Equal(e.LineNumber, e2.LineNumber);
                Assert.Equal(e.Message, e2.Message);
                Assert.Equal(e.ProjectFile, e2.ProjectFile);
            }
        }
        public void SerializationBinary()
        {
            string       message      = "Message";
            string       projectFile  = @"c:\ProjectFile";
            MemoryStream memoryStream = null;
            InvalidProjectFileException invalidProjectFileException =
                new InvalidProjectFileException(projectFile, 1, 2, 3, 4, message, "errorSubCategory", "errorCode", "HelpKeyword");

            try
            {
                memoryStream = new MemoryStream();
                IFormatter binaryForamtter = new BinaryFormatter();
                binaryForamtter.Serialize(memoryStream, invalidProjectFileException);
                memoryStream.Position = 0; // reset pointer into stream for read
                Object returnObj = binaryForamtter.Deserialize(memoryStream);
                Assertion.Assert(returnObj is InvalidProjectFileException);
                InvalidProjectFileException outException = ((InvalidProjectFileException)returnObj);
                Assertion.AssertEquals(message + "  " + projectFile, outException.Message);
                Assertion.AssertEquals("errorSubCategory", outException.ErrorSubcategory);
                Assertion.AssertEquals("errorCode", outException.ErrorCode);
                Assertion.AssertEquals("HelpKeyword", outException.HelpKeyword);
                Assertion.AssertEquals(1, outException.LineNumber);
                Assertion.AssertEquals(2, outException.ColumnNumber);
                Assertion.AssertEquals(3, outException.EndLineNumber);
                Assertion.AssertEquals(4, outException.EndColumnNumber);
                Assertion.AssertEquals(projectFile, outException.ProjectFile);
            }
            finally
            {
                memoryStream.Close();
            }
        }
        public void CtorMessageArity1()
        {
            InvalidProjectFileException invalidProjectFileException = new InvalidProjectFileException("Message");

            Assertion.AssertEquals("Message", invalidProjectFileException.Message);
            Assertion.AssertEquals(0, invalidProjectFileException.LineNumber);
            Assertion.AssertEquals(0, invalidProjectFileException.ColumnNumber);
        }
        public void BaseMessage()
        {
            string message = "Message";
            InvalidProjectFileException invalidProjectFileException =
                new InvalidProjectFileException("ProjectFile", 0, 0, 0, 0, message, "errorSubCategory", "errorCode", "HelpKeyword");

            Assertion.AssertEquals(message, invalidProjectFileException.BaseMessage);
        }
        public void Ctor_Arity2InnerException()
        {
            InvalidProjectFileException invalidProjectFileException = new InvalidProjectFileException("Message", new Exception("MessageInner"));

            Assertion.AssertEquals("Message", invalidProjectFileException.Message);
            Assertion.AssertEquals("MessageInner", invalidProjectFileException.InnerException.Message);
            Assertion.AssertEquals(0, invalidProjectFileException.LineNumber);
            Assertion.AssertEquals(0, invalidProjectFileException.ColumnNumber);
        }
Beispiel #11
0
        public void TestCtorMessage()
        {
            InvalidProjectFileException ipfe;
            string message = "message";

            ipfe = new InvalidProjectFileException(message);

            Assert.AreEqual(message, ipfe.Message, "Message");
        }
 public void InvalidSolutionFilters(string slnfValue, string exceptionReason)
 {
     Assert.False(File.Exists("C:\\notAPath2\\MSBuild.Dev.sln"));
     using (TestEnvironment testEnvironment = TestEnvironment.Create())
     {
         TransientTestFolder         folder = testEnvironment.CreateFolder(createFolder: true);
         TransientTestFile           sln    = testEnvironment.CreateFile(folder, "Dev.sln");
         TransientTestFile           slnf   = testEnvironment.CreateFile(folder, "Dev.slnf", slnfValue.Replace(@"C:\\notAPath\\MSBuild.Dev.sln", sln.Path.Replace("\\", "\\\\")));
         InvalidProjectFileException e      = Should.Throw <InvalidProjectFileException>(() => SolutionFile.Parse(slnf.Path));
         e.HelpKeyword.ShouldBe(exceptionReason);
     }
 }
        public void CtorArity4_NullStringOtherParams()
        {
            string message = "Message";
            InvalidProjectFileException invalidProjectFileException =
                new InvalidProjectFileException(new XmlDocument().CreateElement("Name"), message, null, null, null);

            Assertion.AssertEquals(String.Empty, invalidProjectFileException.ProjectFile);

            // preserve a bug in Orcas SP1:  if projectFile is empty but non-null, extra spaces get added to the message.
            Assertion.AssertEquals(message + "  ", invalidProjectFileException.Message);
            Assertion.AssertEquals(null, invalidProjectFileException.ErrorSubcategory);
            Assertion.AssertEquals(null, invalidProjectFileException.ErrorCode);
            Assertion.AssertEquals(null, invalidProjectFileException.HelpKeyword);
        }
        public void CtorArity4_NullMessageString()
        {
            string message = null;
            InvalidProjectFileException invalidProjectFileException =
                new InvalidProjectFileException(new XmlDocument().CreateElement("name"), message, "subcategory", "ErrorCode", "HelpKeyword");

            Assertion.AssertEquals(String.Empty, invalidProjectFileException.ProjectFile);
            Assertion.AssertEquals(message, invalidProjectFileException.Message);
            Assertion.AssertNull(invalidProjectFileException.ErrorSubcategory);
            Assertion.AssertEquals("ErrorCode", invalidProjectFileException.ErrorCode);
            Assertion.AssertEquals("HelpKeyword", invalidProjectFileException.HelpKeyword);
            Assertion.AssertEquals(0, invalidProjectFileException.LineNumber);
            Assertion.AssertEquals(0, invalidProjectFileException.ColumnNumber);
        }
        public void CtorArity4()
        {
            string message = "Message";
            InvalidProjectFileException invalidProjectFileException =
                new InvalidProjectFileException(new XmlDocument().CreateElement("name"), message, "errorSubCategory", "errorCode", "HelpKeyword");

            Assertion.AssertEquals(String.Empty, invalidProjectFileException.ProjectFile);

            // preserve a bug in Orcas SP1:  if projectFile is empty but non-null, extra spaces get added to the message.
            Assertion.AssertEquals(message + "  ", invalidProjectFileException.Message);
            Assertion.AssertEquals("errorSubCategory", invalidProjectFileException.ErrorSubcategory);
            Assertion.AssertEquals("errorCode", invalidProjectFileException.ErrorCode);
            Assertion.AssertEquals("HelpKeyword", invalidProjectFileException.HelpKeyword);
            Assertion.AssertEquals(0, invalidProjectFileException.LineNumber);
            Assertion.AssertEquals(0, invalidProjectFileException.ColumnNumber);
        }
        public void Ctor_Arity9PositveInts()
        {
            string message     = "Message";
            string projectFile = @"c:\ProjectFile";
            InvalidProjectFileException invalidProjectFileException =
                new InvalidProjectFileException(projectFile, 1, 10, 11, 12, message, "errorSubCategory", "errorCode", "HelpKeyword");

            Assertion.AssertEquals(message + "  " + projectFile, invalidProjectFileException.Message);
            Assertion.AssertEquals("errorSubCategory", invalidProjectFileException.ErrorSubcategory);
            Assertion.AssertEquals("errorCode", invalidProjectFileException.ErrorCode);
            Assertion.AssertEquals("HelpKeyword", invalidProjectFileException.HelpKeyword);
            Assertion.AssertEquals(1, invalidProjectFileException.LineNumber);
            Assertion.AssertEquals(10, invalidProjectFileException.ColumnNumber);
            Assertion.AssertEquals(11, invalidProjectFileException.EndLineNumber);
            Assertion.AssertEquals(12, invalidProjectFileException.EndColumnNumber);
        }
        public void SerializationXML()
        {
            InvalidProjectFileException toolSetException = new InvalidProjectFileException("Message", new Exception("innerException"));
            MemoryStream memoryStream = null;

            try
            {
                memoryStream = new MemoryStream();
                XmlSerializer xs            = new XmlSerializer(typeof(InvalidProjectFileException));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                xs.Serialize(xmlTextWriter, toolSetException);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            }
            finally
            {
                memoryStream.Close();
            }
        }
        public async Task GivenAnInvalidProjectFileShutdownFails()
        {
            var exception = new InvalidProjectFileException("invalid project!");

            var resolverMock = new Mock <IRazorAssemblyResolver>(MockBehavior.Strict);

            resolverMock.Setup(r => r.EnumerateRazorToolAssemblies()).Throws(exception);

            var commandFactoryMock = new Mock <ICommandFactory>(MockBehavior.Strict);

            var manager = new RazorServerManager(resolverMock.Object, commandFactoryMock.Object);

            var result = await manager.ShutdownServerAsync();

            result.Kind.Should().Be(ResultKind.Failure);
            result.Message.Should().Be(exception.Message);
            result.Exception.Should().Be(exception);
        }
        public void ProjectWithEmptySdkNameInValidList()
        {
            _env.SetEnvironmentVariable("MSBuildSDKsPath", _testSdkRoot);

            const string invalidSdkName = "foo;  ;bar";

            InvalidProjectFileException exception = Assert.Throws <InvalidProjectFileException>(() =>
            {
                string content = $@"
                    <Project Sdk=""{invalidSdkName}"">
                        <PropertyGroup>
                            <UsedToTestIfImplicitImportsAreInTheCorrectLocation>null</UsedToTestIfImplicitImportsAreInTheCorrectLocation>
                        </PropertyGroup>
                    </Project>";

                Project project = new Project(ProjectRootElement.Create(XmlReader.Create(new StringReader(content))));
            });

            Assert.Equal("MSB4229", exception.ErrorCode);
        }
Beispiel #20
0
        public static IProjectStore CreateInstance(Microsoft.Expression.Framework.Documents.DocumentReference documentReference, IServiceProvider serviceProvider)
        {
            IProjectStore projectStore;

            try
            {
                projectStore = MSBuildBasedProjectStore.CreateInstance(documentReference, serviceProvider);
            }
            catch (InvalidProjectFileException invalidProjectFileException1)
            {
                InvalidProjectFileException invalidProjectFileException = invalidProjectFileException1;
                MigratingMSBuildStore       migratingMSBuildStore       = new MigratingMSBuildStore(documentReference, serviceProvider)
                {
                    LastError   = invalidProjectFileException,
                    NestedStore = XmlMSBuildProjectStore.CreateInstance(documentReference, serviceProvider)
                };
                projectStore = migratingMSBuildStore;
            }
            return(projectStore);
        }
        public void ProjectWithInvalidSdkName()
        {
            const string invalidSdkName = "SdkWithExtra/Slash/1.0.0";

            InvalidProjectFileException exception = Assert.Throws <InvalidProjectFileException>(() =>
            {
                using (new Helpers.TemporaryEnvironment("MSBuildSDKsPath", _testSdkRoot))
                {
                    string content = $@"
                    <Project Sdk=""{invalidSdkName}"">
                        <PropertyGroup>
                            <UsedToTestIfImplicitImportsAreInTheCorrectLocation>null</UsedToTestIfImplicitImportsAreInTheCorrectLocation>
                        </PropertyGroup>
                    </Project>";

                    Project project = new Project(ProjectRootElement.Create(XmlReader.Create(new StringReader(content))));
                }
            });

            Assert.Equal("MSB4229", exception.ErrorCode);
        }
        public void SerializationBinaryInnerException()
        {
            MemoryStream memoryStream = null;
            InvalidProjectFileException invalidProjectFileException =
                new InvalidProjectFileException("Message", new Exception("innerException"));

            try
            {
                memoryStream = new MemoryStream();
                IFormatter binaryForamtter = new BinaryFormatter();
                binaryForamtter.Serialize(memoryStream, invalidProjectFileException);
                memoryStream.Position = 0; // reset pointer into stream for read
                Object returnObj = binaryForamtter.Deserialize(memoryStream);
                Assertion.Assert(returnObj is InvalidProjectFileException);
                InvalidProjectFileException outException = ((InvalidProjectFileException)returnObj);
                Assertion.AssertEquals("Message", outException.Message);
                Assertion.AssertEquals("innerException", outException.InnerException.Message);
            }
            finally
            {
                memoryStream.Close();
            }
        }
        public void VerifyThrowsWhenResolverHasNoPublicConstructor()
        {
            SdkResolverLoader sdkResolverLoader = new MockSdkResolverLoader
            {
                LoadResolverAssemblyFunc      = (resolverPath, loggingContext, location) => typeof(SdkResolverLoader_Tests).GetTypeInfo().Assembly,
                FindPotentialSdkResolversFunc = rootFolder => new List <string>
                {
                    "myresolver.dll"
                },
                GetResolverTypesFunc = assembly => new[] { typeof(MockSdkResolverNoPublicConstructor) }
            };

            InvalidProjectFileException exception = Should.Throw <InvalidProjectFileException>(() =>
            {
                sdkResolverLoader.LoadResolvers(_loggingContext, ElementLocation.EmptyLocation);
            });

            exception.Message.ShouldStartWith($"The SDK resolver type \"{nameof(MockSdkResolverNoPublicConstructor)}\" failed to load.");

            exception.InnerException.ShouldBeOfType <MissingMethodException>();

            _logger.WarningCount.ShouldBe(0);
            _logger.ErrorCount.ShouldBe(0);
        }
 public void CtorMessageArity1_null()
 {
     string nullString = null; // typed null as to hit correct ctor overloaded.
     InvalidProjectFileException invalidProjectFileException  = new InvalidProjectFileException(nullString);
     InvalidProjectFileException invalidProjectFileException2 = new InvalidProjectFileException(nullString, new Exception("MessageInner"));
 }
 public void CtorArity4_EmptyMessageString()
 {
     string message = String.Empty;
     InvalidProjectFileException invalidProjectFileException =
         new InvalidProjectFileException(new XmlDocument().CreateElement("Name"), message, String.Empty, String.Empty, String.Empty);
 }
Beispiel #26
0
        public void TestGetObjectData1()
        {
            InvalidProjectFileException ipfe = new InvalidProjectFileException();

            ipfe.GetObjectData(null, new StreamingContext());
        }
Beispiel #27
0
 /// <summary>
 /// Log an invalid project file exception
 /// </summary>
 /// <param name="invalidProjectFileException">The invalid Project File Exception which is to be logged</param>
 internal void LogInvalidProjectFileError(InvalidProjectFileException invalidProjectFileException)
 {
     ErrorUtilities.VerifyThrow(_isValid, "must be valid");
     _loggingService.LogInvalidProjectFileError(_eventContext, invalidProjectFileException);
 }
 public void CtorDefault()
 {
     InvalidProjectFileException invalidProjectFileException = new InvalidProjectFileException();
 }
 public void CtorMessageArity1_empty()
 {
     InvalidProjectFileException invalidProjectFileException  = new InvalidProjectFileException(String.Empty);
     InvalidProjectFileException invalidProjectFileException2 = new InvalidProjectFileException(String.Empty, new Exception("MessageInner"));
 }
Beispiel #30
0
 private static ProjectGraphWithPredictionsResult <string> CreateFailureFromInvalidProjectFile(IReadOnlyDictionary <string, string> assemblyPathsToLoad, string locatedMsBuildPath, InvalidProjectFileException e)
 {
     return(ProjectGraphWithPredictionsResult.CreateFailure(
                GraphConstructionError.CreateFailureWithLocation(
                    new Location {
         File = e.ProjectFile, Line = e.LineNumber, Position = e.ColumnNumber
     },
                    e.Message),
                assemblyPathsToLoad,
                locatedMsBuildPath));
 }