Added some comments and start work on unit tests

This commit is contained in:
Stephen Maunder 2017-11-16 11:56:47 +00:00
parent 00305c6baf
commit 34ce2d2c8c
6 changed files with 207 additions and 24 deletions

95
SnipeItPS.build.ps1 Normal file
View file

@ -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+'')(?<ModuleVersion>.*)(?='')') {
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

17
SnipeitPS/.gitlab-ci.yml Normal file
View file

@ -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

View file

@ -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)]

View file

@ -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(

0
SnipeitPS/build.ps1 Normal file
View file

53
Tests/SnipeItPS.Tests.ps1 Normal file
View file

@ -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