I've built a solution with three projects, using libgit2sharp. The solutions are:
1. A class library, referencing libgit2sharp (with a post build event, which copies the native git binaries into the build directory).
2. A console project, which calls a method in the class library.
3. A test project, which calls the same method.
Obviously this setup is analogous to a real situation, where I have a class library (which internally uses libgit2sharp) and an actual project which uses this library.
When I run the console project, and when I run the tests with the built in Visual Studio test runner everything is fine. However when I run the tests they fail with this message:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets (4291)#0: The command "
if not exist "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries" md "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries"
if not exist "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries\x86" md "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries\x86"
xcopy /s /y "R:\8556\25\packages\LibGit2Sharp.0.11.0.0\NativeBinaries\x86\*.*" "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries\x86"
if not exist "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries\amd64" md "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries\amd64"
xcopy /s /y "R:\8556\25\packages\LibGit2Sharp.0.11.0.0\NativeBinaries\amd64\*.*" "R:\8556\25\GitClassLibrary\bin\Debug\NativeBinaries\amd64"" exited with code 4.
I'm not too familiar with post-build events, so I'm not too sure what this is about. But it's definitely something NCrunch is doing that neither a vanilla run or the VS test runner do.
Aha, that fixed the post build event outright failing. But now I get a "System.DllNotFoundException" - i.e. the post build event hasn't properly put the files into a place where they could be found by libgit2sharp.
Is this exception being thrown during test execution? It may be worth right-clicking on the project in the Tests Window, and going to "Advanced->Browse to workspace" if you'd like to compare NCrunch workspace files with those in your normal working solution. This is often a quick and easy way to examine whether every file is where it should be.
I tried copying assemblies to the workspace, this made no difference.
Browsing to the workspace shows me that in the bin folder for the TestProject is TestProject1.dll and TestProject1.pdb, so it looks like the NativeBinaries folder hasn't been copied across.
My guess at the moment is that the post build event is copying the files to the build output directory of GitClassLibrary, although either the test code or the code under test is trying to find the files in the build output directory of TestProject. When running the tests outside of NCrunch, both of these output directories will likely be the same, but in the NCrunch test environment, they are different. The 'Copy referenced assemblies to workspace' option will automatically copy GitClassLibrary into the TestProject output directory, but it won't know about the NativeBinaries directory and thus won't copy it.
Is it possible to also set up the post build event so it will work on TestProject?
Just noting earlier you mentioned that this was a test solution you're working with. If it's something you can share, feel free to submit it to me through the contact form and I'll see if I can find a way to make it work.
I added the post build event to the test project as you suggested, now I end up with a NativeBinaries directory, as expected, in the TestProject1/bin directory. However, the test still fails with a DLL not found exception. My guess is libgit2sharp relies on the working directory being set to the build directory which I suspect isn't how NCrunch sets it up?
The test solution is uploading via the contact form right now.
Thanks for sending through the sample solution. I've managed to get this to work by turning on the 'Copy referenced assemblies to workspace' option for ALL the projects in the solution. It looks as though the issue was that ALL dependencies needed to exist in the build output directory of the test project, and the nested dependencies weren't all being copied without the configuration setting being applied to everything.
The reliance on this configuration setting is unfortunate as it will create more build work for NCrunch while you are coding. Sadly, it look as though the assumptions around assembly referencing are coded into Libgit2sharp itself, so there may not be any other way to make this work without changing the library.
Do you have any recommendations for how libgit2sharp could be changed to fix this? If you do then I may create an issue on the github and link this thread :)
It's tough to give concrete advice without delving into the source code, but generally you should try to avoid loading assemblies into the application domain dynamically from the current directory. Instead, it may be better if there was a way to configure the search path for the native binaries (which are effectively resource files). As the developer integrating with libgit2sharp, you would have knowledge of where these resources are stored and which project they are primarily related to - allowing you to better control the loading behaviour by injecting a path relative to the location of your primary project (perhaps by using GetType().Assembly.Location).