Skip to content

Instantly share code, notes, and snippets.

@nohwnd
nohwnd / conditionals.ps1
Created March 14, 2017 17:27
No logic in tests
#ex1
# say not to this one
$configuration:Servers | foreach {
It "A test" {
# code
}
}
# use this instead
It "A test" -TestCases @(
@nohwnd
nohwnd / TimesTwo.ps1
Created March 19, 2017 09:03
Why Pester tests example
# author Dave Wyatt, I just copied the code from here https://blogs.technet.microsoft.com/heyscriptingguy/2015/12/14/what-is-pester-and-why-should-i-care/
function TimesTwo ($value) {
return $value * 2
}
Describe 'TimesTwo' {
Context 'Numbers' {
It 'Multiplies numbers properly' {
TimesTwo 2 | Should Be 4
}
@nohwnd
nohwnd / PesterInDocker.ps1
Created March 21, 2017 09:28
Run Pester in docker
$path = "C:\Projects\pester_nohwnd"
$volumePath = "//" + ($path -replace "\\","/" -replace "\:")
docker run --rm -v ${volumePath}:/home/pester microsoft/powershell --% cd /home/pester/; import-module ./pester.psd1; $result = Invoke-Pester -PassThru ; Export-Clixml -InputObject $result ./result.clixml
$result = $path + "/result.clixml" | Import-Clixml
$result.TestResult | Where Result -NE Passed
@nohwnd
nohwnd / InCallerScope.ps1
Last active March 23, 2017 17:28
Invoke code in caller scope by manipulating the session state, like in Pester
function Set-ScriptBlockScope
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[scriptblock]
$ScriptBlock,
[Parameter(Mandatory = $true, ParameterSetName = 'FromSessionState')]
[System.Management.Automation.SessionState]
@nohwnd
nohwnd / compound-assertions.ps1
Created August 17, 2017 21:00
Proof of concept to avoid assertion roulette by composing the assertion results and throwing at the end so you always get complete information from your tests
$script:scope = ""
$script:results = @{}
function Get-Scope () { $script:scope }
function Set-Scope ($Name) {
$script:scope = $Name
}
function Add-Result ($Result, $Scope = (Get-Scope)) {
@nohwnd
nohwnd / poopcompiler.ps1
Created December 6, 2017 15:20
emojicode in PowerShell
function 🐙 {
$name,$params = $args.Where({$_ -eq '💩'},'Until')
$null,$groups = $args.Where({$_ -eq '💩'},'SkipUntil')
$params = @($params |? {$_})
$bodyText = $groups | %{
$enumerator = [System.Globalization.StringInfo]::GetTextElementEnumerator($_)
$letters = while ($enumerator.MoveNext())
@nohwnd
nohwnd / foo.tests.ps1
Created December 6, 2017 16:02
pester fails to remove function
InModuleScope Pester {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '', Scope = '*', Target = 'SuppressImportModule')]
$SuppressImportModule = $true
. $PSScriptRoot\Shared.ps1
Describe 'Function-Name' {
Context "asdf" {
@nohwnd
nohwnd / Uninstall-Pester.ps1
Last active March 14, 2024 13:57
Remove built-in version of Pester 3 (or -All) from Windows 10 Program Files and Program Files (x86).
#Requires -RunAsAdministrator
function Uninstall-Pester ([switch]$All) {
if ([IntPtr]::Size * 8 -ne 64) { throw "Run this script from 64bit PowerShell." }
#Requires -RunAsAdministrator
$pesterPaths = foreach ($programFiles in ($env:ProgramFiles, ${env:ProgramFiles(x86)})) {
$path = "$programFiles\WindowsPowerShell\Modules\Pester"
if ($null -ne $programFiles -and (Test-Path $path)) {
if ($All) {
@nohwnd
nohwnd / pileOfCubes.hs
Created March 4, 2018 13:40
Pile of cubes
findNb :: Integer -> Integer
findNb m =
if (volume == m) then
fromIntegral $ length cubes
else
-1
where
cubes = listCubesTillVolume m
volume = last cubes
@nohwnd
nohwnd / helptest.ps1
Created September 30, 2018 08:43
Automatic example testing
# command to be tested
$commandName = 'Get-Command'
# get all examples from the help
$examples = Get-Help $commandName -Examples
# make a describe block that will contain tests for this
Describe "Examples from $commandName" {
$examples.Examples.Example | foreach {
# examples have different format,
# at least the ones I used that MS provided