From fc1d482f0ad828fcfd7e03fa550f5926d6a2d177 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 30 Jun 2023 11:45:06 +0200 Subject: [PATCH 1/6] Add-ToModuleGitCommit --- .../private/Add-ToModuleGit.Tests.Asserts.ps1 | 33 ++++++ .../public/Add-ToModule.Tests.ps1 | 58 ---------- .../public/Add-ToModuleGit.Tests.ps1 | 81 ++++++++++++++ private/Git.Dependency.ps1 | 41 ++++++- public/Add-ToModule.ps1 | 53 ---------- public/Add-ToModuleGit.ps1 | 100 ++++++++++++++++++ 6 files changed, 254 insertions(+), 112 deletions(-) create mode 100644 TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 create mode 100644 TestingHelperTest/public/Add-ToModuleGit.Tests.ps1 create mode 100644 public/Add-ToModuleGit.ps1 diff --git a/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 b/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 new file mode 100644 index 0000000..f8b2bc3 --- /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. + $messageList= git -C $Path log -1 --pretty=%B + + # For some reason there may be several messages. Take the last one. + $lastMessage = $messageList[0] + + Assert-AreEqual -Expected $MessageExpected -Presented $lastMessage -Comment "Git commit message" + } +} \ 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..4a00f79 100644 --- a/private/Git.Dependency.ps1 +++ b/private/Git.Dependency.ps1 @@ -19,7 +19,7 @@ function script:Invoke-GitRepositoryInit{ return $null } - $result = git init $Path + $result = git -C $Path init # check the result of git call if($LASTEXITCODE -ne 0){ @@ -31,6 +31,45 @@ function script:Invoke-GitRepositoryInit{ return $result } +# Create a commit with actual changes +function script:Invoke-GitRepositoryCommit{ + [CmdletBinding()] + param( + [Parameter(Mandatory)][string]$Path, + [Parameter(Mandatory)][string]$Message + ) + + # 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 + $null = git -C $Path add . + + # check the result of git call + if($LASTEXITCODE -ne 0){ + $GITLASTERROR = "Git staginig failed" + return $null + } + + # Commit all changes + $result = git -C $Path commit --allow-empty -m $Message + + # check the result of git call + if($LASTEXITCODE -ne 0){ + $GITLASTERROR = "Git commit failed" + return $null + } + + $GITLASTERROR = $null + return $result +} + + function script:Test-GitRepository{ [CmdletBinding()] param( 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..e4f6693 --- /dev/null +++ b/public/Add-ToModuleGit.ps1 @@ -0,0 +1,100 @@ +# 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"<# Action to perform if the condition is true #> + } + + + $result = Invoke-GitRepositoryCommit -Path $Path -Message $Message + + if(!$result){ + Write-Error "Git commit failed. $GITLASTERROR" + return $ret + } + + # Write warning of the execution if needed + + return $ret + } +} Export-ModuleMember -Function Add-ToModuleGitCommit \ No newline at end of file From e69979da73b21979dd45d58eb52c7c0e9cd99894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Gonz=C3=A1lez?= Date: Fri, 30 Jun 2023 10:01:55 +0000 Subject: [PATCH 2/6] Try to fix git init error on pipe. --- TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 | 4 ++-- private/Git.Dependency.ps1 | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 b/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 index f8b2bc3..a1d28e5 100644 --- a/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 @@ -25,8 +25,8 @@ function Assert-AddGitCommit{ # Extract last commit message from log. $messageList= git -C $Path log -1 --pretty=%B - # For some reason there may be several messages. Take the last one. - $lastMessage = $messageList[0] + + $lastMessage = ($messageList | Out-String).Trim() Assert-AreEqual -Expected $MessageExpected -Presented $lastMessage -Comment "Git commit message" } diff --git a/private/Git.Dependency.ps1 b/private/Git.Dependency.ps1 index 4a00f79..0b51988 100644 --- a/private/Git.Dependency.ps1 +++ b/private/Git.Dependency.ps1 @@ -19,7 +19,7 @@ function script:Invoke-GitRepositoryInit{ return $null } - $result = git -C $Path init + $result = git -C $Path init --initial-branch="main" # check the result of git call if($LASTEXITCODE -ne 0){ From 2a0cbeacadbfb80f6d4fbfb1ee4829d2a491b6b0 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 30 Jun 2023 13:15:20 +0200 Subject: [PATCH 3/6] Trying to fix Git Commit auther issue --- .../private/Git.Dependency.Tests.Helper.ps1 | 26 +++++++++ private/Git.Dependency.ps1 | 53 +++++++++++++++++-- test.ps1 | 2 +- 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 diff --git a/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 b/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 new file mode 100644 index 0000000..b6c5be0 --- /dev/null +++ b/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 @@ -0,0 +1,26 @@ + +function Initialize-GitRepoConfiguration { + [CmdletBinding()] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path + ) + + process{ + $result = git config user.email + #check if its null or empty + if([string]::IsNullOrWhiteSpace($result)){ + if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) { + } + } + + $result = git config user.name + #check if its null or empty + if([string]::IsNullOrWhiteSpace($result)){ + if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) { + } + } + } + +} \ No newline at end of file diff --git a/private/Git.Dependency.ps1 b/private/Git.Dependency.ps1 index 0b51988..ff32eb6 100644 --- a/private/Git.Dependency.ps1 +++ b/private/Git.Dependency.ps1 @@ -4,6 +4,41 @@ $GITLASTERROR = $null +function Initialize-GitRepoConfiguration { + [CmdletBinding()] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path + ) + + process{ + #check if its null or empty + if([string]::IsNullOrWhiteSpace($result)){ + if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) { + $result = git -C $Path config user.email + if($LASTEXITCODE -ne 0){ + $GITLASTERROR = "Git config user.email failed" + return $null + } + } + } + + #check if its null or empty + if([string]::IsNullOrWhiteSpace($result)){ + if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) { + $result = git -C $Path config user.name + if($LASTEXITCODE -ne 0){ + $GITLASTERROR = "Git config user.name failed" + return $null + } + } + } + + return $true + } +} + # Initializae git repository function script:Invoke-GitRepositoryInit{ [CmdletBinding()] @@ -56,8 +91,21 @@ function script:Invoke-GitRepositoryCommit{ return $null } - # Commit all changes - $result = git -C $Path commit --allow-empty -m $Message + + # $result = Initialize-GitRepoConfiguration -Path $Path + # if(!$result){ + # $GITLASTERROR = "Git configuration failed - $GITLASTERROR" + # return $null + # } + + + # Commit all changes depending on auther configuration + $gitUserName = git -C $Path config user.name + if(![string]::IsNullOrWhiteSpace($gitUserName)){ + $result = git -C $Path commit --allow-empty -m $Message + } else { + $result = git -C $Path commit --allow-empty -m $Message --author="TMAgente <>" + } # check the result of git call if($LASTEXITCODE -ne 0){ @@ -69,7 +117,6 @@ function script:Invoke-GitRepositoryCommit{ return $result } - function script:Test-GitRepository{ [CmdletBinding()] param( diff --git a/test.ps1 b/test.ps1 index 25eb386..45c7aae 100644 --- a/test.ps1 +++ b/test.ps1 @@ -50,4 +50,4 @@ Import-TestingHelper -AllowPrerelease # 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 +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName "TestingHelperTest_*git*" From 7a8cb099c755884c11b42f3fe199bf72d20539ac Mon Sep 17 00:00:00 2001 From: rulasg Date: Sat, 1 Jul 2023 11:08:17 +0200 Subject: [PATCH 4/6] Trying to fix pipe git commit author issue --- .../private/Add-ToModuleGit.Tests.Asserts.ps1 | 12 +-- .../private/Git.Dependency.Tests.Helper.ps1 | 6 +- private/Git.Dependency.ps1 | 92 ++++++++++--------- 3 files changed, 56 insertions(+), 54 deletions(-) diff --git a/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 b/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 index a1d28e5..42126ab 100644 --- a/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ToModuleGit.Tests.Asserts.ps1 @@ -22,12 +22,12 @@ function Assert-AddGitCommit{ process{ $Path = $Path | Convert-Path - # Extract last commit message from log. - $messageList= git -C $Path log -1 --pretty=%B + # 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" - - $lastMessage = ($messageList | Out-String).Trim() - - Assert-AreEqual -Expected $MessageExpected -Presented $lastMessage -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/private/Git.Dependency.Tests.Helper.ps1 b/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 index b6c5be0..4e3994a 100644 --- a/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 +++ b/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 @@ -1,6 +1,6 @@ function Initialize-GitRepoConfiguration { - [CmdletBinding()] + [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] @@ -8,17 +8,17 @@ function Initialize-GitRepoConfiguration { ) process{ - $result = git config user.email #check if its null or empty if([string]::IsNullOrWhiteSpace($result)){ if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) { + $result = git config user.email } } - $result = git config user.name #check if its null or empty if([string]::IsNullOrWhiteSpace($result)){ if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) { + $result = git config user.name∫ } } } diff --git a/private/Git.Dependency.ps1 b/private/Git.Dependency.ps1 index ff32eb6..cae80ec 100644 --- a/private/Git.Dependency.ps1 +++ b/private/Git.Dependency.ps1 @@ -4,40 +4,38 @@ $GITLASTERROR = $null -function Initialize-GitRepoConfiguration { - [CmdletBinding()] - param( - [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] - [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path - ) - process{ - #check if its null or empty - if([string]::IsNullOrWhiteSpace($result)){ - if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) { - $result = git -C $Path config user.email - if($LASTEXITCODE -ne 0){ - $GITLASTERROR = "Git config user.email failed" - return $null - } - } - } - - #check if its null or empty - if([string]::IsNullOrWhiteSpace($result)){ - if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) { - $result = git -C $Path config user.name - if($LASTEXITCODE -ne 0){ - $GITLASTERROR = "Git config user.name failed" - return $null - } - } - } - - return $true - } -} +# function Initialize-GitRepoConfiguration { +# [CmdletBinding()] +# param( +# [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] +# [Alias("PSPath")][ValidateNotNullOrEmpty()] +# [string] $Path +# ) +# process{ +# #check if its null or empty +# if([string]::IsNullOrWhiteSpace($result)){ +# if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) { +# $result = git -C $Path config user.email +# if($LASTEXITCODE -ne 0){ +# $GITLASTERROR = "Git config user.email failed" +# return $null +# } +# } +# } +# #check if its null or empty +# if([string]::IsNullOrWhiteSpace($result)){ +# if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) { +# $result = git -C $Path config user.name +# if($LASTEXITCODE -ne 0){ +# $GITLASTERROR = "Git config user.name failed" +# return $null +# } +# } +# } +# return $true +# } +# } # Initializae git repository function script:Invoke-GitRepositoryInit{ @@ -48,12 +46,12 @@ 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 } + # Initialize git repository $result = git -C $Path init --initial-branch="main" # check the result of git call @@ -83,33 +81,37 @@ function script:Invoke-GitRepositoryCommit{ } # Stage all changes - $null = git -C $Path add . + $result = git -C $Path add . # check the result of git call if($LASTEXITCODE -ne 0){ - $GITLASTERROR = "Git staginig failed" + $GITLASTERROR = "Git staginig failed - $result" return $null } - # $result = Initialize-GitRepoConfiguration -Path $Path # if(!$result){ # $GITLASTERROR = "Git configuration failed - $GITLASTERROR" # return $null # } - # Commit all changes depending on auther configuration - $gitUserName = git -C $Path config user.name - if(![string]::IsNullOrWhiteSpace($gitUserName)){ - $result = git -C $Path commit --allow-empty -m $Message - } else { - $result = git -C $Path commit --allow-empty -m $Message --author="TMAgente <>" - } + # $gitUserName = git -C $Path config user.name + # if(![string]::IsNullOrWhiteSpace($gitUserName)){ + # Write-Verbose "Git user.name is $gitUserName" + # $result = git -C $Path commit --allow-empty -m $Message + # } else { + # Write-Verbose "Git user.name is empty. Using fake author TMAgente" + # $result = git -C $Path commit --allow-empty -m $Message --author="TMAgente <>" + # # $result = git -C $Path commit --allow-empty -m $Message --author="TMAgente " + # } + + # We will author all commits with a fake user + $result = git -C $Path commit --allow-empty -m $Message --author="TestingHelper Agent " # check the result of git call if($LASTEXITCODE -ne 0){ - $GITLASTERROR = "Git commit failed" + $GITLASTERROR = "Git commit failed - $result" return $null } From 1e9d1fb8a9c6d1f3c23f6545c0018d45bf94a039 Mon Sep 17 00:00:00 2001 From: rulasg Date: Sat, 1 Jul 2023 12:40:01 +0200 Subject: [PATCH 5/6] truing to fix git commit pipe error and clean some git output --- .../private/Git.Dependency.Tests.Helper.ps1 | 25 ---- private/Git.Dependency.ps1 | 119 +++++++++++++----- public/Add-ToModuleGit.ps1 | 5 +- 3 files changed, 90 insertions(+), 59 deletions(-) diff --git a/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 b/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 index 4e3994a..8b13789 100644 --- a/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 +++ b/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 @@ -1,26 +1 @@ -function Initialize-GitRepoConfiguration { - [CmdletBinding(SupportsShouldProcess)] - param( - [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] - [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path - ) - - process{ - #check if its null or empty - if([string]::IsNullOrWhiteSpace($result)){ - if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) { - $result = git config user.email - } - } - - #check if its null or empty - if([string]::IsNullOrWhiteSpace($result)){ - if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) { - $result = git config user.name∫ - } - } - } - -} \ No newline at end of file diff --git a/private/Git.Dependency.ps1 b/private/Git.Dependency.ps1 index cae80ec..23c4a9a 100644 --- a/private/Git.Dependency.ps1 +++ b/private/Git.Dependency.ps1 @@ -4,38 +4,42 @@ $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" + } -# function Initialize-GitRepoConfiguration { -# [CmdletBinding()] -# param( -# [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] -# [Alias("PSPath")][ValidateNotNullOrEmpty()] -# [string] $Path -# ) -# process{ -# #check if its null or empty -# if([string]::IsNullOrWhiteSpace($result)){ -# if ($PSCmdlet.ShouldProcess("git config user.email", "Init to [you@example.com] ")) { -# $result = git -C $Path config user.email -# if($LASTEXITCODE -ne 0){ -# $GITLASTERROR = "Git config user.email failed" -# return $null -# } -# } -# } -# #check if its null or empty -# if([string]::IsNullOrWhiteSpace($result)){ -# if ($PSCmdlet.ShouldProcess("git config user.name", "Init to [Your Name]")) { -# $result = git -C $Path config user.name -# if($LASTEXITCODE -ne 0){ -# $GITLASTERROR = "Git config user.name failed" -# return $null -# } -# } -# } -# return $true -# } -# } + 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{ @@ -52,7 +56,9 @@ function script:Invoke-GitRepositoryInit{ } # Initialize git repository - $result = git -C $Path init --initial-branch="main" + # Silence warnings from git stream + $result = git -C $Path init --initial-branch="main" 2>$null + # $result = git -C $Path init --quiet --initial-branch="main" # check the result of git call if($LASTEXITCODE -ne 0){ @@ -119,6 +125,46 @@ function script:Invoke-GitRepositoryCommit{ return $result } +function script:Invoke-GitRepositoryCommitV2{ + [CmdletBinding()] + param( + [Parameter(Mandatory)][string]$Path, + [Parameter(Mandatory)][string]$Message + ) + + # Reset git configuration. + $gitReset = Reset-GitRepoConfiguration -Path $Path + if(!$gitReset){ + $GITLASTERROR = "Git Resetting configuration failed - $GITLASTERROR" + return $null + } + + # 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 + } + + $GITLASTERROR = $null + return $result +} + function script:Test-GitRepository{ [CmdletBinding()] param( @@ -130,4 +176,13 @@ function script:Test-GitRepository{ $ret = Test-Path -Path $gitPath return $ret +} + +function script:Test-GitRepositoryV2{ + [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-ToModuleGit.ps1 b/public/Add-ToModuleGit.ps1 index e4f6693..94223c0 100644 --- a/public/Add-ToModuleGit.ps1 +++ b/public/Add-ToModuleGit.ps1 @@ -23,13 +23,14 @@ function Add-ToModuleGitRepository{ 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" + Write-Error "Git init failed - $GITLASTERROR" return $ret } @@ -86,7 +87,7 @@ function Add-ToModuleGitCommit{ } - $result = Invoke-GitRepositoryCommit -Path $Path -Message $Message + $result = Invoke-GitRepositoryCommitV2 -Path $Path -Message $Message if(!$result){ Write-Error "Git commit failed. $GITLASTERROR" From 735cebe4d7b309e9eeaa146ef627913b92286b54 Mon Sep 17 00:00:00 2001 From: rulasg Date: Sat, 1 Jul 2023 12:59:38 +0200 Subject: [PATCH 6/6] Final Cleanup --- .../private/Git.Dependency.Tests.Helper.ps1 | 1 - private/Git.Dependency.ps1 | 73 +------------------ public/Add-ToModuleGit.ps1 | 6 +- test.ps1 | 5 +- 4 files changed, 7 insertions(+), 78 deletions(-) delete mode 100644 TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 diff --git a/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 b/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 deleted file mode 100644 index 8b13789..0000000 --- a/TestingHelperTest/private/Git.Dependency.Tests.Helper.ps1 +++ /dev/null @@ -1 +0,0 @@ - diff --git a/private/Git.Dependency.ps1 b/private/Git.Dependency.ps1 index 23c4a9a..f6cf7c5 100644 --- a/private/Git.Dependency.ps1 +++ b/private/Git.Dependency.ps1 @@ -56,13 +56,12 @@ function script:Invoke-GitRepositoryInit{ } # Initialize git repository - # Silence warnings from git stream + # Silence warnings from git STDERR stream. 2>$null $result = git -C $Path init --initial-branch="main" 2>$null - # $result = git -C $Path init --quiet --initial-branch="main" # check the result of git call if($LASTEXITCODE -ne 0){ - $GITLASTERROR = "Git init failed" + $GITLASTERROR = "Git init failed." return $null } @@ -78,60 +77,6 @@ function script:Invoke-GitRepositoryCommit{ [Parameter(Mandatory)][string]$Message ) - # 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 - } - - # $result = Initialize-GitRepoConfiguration -Path $Path - # if(!$result){ - # $GITLASTERROR = "Git configuration failed - $GITLASTERROR" - # return $null - # } - - # Commit all changes depending on auther configuration - # $gitUserName = git -C $Path config user.name - # if(![string]::IsNullOrWhiteSpace($gitUserName)){ - # Write-Verbose "Git user.name is $gitUserName" - # $result = git -C $Path commit --allow-empty -m $Message - # } else { - # Write-Verbose "Git user.name is empty. Using fake author TMAgente" - # $result = git -C $Path commit --allow-empty -m $Message --author="TMAgente <>" - # # $result = git -C $Path commit --allow-empty -m $Message --author="TMAgente " - # } - - # We will author all commits with a fake user - $result = git -C $Path commit --allow-empty -m $Message --author="TestingHelper Agent " - - # check the result of git call - if($LASTEXITCODE -ne 0){ - $GITLASTERROR = "Git commit failed - $result" - return $null - } - - $GITLASTERROR = $null - return $result -} - -function script:Invoke-GitRepositoryCommitV2{ - [CmdletBinding()] - param( - [Parameter(Mandatory)][string]$Path, - [Parameter(Mandatory)][string]$Message - ) - # Reset git configuration. $gitReset = Reset-GitRepoConfiguration -Path $Path if(!$gitReset){ @@ -165,24 +110,12 @@ function script:Invoke-GitRepositoryCommitV2{ return $result } +# Check if the folder is a git repository function script:Test-GitRepository{ [CmdletBinding()] param( [Parameter(Mandatory)][string]$Path ) - - $gitPath = $Path | Join-Path -ChildPath ".git" - - $ret = Test-Path -Path $gitPath - - return $ret -} - -function script:Test-GitRepositoryV2{ - [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-ToModuleGit.ps1 b/public/Add-ToModuleGit.ps1 index 94223c0..1769db6 100644 --- a/public/Add-ToModuleGit.ps1 +++ b/public/Add-ToModuleGit.ps1 @@ -83,18 +83,18 @@ function Add-ToModuleGitCommit{ # Set messsage if not provided if ([string]::IsNullOrEmpty($Message)) { - $Message = $justCreated ? "TH Init commit" : "TH Commit"<# Action to perform if the condition is true #> + $Message = $justCreated ? "TH Init commit" : "TH Commit" } - $result = Invoke-GitRepositoryCommitV2 -Path $Path -Message $Message + $result = Invoke-GitRepositoryCommit -Path $Path -Message $Message if(!$result){ Write-Error "Git commit failed. $GITLASTERROR" return $ret } - # Write warning of the execution if needed + # TODO: Write warning of the execution if needed return $ret } diff --git a/test.ps1 b/test.ps1 index 45c7aae..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 -TestName "TestingHelperTest_*git*" +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors