Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version
Go to top

Building .NET Coverage Tool

, 25 Aug 2009
This article is a walkthrough for building a .NET coverage tool
Coverage.NET_demo.zip
TestApp
TestApp.exe
TestApp.pdb
Tool
Coverage.Counter.dll
Coverage.exe
Mono.Cecil.dll
Mono.Tools.Pdb2Mdb.dll
Coverage.NET_src.zip
Coverage
Common
Instrument
Properties
Report
Coverage.Counter
Properties
Mono.Cecil
src
AUTHORS
ChangeLog
CodeGen
cecil-gen-attributes.rb
cecil-gen-sources.rb
cecil-gen-tests.rb
cecil-gen-types.rb
cecil-gen.rb
cecil-mig.rb
cecil-update-rev
templates
configure
default.build
Makefile
mono-cecil.pc.in
Mono.Cecil
Mono.Cecil.Binary
Mono.Cecil.Cil
Mono.Cecil.dll.sources
Mono.Cecil.Metadata
Mono.Cecil.Signatures
Mono.Xml
ChangeLog
NEWS
README
standalone.make
TODO
Mono.Tools.Pdb2Mdb
Properties
src
ChangeLog
LICENSE
Makefile
pdb2mdb.exe.sources
//
// Runner.cs
//
// Author:
//   Sergiy Sakharov (sakharov@gmail.com)
//
// (C) 2009 Sergiy Sakharov
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using Coverage.Common;
using Coverage.Instrument;
using Coverage.Report;

namespace Coverage
{
	static class Runner
	{
		public static void Main(string[] args)
		{
			Configuration.Initialize();

			var assemblies = new List<string>();
			var filters = new List<NameFilter>();
			var optionsOnly = false;

			for (var i = 0; i < args.Length; i++)
			{
				switch (args[i])
				{
					case "/x"://next argument is a coverage file name
						optionsOnly = true;
						if (i < args.Length - 1)
							Configuration.CoverageFile = args[i + 1];
						break;
					case "/r":
						optionsOnly = true;
						Configuration.NamingMode = Configuration.NamingModes.BackupOriginals;
						break;
					case "/h":
						ShowHelp();
						return;
					default:
						if(optionsOnly)
							break;

						if(args[i][0] != '-')
							assemblies.AddRange(ResolveFilesByMask(args[i]));
						else
							Configuration.NameFilters.Add(ParseFilter(args[i].Substring(1)));

						break;
				}
			}

			var executor = new Executor(assemblies.ToArray());
			executor.Execute(new CompositeVisitor(new ReportVisitor(), new InstrumentorVisitor()));
		}

		private static NameFilter ParseFilter(string filterStr)
		{
			//default type of filter is Attribute filter
			if (filterStr[1] != ':')
				return new NameFilter { Type = NameFilter.FilterTypes.AttributeFilter, FilteredName = filterStr };

			var filter = new NameFilter();
			filter.Type = (NameFilter.FilterTypes)char.ToLower(filterStr[0]);
			filter.FilteredName = filterStr.Substring(2);

			return filter;
		}

		private static IEnumerable<string> ResolveFilesByMask(string filePathMask)
		{
			var files = Directory.GetFiles(Path.GetDirectoryName(filePathMask), Path.GetFileName(filePathMask));

			return files.Where(
					file =>
					(Path.GetExtension(file) == ".dll" || Path.GetExtension(file) == ".exe") && File.Exists(file)
				);
		}

		private static void ShowHelp()
		{
			Console.WriteLine(
@"Usage: coverage.exe {<assemblyPaths>} [{-[<ExclusionType>:]NameFilter}] [<commands> [<commandArgs>]]

Exclusion Types:
    f:  filter files by name

    s:  filter assemblies by name
           This could be useful if strong name for some
           assembly should be weakened, however, coverage
           report is redundant for it

    t:  filter types by full name

    m:  filter methods by full name

    default (a:)  filter members by their custom attribute names

Commands:
    /r  If this command is selected - instrumented assemblies replace existing ones.
        Old assemblies are backed up along with corresponding pdb files

    /x  Path to a coverage xml file

Example command line to launch instrumenting:
    coverage myapp.exe myapp.lib.dll -CodeGeneratedAttribute -t:Test /r /x coverage2.xml

    This will generate instrumented myapp.exe and myapp.lib.dll, moving old assemblies into
    myapp.bak.exe and myapp.lib.bak.dll respectively. Members marked by attributes that contain
    'CodeGeneratedAttribute' in their name as well as types that contain 'Test' in their full names
    will be excluded from report");
		}
	}
}

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 MIT License

Share

About the Author

Sergiy Sakharov
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 25 Aug 2009
Article Copyright 2009 by Sergiy Sakharov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid