Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Fix null reference when building a project.
Browse files Browse the repository at this point in the history
Pre-build custom tool runner was not handling that IProject.ProjectSpecificItems can be null for a project that has no preferences xml file already created (e.g. project created by Visual Studio and then opened in SharpDevelop).
  • Loading branch information
mrward committed Sep 9, 2012
1 parent a8f92d9 commit 0c5fa97
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public ProjectCustomToolOptions(IProject project)

void GetCustomToolProperties(IProject project)
{
properties = project.ProjectSpecificProperties.Get("customTool", new Properties());
if (project.ProjectSpecificProperties != null) {
properties = project.ProjectSpecificProperties.Get("customTool", new Properties());
} else {
properties = new Properties();
}
}

public bool RunCustomToolOnBuild {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ public class BeforeBuildCustomToolProjectItemsTests

IProject CreateProject(string fileName = @"d:\MyProject\MyProject.csproj")
{
projectHelper = new ProjectHelper(fileName);
CreateProjectWithNoProjectSpecifiedProperties(fileName);
projectHelper.AddProjectSpecificProperties();
return projectHelper.Project;
}

void CreateProjectWithNoProjectSpecifiedProperties(string fileName)
{
projectHelper = new ProjectHelper(fileName);
}

void CreateSolution(params IProject[] projects)
{
IProjectChangeWatcher watcher = MockRepository.GenerateStub<IProjectChangeWatcher>();
Expand Down Expand Up @@ -263,5 +269,16 @@ public void GetProjectItems_SolutionContainingTwoProjectsBothWithFilesAndMatchin
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}

[Test]
public void GetProjectItems_ProjectSpecificPropertiesIsNull_NoProjectItemsReturnedAndNoNullReferenceExceptionThrown()
{
CreateProjectWithNoProjectSpecifiedProperties(@"d:\MyProject\FirstProject.csproj");
CreateBeforeBuildCustomToolProjectItems();

List<FileProjectItem> projectItems = GetProjectItems();

Assert.AreEqual(0, projectItems.Count);
}
}
}
6 changes: 5 additions & 1 deletion src/Main/Base/Test/Utils/ProjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ public ProjectHelper(string fileName)
.Return(null)
.WhenCalled(mi => mi.ReturnValue = new ReadOnlyCollection<ProjectItem>(ProjectItems));

Project.Stub(p => p.ProjectSpecificProperties).Return(ProjectSpecificProperties);
Project.Stub(p => p.SyncRoot).Return(new Object());
}

public void AddProjectSpecificProperties()
{
Project.Stub(p => p.ProjectSpecificProperties).Return(ProjectSpecificProperties);
}

public void AddProjectItem(ProjectItem projectItem)
{
ProjectItems.Add(projectItem);
Expand Down

0 comments on commit 0c5fa97

Please sign in to comment.