...

Text file src/github.com/Microsoft/hcsshim/scripts/Testing.psm1

Documentation: github.com/Microsoft/hcsshim/scripts

     1function New-TestCommand {
     2    [CmdletBinding()]
     3    param (
     4        [Parameter(Mandatory)]
     5        [ValidateSet('Test', 'Bench', 'List')]
     6        [string]
     7        $Action,
     8
     9        [Parameter(Mandatory)]
    10        [string]
    11        $Path,
    12
    13        [Parameter(Mandatory)]
    14        [string]
    15        $Name,
    16
    17        [Parameter(Mandatory)]
    18        [string]
    19        $OutDirectory,
    20
    21        [string]
    22        $OutFile,
    23
    24        [DateTime]
    25        $Date = (Get-Date),
    26
    27        [string]
    28        $Note,
    29
    30        # test parameters
    31        [switch]
    32        $Shuffle,
    33
    34        [switch]
    35        $TestVerbose,
    36
    37        [int]
    38        $Count,
    39
    40        [string]
    41        $BenchTime,
    42
    43        [string]
    44        $Timeout,
    45
    46        [string]
    47        $Run,
    48
    49        [string[]]
    50        $Features
    51    )
    52    Write-Verbose "creating $OutDirectory"
    53    New-Item -ItemType 'directory' -Path $OutDirectory -Force > $null
    54
    55    $testcmd = "$Path `'-test.timeout=$Timeout`' `'-test.count=$Count`' "
    56
    57    if ( $Shuffle ) {
    58        $testcmd += '''-test.shuffle=on'' '
    59    }
    60
    61    if ( $TestVerbose ) {
    62        $testcmd += '''-test.v'' '
    63    }
    64
    65    switch ( $Action ) {
    66        'List' {
    67            if ( -not $Run ) {
    68                $Run = '.'
    69            }
    70            $testcmd += "`'-test.list=$Run`' "
    71        }
    72        'Test' {
    73            if ( $Run ) {
    74                $testcmd += "`'-test.run=$Run`' "
    75            }
    76        }
    77        'Bench' {
    78            if ( -not $Run ) {
    79                $Run = '.'
    80            }
    81            $testcmd += '''-test.run=^#'' ''-test.benchmem'' ' + `
    82                "`'-test.bench=$Run`' `'-test.benchtime=$BenchTime`' "
    83        }
    84    }
    85
    86    foreach ( $Feature in $Features ) {
    87        $Feature = $Feature -replace ' ', ''
    88        if ( $Feature ) {
    89            $testcmd += "`'-feature=$Feature`' "
    90        }
    91    }
    92
    93    $f = $Name + '-' + ($Action.ToLower())
    94    if ( $Note ) {
    95        $f += '-' + $Note
    96    }
    97    $out = Join-Path $OutDirectory "$f-$(Get-Date -Date $date -Format FileDateTime).txt"
    98
    99    return $testcmd, $out
   100}
   101
   102function Invoke-TestCommand {
   103    [CmdletBinding()]
   104    param (
   105        [Parameter(Mandatory)]
   106        [string]
   107        $TestCmd,
   108
   109        [string]
   110        $TestCmdPreamble = $TestCmd,
   111
   112        [string]
   113        $OutputFile = '',
   114
   115        [string]
   116        $OutputCmd,
   117
   118        [switch]
   119        $Preamble,
   120
   121        [DateTime]
   122        $Date = (Get-Date),
   123
   124        [string]
   125        $Note
   126    )
   127    Write-Verbose "Running command: $TestCmd"
   128
   129    if ( -not $OutputFile ) {
   130        $OutputFile = 'nul'
   131    } else {
   132        Write-Verbose "Saving output to: $OutputFile"
   133    }
   134
   135
   136    if ( $Preamble ) {
   137        & {
   138            Write-Output "test.date: $(Get-Date -Date $Date -UFormat '%FT%R%Z' -AsUTC)"
   139            if ( $Note ) {
   140                Write-Output "note: $Note"
   141            }
   142            Write-Output "test.command: $TestCmdPreamble"
   143            if ( Get-Command -ErrorAction Ignore 'git' ) {
   144                Write-Output "pkg.commit: $(git rev-parse HEAD 2>$null)"
   145            }
   146        } | Tee-Object -Encoding utf8 -FilePath $OutputFile
   147    }
   148    Invoke-Expression $TestCmd |
   149        Tee-Object -Encoding utf8 -Append -FilePath $OutputFile
   150
   151    if ( $OutputCmd -and $OutputFile -ne 'nul' ) {
   152        $oc = "$OutputCmd $OutputFile"
   153        Write-Verbose "Running command: $oc"
   154        Invoke-Expression $oc
   155    }
   156}

View as plain text