public void TreatWarningsAsErrorsWhenBuildingSameProjectMultipleTimes() { using (TestEnvironment testEnvironment = TestEnvironment.Create(_output)) { TransientTestProjectWithFiles project2 = testEnvironment.CreateTestProjectWithFiles($@" <Project xmlns=""msbuildnamespace""> <PropertyGroup> <MSBuildWarningsAsErrors>{ExpectedEventCode}</MSBuildWarningsAsErrors> </PropertyGroup> <Target Name=""Build""> <MSBuild Projects=""$(MSBuildThisFileFullPath)"" Targets=""AnotherTarget"" /> </Target> <Target Name=""AnotherTarget""> <Warning Text=""{ExpectedEventMessage}"" Code=""{ExpectedEventCode}"" /> </Target> </Project>"); TransientTestProjectWithFiles project1 = testEnvironment.CreateTestProjectWithFiles($@" <Project xmlns=""msbuildnamespace""> <Target Name=""Build""> <MSBuild Projects=""{project2.ProjectFile}"" Targets=""Build"" /> </Target> </Project>"); MockLogger logger = project1.BuildProjectExpectFailure(); VerifyBuildErrorEvent(logger); } }
public void VariousParameterTypesCanBeTransmittedToAndRecievedFromTaskHost() { using TestEnvironment env = TestEnvironment.Create(_output); string projectContents = $@" <Project> <UsingTask TaskName=""{nameof(TaskBuilderTestTask)}"" AssemblyFile=""{typeof(TaskBuilderTestTask).Assembly.Location}"" TaskFactory=""TaskHostFactory"" /> <Target Name='{nameof(VariousParameterTypesCanBeTransmittedToAndRecievedFromTaskHost)}'> <{nameof(TaskBuilderTestTask)} ExecuteReturnParam=""true"" BoolParam=""true"" BoolArrayParam=""false;true;false"" IntParam=""314"" IntArrayParam=""42;67;98"" StringParam=""stringParamInput"" StringArrayParam=""stringArrayParamInput1;stringArrayParamInput2;stringArrayParamInput3""> <Output PropertyName=""BoolOutput"" TaskParameter=""BoolOutput"" /> <Output PropertyName=""BoolArrayOutput"" TaskParameter=""BoolArrayOutput"" /> <Output PropertyName=""IntOutput"" TaskParameter=""IntOutput"" /> <Output PropertyName=""IntArrayOutput"" TaskParameter=""IntArrayOutput"" /> <Output PropertyName=""EnumOutput"" TaskParameter=""EnumOutput"" /> <Output PropertyName=""StringOutput"" TaskParameter=""StringOutput"" /> <Output PropertyName=""StringArrayOutput"" TaskParameter=""StringArrayOutput"" /> </{nameof(TaskBuilderTestTask)}> </Target> </Project>"; TransientTestProjectWithFiles project = env.CreateTestProjectWithFiles(projectContents); ProjectInstance projectInstance = new(project.ProjectFile); projectInstance.Build(new[] { new MockLogger(env.Output) }).ShouldBeTrue(); }
public void TaskReturnsFailureButDoesNotLogError_ShouldCauseBuildFailure() { using (TestEnvironment env = TestEnvironment.Create(_output)) { TransientTestProjectWithFiles proj = env.CreateTestProjectWithFiles($@" <Project> <UsingTask TaskName = ""ReturnFailureWithoutLoggingErrorTask"" AssemblyName=""net.r_eg.IeXod.Engine.UnitTests""/> <Target Name='Build'> <ReturnFailureWithoutLoggingErrorTask/> </Target> </Project>"); MockLogger logger = proj.BuildProjectExpectFailure(); logger.AssertLogContains("MSB4181"); } }
public void SdkVersionsAreSuccessfullyLoaded() { Dictionary <string, string> expectedVersions = new Dictionary <string, string> { { "foo", "1.0.0" }, { "bar", "2.0.0" } }; using (TestEnvironment testEnvironment = TestEnvironment.Create()) { TransientTestProjectWithFiles projectWithFiles = testEnvironment.CreateTestProjectWithFiles("", relativePathFromRootToProject: @"a\b\c"); WriteGlobalJson(projectWithFiles.TestRoot, expectedVersions); MockSdkResolverContext context = new MockSdkResolverContext(projectWithFiles.ProjectFile); GlobalJsonReader.GetMSBuildSdkVersions(context).ShouldBe(expectedVersions); } }
public void TryGetNuGetVersionForSdkGetsVersionFromGlobalJson() { Dictionary <string, string> expectedVersions = new Dictionary <string, string> { { "foo", "5.11.77" }, { "bar", "2.0.0" } }; using (TestEnvironment testEnvironment = TestEnvironment.Create()) { TransientTestProjectWithFiles projectWithFiles = testEnvironment.CreateTestProjectWithFiles("", relativePathFromRootToProject: @"a\b\c"); GlobalJsonReader_Tests.WriteGlobalJson(projectWithFiles.TestRoot, expectedVersions); MockSdkResolverContext context = new MockSdkResolverContext(projectWithFiles.ProjectFile); VerifyTryGetNuGetVersionForSdk( version: null, expectedVersion: NuGetVersion.Parse(expectedVersions["foo"]), context: context); } }
public void InvalidJsonLogsMessage() { Dictionary <string, string> expectedVersions = new Dictionary <string, string> { { "foo", "1.0.0" }, { "bar", "2.0.0" } }; using (TestEnvironment testEnvironment = TestEnvironment.Create()) { TransientTestProjectWithFiles projectWithFiles = testEnvironment.CreateTestProjectWithFiles(""); string globalJsonPath = WriteGlobalJson(projectWithFiles.TestRoot, expectedVersions, additionalcontent: ", abc"); MockSdkResolverContext context = new MockSdkResolverContext(projectWithFiles.ProjectFile); GlobalJsonReader.GetMSBuildSdkVersions(context).ShouldBeNull(); context.MockSdkLogger.LoggedMessages .ShouldHaveSingleItem() .Key .ShouldBe($"Failed to parse \"{globalJsonPath}\". Invalid JavaScript property identifier character: }}. Path \'msbuild-sdks\', line 6, position 5."); } }
public void ImportedProjectsSdkImportsAreInPreprocessedOutput() { using (TestEnvironment env = TestEnvironment.Create()) { string sdk1 = env.CreateFolder().FolderPath; string sdk2 = env.CreateFolder().FolderPath; var projectOptions = SdkUtilities.CreateProjectOptionsWithResolver(new SdkUtilities.FileBasedMockSdkResolver(new Dictionary <string, string> { { "MSBuildUnitTestSdk1", sdk1 }, { "MSBuildUnitTestSdk2", sdk2 }, })); string sdkPropsPath1 = Path.Combine(sdk1, "Sdk.props"); string sdkTargetsPath1 = Path.Combine(sdk1, "Sdk.targets"); File.WriteAllText(sdkPropsPath1, @"<Project> <PropertyGroup> <SdkProps1Imported>true</SdkProps1Imported> </PropertyGroup> </Project>"); File.WriteAllText(sdkTargetsPath1, @"<Project> <PropertyGroup> <SdkTargets1Imported>true</SdkTargets1Imported> </PropertyGroup> </Project>"); string sdkPropsPath2 = Path.Combine(sdk2, "Sdk.props"); string sdkTargetsPath2 = Path.Combine(sdk2, "Sdk.targets"); File.WriteAllText(sdkPropsPath2, @"<Project> <PropertyGroup> <SdkProps2Imported>true</SdkProps2Imported> </PropertyGroup> </Project>"); File.WriteAllText(sdkTargetsPath2, @"<Project> <PropertyGroup> <SdkTargets2Imported>true</SdkTargets2Imported> </PropertyGroup> </Project>"); TransientTestProjectWithFiles import = env.CreateTestProjectWithFiles(@"<Project Sdk='MSBuildUnitTestSdk2'> <PropertyGroup> <MyImportWasImported>true</MyImportWasImported> </PropertyGroup> </Project>"); string importPath = Path.GetFullPath(import.ProjectFile); string content = $@"<Project Sdk='MSBuildUnitTestSdk1'> <Import Project='{importPath}' /> <PropertyGroup> <p>v1</p> </PropertyGroup> </Project>"; Project project = Project.FromProjectRootElement( ProjectRootElement.Create(XmlReader.Create(new StringReader(content))), projectOptions); StringWriter writer = new StringWriter(); project.SaveLogicalProject(writer); string expected = ObjectModelHelpers.CleanupFileContents( $@"<?xml version=""1.0"" encoding=""utf-16""?> <Project> <!-- ============================================================================================================================================ <Import Project=""Sdk.props"" Sdk=""MSBuildUnitTestSdk1""> This import was added implicitly because the Project element's Sdk attribute specified ""MSBuildUnitTestSdk1"". {sdkPropsPath1.Replace("--", "__")} ============================================================================================================================================ --> <PropertyGroup> <SdkProps1Imported>true</SdkProps1Imported> </PropertyGroup> <!-- ============================================================================================================================================ </Import> ============================================================================================================================================ --> <!-- ============================================================================================================================================ <Import Project=""{importPath.Replace("--", "__")}""> {importPath.Replace("--", "__")} ============================================================================================================================================ --> <!-- ============================================================================================================================================ <Import Project=""Sdk.props"" Sdk=""MSBuildUnitTestSdk2""> This import was added implicitly because the Project element's Sdk attribute specified ""MSBuildUnitTestSdk2"". {sdkPropsPath2.Replace("--", "__")} ============================================================================================================================================ --> <PropertyGroup> <SdkProps2Imported>true</SdkProps2Imported> </PropertyGroup> <!-- ============================================================================================================================================ </Import> {importPath.Replace("--", "__")} ============================================================================================================================================ --> <PropertyGroup> <MyImportWasImported>true</MyImportWasImported> </PropertyGroup> <!-- ============================================================================================================================================ <Import Project=""Sdk.targets"" Sdk=""MSBuildUnitTestSdk2""> This import was added implicitly because the Project element's Sdk attribute specified ""MSBuildUnitTestSdk2"". {sdkTargetsPath2.Replace("--", "__")} ============================================================================================================================================ --> <PropertyGroup> <SdkTargets2Imported>true</SdkTargets2Imported> </PropertyGroup> <!-- ============================================================================================================================================ </Import> {importPath.Replace("--", "__")} ============================================================================================================================================ --> <!-- ============================================================================================================================================ </Import> ============================================================================================================================================ --> <PropertyGroup> <p>v1</p> </PropertyGroup> <!-- ============================================================================================================================================ <Import Project=""Sdk.targets"" Sdk=""MSBuildUnitTestSdk1""> This import was added implicitly because the Project element's Sdk attribute specified ""MSBuildUnitTestSdk1"". {sdkTargetsPath1.Replace("--", "__")} ============================================================================================================================================ --> <PropertyGroup> <SdkTargets1Imported>true</SdkTargets1Imported> </PropertyGroup> <!-- ============================================================================================================================================ </Import> ============================================================================================================================================ --> </Project>"); Helpers.VerifyAssertLineByLine(expected, writer.ToString()); } }