Skip to content

added build scripts and pester tests #43

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

Merged
merged 7 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions azure-functions-powershell-worker.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#

param(
[string[]]
$Tasks
)

if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
Invoke-Build $Tasks $MyInvocation.MyCommand.Path @PSBoundParameters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain what is this for? Is it for when calling azure-functions-powershell-worker.build.ps1 task-A directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you got it.

you can say
azure-functions-powershell-worker.build.ps1 Clean,Build

which will do the same thing as:

Invoke-Build Clean,Build

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, is $MyInvocation.MyCommand.Path @PSBoundParameters needed here? You cannot provide more parameters other than -Tasks when calling azure-functions-powershell-worker.build.ps1, right?

return
}

$BuildRoot = $PSScriptRoot

task Clean {
exec { dotnet clean }
Remove-Item -Recurse -Force src/bin -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force src/obj -ErrorAction SilentlyContinue

# Remove the built nuget package
Remove-Item -Recurse -Force package/bin -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force package/obj -ErrorAction SilentlyContinue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not git clean -fdx?

}

task Build {
exec { dotnet build }
exec { dotnet publish }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pass in the configuration to use for the build and publish?

Set-Location package
exec { dotnet pack }
}

task Test {
Set-Location test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are assuming $pwd is at the root folder, is that a safe assumption? This is the case in other tasks too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assumed because InvokeBuild sets the location to whatever BuildRoot is set to:
https://github.com/nightroman/Invoke-Build/wiki/Build-Scripts-Guidelines#use-the-default-or-custom-buildroot

exec { dotnet test }
Set-Location Modules
Invoke-Pester
}

task . Clean, Build, Test
87 changes: 87 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env pwsh
param(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot header oops

[Parameter()]
[switch]
$Bootstrap,

[Parameter()]
[switch]
$Clean,

[Parameter()]
[switch]
$Test
)

$NeededTools = @{
DotnetSdk = ".NET SDK latest"
PowerShellGet = "PowerShellGet latest"
InvokeBuild = "InvokeBuild latest"
}

function needsDotnetSdk () {
try {
$opensslVersion = (dotnet --version)
} catch {
return $true
}
return $false
}

function needsPowerShellGet () {
$modules = Get-Module -ListAvailable -Name PowerShellGet | Where-Object Version -gt 1.6.0
if ($modules.Count -gt 0) {
return $true
}
return $false
}

function needsInvokeBuild () {
if (Get-Module -ListAvailable -Name InvokeBuild) {
return $false
}
return $true
}

function getMissingTools () {
$missingTools = @()

if (needsDotnetSdk) {
$missingTools += $NeededTools.DotnetSdk
}
if (needsPowerShellGet) {
$missingTools += $NeededTools.PowerShellGet
}
if (needsInvokeBuild) {
$missingTools += $NeededTools.InvokeBuild
}

return $missingTools
}

function hasMissingTools () {
return ((getMissingTools).Count -gt 0)
}

if ($Bootstrap) {
$string = "Here is what your environment is missing:`n"
$missingTools = getMissingTools
if (($missingTools).Count -eq 0) {
$string += "* nothing!`n`n Run this script without a flag to build or a -Clean to clean."
} else {
$missingTools | ForEach-Object {$string += "* $_`n"}
}
Write-Host "`n$string`n"
} elseif(hasMissingTools) {
Write-Host "You are missing needed tools. Run './build.ps1 -Bootstrap' to see what they are."
} else {
if($Clean) {
Invoke-Build Clean
}

Invoke-Build Build

if($Test) {
Invoke-Build Test
}
}
143 changes: 143 additions & 0 deletions test/Modules/Microsoft.Azure.Functions.PowerShellWorker.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#

Describe 'Azure Functions PowerShell Langauge Worker Helper Module Tests' {

# Helper function that tests hashtable equality
function IsEqualHashtable ($h1, $h2) {
# Handle nulls
if (!$h1) {
if(!$h2) {
return $true
}
return $false
}
if (!$h1) {
return $false
}

# If they don't have the same amount of key value pairs, fail early
if ($h1.Keys.Count -ne $h2.Keys.Count){
return $false
}

# Check to make sure every key exists in the other and that the values are the same
foreach ($key in $h1.Keys) {
if (!$h2.ContainsKey($key) -or $h1[$key] -ne $h2[$key]) {
return $false
}
}
return $true
}

Context 'Push-OutputBinding tests' {
BeforeEach {
Import-Module "$PSScriptRoot/../../src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1" -Force
$module = (Get-Module Microsoft.Azure.Functions.PowerShellWorker)[0]
}

It 'Can add a value via parameters' {
$Key = 'Test'
$Value = 5

Push-OutputBinding -Name $Key -Value $Value
$result = & $module { $script:_OutputBindings }
$result[$Key] | Should -BeExactly $Value
}

It 'Can add a value via pipeline - <Description>' -TestCases @(
@{
InputData = @{ Foo = 1; Bar = 'Baz'}
Expected = @{ Foo = 1; Bar = 'Baz'}
Description = 'ValueFromPipeline'
},
@{
InputData = @([PSCustomObject]@{ Name = 'Foo'; Value = 5 }, [PSCustomObject]@{ Name = 'Bar'; Value = 'Baz' })
Expected = @{ Foo = 5; Bar = 'Baz'}
Description = 'ValueFromPipelineByPropertyName'
}) -Test {
param (
[object] $InputData,
[hashtable] $Expected,
[string] $Description
)

$InputData | Push-OutputBinding
$result = & $module { $script:_OutputBindings }
IsEqualHashtable $result $Expected | Should -BeTrue `
-Because 'The hashtables should be identical'
}

It 'Throws if you attempt to overwrite an Output binding' {
Push-OutputBinding Foo 5
{ Push-OutputBinding Foo 6} | Should -Throw
}

It 'Can overwrite values if "-Force" is specified' {
$internalHashtable = & $module { $script:_OutputBindings }
Push-OutputBinding Foo 5
IsEqualHashtable @{Foo = 5} $internalHashtable | Should -BeTrue `
-Because 'The hashtables should be identical'

Push-OutputBinding Foo 6 -Force
IsEqualHashtable @{Foo = 6} $internalHashtable | Should -BeTrue `
-Because '-Force should let you overwrite the output binding'
}
}

Context 'Get-OutputBinding tests' {
BeforeAll {
Import-Module "$PSScriptRoot/../../src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1" -Force
$module = (Get-Module Microsoft.Azure.Functions.PowerShellWorker)[0]
& $module {
$script:_OutputBindings = @{ Foo = 1; Bar = 'Baz'; Food = 'apple'}
}
}

It 'Can get the output binding hashmap - <Description>' -TestCases @(
@{
Query = @{}
Expected = @{ Foo = 1; Bar = 'Baz'; Food = 'apple'}
Description = 'No name specified'
},
@{
Query = @{ Name = 'Foo' }
Expected = @{ Foo = 1; }
Description = 'Explicit name specified'
},
@{
Query = @{ Name = 'DoesNotExist' }
Expected = @{}
Description = 'Explicit name specified that does not exist'
},
@{
Query = @{ Name = 'F*' }
Expected = @{ Foo = 1; Food = 'apple' }
Description = 'Wildcard name specified'
}) -Test {
param (
[object] $Query,
[hashtable] $Expected,
[string] $Description
)

$result = Get-OutputBinding @Query
IsEqualHashtable $result $Expected | Should -BeTrue `
-Because 'The hashtables should be identical'
}

It 'Can use the "-Purge" flag to clear the Output bindings' {
$initialState = (& $module { $script:_OutputBindings }).Clone()

$result = Get-OutputBinding -Purge
IsEqualHashtable $result $initialState | Should -BeTrue `
-Because 'The full hashtable should be returned'

$newState = & $module { $script:_OutputBindings }
IsEqualHashtable @{} $newState | Should -BeTrue `
-Because 'The OutputBindings should be empty'
}
}
}