public void RemoveItemAllOfSeveral() { BuildItemGroup group = new BuildItemGroup(); BuildItem[] item = new BuildItem[3]; item[0] = group.AddNewItem("n1", "i1"); item[1] = group.AddNewItem("n2", "i2"); item[2] = group.AddNewItem("n3", "i3"); group.RemoveItem(item[0]); group.RemoveItem(item[1]); group.RemoveItem(item[2]); Assertion.AssertEquals(0, group.Count); }
private void MoveTo(BuildItemGroup targetGroup, int index) { buildItemGroup.RemoveItem(instance); AddItemAt(targetGroup, index); buildItemGroup = targetGroup; this.index = index; }
public void RemoveEvaluatedItem1() { try { List <string> files = CompatibilityTestHelpers.CreateFiles(4, "foo", "foo", ObjectModelHelpers.TempProjectDir); Project p = new Project(new Engine()); BuildItemGroup group = p.AddNewItemGroup(); group.AddNewItem("foos", Path.Combine(ObjectModelHelpers.TempProjectDir, "*.foo")); object o = p.EvaluatedItems; // this causes the failure group.RemoveItem(p.EvaluatedItems[0]); // Exception thrown here Assertion.Fail("success as failure"); // should not get here due to exception above } catch (Exception e) { if (!(e.GetType().ToString().Contains("InternalErrorException"))) { Assertion.Fail(e.Message + " was thrown"); } else { Assertion.Assert("InternalErrorException was thrown", true); } } finally { CompatibilityTestHelpers.CleanupDirectory(ObjectModelHelpers.TempProjectDir); } }
public void RemoveItemWhenItemIsNull() { BuildItemGroup group = new BuildItemGroup(); group.RemoveItem(null); Assertion.AssertEquals(0, group.Count); }
public void RemoveItemNotBelonging() { XmlElement ig = CreatePersistedItemGroupElement(); BuildItemGroup group = new BuildItemGroup(ig, false, new Project()); BuildItem item = new BuildItem("x", "x1"); group.RemoveItem(item); }
public void RemoveItem1() { XmlElement ig = CreatePersistedItemGroupElement(); BuildItemGroup group = new BuildItemGroup(ig, false, new Project()); BuildItem i2 = group[1]; group.RemoveItem(i2); Assertion.AssertEquals(1, group.Count); Assertion.AssertEquals(1, group.ItemGroupElement.ChildNodes.Count); Assertion.AssertEquals("i1", group[0].Include); Assertion.AssertEquals(null, i2.ParentPersistedItemGroup); }
public void RemoveItemSimple() { project.LoadXml(ProjectContentWithOneBuildItemGroupThreeBuildItems); BuildItemGroup group = GetBuildItemGroupFromProject(project, false); BuildItem item = GetSpecificBuildItemFromBuildItemGroup(group, "n2"); group.RemoveItem(item); Assertion.AssertEquals(2, group.Count); Dictionary <string, string> items = GetDictionaryOfBuildItemsInProject(project, false); Assertion.AssertEquals("i1", items["n1"]); Assertion.AssertEquals("i3", items["n3"]); }
public void TestRemoveItem1() { BuildItemGroup big = new BuildItemGroup(); big.AddNewItem("a", "b"); BuildItem b = big.AddNewItem("b", "c"); big.AddNewItem("c", "d"); big.RemoveItem(b); BuildItem[] items = big.ToArray(); Assert.AreEqual(2, big.Count, "A1"); Assert.AreEqual("a", items [0].Name, "A2"); Assert.AreEqual("c", items [1].Name, "A3"); }
public void RemoveItemOneOfSeveral() { BuildItemGroup group = new BuildItemGroup(); BuildItem item = group.AddNewItem("n1", "i1"); group.AddNewItem("n2", "i2"); group.AddNewItem("n3", "i3"); group.RemoveItem(item); Assertion.AssertEquals(2, group.Count); Dictionary <string, string> items = GetDictionaryOfBuildItemsInBuildItemsGroup(group); Assertion.AssertEquals("i2", items["n2"]); Assertion.AssertEquals("i3", items["n3"]); }
public void ExampleTest() { //// Part 1 of Example test - working with BuildItemGroup where //// you have an XML project project.LoadXml(ProjectContentWithOneBuildItemGroupThreeBuildItems); BuildItemGroup groupOne = GetBuildItemGroupFromProject(project, false); Assertion.AssertEquals("'A'=='B'", groupOne.Condition); Assertion.AssertEquals(3, groupOne.Count); Assertion.AssertEquals(false, groupOne.IsImported); BuildItem itemOne = GetSpecificBuildItemFromBuildItemGroup(groupOne, "n2"); Assertion.AssertEquals("'a2' == 'b2'", itemOne.Condition); groupOne.RemoveItem(itemOne); Assertion.AssertEquals(2, groupOne.Count); BuildItem itemOther = groupOne.AddNewItem("n4", "i4"); Assertion.AssertEquals(3, groupOne.Count); //// Part 2 of Example test - working with BuildItemGroup where //// you have an in-memory buildItemGroup BuildItemGroup groupTwo = new BuildItemGroup(); groupTwo.AddNewItem("name", "include"); Assertion.AssertEquals(1, groupTwo.Count); //// Part 3 of Example test - working with BuildItemGroup where //// you have an XML project that contains an Imported Project Project p = GetProjectThatImportsAnotherProject(null, null); BuildItemGroup groupThree = GetBuildItemGroupFromProject(p, true); Assertion.AssertEquals(true, groupThree.IsImported); BuildItem itemThree = GetSpecificBuildItemFromBuildItemGroup(groupThree, "n3Imported"); Assertion.AssertEquals("n3Importedvalue1", itemThree.GetMetadata("n3ImportedMeta1")); }
private void CreateAssemblyReferenceProjectFile(IDictionary <string, ProjectInfo> projects) { foreach (ProjectInfo projectInfo in projects.Values) { if (m_AssemblyRefProjectFilesHandled.Contains(projectInfo)) { continue; } Debug.Assert(projectInfo.MSBuildProject != null); List <BuildItem> projectReferencesToMove = new List <BuildItem>(); BuildItemGroup assemblyReferencesBuildItemGroup = null; BuildItemGroup projectReferencesBuildItemGroup = null; foreach (BuildItemGroup buildItemGroup in projectInfo.MSBuildProject.ItemGroups) { foreach (BuildItem buildItem in buildItemGroup) { if (buildItem.Name == Reference) { // Keep a bookmark on the BuildItemGroup that holds assembly references assemblyReferencesBuildItemGroup = buildItemGroup; } if (buildItem.Name == ProjectReference) { // Keep a bookmark on the BuildItemGroup that holds project references projectReferencesBuildItemGroup = buildItemGroup; // Take a look at all project references to identify those to convert to assembly references ProjectInfo referencedProjectInfo; string projectGuid = buildItem.GetMetadata(Project); m_ProjectsByGuid.TryGetValue(projectGuid, out referencedProjectInfo); if (referencedProjectInfo != null) { projectReferencesToMove.Add(buildItem); } } } } // If no project references found or none to move, nothing to change; save file as-is if (projectReferencesBuildItemGroup == null || projectReferencesToMove.Count == 0) { //Console.WriteLine("Nothing to do."); } else { if (assemblyReferencesBuildItemGroup == null) { assemblyReferencesBuildItemGroup = projectInfo.MSBuildProject.AddNewItemGroup(); } foreach (BuildItem buildItem in projectReferencesToMove) { projectReferencesBuildItemGroup.RemoveItem(buildItem); // Get the assembly name corresponding to the project GUID foreach (ProjectInfo pinfo in projects.Values) { if (pinfo.ProjectGuid == buildItem.GetMetadata(Project)) { BuildItem newBuildItem = assemblyReferencesBuildItemGroup.AddNewItem(Reference, pinfo.AssemblyName); newBuildItem.SetMetadata("Private", bool.FalseString); newBuildItem.SetMetadata("SpecificVersion", bool.FalseString); if (m_Arguments.HintPath != string.Empty) { string relative = GetRelativePath(Path.GetDirectoryName(projectInfo.Filename), m_Arguments.HintPath); newBuildItem.SetMetadata("HintPath", Path.Combine(relative, pinfo.AssemblyName) + ".dll"); } break; } } } } StringBuilder sb = new StringBuilder(Path.ChangeExtension(projectInfo.Filename, null)); if (m_Arguments.Overwrite == false) { sb.Append(AssemblyRefs); } sb.Append(CSProjFileExtension); string projectFileName = sb.ToString(); projectInfo.MSBuildProject.Save(projectFileName); m_AssemblyRefProjectFilesHandled.Add(projectInfo); } }
private void CreateProjectReferenceProjectFile(IDictionary <string, ProjectInfo> projects) { foreach (ProjectInfo projectInfo in projects.Values) { if (m_ProjectRefProjectFilesHandled.Contains(projectInfo)) { continue; } Debug.Assert(projectInfo.MSBuildProject != null); List <BuildItem> assemblyReferencesToMove = new List <BuildItem>(); BuildItemGroup assemblyReferencesBuildItemGroup = null; BuildItemGroup projectReferencesBuildItemGroup = null; foreach (BuildItemGroup buildItemGroup in projectInfo.MSBuildProject.ItemGroups) { foreach (BuildItem buildItem in buildItemGroup) { // Find all assembly references to convert to project references if (buildItem.Name == Reference) { // Keep a bookmark on the BuildItemGroup that holds assembly references assemblyReferencesBuildItemGroup = buildItemGroup; // Take a look at all assembly references to identify those to convert to project references ProjectInfo referencedProjectInfo; string projectName = GetAssemblyNameFromFullyQualifiedName(buildItem.Include); projects.TryGetValue(projectName, out referencedProjectInfo); if (referencedProjectInfo != null) { assemblyReferencesToMove.Add(buildItem); } } if (buildItem.Name == ProjectReference) { // Keep a bookmark on the BuildItemGroup that holds project references projectReferencesBuildItemGroup = buildItemGroup; } } } // If no assembly references found or none to move, nothing to change; save file as-is if (assemblyReferencesBuildItemGroup == null || assemblyReferencesToMove.Count == 0) { //Console.WriteLine("Nothing to do."); } else { // If no project reference group exists, create one. if (projectReferencesBuildItemGroup == null) { projectReferencesBuildItemGroup = projectInfo.MSBuildProject.AddNewItemGroup(); } // Remove assembly reference and replace by corresponding project reference. foreach (BuildItem buildItem in assemblyReferencesToMove) { assemblyReferencesBuildItemGroup.RemoveItem(buildItem); string replacementAssemblyName = GetAssemblyNameFromFullyQualifiedName(buildItem.Include); var replacementProjectInfo = m_ProjectsByName[replacementAssemblyName.ToUpperInvariant()]; string remplacementInclude = GetRelativePath(Path.GetDirectoryName(projectInfo.Filename), replacementProjectInfo.Filename); BuildItem newBuildItem = projectReferencesBuildItemGroup.AddNewItem(ProjectReference, remplacementInclude); newBuildItem.SetMetadata("Project", replacementProjectInfo.ProjectGuid); newBuildItem.SetMetadata("Name", replacementProjectInfo.ProjectName); newBuildItem.SetMetadata("Private", bool.FalseString); } } StringBuilder sb = new StringBuilder(Path.ChangeExtension(projectInfo.Filename, null)); if (m_Arguments.Overwrite == false) { sb.Append(ProjectRefs); } sb.Append(CSProjFileExtension); string projectFileName = sb.ToString(); projectInfo.MSBuildProject.Save(projectFileName); m_ProjectRefProjectFilesHandled.Add(projectInfo); } }
// NOTE: maybe it should throw an exception? // at the moment it probably doesn't find a "null" element public void TestRemoveItem2() { BuildItemGroup big = new BuildItemGroup(); big.RemoveItem(null); }