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.
- Class library project -> Evil
- Console application project -> Proj1 (references Evil)
- 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).