Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion source/functions/Add-MFAzureDevOpsScaffold.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{

$WarningPreference = 'SilentlyContinue'
#Reference Current Path
$currentPath = $(get-location).path
$sourcePath = join-path -path $currentPath -childPath 'source'
Expand Down Expand Up @@ -106,5 +110,5 @@ Describe 'Add-MFAzureDevOpsScaffold should copy if files exist and -force is use
AfterAll{
Remove-Variable mockPsScriptRoot -ErrorAction Ignore
Set-Location $currentPath
remove-Item $testPath -Force -Recurse
remove-Item $testPath -Force -Recurse -ProgressAction SilentlyContinue
}
6 changes: 5 additions & 1 deletion source/functions/Add-MFGithubScaffold.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{

$WarningPreference = 'SilentlyContinue'
#Reference Current Path
$currentPath = $(get-location).path
$sourcePath = join-path -path $currentPath -childPath 'source'
Expand Down Expand Up @@ -105,5 +109,5 @@ Describe 'Add-MFGithubScaffold should copy if files exist and -force is used' {
AfterAll{
Remove-Variable mockPsScriptRoot -ErrorAction Ignore
Set-Location $currentPath
remove-Item $testPath -Force -Recurse
remove-Item $testPath -Force -Recurse -ProgressAction SilentlyContinue
}
82 changes: 82 additions & 0 deletions source/functions/Add-MFProjectScripts.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{

$WarningPreference = 'SilentlyContinue'
$currentPath = $(Get-Location).path
$sourcePath = Join-Path $currentPath 'source'

$mockPsScriptRoot = $sourcePath

$testPath = Join-Path $currentPath 'scriptScaffoldTest'
if(!(Test-Path $testPath)){
New-Item -ItemType Directory -Path $testPath
}

Set-Location $testPath

$scriptsFolder = Join-Path $testPath 'scripts'
$pesterScript = Join-Path $scriptsFolder 'Invoke-MFPester.ps1'

$fileName = $PSCommandPath.Replace('.Tests.ps1','.ps1')
$functionName = 'Add-MFProjectScripts'
. $fileName

}

Describe 'Check Clean Environment' {
BeforeAll {
Write-Warning "PSCommandPath: $PSCommandPath; scriptToLoad: $($PSCommandPath.Replace('.Tests.ps1','.ps1'))"
}
It 'Should have loaded the script directly, not from the module' {
$PSCommandPath.Replace('.Tests.ps1','.ps1') | Should -be $fileName
(Get-Command $functionName).source | Should -BeNullOrEmpty
}
}

Describe 'Add-MFProjectScripts should Copy Files' {
BeforeAll{
Add-MFProjectScripts -WarningAction SilentlyContinue
}
It 'Should have created the scripts folder' {
Get-Item $scriptsFolder -Force | Should -Not -BeNullOrEmpty
}
It 'Should have created the Invoke-MFPester script' {
Get-Item $pesterScript | Should -Not -BeNullOrEmpty
Get-Content $pesterScript | Should -Not -BeNullOrEmpty
}
}

Describe 'Add-MFProjectScripts should NOT copy if files exist' {
BeforeAll{
$endOfFileString = "`n#### End of File Change"
$endOfFileString | Out-File $pesterScript -Append
Add-MFProjectScripts -WarningAction SilentlyContinue
}

It 'Rerunning should not overwrite our end of file message in the Pester script' {
Get-Item $pesterScript | Should -Not -BeNullOrEmpty
Get-Content $pesterScript | Should -Not -BeNullOrEmpty
Get-Content $pesterScript | Should -Contain '#### End of File Change'
}
}

Describe 'Add-MFProjectScripts should copy if files exist and -Force is used' {
BeforeAll{
Add-MFProjectScripts -Force -WarningAction SilentlyContinue
}

It 'Rerunning with -Force should remove our end of file message from the Pester script' {
Get-Item $pesterScript | Should -Not -BeNullOrEmpty
Get-Content $pesterScript | Should -Not -BeNullOrEmpty
Get-Content $pesterScript | Should -Not -Contain '#### End of File Change'
}
}


AfterAll{
Remove-Variable mockPsScriptRoot -ErrorAction Ignore
Set-Location $currentPath
Remove-Item $testPath -Force -Recurse -ProgressAction SilentlyContinue
}
129 changes: 129 additions & 0 deletions source/functions/Add-MFProjectScripts.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
function Add-MFProjectScripts
{

<#
.SYNOPSIS
Adds a scripts scaffold to a ModuleForge project.

.DESCRIPTION
Copies script templates from ModuleForge's resource\scripts folder into a scripts
subfolder at the project root. Skips existing files by default.

Includes Invoke-MFPester.ps1 — a standalone Pester runner that does not depend on
the ModuleForge module, safe for use in clean CI environments.

This function is also called automatically by add-mfFilesAndFolders during project
creation, at which point moduleForgeConfig.xml may not yet exist. A warning is
emitted in that case but the copy proceeds normally.

.EXAMPLE
Add-MFProjectScripts

#### DESCRIPTION
Copies script templates into the scripts folder, skipping any that already exist.

.EXAMPLE
Add-MFProjectScripts -Force

#### DESCRIPTION
Copies script templates and overwrites any existing files in the scripts folder.

.NOTES
Author: Adrian Andersson

#>

[CmdletBinding()]
PARAM(
#Root path of the module. Uses the current working directory by default
[Parameter()]
[alias('Path')]
[string]$ModulePath = $(Get-Location).path,
#Module Config reference
[Parameter(DontShow)]
[string]$ConfigFile = 'moduleForgeConfig.xml',
#Scripts folder name
[Parameter(DontShow)]
[string]$ScriptsFolder = 'scripts',
#Should we overwrite if files exist?
[switch]$Force
)
begin{
#Return the script name when running verbose, makes it tidier
write-verbose "===========Executing $($MyInvocation.InvocationName)==========="
#Return the sent variables when running debug
Write-Debug "BoundParams: $($MyInvocation.BoundParameters|Out-String)"
$skipProcess = $false
if($mockPsScriptRoot)
{
write-warning 'Assuming PSScriptRoot from $mockPsScriptRoot. This should only be done for testing'
$resourceFolder = Join-Path $mockPsScriptRoot 'resource'
}else{
$resourceFolder = Join-Path $PSScriptRoot 'resource'
}
write-verbose "ResourceFolder: $resourceFolder"

if(!(Test-Path $resourceFolder))
{
Write-Warning 'Add-MFProjectScripts: resource folder not found in ModuleForge module — skipping script scaffold'
$skipProcess = $true
}

if(!$skipProcess)
{
$resourceFolderScripts = Join-Path $resourceFolder 'scripts'
if(!(Test-Path $resourceFolderScripts))
{
Write-Warning 'Add-MFProjectScripts: scripts folder not found in ModuleForge resource — skipping script scaffold'
$skipProcess = $true
}
}

if(!$skipProcess)
{
$mfConfigFile = Join-Path $ModulePath $ConfigFile
if(!(Test-Path $mfConfigFile))
{
Write-Warning 'No ModuleForge config file found. Ensure this is a ModuleForge project.'
}
$moduleScriptsFolder = Join-Path $ModulePath $ScriptsFolder
}
}

process{
if($skipProcess){ return }
if(!(Test-Path $moduleScriptsFolder)){
write-verbose "$moduleScriptsFolder does not exist, creating"
New-Item -ItemType Directory -Path $moduleScriptsFolder
}else{
write-verbose "$moduleScriptsFolder already exists"
}

$childItemSource = Get-ChildItem $resourceFolderScripts -Recurse
$childItemSource.foreach{
$destinationPath = $_.FullName -replace [regex]::Escape($resourceFolderScripts), $moduleScriptsFolder
write-verbose "DestinationPath: $destinationPath"
if($destinationPath -eq $_.FullName)
{
throw "Destination path matches original file path. Replace has not worked `n$($destinationPath) -> $($_.FullName)"
}
if(Test-Path $destinationPath){
if($Force)
{
if($_.PSIsContainer){
Write-Verbose "Skipping directory: $($_.FullName)"
}else{
write-warning "Overwrite $destinationPath"
Copy-Item -Path $_.FullName -Destination $destinationPath -Force
}
}else{
write-warning "Skipping $destinationPath as it exists"
}
}else{
write-verbose "Copying $($_) to $destinationPath"
Copy-Item -Path $_.FullName -Destination $destinationPath
}
}
}

}
5 changes: 3 additions & 2 deletions source/functions/Build-MFProject.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ param()

BeforeAll{

$WarningPreference = 'SilentlyContinue'
#Reference Current Path
$currentPath = $(get-location).path
$sourcePath = join-path -path $currentPath -childPath 'source'

$dependencies = [ordered]@{
functions = @('Get-MFFolderItems.ps1','Get-MFDependencyTree.ps1','Get-MFFolderItemDetails.ps1','New-MFProject.ps1','Register-MFLocalPsResourceRepository.ps1','Remove-MFLocalPsResourceRepository.ps1','Add-MFRepositoryXmlData.ps1')
functions = @('Get-MFFolderItems.ps1','Get-MFDependencyTree.ps1','Get-MFFolderItemDetails.ps1','New-MFProject.ps1','Register-MFLocalPsResourceRepository.ps1','Remove-MFLocalPsResourceRepository.ps1','Add-MFRepositoryXmlData.ps1','Add-MFProjectScripts.ps1')
private = @('add-mfFilesAndFolders.ps1')
}

Expand Down Expand Up @@ -292,7 +293,7 @@ describe 'Build-MFProject with ReleaseNotes' {
afterAll {

Set-Location $currentPath
Remove-Item $testPath -Force -Recurse -ErrorAction Ignore
Remove-Item $testPath -Force -Recurse -ErrorAction Ignore -ProgressAction SilentlyContinue
#Remove-Item $repoTestPath -Recurse -Force -ErrorAction Ignore
start-sleep -Seconds 2 #Give it 2 seconds to remove the folder
}
4 changes: 4 additions & 0 deletions source/functions/Get-MFDependencyTree.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{

$WarningPreference = 'SilentlyContinue'
#Reference Current Path
$currentPath = $(get-location).path
$sourcePath = join-path -path $currentPath -childPath 'source'
Expand Down
4 changes: 4 additions & 0 deletions source/functions/Get-MFFolderItemDetails.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{

$WarningPreference = 'SilentlyContinue'
#Reference Current Path
$currentPath = $(get-location).path
$sourcePath = join-path -path $currentPath -childPath 'source'
Expand Down
7 changes: 4 additions & 3 deletions source/functions/Get-MFFolderItems.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ param()

BeforeAll{

$WarningPreference = 'SilentlyContinue'
#Reference Current Path
$currentPath = $(get-location).path
$sourcePath = join-path -path $currentPath -childPath 'source'
Expand Down Expand Up @@ -82,7 +83,7 @@ Describe 'Get-MFFolderItems throws on invalid destination' {
{ Get-MFFolderItems -Path $privatePath -Destination $trailingDest } | Should -Not -Throw
}
AfterAll {
Remove-Item $tempCopyLocation -Recurse -Force -ErrorAction Ignore
Remove-Item $tempCopyLocation -Recurse -Force -ErrorAction Ignore -ProgressAction SilentlyContinue
}
}

Expand All @@ -102,10 +103,10 @@ Describe 'Get-MFFolderItems respects .mfignore' {
$ignoreItems.Name | Should -Contain 'include-me.ps1'
}
AfterAll {
Remove-Item $ignorePath -Recurse -Force -ErrorAction Ignore
Remove-Item $ignorePath -Recurse -Force -ErrorAction Ignore -ProgressAction SilentlyContinue
}
}

AfterAll {
remove-item -Recurse -Path $tempCopyLocation -Force -ErrorAction Ignore
remove-item -Recurse -Path $tempCopyLocation -Force -ErrorAction Ignore -ProgressAction SilentlyContinue
}
6 changes: 5 additions & 1 deletion source/functions/Get-MFGitChangeLog.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{


$WarningPreference = 'SilentlyContinue'
#Load This File
. $PSCommandPath.Replace('.Tests.ps1','.ps1')

Expand Down
10 changes: 7 additions & 3 deletions source/functions/Get-MFGitLatestVersion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{


$WarningPreference = 'SilentlyContinue'
#Load This File
. $PSCommandPath.Replace('.Tests.ps1','.ps1')

Expand Down Expand Up @@ -52,7 +56,7 @@ Describe 'Get-MFGitLatestVersion' {
}

process{
Write-Verbose "Command provided: $commandLine"
#Write-Verbose "Command provided: $commandLine"
switch -Wildcard ($commandLine) {
'--version' { return 'git version 2.30.0.mock' }
'tag *' {$global:LASTEXITCODE = 0 ;return $tags } #Need to make sure we are setting the exitcode to 0 for this, else the get-mfGitlatestVersion may trap incorrectly
Expand All @@ -63,7 +67,7 @@ Describe 'Get-MFGitLatestVersion' {

Set-Alias -name 'git' -Value invoke-GitCommand

$latestVer = get-mfGitLatestVersion -Verbose
$latestVer = get-mfGitLatestVersion
}

It 'should Mock Git Version Correctly' {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification='PSScriptAnalyzer cannot see Pester BeforeAll scoping')]
param()

BeforeAll{
$WarningPreference = 'SilentlyContinue'
#Reference Current Path
$currentPath = $(get-location).path

Expand Down Expand Up @@ -84,5 +88,5 @@ describe 'Get-MFLatestSemverFromBuildManifest without prerelease' {

AfterAll{
set-location $currentPath
remove-item -Path $testPath -Force -recurse
remove-item -Path $testPath -Force -recurse -ProgressAction SilentlyContinue
}
1 change: 1 addition & 0 deletions source/functions/Get-MFNextSemver.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
param()

BeforeAll{
$WarningPreference = 'SilentlyContinue'
#Load This File
. $PSCommandPath.Replace('.Tests.ps1','.ps1')
}
Expand Down
Loading
Loading