Set-PSDebug -Off
$ErrorActionPreference='Stop'

Write-Host "--- Extracting the artifacts ---"
mkdir -Force artifacts | out-null
tar xzf artifacts.tar.gz -C artifacts

pushd artifacts/bin

function check_help_version {
  try {
    .\bitcoind.exe -version
    .\bitcoind.exe -help
    .\bitcoin-qt.exe -version
    .\bitcoin-qt.exe -help
    .\bitcoin-cli.exe -version
    .\bitcoin-cli.exe -help
    .\bitcoin-tx.exe -help
    .\bitcoin-wallet -help
  }
  catch {
    Write-Error $_
  }
  finally {
    Stop-Process -name bitcoin-qt -Force -ErrorAction SilentlyContinue
  }
}

function check_bitcoind {
  trap {
    Stop-Process -name bitcoind -Force 
  }

  Write-Host "Launching bitcoind in the background"
  Start-Process -NoNewWindow .\bitcoind.exe "-noprinttoconsole"

  for($i=60; $i -gt 0; $i--) {
    Start-Sleep -Seconds 1
    if(.\bitcoin-cli.exe help) {
      break
    }
  }
  if($i -eq 0) {
    throw "Failed to start bitcoind"
  }

  Write-Host "Stopping bitcoind"
  .\bitcoin-cli.exe stop

  for($i=60; $i -gt 0; $i--) {
    Start-Sleep -Seconds 1
    if(-Not (Get-Process -Name bitcoind -ErrorAction SilentlyContinue)) {
      break
    }
  }
  if($i -eq 0) {
    throw "Failed to stop bitcoind"
  }
}

Write-Host "--- Checking helps and versions ---"
check_help_version

Write-Host "--- Checking bitcoind can run and communicate via bitcoin-cli ---"
check_bitcoind

Write-Host "--- Running bitcoin unit tests ---"
.\test_bitcoin.exe
Write-Host "--- Running bitcoin-qt unit tests ---"
.\test_bitcoin-qt.exe -platform windows
Write-Host "--- Running pow unit tests ---"
.\test-pow.exe
Write-Host "--- Running avalanche unit tests ---"
# FIXME: figure out why the poll_inflight_timeout test fails and fix it
.\test-avalanche.exe -t !processor_tests/poll_inflight_timeout

popd

Write-Host -ForegroundColor Green "--- All checks passed ---"

Add a Powershell script for running tests on windows
[CI] Add a Powershell script for running tests on windows

Summary:
This diff adds a Powershell script which takes the build artifacts from
a windows cross build as an input and run some basic tests on it.

This version is simple enough to run on any Windows 10 teamcity agent
and requires no additional dependency nor VCS checkout. More tests can
be added later as needed. The avalanche test
`processor_tests/poll_inflight_timeout` will hang forever and needs
further investigation, it is disabled for now.

Test Plan:
I setup a Windows 10 VM, installed the teamcity agent software and ran
this script from the teamcity config. Example output:
https://build.bitcoinabc.org/viewLog.html?buildId=65812&buildTypeId=BitcoinABC_Master_CrossBuilds_TestNativeWindows&tab=buildLog

Reviewers: #bitcoin_abc, jasonbcox, deadalnix

Reviewed By: #bitcoin_abc, jasonbcox, deadalnix

Subscribers: deadalnix, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D7559
