Skip to content

Commit

Permalink
Improve logging and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
leojonathanoh committed Sep 16, 2023
1 parent 2a1b6e6 commit 908d605
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
72 changes: 72 additions & 0 deletions Generate-GitBranches.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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'
)
}
}

}
4 changes: 3 additions & 1 deletion Generate-GitBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }
Expand Down Expand Up @@ -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" }
Expand Down

0 comments on commit 908d605

Please sign in to comment.