Click here to Skip to main content
15,888,177 members
Articles / Programming Languages / C#

Visualizing Project Dependencies Automatically

Rate me:
Please Sign up or sign in to vote.
4.09/5 (11 votes)
4 Sep 2007CPOL3 min read 42.9K   428   32  
Have a large code tree? Wondering which projects refer to which other ones? Manually run this console app, schedule it to run nightly or after each build.
using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;
using CompanyName.DependencyTracker;

namespace TestDependencyFinder
{
    [TestFixture]
    public class TestDependencyFinder
    {
        [Test]
        [ExpectedException(typeof(System.IO.DirectoryNotFoundException))]
        public void TestNotFound()
        {
            new DependencyFinder(@"Z:\xyz").GetAllProjects();
        }

        [Test]
        public void TestOneFound()
        {
            DependencyFinder finder = new DependencyFinder(@"..\..\..\SampleProjects\Applications");
            ProjectList projects = finder.GetAllProjects();

            Assert.AreEqual(1, projects.Count);
            Assert.AreEqual(projects.ToString(), "Application1"); 
        }

        [Test]
        public void TestTwoFound()
        {
            DependencyFinder finder = new DependencyFinder(@"..\..\..\SampleProjects\LibraryCode");
            ProjectList projects = finder.GetAllProjects();

            Assert.AreEqual(2, projects.Count);
            Assert.AreEqual(projects.ToString(), "CommonLibrary1, BusinessObject1");
        }

        [Test]
        public void TestOneDependency()
        {
            DependencyFinder finder = new DependencyFinder(@"..\..\..\SampleProjects\LibraryCode\BusinessObject1");
            ProjectList projects = finder.GetAllProjects();

            Assert.AreEqual(1, projects.Count);
            Assert.AreEqual(projects.ToString(), "BusinessObject1");
            Assert.AreEqual(1, projects[0].ReferencedProjects.Count); //references library
            Assert.AreEqual(0, projects[0].ReferencedProjects[0].ReferencedProjects.Count); //library refers to nothing
        }

        [Test]
        public void TestNestedDependency()
        {
            DependencyFinder finder = new DependencyFinder(@"..\..\..\SampleProjects\Applications\Application1");
            ProjectList projects = finder.GetAllProjects();

            Assert.AreEqual(1, projects.Count);
            Assert.AreEqual(projects.ToString(), "Application1");
            Assert.AreEqual(2, projects[0].ReferencedProjects.Count); //references businessobjects and library
            Assert.AreEqual(1, projects[0].ReferencedProjects[0].ReferencedProjects.Count); //busobjs referes to library
        }

        [Test]
        public void TestVisualizeAll() //visualize it, can't really unit test this so output and verify its there
        {
            DependencyFinder finder = new DependencyFinder(@"..\..\..\SampleProjects");
            string TempOutFile = @"..\..\..\SampleProjects\AllCode.jpg"; //System.IO.Path.GetTempFileName();
            finder.VisualizeProjects(TempOutFile);

            Assert.IsTrue(System.IO.File.Exists(TempOutFile));
            System.IO.File.Delete(TempOutFile);
        }

        [Test]
        public void TestVisualizeLibraryCode() //visualize it, can't really unit test this so output and verify its there
        {
            DependencyFinder finder = new DependencyFinder(@"..\..\..\SampleProjects\LibraryCode");
            string TempOutFile = @"..\..\..\SampleProjects\AllLibraryCode.jpg"; //System.IO.Path.GetTempFileName();
            finder.VisualizeProjects(TempOutFile);

            Assert.IsTrue(System.IO.File.Exists(TempOutFile));
            System.IO.File.Delete(TempOutFile);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Comments and Discussions