[GAP-068]: verify complete deployed Strategies source folder
This commit is contained in:
@@ -19,7 +19,10 @@ The script prints `PASS` or `FAIL` for:
|
||||
- `MaxDailyTrades=1`
|
||||
4. Most recent settings export `Account` and `Instrument` match the expected arguments
|
||||
5. Deployed `NT8StrategyBase.cs` hash matches the repo copy
|
||||
6. Free disk space in the log directory drive is greater than `1 GB`
|
||||
6. All four `.cs` files copied by `deploy-to-nt8.bat` (`NT8StrategyBase.cs`, `SimpleORBNT8.cs`, `BaseNT8StrategyWrapper.cs`, `SimpleORBNT8Wrapper.cs`) match repository SHA-256 hashes, and no unexpected `.cs` files exist in `Strategies`
|
||||
7. Free disk space in the log directory drive is greater than `1 GB`
|
||||
|
||||
The folder check allows root-level `@`/`@@` NinjaTrader and operator strategies plus `ORBFibPullbackV1.cs` and `MinimalTestStrategy.cs`. It reports discrepancies with file timestamps; unexpected files also report same-named matches under `src` and any duplicate declared type found in `NT8.Strategies.dll` or `NinjaTrader.Custom.dll`. It never deletes deployed files. `MinimalTestStrategy.cs` remains allow-listed despite the known `Verify-Deployment.ps1`/batch deploy mismatch (GAP-073).
|
||||
|
||||
Every failure prints a specific remedy. The script exits `0` only when every check passes. Any failure returns exit code `1`.
|
||||
|
||||
@@ -77,6 +80,12 @@ powershell -ExecutionPolicy Bypass -File .\tools\preflight\preflight.ps1 `
|
||||
|
||||
PowerShell also accepts `-Freeze`.
|
||||
|
||||
The fixture test for the whole-folder comparison logic can be run directly with:
|
||||
|
||||
```powershell
|
||||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\tools\preflight\tests\strategy-source-verification.tests.ps1
|
||||
```
|
||||
|
||||
Freeze captures:
|
||||
|
||||
- current settings export
|
||||
|
||||
@@ -38,6 +38,7 @@ function Get-ScriptDirectory {
|
||||
|
||||
$script:ScriptDir = Get-ScriptDirectory
|
||||
$script:RepoRoot = [System.IO.Path]::GetFullPath((Join-Path $script:ScriptDir '..\..'))
|
||||
. (Join-Path $script:ScriptDir 'strategy-source-verification.ps1')
|
||||
if ([string]::IsNullOrEmpty($RepoStrategyBasePath)) {
|
||||
$RepoStrategyBasePath = [System.IO.Path]::GetFullPath((Join-Path $script:RepoRoot 'src\NT8.Adapters\Strategies\NT8StrategyBase.cs'))
|
||||
}
|
||||
@@ -518,6 +519,10 @@ function Invoke-PreflightChecks {
|
||||
}
|
||||
Write-CheckResult -Number 5 -Passed $hashPassed -Title 'Deployed NT8StrategyBase.cs matches repo copy' -Detail $hashDetail -Remedy 'Redeploy NT8StrategyBase.cs from the repo to the NinjaTrader Strategies directory and confirm the deployed source exactly matches the repo copy before enabling.'
|
||||
|
||||
$sourceIssues = @(Compare-DeployedStrategySources -RepositoryRoot $script:RepoRoot -DeployedDirectory $NtStrategiesDirectory)
|
||||
$sourceDetail = if ($sourceIssues.Count -eq 0) { 'all four deploy-managed source files match; no unexpected .cs files found' } else { Format-StrategySourceIssues -Issues $sourceIssues }
|
||||
Write-CheckResult -Number 6 -Passed ($sourceIssues.Count -eq 0) -Title 'All deployed Strategies sources match the repository; no unexpected .cs files' -Detail $sourceDetail -Remedy 'For each discrepancy, compare repository/deployed timestamps and hashes. Restore or redeploy missing/mismatched managed files. Review unexpected files and remove or retain them deliberately; preflight never deletes deployed files.'
|
||||
|
||||
$spacePassed = $false
|
||||
$spaceDetail = ''
|
||||
if ($logDirExists) {
|
||||
@@ -526,7 +531,7 @@ function Invoke-PreflightChecks {
|
||||
$spaceDetail = ('free space={0} GB' -f [Math]::Round(($freeBytes / 1GB), 2))
|
||||
}
|
||||
else { $spaceDetail = 'log directory missing' }
|
||||
Write-CheckResult -Number 6 -Passed $spacePassed -Title 'Disk space greater than 1 GB in log directory drive' -Detail $spaceDetail -Remedy 'Free disk space on the log drive or move the log directory to a drive with more than 1 GB available before enabling the strategy.'
|
||||
Write-CheckResult -Number 7 -Passed $spacePassed -Title 'Disk space greater than 1 GB in log directory drive' -Detail $spaceDetail -Remedy 'Free disk space on the log drive or move the log directory to a drive with more than 1 GB available before enabling the strategy.'
|
||||
}
|
||||
catch {
|
||||
$script:AllPassed = $false
|
||||
|
||||
114
tools/preflight/strategy-source-verification.ps1
Normal file
114
tools/preflight/strategy-source-verification.ps1
Normal file
@@ -0,0 +1,114 @@
|
||||
function Get-ExpectedStrategySourceFiles {
|
||||
param([string]$RepositoryRoot)
|
||||
|
||||
return @{
|
||||
'NT8StrategyBase.cs' = 'src\NT8.Adapters\Strategies\NT8StrategyBase.cs'
|
||||
'SimpleORBNT8.cs' = 'src\NT8.Adapters\Strategies\SimpleORBNT8.cs'
|
||||
'BaseNT8StrategyWrapper.cs' = 'src\NT8.Adapters\Wrappers\BaseNT8StrategyWrapper.cs'
|
||||
'SimpleORBNT8Wrapper.cs' = 'src\NT8.Adapters\Wrappers\SimpleORBNT8Wrapper.cs'
|
||||
}
|
||||
}
|
||||
|
||||
function Get-DeclaredSourceTypeNames {
|
||||
param([string]$FilePath)
|
||||
|
||||
$content = [System.IO.File]::ReadAllText($FilePath)
|
||||
$namespaceMatch = [regex]::Match($content, '(?m)^\s*namespace\s+([\w.]+)')
|
||||
if (-not $namespaceMatch.Success) { return @() }
|
||||
$namespaceName = $namespaceMatch.Groups[1].Value
|
||||
$typeMatches = [regex]::Matches($content, '(?m)^\s*(?:(?:public|internal|protected|private|abstract|sealed|static|partial|new)\s+)*(?:class|struct|interface|enum)\s+(\w+)')
|
||||
return @($typeMatches | ForEach-Object { $namespaceName + '.' + $_.Groups[1].Value } | Select-Object -Unique)
|
||||
}
|
||||
|
||||
function Find-DeployedAssemblyTypeCollisions {
|
||||
param(
|
||||
[string]$FilePath,
|
||||
[string]$AssemblyDirectory
|
||||
)
|
||||
|
||||
$declaredTypes = @(Get-DeclaredSourceTypeNames -FilePath $FilePath)
|
||||
if ($declaredTypes.Count -eq 0 -or -not (Test-Path -LiteralPath $AssemblyDirectory -PathType Container)) { return @() }
|
||||
|
||||
$collisions = @()
|
||||
$assemblies = @(Get-ChildItem -LiteralPath $AssemblyDirectory -File -Filter '*.dll' | Where-Object { $_.Name -in @('NT8.Strategies.dll', 'NinjaTrader.Custom.dll') })
|
||||
foreach ($assemblyFile in $assemblies) {
|
||||
try {
|
||||
$assembly = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($assemblyFile.FullName)
|
||||
foreach ($typeName in $declaredTypes) {
|
||||
if ($assembly.GetType($typeName, $false, $false) -ne $null) {
|
||||
$collisions += ('{0} in {1}' -f $typeName, $assemblyFile.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
# An unavailable dependency must not hide the source-file discrepancy itself.
|
||||
}
|
||||
}
|
||||
return @($collisions | Select-Object -Unique)
|
||||
}
|
||||
|
||||
function Compare-DeployedStrategySources {
|
||||
param(
|
||||
[string]$RepositoryRoot,
|
||||
[string]$DeployedDirectory,
|
||||
[hashtable]$ExpectedFiles = (Get-ExpectedStrategySourceFiles -RepositoryRoot $RepositoryRoot),
|
||||
[string[]]$AllowedForeignNames = @('MinimalTestStrategy.cs', 'ORBFibPullbackV1.cs')
|
||||
)
|
||||
|
||||
$issues = New-Object System.Collections.Generic.List[object]
|
||||
$expectedByDestination = @{}
|
||||
foreach ($destination in $ExpectedFiles.Keys) {
|
||||
$expectedByDestination[$destination] = $ExpectedFiles[$destination]
|
||||
$repoPath = Join-Path $RepositoryRoot $ExpectedFiles[$destination]
|
||||
$deployedPath = Join-Path $DeployedDirectory $destination
|
||||
if (-not (Test-Path -LiteralPath $repoPath -PathType Leaf)) {
|
||||
$issues.Add([pscustomobject]@{ Kind = 'missing-repository'; File = $destination; RepoPath = $ExpectedFiles[$destination]; RepoTimestamp = 'missing'; DeployedTimestamp = if (Test-Path -LiteralPath $deployedPath) { (Get-Item -LiteralPath $deployedPath).LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss') } else { 'missing' }; SameNamedSource = ''; TypeCollisions = @() })
|
||||
continue
|
||||
}
|
||||
$repoFile = Get-Item -LiteralPath $repoPath
|
||||
if (-not (Test-Path -LiteralPath $deployedPath -PathType Leaf)) {
|
||||
$issues.Add([pscustomobject]@{ Kind = 'missing-deployed'; File = $destination; RepoPath = $ExpectedFiles[$destination]; RepoTimestamp = $repoFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); DeployedTimestamp = 'missing'; SameNamedSource = ''; TypeCollisions = @() })
|
||||
continue
|
||||
}
|
||||
$deployedFile = Get-Item -LiteralPath $deployedPath
|
||||
$repoHash = (Get-FileHash -LiteralPath $repoPath -Algorithm SHA256).Hash
|
||||
$deployedHash = (Get-FileHash -LiteralPath $deployedPath -Algorithm SHA256).Hash
|
||||
if ($repoHash -ne $deployedHash) {
|
||||
$issues.Add([pscustomobject]@{ Kind = 'hash-mismatch'; File = $destination; RepoPath = $ExpectedFiles[$destination]; RepoTimestamp = $repoFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); DeployedTimestamp = $deployedFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); SameNamedSource = ''; TypeCollisions = @() })
|
||||
}
|
||||
}
|
||||
|
||||
if (Test-Path -LiteralPath $DeployedDirectory -PathType Container) {
|
||||
$deployedSources = @(Get-ChildItem -LiteralPath $DeployedDirectory -Recurse -File -Filter '*.cs')
|
||||
foreach ($deployedFile in $deployedSources) {
|
||||
$relativePath = $deployedFile.FullName.Substring($DeployedDirectory.TrimEnd('\').Length).TrimStart('\')
|
||||
if ($expectedByDestination.ContainsKey($relativePath)) { continue }
|
||||
$isRootFile = $relativePath.IndexOf('\') -lt 0
|
||||
if ($isRootFile -and ($deployedFile.Name.StartsWith('@', [System.StringComparison]::Ordinal) -or $AllowedForeignNames -contains $deployedFile.Name)) { continue }
|
||||
|
||||
$sameNamed = @(Get-ChildItem -LiteralPath (Join-Path $RepositoryRoot 'src') -Recurse -File -Filter $deployedFile.Name -ErrorAction SilentlyContinue)
|
||||
$sameNamedPath = if ($sameNamed.Count -gt 0) { $sameNamed[0].FullName.Substring($RepositoryRoot.TrimEnd('\').Length).TrimStart('\') } else { 'none under src' }
|
||||
$repoTimestamp = if ($sameNamed.Count -gt 0) { $sameNamed[0].LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss') } else { 'not found' }
|
||||
$collisions = @(Find-DeployedAssemblyTypeCollisions -FilePath $deployedFile.FullName -AssemblyDirectory (Split-Path -Parent $DeployedDirectory))
|
||||
$issues.Add([pscustomobject]@{ Kind = 'unexpected'; File = $relativePath; RepoPath = ''; RepoTimestamp = $repoTimestamp; DeployedTimestamp = $deployedFile.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'); SameNamedSource = $sameNamedPath; TypeCollisions = $collisions })
|
||||
}
|
||||
}
|
||||
else {
|
||||
$issues.Add([pscustomobject]@{ Kind = 'missing-deployed-directory'; File = $DeployedDirectory; RepoPath = ''; RepoTimestamp = 'n/a'; DeployedTimestamp = 'missing'; SameNamedSource = ''; TypeCollisions = @() })
|
||||
}
|
||||
|
||||
return @($issues.ToArray())
|
||||
}
|
||||
|
||||
function Format-StrategySourceIssues {
|
||||
param([object[]]$Issues)
|
||||
|
||||
$lines = @()
|
||||
foreach ($issue in $Issues) {
|
||||
$line = ('{0}: {1}; repo timestamp={2}; deployed timestamp={3}' -f $issue.Kind, $issue.File, $issue.RepoTimestamp, $issue.DeployedTimestamp)
|
||||
if ($issue.SameNamedSource) { $line += '; same-named source=' + $issue.SameNamedSource }
|
||||
if ($issue.TypeCollisions.Count -gt 0) { $line += '; GAP-065 type collision: ' + [string]::Join(', ', $issue.TypeCollisions) }
|
||||
$lines += $line
|
||||
}
|
||||
return [string]::Join([Environment]::NewLine, $lines)
|
||||
}
|
||||
41
tools/preflight/tests/strategy-source-verification.tests.ps1
Normal file
41
tools/preflight/tests/strategy-source-verification.tests.ps1
Normal file
@@ -0,0 +1,41 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
. (Join-Path $PSScriptRoot '..\strategy-source-verification.ps1')
|
||||
|
||||
$tempRoot = Join-Path ([System.IO.Path]::GetTempPath()) ('strategy-source-verification-' + [guid]::NewGuid().ToString('N'))
|
||||
$repoRoot = Join-Path $tempRoot 'repo'
|
||||
$deployedDirectory = Join-Path $tempRoot 'deployed\Strategies'
|
||||
$pipelineFiles = Get-ExpectedStrategySourceFiles -RepositoryRoot $repoRoot
|
||||
if ($pipelineFiles.Count -ne 4 -or -not $pipelineFiles.ContainsKey('NT8StrategyBase.cs') -or -not $pipelineFiles.ContainsKey('SimpleORBNT8.cs') -or -not $pipelineFiles.ContainsKey('BaseNT8StrategyWrapper.cs') -or -not $pipelineFiles.ContainsKey('SimpleORBNT8Wrapper.cs')) {
|
||||
throw 'Deploy-managed source allow-list does not match deployment contract.'
|
||||
}
|
||||
$null = New-Item -ItemType Directory -Path (Join-Path $repoRoot 'src\Fixtures') -Force
|
||||
$null = New-Item -ItemType Directory -Path $deployedDirectory -Force
|
||||
try {
|
||||
$repoFiles = @{
|
||||
'Match.cs' = 'src\Fixtures\Match.cs'
|
||||
'Mismatch.cs' = 'src\Fixtures\Mismatch.cs'
|
||||
'Missing.cs' = 'src\Fixtures\Missing.cs'
|
||||
}
|
||||
[System.IO.File]::WriteAllText((Join-Path $repoRoot $repoFiles['Match.cs']), 'same')
|
||||
[System.IO.File]::WriteAllText((Join-Path $repoRoot $repoFiles['Mismatch.cs']), 'repository')
|
||||
[System.IO.File]::WriteAllText((Join-Path $repoRoot $repoFiles['Missing.cs']), 'repository-only')
|
||||
[System.IO.File]::WriteAllText((Join-Path $repoRoot 'src\Fixtures\Unexpected.cs'), "namespace Fixture {`r`n public class OrphanType {}`r`n}")
|
||||
[System.IO.File]::WriteAllText((Join-Path $deployedDirectory 'Match.cs'), 'same')
|
||||
[System.IO.File]::WriteAllText((Join-Path $deployedDirectory 'Mismatch.cs'), 'deployed')
|
||||
[System.IO.File]::WriteAllText((Join-Path $deployedDirectory 'Unexpected.cs'), "namespace Fixture {`r`n public class OrphanType {}`r`n}")
|
||||
[System.IO.File]::WriteAllText((Join-Path $deployedDirectory '@Sample.cs'), 'operator-owned')
|
||||
|
||||
$issues = @(Compare-DeployedStrategySources -RepositoryRoot $repoRoot -DeployedDirectory $deployedDirectory -ExpectedFiles $repoFiles)
|
||||
if ($issues.Count -ne 3) { throw "Expected 3 discrepancies, got $($issues.Count)." }
|
||||
if (@($issues | Where-Object Kind -eq 'hash-mismatch').Count -ne 1) { throw 'Expected one hash mismatch.' }
|
||||
if (@($issues | Where-Object Kind -eq 'missing-deployed').Count -ne 1) { throw ('Expected one missing deployed file. Got: ' + [string]::Join(', ', @($issues | ForEach-Object { $_.Kind }))) }
|
||||
$unexpected = @($issues | Where-Object Kind -eq 'unexpected')
|
||||
if ($unexpected.Count -ne 1 -or $unexpected[0].File -ne 'Unexpected.cs') { throw 'Unexpected source file was not rejected.' }
|
||||
if ($unexpected[0].SameNamedSource -ne 'src\Fixtures\Unexpected.cs') { throw 'Unexpected file did not report its matching src path.' }
|
||||
if ($unexpected[0].RepoTimestamp -eq 'not found' -or $unexpected[0].DeployedTimestamp -eq 'missing') { throw 'Unexpected-file timestamps were not reported.' }
|
||||
if (@(Get-DeclaredSourceTypeNames -FilePath (Join-Path $deployedDirectory 'Unexpected.cs')) -notcontains 'Fixture.OrphanType') { throw 'Source type-name detection did not identify the orphan type.' }
|
||||
Write-Output 'PASS strategy-source verification fixture (match, mismatch, missing, allow-listed, unexpected)'
|
||||
}
|
||||
finally {
|
||||
Remove-Item -LiteralPath $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
Reference in New Issue
Block a user