August 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Completing the NUnit Project Template for .NET, Part 2

  • April 27, 2004
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Introduction

In this article—Part 2 of 2—I will complete the project template by demonstrating how to modify the supporting script to add a reference to import the nunit.framework.dll, how to define the wizard launching file, add the VSDir file entries, and we will take our new test library, created by the template, for a spin around the block.

This second half of the article requires you to modify VS.NET-installed files. It is a good idea to make copies of these files before making modifications. Without an xcopy backup, you may have to re-install VS.NET if you make a mistake.

Modifying Wizard Script Files

In Part 1, we copied the ClassLibrary project template and renamed the containing folder TestClassLibrary. After using the ClassLibrary project template as a good starting point, we modified the template class file to reflect the code we want NUnit test projects to have. The next step is to modify script files and wizard launching files so that the vsWizard.dll—included with VS.NET—knows what elements to add to a new project.

Picking up where we left off, we need to modify a script file the vsWizard.dll uses to aid in creating the project. The relative path to one of these files is named scripts\1033\default.js. Default.js contains a Jscript event handler named OnFinish. OnFinish adds some elements to the newly created project. We need to extend the copied default.js file to call a method that will add a reference to the nunit.framework.dll. Listing 1 is almost an exact copy of the default.js file with the one necessary modification shown in bold.

Listing 1: The slightly modified scripts\1033\default.js.

//
// Copyright (c) Microsoft Corporation 2001-2002. All rights
// reserved.
//

function OnFinish(selProj, selObj)
{
  var oldSuppressUIValue = true;
  try
  {
    oldSuppressUIValue  = dte.SuppressUI;
    var bSilent         = wizard.FindSymbol("SILENT_WIZARD");
    dte.SuppressUI      = bSilent;

    var strProjectName  = wizard.FindSymbol("PROJECT_NAME");
    var strProjectPath  = wizard.FindSymbol("PROJECT_PATH");
    var strTemplatePath = wizard.FindSymbol("TEMPLATES_PATH");
    var strTemplateFile = strTemplatePath + "\\ClassLibrary.vbproj";

    var project = CreateVSProject(strProjectName, ".vbproj", 
      strProjectPath, strTemplateFile);
    if( project )
    {
      strProjectName = project.Name;    //In case it got changed

      var item;
      var editor;

      var strRawGuid = wizard.CreateGuid();
      wizard.AddSymbol("GUID_ASSEMBLY",
                       wizard.FormatGuid(strRawGuid, 0));

      strTemplateFile = strTemplatePath + "\\AssemblyInfo.vb"; 
      item = AddFileToVSProject("AssemblyInfo.vb", project, 
        project.ProjectItems, strTemplateFile, false);
      if( item )
      {
        item.Properties("SubType").Value = "Code";
      }

      strTemplateFile = strTemplatePath + "\\Class.vb"; 
      item = AddFileToVSProject("Class1.vb", project, 
        project.ProjectItems, strTemplateFile, false);
      if( item )
      {
        item.Properties("SubType").Value = "Code";
        editor = item.Open(vsViewKindPrimary);
        editor.Visible = true;
      }
            
      // new code here
      AddReferencesForWizard(project);
            
      project.Save();
    }
    return 0;
  }
  catch(e)
  {   
    switch(e.number)
    {
      case -2147024816    /* FILE_ALREADY_EXISTS */ :
        return -2147213313;

      default:
        SetErrorInfo(e);
        return e.number;
    }
  }
  finally
  {
    dte.SuppressUI = oldSuppressUIValue;
  }
}

AddReferencesForWizard is a function that we will implement in a second script file. The result of the behavior is that the new project created from the template, represented by the object named project, will have the NUnit assembly reference added to it.

The (relative path) VBWizards\1033\common.js contains behaviors available to all templates. Common.js is quite a bit longer, so Listing 2 only contains the code you will need to add to this file.

Listing 2: Two new methods added to VBWizards\1033\common.js.

function GetVBReferenceManager(proj)
{
  var VSProject  = proj.Object;
  var refmanager = VSProject.References;
  return refmanager;
}


function AddReferencesForWizard(proj)
{
  var refManager          = GetVBReferenceManager(proj);
  var isReferenceExpanded = IsReferenceExpanded(proj);
  refManager.Add("NUnit.Framework");
  
  if( isReferenceExpanded )
    CollapseReferenceNodes(proj);
}

GetVBReferenceManager returns an instance of the references collection associated with a project. AddReferencesForWizard adds a reference to nunit.framework.dll. Simply referring to the assembly by name is acceptable because NUnit strongly names its assemblies and registers them in the GAC when NUnit is installed.

After modifying the script files, our wizard is complete. All that's left to do is let the vsWizard.dll know about our new project template.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel