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
33 changes: 33 additions & 0 deletions TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Git Repository
function Assert-AddGitRepository{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
$Path = $Path | Convert-Path

Assert-ItemExist -Path ($Path | Join-Path -ChildPath ".git") -Comment ".git"
}
}

function Assert-AddGitCommit{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path,
[Parameter(Mandatory)][string]$MessageExpected
)
process{
$Path = $Path | Convert-Path

# Extract last commit message from log to check body message
$body= (git -C $Path log -1 --pretty=%B | out-string).Trim()
Assert-AreEqual -Expected $MessageExpected -Presented $body -Comment "Git commit message"

# Extarct last commit message from log to check author
$author = git -C $Path log -1 --pretty='[%an][%ae]'
Assert-AreEqual -Expected "[TestingHelper Agent][tha@sample.com]" -Presented $author -Comment "Git commit author"
}
}
58 changes: 0 additions & 58 deletions TestingHelperTest/public/Add-ToModule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,61 +68,3 @@ function TestingHelperTest_AddToModule_FULL_PipeCalls_Module{
Remove-Module -Name "MyModule"
}

function TestingHelperTest_AddToModuleGitRepository_PipeCalls_Folder{

New-TestingFolder -Path "folderName"

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
$result | Assert-AddGitRepository

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru @WarningParameters
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar

}

function TestingHelperTest_AddToModuleGitRepository_PipeCalls_Folder_Force{

New-TestingFolder -Path "folderName"

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
$result | Assert-AddGitRepository

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force -PassThru @WarningParameters
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
}

function TestingHelperTest_AddToModuleGitRepository_PipeCalls_Folder_WhatIf_DoubleCall{

$folder = New-TestingFolder -Path "folderName" -PassThru

# WhatIf
$result = $folder | Add-TT_ToModuleGitRepository -Whatif @WarningParameters
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# First call
$result = $folder | Add-TT_ToModuleGitRepository @WarningParameters
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# Second call
$result = $folder | Assert-AddGitRepository
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# Second call Whatif
$result = $folder | Add-TT_ToModuleGitRepository -whatif @WarningParameters
Assert-IsNull -Object $result
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar

# Second call -force -whatif
$result = $folder | Add-TT_ToModuleGitRepository -whatif -force @WarningParameters
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# Second call -force
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force @WarningParameters
Assert-IsNull -Object $result
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
}

81 changes: 81 additions & 0 deletions TestingHelperTest/public/Add-ToModuleGit.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
function TestingHelperTest_AddToModuleGitRepository_Init_PipeCalls_Folder{

New-TestingFolder -Path "folderName"

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
$result | Assert-AddGitRepository

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru @WarningParameters
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar

}

function TestingHelperTest_AddToModuleGitRepository_Init_PipeCalls_Folder_Force{

New-TestingFolder -Path "folderName"

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -PassThru
$result | Assert-AddGitRepository

$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force -PassThru @WarningParameters
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
}

function TestingHelperTest_AddToModuleGitRepository_Init_PipeCalls_Folder_WhatIf_DoubleCall{

$folder = New-TestingFolder -Path "folderName" -PassThru

# WhatIf
$result = $folder | Add-TT_ToModuleGitRepository -Whatif @WarningParameters
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# First call
$result = $folder | Add-TT_ToModuleGitRepository @WarningParameters
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# Second call
$result = $folder | Assert-AddGitRepository
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# Second call Whatif
$result = $folder | Add-TT_ToModuleGitRepository -whatif @WarningParameters
Assert-IsNull -Object $result
Assert-Contains -Expected "Git repository already exists." -Presented $warningVar

# Second call -force -whatif
$result = $folder | Add-TT_ToModuleGitRepository -whatif -force @WarningParameters
Assert-IsNull -Object $result
Assert-Count -Expected 0 -Presented $warningVar

# Second call -force
$result = Get-Item -path "folderName" | Add-TT_ToModuleGitRepository -Force @WarningParameters
Assert-IsNull -Object $result
Assert-Contains -Expected "Reinitialized existing Git repository." -Presented $warningVar
}

function TestingHelperTest_AddToModuleGitCommit_PipeCalls_Folder{

$folder = New-TestingFolder -Path "folderName" -PassThru

$result = $folder | Add-TT_ToModuleGitCommit @ErrorParameters
Assert-IsNull -Object $result
Assert-Contains -Expected "Git repository does not exist. Use -Force or Add-ToModuleGitRepository to create it." -Presented $errorVar

# -Force
$result = $folder | Add-TT_ToModuleGitCommit -Force -Passthru @ErrorParameters
$result | Assert-AddGitRepository
$result | Assert-AddGitCommit -MessageExpected "TH Init commit"

# No Message
$result | Add-TT_ToModuleGitCommit -PassThru @ErrorParameters
$result | Assert-AddGitCommit -MessageExpected "TH Commit"

# With Message
$result | Add-TT_ToModuleGitCommit -Message "Some message to the commit" -PassThru @ErrorParameters
$result | Assert-AddGitCommit -MessageExpected "Some message to the commit"

}

92 changes: 84 additions & 8 deletions private/Git.Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,43 @@

$GITLASTERROR = $null

# Reset git configuration
function Reset-GitRepoConfiguration {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)

begin{
$userName = "TestingHelper Agent"
$userEmail = "tha@sample.com"
}

process{
#check if its null or empty
if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) {
$result1 = git -C $Path config user.email $userEmail
if($LASTEXITCODE -ne 0){
$GITLASTERROR = "Git config user.email failed - $result1"
return $null
}
}

#check if its null or empty
if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) {
$result2 = git -C $Path config user.name $userName
if($LASTEXITCODE -ne 0){
$GITLASTERROR = "Git config user.name failed - $result2"
return $null
}
}

return $true
}
}

# Initializae git repository
function script:Invoke-GitRepositoryInit{
[CmdletBinding()]
Expand All @@ -13,33 +50,72 @@ function script:Invoke-GitRepositoryInit{

# check if git is installed
$gitPath = Get-Command -Name git -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source

if(!$gitPath){
$GITLASTERROR = "Git is not installed"
return $null
}

$result = git init $Path
# Initialize git repository
# Silence warnings from git STDERR stream. 2>$null
$result = git -C $Path init --initial-branch="main" 2>$null

# check the result of git call
if($LASTEXITCODE -ne 0){
$GITLASTERROR = "Git init failed"
$GITLASTERROR = "Git init failed."
return $null
}

$GITLASTERROR = $null
return $result
}

function script:Test-GitRepository{
# Create a commit with actual changes
function script:Invoke-GitRepositoryCommit{
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Path
[Parameter(Mandatory)][string]$Path,
[Parameter(Mandatory)][string]$Message
)

$gitPath = $Path | Join-Path -ChildPath ".git"
# Reset git configuration.
$gitReset = Reset-GitRepoConfiguration -Path $Path
if(!$gitReset){
$GITLASTERROR = "Git Resetting configuration failed - $GITLASTERROR"
return $null
}

$ret = Test-Path -Path $gitPath
# check if git is installed
$gitPath = Get-Command -Name git -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source
if(!$gitPath){
$GITLASTERROR = "Git is not installed"
return $null
}

# Stage all changes
$result = git -C $Path add .
# check the result of git call
if($LASTEXITCODE -ne 0){
$GITLASTERROR = "Git staginig failed - $result"
return $null
}

# Commit
$result = git -C $Path commit --allow-empty -m $Message
if($LASTEXITCODE -ne 0){
$GITLASTERROR = "Git commit failed - $result"
return $null
}

return $ret
$GITLASTERROR = $null
return $result
}

# Check if the folder is a git repository
function script:Test-GitRepository{
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Path
)
# check if we are on a git folder
return ((git -C $Path rev-parse --is-inside-work-tree 2>$null) -eq "true")
}
53 changes: 0 additions & 53 deletions public/Add-ToModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,6 @@ function Add-ToModuleDevContainerJson{
}
} Export-ModuleMember -Function Add-ToModuleDevContainerJson

# Adds git repository to the module
function Add-ToModuleGitRepository{
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path,
[Parameter(ValueFromPipelineByPropertyName)][switch]$Force,
[Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru
)

process{
$Path = NormalizePath -Path:$Path ?? return $null
$ret = ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru

# check if git was initialized before on this folder

if((Test-GitRepository -Path $Path) -and (!$Force)){
Write-Warning "Git repository already exists."
return $ret
}

if ($PSCmdlet.ShouldProcess($Path, "Git init")) {

$result = Invoke-GitRepositoryInit -Path $Path
} else {
# Fake a success run
$result = "Initialized empty Git repository in"
}

if(!$result){
Write-Error "Git init failed. $GITLASTERROR"
return $ret
}

# Write warning of the execution if needed
# SUCCESS "Initialized empty Git repository in $Path/.git/"
# ALREADY "Reinitialized existing Git repository in $Path/.git/"
if (!($result.StartsWith("Initialized empty Git repository in"))) {

if($result.StartsWith("Reinitialized existing Git repository in") -and $Force){
Write-Warning "Reinitialized existing Git repository."

} else {
Write-Warning "Git init may have failed. Please check the output"
}
}

return $ret

}
} Export-ModuleMember -Function Add-ToModuleGitRepository

# Add License file
function Add-ToModuleLicense{
[CmdletBinding(SupportsShouldProcess)]
Expand Down
Loading