2

Now before this is marked as duplicate I saw other, similar, related questions, but I don't really think they are a good fit to my scenario.

I'm dealing with badly written, evil code that I have no choice but to use in unit tests. It is essentially a library to read and merge some special formatted config files. Now in the code assumption is made that it will be used from executable applications, so it tries to load .exe config files. Obviously this is not the case if used from unit tests, so it fails by throwing an exception.

To somehow overcome this limitation, I want to use preprcessor directives to change some parts of the code when used from unit test project.

Let's say I have 3 projects.

  1. Class library project -> Evil
  2. Console application project -> Proj1 (references Evil)
  3. Unit test project -> Test1 (references Proj1 and Evil)

Test1 & Evil have additional UNITTEST symbol defined (Build tab -> Conditional compilation symbols), and Evil has some code like this

#if !UNITTEST
// do evil stuff
#else
// do stuff for unit test
#endif

Now what I want is to define #UNITTEST in Test1 project's test class and have it reflected in Evil project.

From what I've seen in other answers - the only suggested way is to build with proper symbols for each case (when running unit tests, when debugging locally, when preparing a package for release), which looks cumbersome to me.

What I'm looking for is a way to do less manual intervention and be able to easily have the version I need. As an example, when I build and Test1 has UNITTEST symbol defined then Evil is built with UNITTEST symbol as well (and copied to Test1's output) and the Proj1 will always reference version of Evil built with DEBUG or RELEASE based on Proj1's setup.

Is there any way to achieve this ?

I'm open to other suggestions to solve the problem. Unfortunately refactoring/replacing Evil with something else is not an option (too much effort which can't be done now).

Community
  • 1
  • 1
Michael
  • 2,961
  • 2
  • 28
  • 54
  • Are you asking whether it is possible to cascade precompiler directives ? – PhillipH Mar 16 '17 at 20:39
  • I guess this post has 2 questions - is it possible to cascade precompiler directive & if so how ? – Michael Mar 16 '17 at 20:44
  • 3
    Then the answer is "no". C# does not support #include so a directive to the compiler cannot be applied to multiple projects in a VS.net solution. – PhillipH Mar 17 '17 at 07:33
  • If the only issue you are trying to solve is to open config from 'exe' file - then why not just create a copy of .config.dll -> .config.exe during tests build? – Lanorkin Mar 17 '17 at 16:04
  • @Lanorkin thanks or the input - that's not the only issue unfortunately (otherwise that small piece could have been refactored). There are many other similar assumptions. I brought `.exe` config example only for the sake of clarity. – Michael Mar 18 '17 at 17:14

0 Answers0