aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/.appveyor/test.ps1
blob: 71cb340bdaf908df68642d6124953ec3fc943829 (plain) (blame)
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
param(
    [string] $BuildDir = $null,
    [string] $ProjectDir = $null,
    [string] $Configuration = $null,
    [string] $Platform = $null
)

$ErrorActionPreference = "Stop";
Set-PSDebug -Strict

function Invoke-Exe {
    param(
        [ScriptBlock] $Cmd,
        [int[]] $AllowedExitCodes = @(0)
    )

    $backupErrorActionPreference = $script:ErrorActionPreference
    $script:ErrorActionPreference = 'Continue'

    try {
        $result = & $Cmd
        if ($AllowedExitCodes -notcontains $LastExitCode) {
            throw "External command failed with exit code ${LastExitCode}: $Cmd"
        }
        return $result
    } finally {
        $script:ErrorActionPreference = $backupErrorActionPreference
    }
}

function Test-AppVeyor {
    return Test-Path env:APPVEYOR
}

function Set-AppVeyorDefaults {
    $script:ProjectDir = $env:APPVEYOR_BUILD_FOLDER
    $script:BuildDir = 'C:\Projects\build'
    $script:Platform = $env:PLATFORM
    $script:Configuration = $env:CONFIGURATION
}

function Add-UtilsPath {
    param(
        [Parameter(Mandatory=$true)]
        [string] $BuildDir,
        [Parameter(Mandatory=$true)]
        [string] $Configuration
    )

    $env:PATH = "$BuildDir\service\utils\$Configuration;${env:PATH}"
    $env:PATH = "$BuildDir\wrappers\simple\utils\$Configuration;${env:PATH}"
}

function Get-DriverName {
    param(
        [Parameter(Mandatory=$true)]
        [string] $DriverSpec
    )

    return Split-Path -Path $DriverSpec -Leaf
}

function Get-DriverBinDir {
    param(
        [Parameter(Mandatory=$true)]
        [string] $ProjectDir,
        [Parameter(Mandatory=$true)]
        [string] $DriverSpec,
        [Parameter(Mandatory=$true)]
        [string] $Platform,
        [Parameter(Mandatory=$true)]
        [string] $Configuration
    )

    $Platform = switch ($Platform) {
        'Win32' { 'x86' }
        'x64' { 'x64' }
        default { throw "Unsupported platform: $Platform" }
    }

    $driver_name = Get-DriverName -DriverSpec $DriverSpec

    return "$ProjectDir\km\build\wdk8.1update\$DriverSpec\bin\Windows7\$Platform\$Configuration"
}

function Get-DriverSysPath {
    param(
        [Parameter(Mandatory=$true)]
        [string] $ProjectDir,
        [Parameter(Mandatory=$true)]
        [string] $DriverSpec,
        [Parameter(Mandatory=$true)]
        [string] $Platform,
        [Parameter(Mandatory=$true)]
        [string] $Configuration
    )

    $bin_dir = Get-DriverBinDir `
        -ProjectDir $ProjectDir `
        -DriverSpec $DriverSpec `
        -Platform $Platform     `
        -Configuration $Configuration
    $driver_name = Get-DriverName -DriverSpec $DriverSpec

    return "$bin_dir\$driver_name.vs12.sys"
}

function Start-Driver {
    param(
        [Parameter(Mandatory=$true)]
        [string] $ProjectDir,
        [Parameter(Mandatory=$true)]
        [string] $DriverSpec,
        [Parameter(Mandatory=$true)]
        [string] $Platform,
        [Parameter(Mandatory=$true)]
        [string] $Configuration
    )

    $service_name = Get-DriverName -DriverSpec $DriverSpec
    $sys_path = Get-DriverSysPath `
        -ProjectDir $ProjectDir   `
        -DriverSpec $DriverSpec   `
        -Platform $Platform       `
        -Configuration $Configuration

    Write-Host "Starting driver $service_name..."
    Invoke-Exe { install_service.exe $service_name $sys_path }
    Invoke-Exe { start_service.exe $service_name }
}

function Stop-Driver {
    param(
        [Parameter(Mandatory=$true)]
        [string] $DriverSpec
    )

    $service_name = Get-DriverName -DriverSpec $DriverSpec

    Write-Host "Stopping driver $service_name..."
    Invoke-Exe { stop_service.exe $service_name }
    Invoke-Exe { uninstall_service.exe $service_name }
}

function Exchange-Ints {
    param(
        [Parameter(Mandatory=$true)]
        [int] $NewInt
    )

    return Invoke-Exe { exchange_ints.exe $NewInt }
}

function Verify-ExchangeInts {
    param(
        [Parameter(Mandatory=$true)]
        [int] $NewInt,
        [Parameter(Mandatory=$true)]
        [int] $OldInt
    )

    $old_int = Invoke-Exe { exchange_ints.exe $NewInt }
    if ($old_int -ne $OldInt) {
        throw "Expected $OldInt, got: $old_int"
    } else {
        Write-Host "Previous integer: $old_int, new integer: $NewInt"
    }
}

function Run-ProjectTests {
    param(
        [Parameter(Mandatory=$true)]
        [string] $ProjectDir,
        [Parameter(Mandatory=$true)]
        [string] $BuildDir,
        [Parameter(Mandatory=$true)]
        [string] $Platform,
        [Parameter(Mandatory=$true)]
        [string] $Configuration
    )

    Add-UtilsPath -BuildDir $BuildDir -Configuration $Configuration
    Start-Driver                `
        -ProjectDir $ProjectDir `
        -DriverSpec 'simple'    `
        -Platform $Platform     `
        -Configuration $Configuration
    Verify-ExchangeInts -NewInt 69 -OldInt 42
    Verify-ExchangeInts -NewInt 100500 -OldInt 69
    Stop-Driver -DriverSpec 'simple'
}

function Run-ProjectTestsAppVeyor {
    if (Test-AppVeyor) {
        Set-AppVeyorDefaults
        $appveyor_cwd = pwd
    }

    try {
        Run-ProjectTests                   `
            -ProjectDir $script:ProjectDir `
            -BuildDir $script:BuildDir     `
            -Platform $script:Platform     `
            -Configuration $script:Configuration
    } finally {
        if (Test-AppVeyor) {
            cd $appveyor_cwd
            Set-PSDebug -Off
        }
    }
}

Run-ProjectTestsAppVeyor