Esempio n. 1
0
        // Ignore: Changes to the current directory interfere with the toolset reader.
        public void ProjectItemSpecTooLong()
        {
            string currentDirectory = Environment.CurrentDirectory;
            try
            {
                Environment.CurrentDirectory = Path.GetTempPath();

                string tempPath = Path.GetTempPath();

                string tempProject = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`TargetA; TargetB; TargetC` ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>

                    <Target Name=`TargetA` Outputs=`a1.dll`/>
                    <Target Name=`TargetB` Outputs=`b1.dll; b2.dll`/>
                    <Target Name=`TargetC` Outputs=`@(C_Outputs)`>
                        <CreateItem Include=`c1.dll` AdditionalMetadata=`MSBuildSourceProjectFile=birch; MSBuildSourceTargetName=oak`>
                            <Output ItemName=`C_Outputs` TaskParameter=`Include`/>
                        </CreateItem>
                    </Target>
                </Project>
                ");

                string fileName = Path.GetFileName(tempProject);

                string projectFile1 = null;
                for (int i = 0; i < 250; i++)
                {
                    projectFile1 += "..\\";
                }

                int rootLength = Path.GetPathRoot(tempPath).Length;
                string tempPathNoRoot = tempPath.Substring(rootLength);

                projectFile1 += Path.Combine(tempPathNoRoot, fileName);
                try
                {
                    MSBuild msbuildTask = new MSBuild();
                    msbuildTask.BuildEngine = new MockEngine();

                    msbuildTask.Projects = new ITaskItem[] { new TaskItem(projectFile1) };

                    bool success = msbuildTask.Execute();
                    Assert.IsTrue(success, "Build failed.  See 'Standard Out' tab for details.");
                }
                finally
                {
                    File.Delete(tempProject);
                }
            }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }
        }
Esempio n. 2
0
        public void StopOnFirstFailureandBuildInParallelMultipleNode()
        {
            string project1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                  <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                      <Target Name='msbuild'>
                          <Error Text='Error'/>
                      </Target>
                  </Project>
                  ");

            string project2 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                   <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                       <Target Name='msbuild'>
                           <Message Text='SecondProject'/>
                       </Target>
                    </Project>
                  ");

            try
            {
                ITaskItem[] projects = new ITaskItem[]
                {
                    new TaskItem(project1), new TaskItem(project2)
                };

                // Test the various combinations of BuildInParallel and StopOnFirstFailure when the msbuild task is told there are multiple nodes 
                // running in the system
                for (int i = 0; i < 4; i++)
                {
                    MSBuild msbuildTask = new MSBuild();
                    MockEngine mockEngine = new MockEngine();
                    mockEngine.IsRunningMultipleNodes = true;
                    msbuildTask.BuildEngine = mockEngine;
                    msbuildTask.Projects = projects;
                    msbuildTask.Targets = new string[] { "msbuild" };
                    // Make success true as the expected resultis false
                    bool success = true;
                    switch (i)
                    {
                        case 0:
                            // Verify setting BuildInParallel and StopOnFirstFailure to 
                            // true will not cause the msbuild task to set BuildInParallel to false during the execute
                            msbuildTask.BuildInParallel = true;
                            msbuildTask.StopOnFirstFailure = true;
                            success = msbuildTask.Execute();
                            // Verify build did build second project which has the message SecondProject
                            mockEngine.AssertLogContains("SecondProject");
                            // Verify the correct msbuild task messages are in the log
                            Assert.IsTrue(msbuildTask.BuildInParallel, "Iteration of 0 Expected BuildInParallel to be true");
                            break;
                        case 1:
                            // Verify setting BuildInParallel to true and StopOnFirstFailure to 
                            // false will cause no change in BuildInParallel
                            msbuildTask.BuildInParallel = true;
                            msbuildTask.StopOnFirstFailure = false;
                            success = msbuildTask.Execute();
                            // Verify build did build second project which has the message SecondProject
                            mockEngine.AssertLogContains("SecondProject");
                            // Verify the correct msbuild task messages are in the log
                            Assert.IsTrue(msbuildTask.BuildInParallel, "Iteration of 1 Expected BuildInParallel to be true");
                            break;
                        case 2:
                            // Verify setting BuildInParallel to false and StopOnFirstFailure to 
                            // true will cause no change in BuildInParallel
                            msbuildTask.BuildInParallel = false;
                            msbuildTask.StopOnFirstFailure = true;
                            success = msbuildTask.Execute();
                            // Verify build did not build second project which has the message SecondProject
                            mockEngine.AssertLogDoesntContain("SecondProject");
                            // Verify the correct msbuild task messages are in the log
                            Assert.IsFalse(msbuildTask.BuildInParallel, "Iteration of 2 Expected BuildInParallel to be false");
                            break;

                        case 3:
                            // Verify setting BuildInParallel to false and StopOnFirstFailure to 
                            // false will cause no change in BuildInParallel
                            msbuildTask.BuildInParallel = false;
                            msbuildTask.StopOnFirstFailure = false;
                            success = msbuildTask.Execute();
                            // Verify build did build second project which has the message SecondProject
                            mockEngine.AssertLogContains("SecondProject");
                            // Verify the correct msbuild task messages are in the log
                            Assert.IsFalse(msbuildTask.BuildInParallel, "Iteration of 3 Expected BuildInParallel to be false");
                            break;
                    }
                    // The build should fail as the first project has an error
                    Assert.IsFalse(success, "Iteration of i " + i + " Build Succeeded.  See 'Standard Out' tab for details.");
                }
            }
            finally
            {
                File.Delete(project1);
                File.Delete(project2);
            }
        }
Esempio n. 3
0
        public void TargetsWithSeparationChars()
        {
            string projectFile1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion=`msbuilddefaulttoolsversion`>
                    <Target Name=`Clean` />
                    <Target Name=`Build` />
                    <Target Name=`BuildAgain` />
                </Project>
                ");

            string projectFile2 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion=`msbuilddefaulttoolsversion`>
                    <PropertyGroup>
                        <Targets>Clean%3BBuild%3CBuildAgain</Targets>
                    </PropertyGroup>

                    <ItemGroup>
                        <ProjectFile Include=`" + projectFile1 + @"` />
                    </ItemGroup>
                   
                    <Target Name=`Build` Outputs=`$(SomeOutputs)`>
                        <MSBuild Projects=`@(ProjectFile)` Targets=`$(Targets)` TargetAndPropertyListSeparators=`%3B;%3C` />
                    </Target>
                </Project>
                ");

            try
            {
                ITaskItem[] projects = new ITaskItem[]
                {
                    new TaskItem(projectFile2)
                };

                MSBuild msbuildTask = new MSBuild();
                msbuildTask.BuildEngine = new MockEngine();
                msbuildTask.Projects = projects;

                bool success = msbuildTask.Execute();
                Assert.IsTrue(success, "Build failed.  See 'Standard Out' tab for details.");
            }
            finally
            {
                File.Delete(projectFile1);
                File.Delete(projectFile2);
            }
        }
Esempio n. 4
0
        public void DifferentAdditionalPropertiesWithoutDefault()
        {
            string projectFile1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`TargetA; TargetB` xmlns=`msbuildnamespace` ToolsVersion='msbuilddefaulttoolsversion'>

                    <Target Name=`TargetA` Outputs=`a1.dll` Condition=`'$(MyPropG)'=='1'`/>
                    <Target Name=`TargetB` Outputs=`b1.dll` Condition=`'$(MyPropA)'=='1'`/>
                   
                </Project>
                ");

            string projectFile2 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`TargetG; TargetH` xmlns=`msbuildnamespace` ToolsVersion='msbuilddefaulttoolsversion'>
                    <Target Name=`TargetG` Outputs=`g1.dll` Condition=`'$(MyPropG)'=='1'` />
                    <Target Name=`TargetH` Outputs=`h1.dll` Condition=`'$(MyPropA)'=='1'` />
                </Project>
                ");

            try
            {
                ITaskItem[] projects = new ITaskItem[]
                {
                    new TaskItem(projectFile1),
                    new TaskItem(projectFile2)
                };

                projects[0].SetMetadata("AdditionalProperties", "MyPropA=1");
                projects[1].SetMetadata("AdditionalProperties", "MyPropA=1");

                MSBuild msbuildTask = new MSBuild();
                msbuildTask.BuildEngine = new MockEngine();
                msbuildTask.Projects = projects;

                bool success = msbuildTask.Execute();
                Assert.IsTrue(success, "Build failed.  See 'Standard Out' tab for details.");

                string expectedItemOutputs = string.Format(@"
                    b1.dll : MSBuildSourceProjectFile={0} ; MSBuildSourceTargetName=TargetB
                    h1.dll : MSBuildSourceProjectFile={1} ; MSBuildSourceTargetName=TargetH
                    ", projectFile1, projectFile2);

                ObjectModelHelpers.AssertItemsMatch(expectedItemOutputs, msbuildTask.TargetOutputs, false /* order of items not enforced */);
            }
            finally
            {
                File.Delete(projectFile1);
                File.Delete(projectFile2);
            }
        }
Esempio n. 5
0
        public void DifferentGlobalPropertiesInvalid()
        {
            string projectFile1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`TargetA; TargetB` xmlns=`msbuildnamespace` ToolsVersion='msbuilddefaulttoolsversion'>

                    <Target Name=`TargetA` Outputs=`a1.dll` Condition=`'$(MyProp)'=='0'`/>
                    <Target Name=`TargetB` Outputs=`b1.dll` Condition=`'$(MyProp)'=='1'`/>
                   
                </Project>
                ");

            string projectFile2 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`TargetG; TargetH` xmlns=`msbuildnamespace` ToolsVersion='msbuilddefaulttoolsversion'>
                    <Target Name=`TargetG` Outputs=`g1.dll` Condition=`'$(MyProp)'=='0'` />
                    <Target Name=`TargetH` Outputs=`h1.dll` Condition=`'$(MyProp)'=='1'` />
                </Project>
                ");

            try
            {
                ITaskItem[] projects = new ITaskItem[]
                {
                    new TaskItem(projectFile1),
                    new TaskItem(projectFile1),
                    new TaskItem(projectFile2),
                    new TaskItem(projectFile2)
                };
                projects[1].SetMetadata("Properties", "=1");
                projects[3].SetMetadata("Properties", "=;1");

                MSBuild msbuildTask = new MSBuild();
                msbuildTask.BuildEngine = new MockEngine();

                msbuildTask.Projects = projects;

                bool success = msbuildTask.Execute();
                Assert.IsFalse(success, "Build succeeded.  See 'Standard Out' tab for details.");
            }
            finally
            {
                File.Delete(projectFile1);
                File.Delete(projectFile2);
            }
        }
Esempio n. 6
0
        public void PropertiesWithSeparationChars()
        {
            string projectFile1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion='msbuilddefaulttoolsversion'>
                    <Target Name=`Build` Outputs=`|$(a)|$(b)|$(C)|$(D)|` />
                </Project>
                ");

            string projectFile2 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion=`msbuilddefaulttoolsversion`>
                    <PropertyGroup>
                        <AValues>a%3BA</AValues>
                        <BValues>b;B</BValues>
                        <CValues>c;C</CValues>
                        <DValues>d%3BD</DValues>
                    </PropertyGroup>

                    <ItemGroup>
                        <ProjectFile Include=`" + projectFile1 + @"`>
                            <AdditionalProperties>C=$(CValues)%3BD=$(DValues)</AdditionalProperties>
                        </ProjectFile>
                    </ItemGroup>
                   
                    <Target Name=`Build` Outputs=`$(SomeOutputs)`>
                        <MSBuild Projects=`@(ProjectFile)` Targets=`Build` Properties=`a=$(AValues)%3Bb=$(BValues)` TargetAndPropertyListSeparators=`%3B`>
                            <Output TaskParameter=`TargetOutputs` PropertyName=`SomeOutputs`/>
                        </MSBuild>
                    </Target>	
                </Project>
                ");

            try
            {
                ITaskItem[] projects = new ITaskItem[]
                {
                    new TaskItem(projectFile2)
                };

                MSBuild msbuildTask = new MSBuild();
                msbuildTask.BuildEngine = new MockEngine();
                msbuildTask.Projects = projects;

                bool success = msbuildTask.Execute();
                Assert.IsTrue(success, "Build failed.  See 'Standard Out' tab for details.");

                Assert.AreEqual(5, msbuildTask.TargetOutputs.Length);
                Assert.AreEqual("|a", msbuildTask.TargetOutputs[0].ItemSpec);
                Assert.AreEqual("A|b", msbuildTask.TargetOutputs[1].ItemSpec);
                Assert.AreEqual("B|c", msbuildTask.TargetOutputs[2].ItemSpec);
                Assert.AreEqual("C|d", msbuildTask.TargetOutputs[3].ItemSpec);
                Assert.AreEqual("D|", msbuildTask.TargetOutputs[4].ItemSpec);
            }
            finally
            {
                File.Delete(projectFile1);
                File.Delete(projectFile2);
            }
        }
Esempio n. 7
0
        public void TargetStopOnFirstFailureBuildInParallel()
        {
            string project1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                   <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                   <Target Name='T1'>
                          <Message Text='Proj2 T1 message'/>
                      </Target>
                    <Target Name='T2'>
                          <Message Text='Proj2 T2 message'/>
                      </Target>
                    <Target Name='T3'>
                           <Error Text='Error'/>
                      </Target>
                    </Project>
                  ");

            try
            {
                ITaskItem[] projects = new ITaskItem[]
                {
                    new TaskItem(project1)
                };
                for (int i = 0; i < 6; i++)
                {
                    // Test the case where the error is in the last target
                    MSBuild msbuildTask = new MSBuild();
                    MockEngine mockEngine = new MockEngine();
                    msbuildTask.BuildEngine = mockEngine;
                    msbuildTask.Projects = projects;
                    // Set to true as the expected result is false
                    bool success = true;
                    switch (i)
                    {
                        case 0:
                            // Test the case where the error is in the last project and RunEachTargetSeparately = true
                            msbuildTask.StopOnFirstFailure = true;
                            msbuildTask.RunEachTargetSeparately = true;
                            msbuildTask.Targets = new string[] { "T1", "T2", "T3" };
                            success = msbuildTask.Execute();
                            mockEngine.AssertLogContains("Proj2 T1 message");
                            mockEngine.AssertLogContains("Proj2 T2 message");
                            break;
                        case 1:
                            // Test the case where the error is in the second target out of 3.
                            msbuildTask.StopOnFirstFailure = true;
                            msbuildTask.RunEachTargetSeparately = true;
                            msbuildTask.Targets = new string[] { "T1", "T3", "T2" };
                            success = msbuildTask.Execute();
                            mockEngine.AssertLogContains("Proj2 T1 message");
                            mockEngine.AssertLogDoesntContain("Proj2 T2 message");
                            // The build should fail as the first project has an error
                            break;
                        case 2:
                            // Test case where error is in second last target but stopOnFirstFailure is false
                            msbuildTask.RunEachTargetSeparately = true;
                            msbuildTask.StopOnFirstFailure = false;
                            msbuildTask.Targets = new string[] { "T1", "T3", "T2" };
                            success = msbuildTask.Execute();
                            mockEngine.AssertLogContains("Proj2 T1 message");
                            mockEngine.AssertLogContains("Proj2 T2 message");
                            break;
                        // Test the cases where RunEachTargetSeparately is false. In these cases all of the targets should be submitted at once
                        case 3:
                            // Test the case where the error is in the last project and RunEachTargetSeparately = true
                            msbuildTask.StopOnFirstFailure = true;
                            msbuildTask.Targets = new string[] { "T1", "T2", "T3" };
                            success = msbuildTask.Execute();
                            mockEngine.AssertLogContains("Proj2 T1 message");
                            mockEngine.AssertLogContains("Proj2 T2 message");
                            // The build should fail as the first project has an error
                            break;
                        case 4:
                            // Test the case where the error is in the second target out of 3.
                            msbuildTask.StopOnFirstFailure = true;
                            msbuildTask.Targets = new string[] { "T1", "T3", "T2" };
                            success = msbuildTask.Execute();
                            mockEngine.AssertLogContains("Proj2 T1 message");
                            mockEngine.AssertLogDoesntContain("Proj2 T2 message");
                            // The build should fail as the first project has an error
                            break;
                        case 5:
                            // Test case where error is in second last target but stopOnFirstFailure is false
                            msbuildTask.StopOnFirstFailure = false;
                            msbuildTask.Targets = new string[] { "T1", "T3", "T2" };
                            success = msbuildTask.Execute();
                            mockEngine.AssertLogContains("Proj2 T1 message");
                            mockEngine.AssertLogDoesntContain("Proj2 T2 message");
                            break;
                    }

                    // The build should fail as the first project has an error
                    Assert.IsFalse(success, "Iteration of i:" + i + "Build Succeeded.  See 'Standard Out' tab for details.");
                }
            }
            finally
            {
                File.Delete(project1);
            }
        }
Esempio n. 8
0
        public void SkipRemainingProjects()
        {
            string project1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                  <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                      <Target Name='msbuild'>
                          <Error Text='Error'/>
                      </Target>
                  </Project>
                  ");

            string project2 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                   <Project xmlns='msbuildnamespace' ToolsVersion='msbuilddefaulttoolsversion'>
                       <Target Name='msbuild'>
                           <Message Text='SecondProject'/>
                       </Target>
                    </Project>
                  ");

            try
            {
                // Test the case where there is only one project and it has an error
                ITaskItem[] projects = new ITaskItem[]
                {
                    new TaskItem(project1)
                };

                MSBuild msbuildTask = new MSBuild();
                MockEngine mockEngine = new MockEngine();
                mockEngine.IsRunningMultipleNodes = true;
                msbuildTask.BuildEngine = mockEngine;
                msbuildTask.Projects = projects;
                msbuildTask.Targets = new string[] { "msbuild" };
                msbuildTask.BuildInParallel = false;
                msbuildTask.StopOnFirstFailure = true;
                bool success = msbuildTask.Execute();
                Assert.IsFalse(success, "Build Succeeded.  See 'Standard Out' tab for details.");

                // Test the case where there are two projects and the last one has an error
                projects = new ITaskItem[]
                {
                    new TaskItem(project2), new TaskItem(project1)
                };

                msbuildTask = new MSBuild();
                mockEngine = new MockEngine();
                mockEngine.IsRunningMultipleNodes = true;
                msbuildTask.BuildEngine = mockEngine;
                msbuildTask.Projects = projects;
                msbuildTask.Targets = new string[] { "msbuild" };
                msbuildTask.BuildInParallel = false;
                msbuildTask.StopOnFirstFailure = true;
                success = msbuildTask.Execute();
                Assert.IsFalse(success, "Build Succeeded.  See 'Standard Out' tab for details.");
            }
            finally
            {
                File.Delete(project1);
                File.Delete(project2);
            }
        }
Esempio n. 9
0
        public void OutputItemsAreTaggedWithProjectFileAndTargetName()
        {
            string projectFile1 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`TargetA; TargetB; TargetC` ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>

                    <Target Name=`TargetA` Outputs=`a1.dll`/>
                    <Target Name=`TargetB` Outputs=`b1.dll; b2.dll`/>
                    <Target Name=`TargetC` Outputs=`@(C_Outputs)`>
                        <CreateItem Include=`c1.dll`>
                            <Output ItemName=`C_Outputs` TaskParameter=`Include`/>
                        </CreateItem>
                    </Target>
                </Project>
                ");

            string projectFile2 = ObjectModelHelpers.CreateTempFileOnDisk(@"
                <Project DefaultTargets=`TargetG; TargetH` ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>
                    <Target Name=`TargetG` Outputs=`g1.dll; g2.dll`/>
                    <Target Name=`TargetH` Outputs=`h1.dll`/>
                </Project>
                ");

            try
            {
                MSBuild msbuildTask = new MSBuild();
                msbuildTask.BuildEngine = new MockEngine();

                msbuildTask.Projects = new ITaskItem[] { new TaskItem(projectFile1), new TaskItem(projectFile2) };

                bool success = msbuildTask.Execute();
                Assert.IsTrue(success, "Build failed.  See 'Standard Out' tab for details.");

                string expectedItemOutputs = string.Format(@"
                    a1.dll : MSBuildSourceProjectFile={0} ; MSBuildSourceTargetName=TargetA
                    b1.dll : MSBuildSourceProjectFile={0} ; MSBuildSourceTargetName=TargetB
                    b2.dll : MSBuildSourceProjectFile={0} ; MSBuildSourceTargetName=TargetB
                    c1.dll : MSBuildSourceProjectFile={0} ; MSBuildSourceTargetName=TargetC
                    g1.dll : MSBuildSourceProjectFile={1} ; MSBuildSourceTargetName=TargetG
                    g2.dll : MSBuildSourceProjectFile={1} ; MSBuildSourceTargetName=TargetG
                    h1.dll : MSBuildSourceProjectFile={1} ; MSBuildSourceTargetName=TargetH
                    ", projectFile1, projectFile2);

                ObjectModelHelpers.AssertItemsMatch(expectedItemOutputs, msbuildTask.TargetOutputs, false /* order of items not enforced */);
            }
            finally
            {
                File.Delete(projectFile1);
                File.Delete(projectFile2);
            }
        }