From 34ce2d2c8c7248b15c9ec5dd5b178c86c0752a23 Mon Sep 17 00:00:00 2001 From: Stephen Maunder Date: Thu, 16 Nov 2017 11:56:47 +0000 Subject: [PATCH] Added some comments and start work on unit tests --- SnipeItPS.build.ps1 | 95 ++++++++++++++++++++++++++++++++++++ SnipeitPS/.gitlab-ci.yml | 17 +++++++ SnipeitPS/Public/Assets.psm1 | 49 ++++++++++--------- SnipeitPS/Public/Users.psm1 | 17 +++++++ SnipeitPS/build.ps1 | 0 Tests/SnipeItPS.Tests.ps1 | 53 ++++++++++++++++++++ 6 files changed, 207 insertions(+), 24 deletions(-) create mode 100644 SnipeItPS.build.ps1 create mode 100644 SnipeitPS/.gitlab-ci.yml create mode 100644 SnipeitPS/build.ps1 create mode 100644 Tests/SnipeItPS.Tests.ps1 diff --git a/SnipeItPS.build.ps1 b/SnipeItPS.build.ps1 new file mode 100644 index 0000000..cfc84e5 --- /dev/null +++ b/SnipeItPS.build.ps1 @@ -0,0 +1,95 @@ +[CmdletBinding()] +param() + +$DebugPreference = "SilentlyContinue" +$WarningPreference = "Continue" +if ($PSBoundParameters.ContainsKey('Verbose')) { + $VerbosePreference = "Continue" +} + +if (!($env:releasePath)) { + $releasePath = "$BuildRoot\Release" +} +else { + $releasePath = $env:releasePath +} +$env:PSModulePath = "$($env:PSModulePath);$releasePath" + + +# Ensure Invoke-Build works in the most strict mode. +Set-StrictMode -Version Latest + + +# region build +# Synopsis: Build shippable release +task Build GenerateRelease, UpdateManifest + +# Synopsis: Generate .\Release structure +task GenerateRelease { + # Setup + if (-not (Test-Path "$releasePath\SnipeitPS")) { + $null = New-Item -Path "$releasePath\SnipeitPS" -ItemType Directory + } + + # Copy module + Copy-Item -Path "$BuildRoot\SnipeitPS\*" -Destination "$releasePath\SnipeitPS" -Recurse -Force + # Copy additional files + <#$additionalFiles = @( + "$BuildRoot\CHANGELOG.md" + "$BuildRoot\LICENSE" + "$BuildRoot\README.md" + ) + Copy-Item -Path $additionalFiles -Destination "$releasePath\SnipeitPS" -Force#> +} + +# Synopsis: Update the manifest of the module +task UpdateManifest GetVersion, { + $ModuleAlias = (Get-Alias | Where source -eq JiraPS) + + Remove-Module JiraPS -ErrorAction SilentlyContinue + Import-Module "$releasePath\SnipeitPS\SnipeitPS.psd1" + Update-Metadata -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -PropertyName ModuleVersion -Value $script:Version + # Update-Metadata -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -PropertyName FileList -Value (Get-ChildItem $releasePath\SnipeitPS -Recurse).Name + if ($ModuleAlias) { + Update-Metadata -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -PropertyName AliasesToExport -Value @($ModuleAlias.Name) + } + Set-ModuleFunctions -Name "$releasePath\SnipeitPS\SnipeitPS.psd1" -FunctionsToExport ([string[]](Get-ChildItem "$releasePath\SnipeitPS\public\*.ps1").BaseName) +} + +task GetVersion { + $manifestContent = Get-Content -Path "$releasePath\SnipeitPS\SnipeitPS.psd1" -Raw + if ($manifestContent -notmatch '(?<=ModuleVersion\s+=\s+'')(?.*)(?='')') { + throw "Module version was not found in manifest file," + } + + $currentVersion = [Version] $Matches.ModuleVersion + if ($env:APPVEYOR_BUILD_NUMBER) { + $newRevision = $env:APPVEYOR_BUILD_NUMBER + } + else { + $newRevision = 0 + } + $script:Version = New-Object -TypeName System.Version -ArgumentList $currentVersion.Major, + $currentVersion.Minor, + $newRevision +} + + +# endregion + + +#region Cleaning tasks +task Clean RemoveGeneratedFiles +# Synopsis: Remove generated and temp files. +task RemoveGeneratedFiles { + $itemsToRemove = @( + 'Release' + '*.htm' + 'TestResult.xml' + ) + Remove-Item $itemsToRemove -Force -Recurse -ErrorAction 0 +} + + + +task . Build, Clean \ No newline at end of file diff --git a/SnipeitPS/.gitlab-ci.yml b/SnipeitPS/.gitlab-ci.yml new file mode 100644 index 0000000..c3b8b1a --- /dev/null +++ b/SnipeitPS/.gitlab-ci.yml @@ -0,0 +1,17 @@ +stages: + - test +# - release +variables: + GIT_SSL_NO_VERIFY: "true" +Test: + stage: test + script: + - powershell -Command "./build.ps1" "-Task Pester" +# except: +# - master +#Release: +# stage: release +# script: +# - .\build.ps1 -Tasks 'analyze','test','release' +# only: +# - master \ No newline at end of file diff --git a/SnipeitPS/Public/Assets.psm1 b/SnipeitPS/Public/Assets.psm1 index eccc456..8b695f6 100644 --- a/SnipeitPS/Public/Assets.psm1 +++ b/SnipeitPS/Public/Assets.psm1 @@ -1,3 +1,21 @@ +<# +.SYNOPSIS +# Gets a list of Snipe-it Assets + +.PARAMETER url +URL of Snipeit system, can be set using Set-Info command + +.PARAMETER apiKey +Users API Key for Snipeit, can be set using Set-Info command + +.EXAMPLE +Get-Asset -url "https://assets.dip.co.uk" -token "token..." + +.EXAMPLE +Get-Asset -url "https://assets.dip.co.uk" -token "token..." | Where-Object {$_.name -eq "SUPPORT23" } + +#> + function Get-Asset() { Param( @@ -33,25 +51,17 @@ function New-Asset() [parameter(mandatory=$true)] [string]$apiKey, - [string]$CPU, - - [string]$memory, - - [string]$OSDrive, - - [string]$OS + [hashtable] $customfields ) $Values = @{ "name" = $Name "status_id" = $status_id "model_id" = $model_id - _snipeit_cpu_2 = $CPU - _snipeit_memory_gb_3 = $memory - _snipeit_os_drive_4 = $OSDrive - _snipeit_os_5 = $OS } + $Values += $customfields + $Body = $Values | ConvertTo-Json; $result = Invoke-Method -URi "$url/api/v1/hardware" ` @@ -83,25 +93,16 @@ function Set-Asset() [parameter(mandatory=$true)] [string]$apiKey, - [string]$CPU, - - [string]$memory, - - [string]$OSDrive, - - [string]$OS + [hashtable] $customfields ) $Values = @{ - "name" = $asset_name + "name" = $Name "status_id" = $status_id "model_id" = $model_id - "_snipeit_cpu_2" = $CPU - "_snipeit_memory_gb_3" = $memory - "_snipeit_os_drive_4" = $OSDrive - "_snipeit_os_5" = $OS } + $Values += $customfields $Body = $Values | ConvertTo-Json; $result = Invoke-Method -URi "$url/api/v1/hardware/$id" ` @@ -112,7 +113,7 @@ function Set-Asset() $result } -function Checkout-Asset() +function Set-AssetOwner() { Param( [parameter(mandatory=$true)] diff --git a/SnipeitPS/Public/Users.psm1 b/SnipeitPS/Public/Users.psm1 index 78a5841..c2b3cee 100644 --- a/SnipeitPS/Public/Users.psm1 +++ b/SnipeitPS/Public/Users.psm1 @@ -1,3 +1,20 @@ +<# +.SYNOPSIS +# Gets a list of Snipe-it Users + +.PARAMETER url +URL of Snipeit system, can be set using Set-Info command + +.PARAMETER apiKey +Users API Key for Snipeit, can be set using Set-Info command + +.EXAMPLE +Get-Users -url "https://assets.dip.co.uk" -token "token..." + +.EXAMPLE +Get-Users -url "https://assets.dip.co.uk" -token "token..." | Where-Object {$_.username -eq "stephenm" } + +#> function Get-Users() { Param( diff --git a/SnipeitPS/build.ps1 b/SnipeitPS/build.ps1 new file mode 100644 index 0000000..e69de29 diff --git a/Tests/SnipeItPS.Tests.ps1 b/Tests/SnipeItPS.Tests.ps1 new file mode 100644 index 0000000..40d25e9 --- /dev/null +++ b/Tests/SnipeItPS.Tests.ps1 @@ -0,0 +1,53 @@ +$ModulePath = Split-Path -Path $PSScriptRoot -Parent +$ModuleName = Split-Path -Path $ModulePath -Leaf + +# Make sure one or multiple versions of the module are not loaded +Get-Module -Name $ModuleName | Remove-Module + +# Import the module and store the information about the module +$ModuleInformation = Import-Module -Name "$ModulePath\$ModuleName.psd1" -PassThru +$ModuleInformation | Format-List + +# Get the functions present in the Manifest +$ExportedFunctions = $ModuleInformation.ExportedFunctions.Values.Name + +# Get the functions present in the Public folder +$PS1Functions = Get-ChildItem -Path "$ModulePath\Public\*.ps1" + +Describe "$ModuleName Module - Testing Manifest File (.psd1)" { + Context "Manifest" { + It "Should contain RootModule" { + $ModuleInformation.RootModule | Should Not BeNullOrEmpty + } + + It "Should contain ModuleVersion" { + $ModuleInformation.Version | Should Not BeNullOrEmpty + } + + It "Should contain GUID" { + $ModuleInformation.Guid | Should Not BeNullOrEmpty + } + + It "Should contain Author" { + $ModuleInformation.Author | Should Not BeNullOrEmpty + } + + It "Should contain Description" { + $ModuleInformation.Description | Should Not BeNullOrEmpty + } + + It "Compare the count of Function Exported and the PS1 files found" { + $status = $ExportedFunctions.Count -eq $PS1Functions.Count + $status | Should Be $true + } + + It "Compare the missing function" { + If ($ExportedFunctions.count -ne $PS1Functions.count) { + $Compare = Compare-Object -ReferenceObject $ExportedFunctions -DifferenceObject $PS1Functions.Basename + $Compare.InputObject -Join ',' | Should BeNullOrEmpty + } + } + } +} + +Get-Module -Name $ModuleName | Remove-Module \ No newline at end of file