Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange error when running pester with a function that calls Start-Process #353

Closed
glennsarti opened this issue May 17, 2015 · 2 comments · Fixed by #354
Closed

Strange error when running pester with a function that calls Start-Process #353

glennsarti opened this issue May 17, 2015 · 2 comments · Fixed by #354

Comments

@glennsarti
Copy link
Contributor

I am trying to write unit tests for a function which eventually calls Start-Process. I've written a series of Pester tests that work fine in Powershell 4.0 and 3.0 but in 2.0 I get some strange behaviour.

Setup;
Windows 8.1 64bit - PS 4.0, .Net 4.5.1
Pester 3.3.8

Function under test - This function is in a module;

...
$result = (Start-Process -FilePath $JavaCMD.java `
    -ArgumentList $ShellArgs -Wait:$Wait -NoNewWindow:$Wait -PassThru)
...

I am not even Mocking the the Start-Process function but I received the error;

The variable cannot be validated because the value is not a valid value for the ArgumentList variable.

Note the double space in between 'value' and 's'. It's like Pester is mucking up the value being passed but I don't see how. All Mocks are within the context scope

Things I've tried but made no difference;

  • Mocking Start-Process
  • Removing the -ArgumentList from the function call (This is REALLY weird that it still gives an error!)
  • Putting in a bad parameter in the function gave a different validation message i.e. Using -ArgumentList @() gave the following error message;
    [-] Error occurred in Context block 118ms Cannot validate argument on parameter 'ArgumentList'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again. This error is expected

For the moment I'm just going to avoid those tests on PS 2.0 but any help with this issue would be great.

@nohwnd
Copy link
Member

nohwnd commented May 17, 2015

Thanks for reporting the issue. Could you please share code+test example that does not work for you? That way we can run it on our systems and determine what is wrong, without much guessing.

Btw I don't see any double spaces there. The markdown probably stripped them for you.

@glennsarti
Copy link
Contributor Author

Source Files;
https://github.com/glennsarti/spike-Neo4J-Windows-Experiments/tree/master/posh-neo-cm/src/Neo4j-Management

Test Files;
https://github.com/glennsarti/spike-Neo4J-Windows-Experiments/tree/master/posh-neo-cm/tests/unit

The script under test is Start-Neo4jShell

The current test file has a Powershell Version IF around it. You'll need to remove that if for the error to occur.

dlwyatt added a commit to dlwyatt/Pester that referenced this issue May 18, 2015
Repro for pester#353 in PowerShell 2.0.  I wasn't able to reproduce this using a function for some reason, but mocking Start-Process does cause the error to occur.

Incidentally, it's being caused by this line in MockPrototype:

[object] $ArgumentList = Get-Variable -Name args -ValueOnly -Scope Local -ErrorAction $IgnoreErrorPreference

It appears to be a problem when the mocked command also has an -ArgumentList parameter, which contains Validation attributes such as ValidateNotNullOrEmpty() (though why I wasn't able to reproduce this with a mocked function instead of Start-Process, I don't know.)
dlwyatt added a commit to dlwyatt/Pester that referenced this issue May 18, 2015
$ArgumentList variable renamed to something that's extremely unlikely to conflict with an actual parameter name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants