diff --git a/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 b/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 new file mode 100644 index 0000000..42126ab --- /dev/null +++ b/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 @@ -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" + } +} \ No newline at end of file diff --git a/TestingHelperTest/public/Add-ToModule.Tests.ps1 b/TestingHelperTest/public/Add-ToModule.Tests.ps1 index 3ae5ad8..a6fbd2e 100644 --- a/TestingHelperTest/public/Add-ToModule.Tests.ps1 +++ b/TestingHelperTest/public/Add-ToModule.Tests.ps1 @@ -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 -} - diff --git a/TestingHelperTest/public/Add-ToModuleGit.Tests.ps1 b/TestingHelperTest/public/Add-ToModuleGit.Tests.ps1 new file mode 100644 index 0000000..ae7150a --- /dev/null +++ b/TestingHelperTest/public/Add-ToModuleGit.Tests.ps1 @@ -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" + +} + diff --git a/private/Git.Dependency.ps1 b/private/Git.Dependency.ps1 index d235007..f6cf7c5 100644 --- a/private/Git.Dependency.ps1 +++ b/private/Git.Dependency.ps1 @@ -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()] @@ -13,17 +50,18 @@ 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 } @@ -31,15 +69,53 @@ function script:Invoke-GitRepositoryInit{ 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") } \ No newline at end of file diff --git a/public/Add-ToModule.ps1 b/public/Add-ToModule.ps1 index 94c9709..7a97506 100644 --- a/public/Add-ToModule.ps1 +++ b/public/Add-ToModule.ps1 @@ -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)] diff --git a/public/Add-ToModuleGit.ps1 b/public/Add-ToModuleGit.ps1 new file mode 100644 index 0000000..1769db6 --- /dev/null +++ b/public/Add-ToModuleGit.ps1 @@ -0,0 +1,101 @@ +# 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 + +function Add-ToModuleGitCommit{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru, + [Parameter()][string]$Message + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + $ret = ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + + # no Git Repository and no Force + if(!(Test-GitRepository -Path $Path) ){ + #check for force + if(!$Force){ + Write-Error "Git repository does not exist. Use -Force or Add-ToModuleGitRepository to create it." + return $ret + } else { + # Create Git Repository + Add-ToModuleGitRepository -Path $Path + $justCreated = $true + # no need to control errors. Call will display them + } + } + + # Set messsage if not provided + if ([string]::IsNullOrEmpty($Message)) { + $Message = $justCreated ? "TH Init commit" : "TH Commit" + } + + + $result = Invoke-GitRepositoryCommit -Path $Path -Message $Message + + if(!$result){ + Write-Error "Git commit failed. $GITLASTERROR" + return $ret + } + + # TODO: Write warning of the execution if needed + + return $ret + } +} Export-ModuleMember -Function Add-ToModuleGitCommit \ No newline at end of file diff --git a/test.ps1 b/test.ps1 index 25eb386..54c32ff 100644 --- a/test.ps1 +++ b/test.ps1 @@ -47,7 +47,4 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddTestToModuleAll_Simple -# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddModule_DefaultManifest -# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddToModuleGitRepository_PipeCalls_Folder_WhatIf_DoubleCall Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors