Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Issues copying a file during test
gregm
#1 Posted : Thursday, April 3, 2014 11:02:10 PM(UTC)
Rank: Member

Groups: Registered
Joined: 4/3/2014(UTC)
Posts: 10
Location: United States of America

Thanks: 3 times
Was thanked: 2 time(s) in 2 post(s)
I am attempting to test how a dynamically loaded library (I.e. loading an assembly using Assembly.LoadFrom method). As part of that, I need to copy the file, during the test, to some place that it works. For normal test runners (MS Test), I 'm using Assembly.GetExecutingAssembly().Location, to figure out the destination and source folders (the assembly that I'm loading is part of the same solution). However, this so doesn't work with NCrunch.

The problem is that I can't figure out the solution folder and thus the source folder when the test is run by NCrunch. Is there a way to do this?
Remco
#2 Posted : Friday, April 4, 2014 1:28:12 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi, thanks for posting!

Because NCrunch sandboxes each project in a separate dynamically assigned location, there won't be a single path that you can use to reference this assembly if it exists as a project within your solution.

If it is merely a referenced assembly (and not a project), the best way is to ensure the reference exists in a path that can be resolved relative to the project referencing it, then make sure it is included using the additional files to include configuration setting. You can then load the assembly using the relative path.

If the loaded assembly is also a project within your solution, things get a little more complicated. One way you can find the project output assembly as built by NCrunch is by adding a project reference to its source project from your test project, then using typeof(ClassWithinReferencedProject).Assembly.Location to find the assembly at runtime. Of course, this will automatically load the assembly into your application domain automatically, so it probably won't be great for testing the Assembly.LoadFrom call. Alternatively, you can look into using the methods available on the NCrunchFramework class to find the assembly using information provided by NCrunch. You can find an example of this in the tests that build their own appdomains example.
gregm
#3 Posted : Friday, April 4, 2014 4:47:38 PM(UTC)
Rank: Member

Groups: Registered
Joined: 4/3/2014(UTC)
Posts: 10
Location: United States of America

Thanks: 3 times
Was thanked: 2 time(s) in 2 post(s)
I attempted to use the "additional files to include", and to use a relative path, but I don't think I understand how this works.

Here is a snippet of the code I'm using:
Code:
      var relativePath = @"..\TestWorkflow.V1\bin\Debug\TestWorkflow.V1.dll";
      File.Copy( relativePath, workflowAssemblyDestPath );
      Assert.IsTrue( File.Exists( workflowAssemblyDestPath ) );

The relative path shown was added to the Additional Files to Include for the test project. It couldn't find this relative path (i.e. File.Copy throws an exception).

I definitely don't want to reference the project as the whole point is to load the assembly at runtime. Since you are using these workspaces, I need to get this assembly along side the testing assembly somehow.
Remco
#4 Posted : Friday, April 4, 2014 10:48:23 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Ok - it looks like the referenced assembly is being built from another project in your solution, so the 'Additional files to include' isn't the way to solve this.

The problem is that you're needing to reference an assembly that is built by NCrunch, and is sitting in one of its workspaces. You need to find this assembly before you can copy or load it. Using the NCrunchEnvironment.GetImplicitlyReferencedAssemblyLocations method in combination with the implicit project dependencies setting may be the only way you can solve this problem.

I also recommend having a read about project atomicity, as this can give some useful information about how NCrunch behaves in this situation and the limitations arising from this.
1 user thanked Remco for this useful post.
gregm on 4/4/2014(UTC)
gregm
#5 Posted : Friday, April 4, 2014 10:59:26 PM(UTC)
Rank: Member

Groups: Registered
Joined: 4/3/2014(UTC)
Posts: 10
Location: United States of America

Thanks: 3 times
Was thanked: 2 time(s) in 2 post(s)
I actually figured this out. Three things were needed:

1. I set the "Copy referenced assemblies to workspace" for the test project.
2. I used the typeof( ClassInTestProject ).Assembly to get the location of the running code (this is the destination folder of the copy).
3. Used the NCrunchEnvironment.GetOriginalSolutionPath() to get the solution path to find the source folder.

I'm also using the NCrunch.Framework.NCrunchEnvironment.NCrunchIsResident() to allow for both NCrunch and other test runners to execute the test.

The other issue I ran into was that I was deleting the file before copying it (if it existed), but apparently NCrunch is running stuff so fast that didn't work :D.

I still don't understand what the "additional files to include" is supposed to do...but I'll save that for another day!
Remco
#6 Posted : Saturday, April 5, 2014 1:27:51 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Great stuff! Something I'll warn you about with the NCrunchEnvironment.GetOriginalSolutionPath is that this will break NCrunch's workspace isolation .. if you're using this to access TestWorkflow.V1.dll then you might end up with file locking issues between VS/NCrunch and you probably won't see code coverage for the contents of this assembly. I'm not completely sure as to how things are set up on your side, so perhaps this is OK for you. Don't worry about the additional files to include, this will make much more sense when you hit a problem that requires it :)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.051 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download