From 908d605bc4debdfaa1d34adf016889e2dd75f388 Mon Sep 17 00:00:00 2001 From: Leonard Jonathan Oh Date: Sat, 16 Sep 2023 06:33:42 +0000 Subject: [PATCH] Improve logging and add test --- Generate-GitBranches.Tests.ps1 | 72 ++++++++++++++++++++++++++++++++++ Generate-GitBranches.ps1 | 4 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 Generate-GitBranches.Tests.ps1 diff --git a/Generate-GitBranches.Tests.ps1 b/Generate-GitBranches.Tests.ps1 new file mode 100644 index 00000000..72c87d6b --- /dev/null +++ b/Generate-GitBranches.Tests.ps1 @@ -0,0 +1,72 @@ +Describe "Generate-GitBranches.ps1" { + + BeforeEach { + $games = Get-Content $PSScriptRoot/games.json -Encoding utf8 | ConvertFrom-Json -AsHashtable + + # Copy the git repository + $testDrive = "TestDrive:\" + $sourceRepo = $PSScriptRoot + $destinationRepo = "$testDrive/$( (Get-Item $sourceRepo).Name )" + Copy-Item $sourceRepo $destinationRepo -Recurse -Force + cd $destinationRepo + } + + AfterEach { + cd $sourceRepo + Remove-Item $testDrive/* -Recurse -Force + } + + It "Parameter validation" { + { + ./Generate-GitBranches.ps1 -ErrorAction Stop + } | Should -Throw "-Path cannot be empty" + } + + It "Creates branches of a target repo" { + ./Generate-GitBranches.ps1 -TargetRepoPath $destinationRepo -ErrorAction Stop + + $branches = git branch | % { $_.Trim() } | ? { $_ -match '^steam-' } + $branches.Count | Should -Be $games.Count + foreach ($b in $branches) { + git ls-tree -r --name-only $b | Should -Be @( + '.env' + '.gitlab-ci.yml' + '.state' + 'build.sh' + 'build/Dockerfile' + 'notify.sh' + 'update/Dockerfile' + ) + } + } + + It "Updates branches of a target repo" { + # Create branches first + $currentBranch = git rev-parse --abbrev-ref HEAD + if ($LASTEXITCODE) { throw } + foreach ($g in $games) { + $branch = "$( $g['game_platform'] )-$( $g['game_engine'] )-$( $g['game'] )" + git checkout -b "$branch" + if ($LASTEXITCODE) { throw } + } + git checkout "$currentBranch" + if ($LASTEXITCODE) { throw } + + ./Generate-GitBranches.ps1 -TargetRepoPath $destinationRepo -ErrorAction Stop + + $branches = git branch | % { $_.Trim() } | ? { $_ -match '^steam-' } + $branches.Count | Should -Be $games.Count + foreach ($b in $branches) { + git ls-tree -r --name-only $b | Should -Be @( + '.env' + '.gitlab-ci.yml' + '.state' + 'build.sh' + 'build/Dockerfile' + 'notify.sh' + 'update/Dockerfile' + ) + } + } + +} diff --git a/Generate-GitBranches.ps1 b/Generate-GitBranches.ps1 index 4fcb42ba..59065d2f 100644 --- a/Generate-GitBranches.ps1 +++ b/Generate-GitBranches.ps1 @@ -46,7 +46,7 @@ try { } $masterTrackedFiles = git ls-files if ($LASTEXITCODE) { throw } - $existingBranch = git rev-parse --verify $branch + $existingBranch = git rev-parse --verify $branch 2>$null if ($existingBranch) { "Updating branch '$branch'" | Write-Host -ForegroundColor Green if ($Pull) { @@ -63,6 +63,7 @@ try { if ($LASTEXITCODE) { throw } } + "Checking out files" | Write-Host -ForegroundColor Green $masterTrackedFiles | Get-Item -Force | Remove-Item -Recurse -Force git checkout master -- build if ($LASTEXITCODE) { throw } @@ -98,6 +99,7 @@ BASE_SIZE=0 LAYERED_SIZE=0 '@ | Out-File .state -Encoding utf8 -Force + "Committing files" | Write-Host -ForegroundColor Green git add . if ($LASTEXITCODE) { throw } $msg = if ($existingBranch) { "Update files" } else { "Add files" }