Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Releasing class library source, without signing key file, but unit-tests requires access to internal classes, what to do?

The situation is as follows:

  1. I want to release the full source to a class library
  2. I want to release binaries as well, signed by me, with a key file I don't want to publish
  3. I will provide batch files, and pre-build stepts, that creates a new key file locally if not present, so that anyone can quickly start using the source code
  4. The test project needs reference to an internal class in the main project
  5. To get access to the internal class, I need to add a [assembly: InternalsVisibleTo("...")] attribute to the main project AssemblyInfo.cs file
  6. Since I'm signing the project output, I need to specify a PublicKey portion of that attribute
  7. This will then be bound to the key file, that I'm unwilling to publish

So, how do I solve this?

If I sign the main project output, and not the test library, and specify only the assembly name in the InternalsVisibleTo attribute, I get this compile-time error:

Error 1 Friend assembly reference 'Mercurial.Net.Tests' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations. C:\Dev\VS.NET\Mercurial.Net\Mercurial.Net\Properties\AssemblyInfo.cs 22 31 Mercurial.Net

So apparently not signing the test project output isn't enough.

Is my only option to remove the settings that sign the projects, and modify the project files as part of my binaries build script? ie. hunt down the <SignAssembly>false</SignAssembly> element of the project file and modify it, before building?

like image 829
Lasse V. Karlsen Avatar asked Nov 14 '10 19:11

Lasse V. Karlsen


2 Answers

Is it an option to have an SNK file you release just for testing?

You can then have two InternalsVisibleTo and switch which one you use with an #if, e.g.:

#if FOR_RELEASE
[InternalsVisibleTo(... your private SNK ...)]
#else
[InternalsVisibleTo(... test SNK which you release ...)]
#endif

You can then set FOR_RELEASE when you're creating your builds that you want to publish.

like image 197
Pieter van Ginkel Avatar answered Nov 10 '22 01:11

Pieter van Ginkel


What I did with my OS projects is simple: I have a private SNK that I only use when building the projects for a release. The projects are only signed when I compile for a release and normally the projects don't reference a SNK. This makes it easy to use the InternalsVisibleTo attribute, because without a SNK this always works.

like image 1
Steven Avatar answered Nov 10 '22 03:11

Steven