Any dynamic way to use "using" statement in PowerShell? - powershell

I'm having issues with the using statement which can be seen here. This statement (unlike Import-Module) is required in order to get an actual class definition from a PowerShell module. I have a use case for this because I need the class definition itself for Pester unit testing.
The problem is I can't find any way to provide the module path dynamically. I'm unable to use variables like $PSScriptPath in the path. I'm only able to provide it with a absolute path or a relative path. Neither of these really work for me.
Absolute path: Issue here is unit tests will be run on different machines (build servers). Therefore, having a hardcoded absolute path from a dev machine isn't an option.
Relative path: This also is problematic, since unit tests are triggered from a base directory which then run all Test files recursively. So I would have to Set-Location to specific directories for each test in order for relative paths to function. To tests dozens of modules, this is a lot of overhead to have to manage when I should be able to just run all my unit tests without this hassle.
Is there a better way I can manage this? General directory structure is like this...
src
project1
someModule1
someModule1.psm1
someModule2
someModule2.psm1
someModule3
someModule3.psm1
tests
someModule1.Tests.ps1
someModule2.Tests.ps1
someModule3.Tests.ps1
From src directory, run Invoke-Pester -Script . to run all unit test files. The "Tests" files need to be able to import classes from their corresponding modules. Worse yet, I'm not getting any errors when the using statement doesn't find the module path unless I run the using statement interactively in the console. Any ideas?

Are the modules installed properly in a PSmodule path?
Are the modules installed. If so you can get the path to them by querying:
(Get-Module -Name someModulex -ListAvailable).ModuleBase

Related

How to run tests under a specific folder with NUnit 3?

I have a lot of tests (classes) and they are placed to different folders.
It looks like this:
I want to run all the tests under a specific folder including all sub-folders.
Please help me to do it using NUnit 3 console commands.
Here is a manual for this https://github.com/nunit/docs/wiki/Test-Selection-Language
The thing is: if I use: --where "test==LottoSend.com.TestCases.BackOffice" then it runs only tests under sub-folders in BackOffice folder (CMS, Packages, Raffle, etc.) but it doesn't run tests placed directly in "BackOffice" folder (such as BlackListTests.cs etc.)
Maybe I need to use another parameter for this?
NUnit knows nothing about the location of your source code. It doesn't look need or look at source code at all, but at the compiled test assembly.
If it's running the tests in the BackOffice folder, it's because they are all defined in the namespace "LottoSend.com.TestCases.BackOffice" - not because of what folder they are in.
What namespaces are used in your subfolders? Common practice would put the code under CMS in "LottoSend.com.TestCases.BackOffice.CMS" but it's up to you how you write the code.
So your choice is to either change the namespaces to match the folders or move the code to a folder that matches the namespace.

Is there an environment variable or way to get custom task directory in VSO Build?

So I'm trying to get the custom build task directory name from powershell when executing a custom build task.
The purpose is that I want jshint to run on build time, and I've got it doing so, but the .jshintignore file needs to know a relative path to exclude files or folders.
So I need be able to get that path at runtime in order to know how many "../" to add on to the excluded files for the minmatch engine, which is what jshint uses, to match them.
I can, of course, hard code it, but that's really not what I'd prefer to do.
Yes, you can use the Agent.HomeDirectory variable.
Agent.HomeDirectory | AGENT_HOMEDIRECTORY | The directory the agent is
installed into. This contains the agent bits
The tasks folder will be $(Agent.HomeDirectory)\tasks(TaskFolder)\

run pydev project from file-system (with imports from different packages)

I want to run my working pydev project python code by double clicking the main module (outside of eclipse): xxx.py
The problem is that due to my imports being in different packages:
from src.apackage.amodule import obj
when xxx.py is double clicked it complains it doesn't know where the imports are (even though when I run xxx.py in pydev it magically knows what I'm importing).
A simple workaround is to remove all of the packages and move all of the modules into one directory (that obviously works but is very inconvenient)
How can I run my code in the file system without doing that work around?
This page answers my question excellently:
http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/
Bottom line is always execute your code from the top, highest level, root directory (e.g. using a minimal main.py file that executes the main script of your program). Then, use absolute imports always and you never have a missing module issue since you start the program from the top directory and all imports are based off that 'home' path.
The problem you encountered is the natural behavior of most languages. A programm only knows about its working path (the path it is started in), the paths which are registered in the environment variables and at least relative paths.
The "magic" of the executable you created is therefore: It collects all scripts/modules needed, and copies/combines them next to/in the executable. The Executable then runs within the directory where all other scripts also reside and voila ...
If you are not happy with your workaround of creating an executable every time you want to run your project without PyDev there are two alternatives.
First but not the one I would suggest is registering the working path into in the environment variables.
Second and the one I think is much better: Create a link to the python executable and alter the calling string of the textfield "Target:". Append the path to your script you would like to run. Then alter the textfield "Start in:" and enter the project directory. After you did this you should be able to start your project with a simple double click.
(If you rely on external libraries which are neither on the path nor in you project you could search for appending paths temporarily to the pythonpath via the sys module.)
I hope I could help a bit.

NUnit read file from source Bin

I've a class library in visual studio with a method that just checks if specified file exists or not. If I pass just file name (without full path) of some text file which exists in the bin directory, it works fine by identifying its existence.
Hence File.Exists("myfile.txt") works if myfile.txt is in bin directory.
But when I load a test case from NUnit GUI which executes this method, it fails to read the file. Likely because bin directory executing NUnit is different than original bin where dll and myfile.txt reside. How can I tackle this in my NUnit without resorting to hardcoded full path?
In your tests pass a relative path to the method of the class under test. This avoids resorting to a hard coded full path and as long as your test project is always in the same location relative to your source project it'll work.
e.g. if you have your source set up like this:
\Solution\src\Project\bin\debug\myFile.txt
\Solution\test\TestProject\bin\debug\TestAssembly.dll
The relative path will be #"..\..\..\..\Project\bin\debug\myfile.txt"
Update
I'm not quite sure why your tests are running from a temporary folder. I either use a test runner such as Resharper or set up my test project as follows:
Open the project properties for the project containing your tests.
Go to the Debug tab and set the following values:
Start external program: Enter the location of nunit.exe, e.g. on my PC it's installed to C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit.exe.
Command line arguments: Enter the name of your assembly containing your tests followed by the run argument, e.g. TestProject.dll /run.
Set the project containing your tests as the StartUp Project.
Hit F5.
This way your tests will always run from bin\debug (depending on how your build is configured), so you can rely on projects always being in the same relative location.

Environment.CurrentDirectory with NUnit GUI differs to the TeamCity value, how can I sync them?

As above really, I have some integration tests that use files from a relative file path. To help picture it here is the file structure:
/Dependencies
/VideoTests/bin/release/video.dll
/SearchTests/bin/release/search.dll
/OtherProjects
The GUI is running the tests from the root, however when TeamCity runs the tests it is running the tests from each test dlls bin directory. Now I don't mind which one I can get to follow the other but I do need them to be the same otherwise my relative paths just won't work!
Any ideas?
P.S. Using TeamCity 5.0 and NUnit 2.5.
You probably don't want to rely on CurrentDirectory. I'd suggest reading the doc, but the main point you'll want to take away is that the CurrentDirectory is where the .exe was started from: it could be any path in the system. For example, let's assume your users add your .exe (or whatever .exe uses your DLLs) to their path. They could then navigate to c:\foo\bar and start the .exe from there, which would set the CurrentDirectory to "C:\foo\bar" and you may not be able to deal with that.
I think it would be preferable for you to rework whatever you're doing so you don't rely on CurrentDirectory. What problems are you encountering by relying on CurrentDirectory right now?
Have you made sure that both TeamCity and NUnit are using the same working directory when starting the application?
And if they aren't, you could adjust the current directory in the test code.