Code Monkey home page Code Monkey logo

Comments (5)

fflaten avatar fflaten commented on September 23, 2024

Hi. This should work. Have you verified the contents of $azResources? And/or the value of $LASTEXITCODE before these lines?

$LASTEXITCODE is only set by applications, so the value 1 could originate from a prior command. If the mock works as expected, it wouldn't change that variable back to 0.

from pester.

ktran1005 avatar ktran1005 commented on September 23, 2024

Hi @fflaten, I know the return value from the az command will be under JSON format so my actual mock looks like this:
Mock az { '[ { "id": "something...", "name": "something..." } ]' } -ParameterFilter {"$args" -match 'resource list'}
then I passed arguments for the script that I want to test:
& "$PSScriptRoot\myscript.ps1" $arg1 $arg2...
Should -Invoke az
I just checked the $LASTEXITCODE (by using Write-Host $LASTEXITCODE) before and after executing az command the value of $LASTEXITCODE is blank. I commented out all the code in that script and leave only 2 lines as given, it throws the same error.

Update: I checked the content of $azResources. It matches with the value I set in my mock.

from pester.

fflaten avatar fflaten commented on September 23, 2024

Update: I checked the content of $azResources. It matches with the value I set in my mock.

Then $LASTEXITCODE is changed by something else in the script if it changes during the test. Pester only modifies it at the end of Invoke-Pester to number of failed tests.

Isolated repro:

Describe 'd' {
    It 'i' {
        & whoami # to reset lastexitcode
        $LASTEXITCODE | Should -Be 0 -Because 'should not be non-zero before we start'

        $scriptContent = @'
$azResources = (az resource list --resource-group $args[0]) | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) { throw "az resource list returned a $LASTEXITCODE exit code." }
$azResources
'@
        Set-Content -Path 'TestDrive:/myscript.ps1' -Value $scriptContent

        Mock -CommandName az -ParameterFilter { "$args" -match 'resource list' } -MockWith {
            '[ { "id": "something...", "name": "something..." } ]'
        }

        $res = & 'TestDrive:/myscript.ps1' 'hello'
        $res.id | Should -Be 'something...'
        $LASTEXITCODE | Should -Be 0
    }
}

# Tests pass

So ready to close? 🙂

from pester.

ktran1005 avatar ktran1005 commented on September 23, 2024

Hi @fflaten. Thanks for your suggestion. I followed your code above and run from my side. Everything works fine. However, when I tried to do something like this:

myscript.ps1:

write-host $LASTEXITCODE # gives me blank here leading to if statement
$azResources = (az resource list --resource-group $args[0]) | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) { throw "az resource list returned a $LASTEXITCODE exit code." }

myscript.Tests.ps1

Describe 'a' {
    It 'testing' {
       Mock -CommandName az -ParameterFilter { "$args" -match 'resource list' } -MockWith {
           '[ { "id": "something...", "name": "something..." } ]'
       }
       $res = & './myscript' 'hello'
    }
}

For some reasons, the $LASTEXITCODE starts from blank instead of 0 as you said. I have no idea why this happens. I don't know if I have to reset like the way you did before executing the script to set $LASTEXITCODE to 0

from pester.

fflaten avatar fflaten commented on September 23, 2024

I don't know and can't reproduce unfortunately. Code above prints 0 for me as expected.

You could try Write-Host $global:LASTEXITCODE and see if that works, in case something has set $LASTEXITCODE to ex. $null earlier in your code (or session profile?). Whatever it is, it doesn't seem to be Pester-specific, so closing this issue. 🙂

To further troubleshoot, I can recommend the PowerShell Discord-server (https://aka.ms/psdiscord). There's also a #testing channel for discussion regarding Pester and other testing.

from pester.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.