summaryrefslogtreecommitdiff
path: root/winbuild/regress.ps1
blob: be33df0e6985c4c99be2ce0baea46c859fccd89c (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<#
.SYNOPSIS
    Run regression test on Windows.
.DESCRIPTION
    Build test programs and run them.
.PARAMETER Target
    Specify the target of MSBuild. "Build&Go"(default), "Build" or
    "Clean" is available.
.PARAMETER TestList
    Specify the list of test cases. If this parameter isn't specified(default),
    all test cases are executed.
.PARAMETER Ansi
    Specify this switch in case of testing Ansi drivers.
.PARAMETER DeclareFetch
    Specify Use Declare/Fetch mode. "On"(default), "off" or "both" is available.
.PARAMETER DsnInfo
	Specify the dsn info "SERVER=${server}|DATABASE=${database}|PORT=${port}|UID=${uid}|PWD=${passwd}"	
.PARAMETER VCVersion
    Used Visual Studio version is determined automatically unless this
    option is specified.
.PARAMETER Platform
    Specify platforms to test. "x64"(default), "Win32" or "both" is available.
.PARAMETER Toolset
    MSBuild PlatformToolset is determined automatically unless this
    option is specified. Currently "v100", "Windows7.1SDK", "v110",
    "v110_xp", "v120", "v120_xp", "v140" or "v140_xp" is available.
.PARAMETER MSToolsVersion
    This option is deprecated. MSBuild ToolsVersion is determined
    automatically unless this option is specified.  Currently "4.0",
    "12.0" or "14.0" is available.
.PARAMETER Configuration
    Specify the configuration used to build executables for the regression tests. "Release"(default) or "Debug".
.PARAMETER DriverConfiguration
    Specify the configuration which was used to build the driver dlls to test. "Release"(default) or "Debug".
.PARAMETER BuildConfigPath
    Specify the configuration xml file name if you want to use
    the configuration file other than standard one.
    The relative path is relative to the current directory.
.PARAMETER ReinstallDriver
    Reinstall the driver in any case.
.PARAMETER ExpectMimalloc
    Specify whether usage of the mimalloc allocator is expected.
.EXAMPLE
    > .\regress
	Build with default or automatically selected parameters
	and run tests.
.EXAMPLE
    > .\regress Clean
	Clean all generated files.
.EXAMPLE
    > .\regress -TestList connect, deprecated
	Build and run connect-test and deprecated-test.
.EXAMPLE
    > .\regress -Ansi
	Build and run with ANSI version of drivers.
.EXAMPLE
    > .\regress -V(CVersion) 14.0
	Build using Visual Studio 14.0 environment and run tests.
.EXAMPLE
    > .\regress -P(latform) x64
	Build 64bit test programs and run them.
.NOTES
    Author: Hiroshi Inoue
    Date:   August 2, 2016
#>

#
#	build 32bit & 64bit dlls for VC10 or later
#
Param(
[ValidateSet("Build&Go", "Build", "Clean")]
[string]$Target="Build&Go",
[string[]]$TestList,
[switch]$Ansi,
[string]$VCVersion,
[ValidateSet("Win32", "x64", "both")]
[string]$Platform="both",
[string]$Toolset,
[ValidateSet("", "4.0", "12.0", "14.0")]
[string]$MSToolsVersion,
[ValidateSet("Debug", "Release")]
[String]$Configuration="Release",
[ValidateSet("Debug", "Release")]
[String]$DriverConfiguration="Release",
[string]$BuildConfigPath,
[ValidateSet("off", "on", "both")]
[string]$DeclareFetch="on",
[string]$DsnInfo,
[string]$SpecificDsn,
[switch]$ReinstallDriver,
[switch]$ExpectMimalloc
)


function testlist_make($testsf)
{
	$testbins=@()
	$testnames=@()
	$dirnames=@()
	$testexes=@()
	$f = (Get-Content -Path $testsf) -as [string[]]
	$nstart=$false
	foreach ($l in $f) {
		if ($l[0] -eq "#") {
			continue
		}
		$sary=-split $l
		if ($sary[0] -eq "#") {
			continue
		}
		if ($sary[0] -eq "TESTBINS") {
			$nstart=$true
			$sary[0]=$null
			if ($sary[1] -eq "=") {
				$sary[1]=$null
			}
		}
		if ($nstart) {
			if ($sary[$sary.length - 1] -eq "\") {
				$sary[$sary.length - 1] = $null
			} else {
				$nstart=$false
			}
			$testbins+=$sary
			if (-not $nstart) {
				break
			}
		}
	}
	for ($i=0; $i -lt $testbins.length; $i++) {
		Write-Debug "$i : $testbins[$i]"
	}

	foreach ($testbin in $testbins) {
		if ("$testbin" -eq "") {
			continue
		}
		$sary=$testbin.split("/")
		$testname=$sary[$sary.length -1]
		$dirname=""
		for ($i=0;$i -lt $sary.length - 1;$i++) {
			$dirname+=($sary[$i]+"`\")
		}
		Write-Debug "testbin=$testbin => testname=$testname dirname=$dirname"
		$dirnames += $dirname
		$testexes+=($dirname+$testname+".exe")
		$testnames+=$testname.Replace("-test","")
	}

	return $testexes, $testnames, $dirnames
}

function vcxfile_make($testnames, $dirnames, $vcxfile)
{
# here-string
	@'
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!--
	 This file is automatically generated by regress.ps1
	 and used by MSBuild.
    -->
    <PropertyGroup>
	<scriptPath>.</scriptPath>
    </PropertyGroup>
    <PropertyGroup>
	<Configuration>Release</Configuration>
	<srcPath>$(scriptPath)\..\test\src\</srcPath>
    </PropertyGroup>
    <Target Name="Build">
        <MSBuild Projects="$(scriptPath)\regress_one.vcxproj"
	  Targets="ClCompile"
	  Properties="TestName=common;Configuration=$(Configuration);srcPath=$(srcPath)"/>
'@ > $vcxfile

	for ($i=0; $i -lt $testnames.length; $i++) {
		$testname=$testnames[$i]
		$dirname=$dirnames[$i]
		$testname+="-test"
# here-string
		@"
        <MSBuild Projects="${scriptPath}\regress_one.vcxproj"
	  Targets="Build"
	  Properties="TestName=$testname;Configuration=`$(Configuration);srcPath=`$(srcPath);SubDir=$dirname"/>
"@ >> $vcxfile
	}
# here-string
	@'
        <MSBuild Projects="$(scriptPath)\regress_one.vcxproj"
	  Targets="Build"
	  Properties="TestName=runsuite;Configuration=$(Configuration);srcPath=$(srcPath)..\"/>
        <MSBuild Projects="$(scriptPath)\regress_one.vcxproj"
	  Targets="Build"
	  Properties="TestName=RegisterRegdsn;Configuration=$(Configuration);srcPath=$(srcPath)..\"/>
        <!-- MSBuild Projects="$(scriptPath)\regress_one.vcxproj"
	  Targets="Build"
	  Properties="TestName=ConfigDsn;Configuration=$(Configuration);srcPath=$(srcPath)..\"/-->
        <MSBuild Projects="$(scriptPath)\regress_one.vcxproj"
	  Targets="Build"
	  Properties="TestName=reset-db;Configuration=$(Configuration);srcPath=$(srcPath)..\"/>
    </Target>
    <Target Name="Clean">
        <MSBuild Projects="$(scriptPath)\regress_one.vcxproj"
	  Targets="Clean"
	  Properties="Configuration=$(Configuration);srcPath=$(srcPath)"/>
    </Target>
</Project>
'@ >> $vcxfile

}

function RunTest($scriptPath, $Platform, $testexes)
{
	$originalErrorActionPreference = $ErrorActionPreference

	# Run regression tests
	if ($Platform -eq "x64") {
		$targetdir="test_x64"
	} else {
		$targetdir="test_x86"
	}
	$revsdir=$scriptPath
	$origdir="${revsdir}\..\test"

	try {
		$regdiff="regression.diffs"
		$RESDIR="results"
		if (Test-Path $regdiff) {
			Remove-Item $regdiff
		}
		New-Item $RESDIR -ItemType Directory -Force > $null
		Get-Content "${origdir}\sampletables.sql" | .\reset-db
		if ($LASTEXITCODE -ne 0) {
			throw "`treset_db error"
		}
		$cnstr = @()
		switch ($DeclareFetch) {
			"off"	{ $cnstr += "UseDeclareFetch=0" }
			"on"	{ $cnstr += "UseDeclareFetch=1" }
			"both"	{ $cnstr += "UseDeclareFetch=0"
				  $cnstr += "UseDeclareFetch=1" }
		}
		if ($cnstr.length -eq 0) {
			$cnstr += $null
		}
		# Temporarily set $ErrorActionPreference to "Continue" because MIMALLOC_VERBOSE writes to stderr
		$ErrorActionPreference = "Continue"
		$env:MIMALLOC_VERBOSE = 1
		for ($i = 0; $i -lt $cnstr.length; $i++)
		{
			$env:COMMON_CONNECTION_STRING_FOR_REGRESSION_TEST = $cnstr[$i]
			if ("$SpecificDsn" -ne "") {
				$env:COMMON_CONNECTION_STRING_FOR_REGRESSION_TEST += ";Database=contrib_regression;ConnSettings={set lc_messages='C'}"
			}
			write-host "`n`tSetting by env variable:$env:COMMON_CONNECTION_STRING_FOR_REGRESSION_TEST"
			.\runsuite $testexes --inputdir=$origdir 2>&1 | Tee-Object -Variable runsuiteOutput

			# Check whether mimalloc ran by searching for a verbose message from mimalloc
			if ($ExpectMimalloc -xor ($runsuiteOutput -match "mimalloc: process done")) {
				throw "`tmimalloc usage was expected to be $ExpectMimalloc"
			}
		}
	} catch [Exception] {
		throw $error[0]
	} finally {
		$env:COMMON_CONNECTION_STRING_FOR_REGRESSION_TEST = $null
		$env:MIMALLOC_VERBOSE = $null
		$ErrorActionPreference = $originalErrorActionPreference
	}
}

function SpecialDsn($testdsn, $testdriver, $dsninfo)
{
	function input-dsninfo($server="localhost", $uid="postgres", $passwd="postgres", $port="5432", $database="contrib_regression")
	{
		$in = read-host "Server [$server]"
		if ("$in" -ne "") {
			$server = $in
		}
		$in = read-host "Port [$port]"
		if ("$in" -ne "") {
			$port = $in
		}
		$in = read-host "Username [$uid]"
		if ("$in" -ne "") {
			$uid = $in
		}
		$in = read-host -assecurestring "Password [$passwd]"
		if ($in.Length -ne 0) {
			$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($in)
			$passwd = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)
		}
		return "SERVER=${server}|DATABASE=${database}|PORT=${port}|UID=${uid}|PWD=${passwd}"
	}

	$reinst = $ReinstallDriver
	$regProgram = "./RegisterRegdsn.exe"
	& $regProgram "check_dsn" $testdsn
	switch ($LastExitCode) { 
	 -1 {
		Write-Host "`tAdding System DSN=$testdsn Driver=$testdriver"
		if ($dsninfo.Length -eq 0) {
			$prop = input-dsninfo
		} else {
			$prop = $dsninfo
		}
		$prop += "|Debug=0|Commlog=0|ConnSettings=set+lc_messages='C'"
		$proc = Start-Process $regProgram -Verb runas -Wait -PassThru -ArgumentList "register_dsn $testdriver $testdsn $prop `"$dlldir`" Driver=${dllname}|Setup=${setup}"
		if ($proc.ExitCode -ne 0) {
			throw "`tAddDsn $testdsn error"
		}
		}
	 -2 {
		$reinst = $true
		<# Write-Host "`tReinstalling Driver=$testdriver"
		$proc = Start-Process $regProgram -Verb runas -Wait -PassThru -ArgumentList "reinstall_driver $testdriver `"$dlldir`" Driver=${dllname}|Setup=${setup}" #>
		}
	 0 {}
	 default {
		throw "$regProgram error"
		}
	}
	if ($reinst) {
		Write-Host "`tReinstalling Driver=$testdriver"
		$proc = Start-Process $regProgram -Verb runas -Wait -PassThru -ArgumentList "reinstall_driver $testdriver `"$dlldir`" Driver=${dllname}|Setup=${setup}"

	}
}

$scriptPath = (Split-Path $MyInvocation.MyCommand.Path)
$usingExe=$true
$testsf="$scriptPath\..\test\tests"
Write-Debug testsf=$testsf

$arrays=testlist_make $testsf
if ($null -eq $TestList) {
	$TESTEXES=$arrays[0]
	$TESTNAMES=$arrays[1]
	$DIRNAMES=$arrays[2]
} else {
	$err=$false
	$TESTNAMES=$TestList
	$TESTEXES=@()
	$DIRNAMES=@()
	foreach ($l in $TestList) {
		for ($i=0;$i -lt $arrays[1].length;$i++) {
			if ($l -eq $arrays[1][$i]) {
				$TESTEXES+=$arrays[0][$i]
				$DIRNAMES+=$arrays[2][$i]
				break
			}
		}
<#		if ($i -ge $arrays[1].length) {
			Write-Host "!! test case $l doesn't exist"
			$err=$true
		} #>
	}
	if ($err) {
		return
	}
}

Import-Module "$scriptPath\Psqlodbc-config.psm1"
$configInfo = LoadConfiguration $BuildConfigPath $scriptPath
$objbase = GetObjbase "$scriptPath\.."
$pushdir = GetObjbase "$scriptPath"

Import-Module ${scriptPath}\MSProgram-Get.psm1
$rtnArray=Find-MSBuild ([ref]$VCVersion) ($MSToolsVersion) ([ref]$Toolset) $configInfo
$msbuildexe=$rtnArray[0]
$MSToolsV=$rtnArray[1]
write-host "vcversion=$VCVersion toolset=$Toolset"

Remove-Module MSProgram-Get
Remove-Module Psqlodbc-config

$vcxfile="$objbase\generated_regress.vcxproj"
vcxfile_make $TESTNAMES $DIRNAMES $vcxfile

if ($Platform -ieq "both") {
	$pary = @("Win32", "x64")
} else {
	$pary = @($Platform)
}

$vcx_target=$target
if ($target -ieq "Build&Go") {
	$vcx_target="Build"
}
if ($Ansi) {
	write-host ** testing Ansi driver **
	$testdriver="postgres_deva"
	$testdsn="psqlodbc_test_dsn_ansi"
	$ansi_dir_part="ANSI"
	$dllname="psqlsetupa.dll"
	$setup="psqlsetupa.dll"
} else {
	write-host ** testing unicode driver **
	$testdriver="postgres_devw"
	$testdsn="psqlodbc_test_dsn"
	$ansi_dir_part="Unicode"
	$dllname="psqlsetup.dll"
	$setup="psqlsetup.dll"
}
if ($DriverConfiguration -ieq "Debug") {
	$testdriver += "_debug"
	$testdsn += "_debug"
}
if ("$DsnInfo" -ne "") {
	Write-Host "`tDsn Info=$DsnInfo"
	$dsninfo=$DsnInfo
}
if ("$SpecificDsn" -ne "") {
	Write-Host "`tSpecific DSN=$SpecificDsn"
	$testdsn=$SpecificDsn
}
foreach ($pl in $pary) {
	cd $scriptPath
	& ${msbuildexe} ${vcxfile} /tv:$MSToolsV "/p:Platform=$pl;Configuration=$Configuration;PlatformToolset=${Toolset}" /t:$vcx_target /p:VisualStudioVersion=${VCVersion} /p:scriptPath=${scriptPath} /Verbosity:minimal
	if ($LASTEXITCODE -ne 0) {
		throw "`nCompile error"
	}

	if (($target -ieq "Clean") -or ($target -ieq "Build")) {
		continue
	}

	switch ($pl) {
	 "Win32" {
			$targetdir="test_x86"
			$bit="32-bit"
			$dlldir="$objbase\x86_${ansi_dir_part}_$DriverConfiguration"
		}
	 default {
			$targetdir="test_x64"
			$bit="64-bit"
			$dlldir="$objbase\x64_${ansi_dir_part}_$DriverConfiguration"
		}
	}
	pushd $pushdir\$targetdir

	$env:PSQLODBC_TEST_DSN = $testdsn
	try {
		SpecialDsn $testdsn $testdriver $dsninfo
		RunTest $scriptPath $pl $TESTEXES
	} catch [Exception] {
		throw $error[0]
	} finally {
		popd
		$env:PSQLODBC_TEST_DSN = $null
	}
}