|
| 1 | +$ErrorActionPreference = 'Stop' |
| 2 | + |
| 3 | +$testsRoot = 'C:/Projects/ThemisDB/tests' |
| 4 | +$rootFiles = Get-ChildItem $testsRoot -File -Filter 'test_*.cpp' |
| 5 | + |
| 6 | +$createdDirs = New-Object System.Collections.Generic.HashSet[string] |
| 7 | +$createdCmake = New-Object System.Collections.Generic.HashSet[string] |
| 8 | +$patchedCmake = New-Object System.Collections.Generic.HashSet[string] |
| 9 | +$moved = 0 |
| 10 | + |
| 11 | +function New-ModuleCMake { |
| 12 | + param( |
| 13 | + [string]$Module, |
| 14 | + [string]$FilePath |
| 15 | + ) |
| 16 | + |
| 17 | + $upper = ($Module.ToUpper() -replace '[^A-Z0-9]', '_') |
| 18 | + $content = @" |
| 19 | +# Auto-generated module tests for prefix '$Module' |
| 20 | +
|
| 21 | +file(GLOB ${upper}_PREFIX_TEST_SOURCES |
| 22 | + "`$`{CMAKE_CURRENT_SOURCE_DIR}/test_${Module}_*.cpp" |
| 23 | +) |
| 24 | +
|
| 25 | +foreach(_src IN LISTS ${upper}_PREFIX_TEST_SOURCES) |
| 26 | + get_filename_component(_stem "`$`{_src}" NAME_WE) |
| 27 | + set(_target "module_${Module}_`$`{_stem}_focused") |
| 28 | + set(_ctest "`$`{_stem}_${Module}_FocusedTests") |
| 29 | +
|
| 30 | + if(TARGET `$`{_target}) |
| 31 | + continue() |
| 32 | + endif() |
| 33 | +
|
| 34 | + add_executable(`$`{_target} "`$`{_src}") |
| 35 | + target_include_directories(`$`{_target} PRIVATE |
| 36 | + `$`{THEMIS_ROOT_DIR}/include |
| 37 | + `$`{THEMIS_ROOT_DIR}/src |
| 38 | + ) |
| 39 | + target_link_libraries(`$`{_target} PRIVATE |
| 40 | + `$`{TEST_LIBS} |
| 41 | + themis_core |
| 42 | + spdlog::spdlog |
| 43 | + Threads::Threads |
| 44 | + ) |
| 45 | + target_compile_definitions(`$`{_target} PRIVATE THEMIS_TEST_BUILD=1) |
| 46 | +
|
| 47 | + themis_register_module_focused_test( |
| 48 | + MODULE ${Module} |
| 49 | + NAME `$`{_ctest} |
| 50 | + TARGET `$`{_target} |
| 51 | + TIER unit |
| 52 | + TIMEOUT 120 |
| 53 | + LABELS ${Module} |
| 54 | + ) |
| 55 | +endforeach() |
| 56 | +"@ |
| 57 | + |
| 58 | + $enc = New-Object System.Text.UTF8Encoding($false) |
| 59 | + [System.IO.File]::WriteAllText($FilePath, $content, $enc) |
| 60 | +} |
| 61 | + |
| 62 | +function Ensure-PrefixBlock { |
| 63 | + param( |
| 64 | + [string]$Module, |
| 65 | + [string]$FilePath |
| 66 | + ) |
| 67 | + |
| 68 | + $marker = "# AUTOGEN PREFIX BLOCK: $Module" |
| 69 | + $text = [System.IO.File]::ReadAllText($FilePath) |
| 70 | + if ($text.Contains($marker)) { |
| 71 | + return $false |
| 72 | + } |
| 73 | + |
| 74 | + $upper = ($Module.ToUpper() -replace '[^A-Z0-9]', '_') |
| 75 | + $append = @" |
| 76 | +
|
| 77 | +$marker |
| 78 | +file(GLOB ${upper}_AUTOGEN_PREFIX_TEST_SOURCES |
| 79 | + "`$`{CMAKE_CURRENT_SOURCE_DIR}/test_${Module}_*.cpp" |
| 80 | +) |
| 81 | +
|
| 82 | +foreach(_src IN LISTS ${upper}_AUTOGEN_PREFIX_TEST_SOURCES) |
| 83 | + get_filename_component(_stem "`$`{_src}" NAME_WE) |
| 84 | + set(_target "module_${Module}_`$`{_stem}_autofocused") |
| 85 | + set(_ctest "`$`{_stem}_${Module}_AutoFocusedTests") |
| 86 | +
|
| 87 | + if(TARGET `$`{_target}) |
| 88 | + continue() |
| 89 | + endif() |
| 90 | +
|
| 91 | + add_executable(`$`{_target} "`$`{_src}") |
| 92 | + target_include_directories(`$`{_target} PRIVATE |
| 93 | + `$`{THEMIS_ROOT_DIR}/include |
| 94 | + `$`{THEMIS_ROOT_DIR}/src |
| 95 | + ) |
| 96 | + target_link_libraries(`$`{_target} PRIVATE |
| 97 | + `$`{TEST_LIBS} |
| 98 | + themis_core |
| 99 | + spdlog::spdlog |
| 100 | + Threads::Threads |
| 101 | + ) |
| 102 | + target_compile_definitions(`$`{_target} PRIVATE THEMIS_TEST_BUILD=1) |
| 103 | +
|
| 104 | + themis_register_module_focused_test( |
| 105 | + MODULE ${Module} |
| 106 | + NAME `$`{_ctest} |
| 107 | + TARGET `$`{_target} |
| 108 | + TIER unit |
| 109 | + TIMEOUT 120 |
| 110 | + LABELS ${Module} autogen |
| 111 | + ) |
| 112 | +endforeach() |
| 113 | +"@ |
| 114 | + |
| 115 | + $enc = New-Object System.Text.UTF8Encoding($false) |
| 116 | + [System.IO.File]::WriteAllText($FilePath, ($text + $append), $enc) |
| 117 | + return $true |
| 118 | +} |
| 119 | + |
| 120 | +foreach ($f in $rootFiles) { |
| 121 | + $base = $f.BaseName -replace '^test_', '' |
| 122 | + $prefix = ($base -split '_')[0].ToLower() |
| 123 | + if ([string]::IsNullOrWhiteSpace($prefix)) { |
| 124 | + continue |
| 125 | + } |
| 126 | + |
| 127 | + $dir = Join-Path $testsRoot $prefix |
| 128 | + if (-not (Test-Path $dir)) { |
| 129 | + New-Item -ItemType Directory -Path $dir | Out-Null |
| 130 | + $createdDirs.Add($prefix) | Out-Null |
| 131 | + } |
| 132 | + |
| 133 | + $cmake = Join-Path $dir 'CMakeLists.txt' |
| 134 | + if (-not (Test-Path $cmake)) { |
| 135 | + New-ModuleCMake -Module $prefix -FilePath $cmake |
| 136 | + $createdCmake.Add($prefix) | Out-Null |
| 137 | + } |
| 138 | + else { |
| 139 | + if (Ensure-PrefixBlock -Module $prefix -FilePath $cmake) { |
| 140 | + $patchedCmake.Add($prefix) | Out-Null |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + $destPath = Join-Path $dir $f.Name |
| 145 | + if (Test-Path $destPath) { |
| 146 | + $srcHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $f.FullName).Hash |
| 147 | + $dstHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $destPath).Hash |
| 148 | + if ($srcHash -eq $dstHash) { |
| 149 | + Remove-Item -LiteralPath $f.FullName |
| 150 | + } |
| 151 | + else { |
| 152 | + $nameNoExt = [System.IO.Path]::GetFileNameWithoutExtension($f.Name) |
| 153 | + $ext = [System.IO.Path]::GetExtension($f.Name) |
| 154 | + $candidate = Join-Path $dir ("${nameNoExt}_root${ext}") |
| 155 | + $i = 1 |
| 156 | + while (Test-Path $candidate) { |
| 157 | + $candidate = Join-Path $dir ("${nameNoExt}_root${i}${ext}") |
| 158 | + $i++ |
| 159 | + } |
| 160 | + Move-Item -LiteralPath $f.FullName -Destination $candidate |
| 161 | + } |
| 162 | + } |
| 163 | + else { |
| 164 | + Move-Item -LiteralPath $f.FullName -Destination $destPath |
| 165 | + } |
| 166 | + $moved++ |
| 167 | +} |
| 168 | + |
| 169 | +Write-Output ('MOVED=' + $moved) |
| 170 | +Write-Output ('CREATED_DIRS=' + $createdDirs.Count) |
| 171 | +Write-Output ('CREATED_CMAKES=' + $createdCmake.Count) |
| 172 | +Write-Output ('PATCHED_CMAKES=' + $patchedCmake.Count) |
| 173 | +Write-Output ('ROOT_TEST_CPP_AFTER=' + ((Get-ChildItem $testsRoot -File -Filter 'test_*.cpp' | Measure-Object).Count)) |
0 commit comments