File: testfuncs.ps1

package info (click to toggle)
par2cmdline 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,204 kB
  • sloc: cpp: 14,330; sh: 6,457; makefile: 204
file content (223 lines) | stat: -rw-r--r-- 6,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# par2cmdline Windows Test Helper Functions
# This file is sourced by individual test scripts to provide common functionality

$ErrorActionPreference = "Stop"

# Determine directories
# When dot-sourced, $PSScriptRoot gives the directory of THIS file (testfuncs.ps1)
# This is more reliable than $MyInvocation.MyCommand.Path when dot-sourcing
if ($PSScriptRoot) {
    $script:TestScriptDir = $PSScriptRoot
} else {
    # Fallback for older PowerShell or unusual invocation
    $script:TestScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
    if (-not $script:TestScriptDir) {
        $script:TestScriptDir = $PWD.Path
    }
}
$script:RootDir = Split-Path -Parent $script:TestScriptDir
$script:TestDataDir = $script:TestScriptDir

# Sync .NET current directory with PowerShell - needed for [System.IO.File] methods
[System.IO.Directory]::SetCurrentDirectory($PWD.Path)

# Find par2.exe binary
function Find-Par2Binary {
    # Check if PARBINARY environment variable is set
    if ($env:PARBINARY -and (Test-Path $env:PARBINARY)) {
        return $env:PARBINARY
    }

    # Check common build output locations relative to root
    $candidates = @(
        (Join-Path $script:RootDir "x64\Release\par2.exe"),
        (Join-Path $script:RootDir "x64\Debug\par2.exe"),
        (Join-Path $script:RootDir "Win32\Release\par2.exe"),
        (Join-Path $script:RootDir "Win32\Debug\par2.exe"),
        (Join-Path $script:RootDir "ARM64\Release\par2.exe"),
        (Join-Path $script:RootDir "ARM64\Debug\par2.exe"),
        (Join-Path $script:RootDir "build\par2.exe"),
        (Join-Path $script:RootDir "par2.exe")
    )

    foreach ($candidate in $candidates) {
        if (Test-Path $candidate) {
            return (Resolve-Path $candidate).Path
        }
    }

    throw "Could not find par2.exe. Please build the project first or set PARBINARY environment variable."
}

# Get the par2 binary path and store in environment variable for cross-script access
if (-not $env:PARBINARY -or -not (Test-Path $env:PARBINARY)) {
    $env:PARBINARY = Find-Par2Binary
}
$script:Par2Binary = $env:PARBINARY

# Extract a .tar.gz file (requires tar command available on Windows 10+)
function Expand-TarGz {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Archive,
        [Parameter(Mandatory=$true)]
        [string]$Destination
    )

    if (-not (Test-Path $Archive)) {
        throw "Archive not found: $Archive"
    }

    # Use tar command (available on Windows 10 1803+)
    $tarResult = & tar -xzf $Archive -C $Destination 2>&1
    if ($LASTEXITCODE -ne 0) {
        throw "Failed to extract $Archive : $tarResult"
    }
}

# Create a banner for test output
function Write-Banner {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Message
    )

    $dashes = "-" * $Message.Length
    Write-Host $dashes
    Write-Host $Message
    Write-Host $dashes
}

# Run par2
# Unified function handling both console output and capturing
function Invoke-Par2 {
    param(
        [Parameter(Mandatory=$true)]
        [string[]]$Arguments,
        
        [Parameter(Mandatory=$false)]
        [switch]$ReturnObject
    )

    # Get the par2 binary path from environment variable
    $par2Path = $env:PARBINARY
    if (-not $par2Path) {
        throw "PARBINARY environment variable not set"
    }

    $tempOut = [System.IO.Path]::GetTempFileName()
    $tempErr = [System.IO.Path]::GetTempFileName()

    try {
        # Use Start-Process with explicit WorkingDirectory for output capture
        $process = Start-Process -FilePath $par2Path -ArgumentList $Arguments -NoNewWindow -Wait -PassThru -WorkingDirectory $PWD.Path -RedirectStandardOutput $tempOut -RedirectStandardError $tempErr
        
        $stdOutContent = Get-Content $tempOut -Raw
        if ($null -eq $stdOutContent) { $stdOutContent = "" }
        
        $stdErrContent = Get-Content $tempErr -Raw
        if ($null -eq $stdErrContent) { $stdErrContent = "" }

        if ($ReturnObject) {
            return [pscustomobject]@{
                ExitCode = $process.ExitCode
                StdOut = $stdOutContent
                StdErr = $stdErrContent
            }
        } else {
            # Display output if not returning object
            if ($stdOutContent) {
                Write-Host $stdOutContent
            }
            if ($stdErrContent) {
                Write-Host $stdErrContent -ForegroundColor Yellow
            }
            return $process.ExitCode
        }
    }
    finally {
        if (Test-Path $tempOut) { Remove-Item $tempOut -Force -ErrorAction SilentlyContinue }
        if (Test-Path $tempErr) { Remove-Item $tempErr -Force -ErrorAction SilentlyContinue }
    }
}

# Compare two files by hash
function Compare-Files {
    param(
        [Parameter(Mandatory=$true)]
        [string]$File1,
        [Parameter(Mandatory=$true)]
        [string]$File2
    )

    if (-not (Test-Path $File1)) { return $false }
    if (-not (Test-Path $File2)) { return $false }

    $hash1 = Get-FileHash -Path $File1 -Algorithm SHA256
    $hash2 = Get-FileHash -Path $File2 -Algorithm SHA256

    return $hash1.Hash -eq $hash2.Hash
}

# Create random data file
function New-RandomFile {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Path,
        [Parameter(Mandatory=$true)]
        [int]$SizeBytes
    )

    $bytes = New-Object byte[] $SizeBytes
    $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
    $rng.GetBytes($bytes)
    [System.IO.File]::WriteAllBytes($Path, $bytes)
    $rng.Dispose()
}

# Setup test environment - creates run directory and changes to it
function Initialize-Test {
    param(
        [Parameter(Mandatory=$true)]
        [string]$TestName
    )

    # Use the test script directory as root, not the current working directory
    $script:TestRoot = $script:TestScriptDir
    $script:RunDir = Join-Path $script:TestRoot "run$TestName"

    # Cleanup any previous run
    if (Test-Path $script:RunDir) {
        Remove-Item -Recurse -Force $script:RunDir
    }

    New-Item -ItemType Directory -Path $script:RunDir -Force | Out-Null
    Set-Location $script:RunDir
    
    # Sync .NET current directory with PowerShell
    [System.IO.Directory]::SetCurrentDirectory($PWD.Path)
}

# Cleanup test environment
function Complete-Test {
    Set-Location $script:TestRoot
    if (Test-Path $script:RunDir) {
        Remove-Item -Recurse -Force $script:RunDir -ErrorAction SilentlyContinue
    }
}

# Exit with error
function Exit-TestWithError {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Message
    )

    Write-Host "ERROR: $Message" -ForegroundColor Red
    Complete-Test
    exit 1
}

# Export commonly needed variables
$script:TESTDATA = $script:TestDataDir
$script:PARBINARY = $env:PARBINARY