My problem is that, somewhere, somehow, and only sporadically, Environment.CurrentDirectory
gets set to C:\Windows\System32
when my test suite is running. A few tests depend on the current directory, and they fail as a result.
I've done a find-all in my project and confirmed that none of my code is setting that property. I use numerous 3rd-party libraries though, so my suspicion is that one of them is modifying the current directory.
I've done some searching specifically for the current directory changing to system32, but I've come up empty. Are there some common reasons why this might happen?
The other approach, which similarly came up empty, is: how might I track down the culprit? As far as I know, I can't have Visual Studio break anywhere when a value has changed. Because it's an environment variable and in the .NET libraries, I can't set a breakpoint in the setter.
I thought of using procmon or something, which hopefully records when the current directory has changed, but then it's not pausing the execution when it happens, so I still have no idea where it occurred.
I'm just at a loss as to how I might debug this. Any ideas? Or is my only recourse to save Environment.CurrentDirectory
to a global readonly field and refer exclusively to that in my code, allowing whatever it is to modify the current directory with impunity?
First of all, it's a very bad approach to do unit testing that relies on external sources. When somebody else will test your code, the unit tests depending on the current directory might fail.
Why don't you use Microsoft Fakes to return a specific value for the Directory?
In order to create a fake, in the reference of the Unit Test, right click the reference and click on "Add Fakes Assembly".
For Example, if your unit test relies on the Current Date, a Fake can generate another date/time with the following code:
ShimDateTime.NowGet = ()=> new DateTime(2012, 12, 21);
I recommended reading this post. It's of a very high value: http://www.dotnetcurry.com/showarticle.aspx?ID=963
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With