-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathrun_tests.ps1
More file actions
164 lines (144 loc) · 4.99 KB
/
run_tests.ps1
File metadata and controls
164 lines (144 loc) · 4.99 KB
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
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$PackageToTest="UnitTesting",
[Parameter(Mandatory = $false)]
[switch] $syntax_test,
[Parameter(Mandatory = $false)]
[switch] $syntax_compatibility,
[Parameter(Mandatory = $false)]
[switch] $color_scheme_test,
[Parameter(Mandatory = $false)]
[switch] $coverage
)
$ErrorActionPreference = 'stop'
# UTF8 encoding without preamble (default in .NET is with preamble).
new-variable -name 'UTF8Encoding' -option CONSTANT -scope 'script' `
-value (new-object System.Text.UTF8Encoding -argumentlist $false)
# todo(guillermooo): Make this configurable.
$packagesPath = 'C:\st\Data\Packages'
$stPath = 'C:\st\sublime_text.exe'
$outDir = "$packagesPath\User\UnitTesting\$PackageToTest"
$outFile = "$outDir\result"
$coverageFile = "$outDir\coverage"
[void] (new-item -itemtype file $outFile -force)
remove-item $outFile -force -erroraction silentlycontinue
$schedule_source = "$packagesPath\UnitTesting\sbin\run_scheduler.py"
$schedule_target = "$packagesPath\UnitTesting\zzz_run_scheduler.py"
function addSchedule {
# Configure packages to be tested.
$jpath = "$packagesPath\User\UnitTesting\schedule.json"
if (test-path $jpath) {
$schedule = convertfrom-json "$(get-content $jpath)"
}
else {
[void] (new-item -itemtype file -path $jpath -force)
# Only way of using encoding object.
[System.IO.File]::WriteAllText($jpath, "[]", $UTF8Encoding)
$schedule = convertfrom-json "$(get-content $jpath)"
}
$found = (@($schedule | foreach-object { $_.package }) -eq $PackageToTest).length
if ($found -eq 0) {
$schedule_info = @{
"package" = $PackageToTest;
"output" = $outFile;
"syntax_test" = $syntax_test.IsPresent;
"syntax_compatibility" = $syntax_compatibility.IsPresent;
'color_scheme_test' = $color_scheme_test.IsPresent;
"coverage" = $coverage.IsPresent
}
write-verbose "Schedule:"
foreach ($h in $schedule_info.GetEnumerator()) {
write-verbose " $($h.Name): $($h.Value)"
}
$schedule += @($schedule_info)
}
[System.IO.File]::WriteAllText(
$jpath, (convertto-json $schedule), $UTF8Encoding)
}
function injectScheduler {
if (test-path $schedule_target) {
remove-item $schedule_target -force
}
if (-not (test-path $schedule_target)) {
copy-item $schedule_source $schedule_target -force
}
}
for ($i=1; $i -le 3; $i++) {
addSchedule
injectScheduler
start-process $stPath
$startTime = get-date
$timeout = $false
while (-not (test-path $outFile) -or (get-item $outFile).length -eq 0) {
write-host -nonewline "."
if (((get-date) - $startTime).totalseconds -ge 10) {
write-host
$timeout = $true
if ($i -eq 3) {
throw "Timeout: Sublime Text is not responding."
}
break
}
start-sleep -seconds 1
}
if ($timeout) {
stop-process -force -processname sublime_text -ea silentlycontinue
if (test-path $schedule_target) {
remove-item $schedule_target -force
}
continue
}
write-host
break
}
write-verbose "start to read output"
$copy = "$outfile.copy"
$read = 0
$done = $false
while ($true) {
# XXX(guillermooo): We can't open a file already opened by another
# process. By copying the file first, we can work around this. (But if
# we can copy it we should be able to read it too?).
# Powershell's `get-content $path -tail 1 -wait` is in fact able to read
# from an already opened file. Perhaps it uses the same workaround as we
# do here?
copy-item $outfile $copy -force
$lines = (get-content $copy)
$lines = $lines | select-object -skip $read
$count = $lines.count
if ($count -gt 0){
foreach ($i in 0..($count-1)){
$l = $lines | select-object -index $i
# do not print the last line, may be incomplete
if ($i -lt $count-1){
write-output $l
}
if ($l -match "^(OK|FAILED|ERROR)\b") {
$success = ($matches[1] -eq "OK")
}
if ($l -match "^UnitTesting: Done\.$") {
write-output $l
$done = $true
break
}
}
$read = $read + $count - 1
if ($done) { break }
}
start-sleep -milliseconds 200
}
# restore .coverage if it exists, needed for coveralls
if (test-path $coverageFile) {
copy-item $coverageFile ".\.coverage" -force
$cwd = (get-item -Path ".\" -verbose).fullname.replace("\", "\\")
$pkgpath = "$packagesPath\$PackageToTest".replace("\", "\\")
$data = (get-content ".\.coverage") -replace [regex]::escape($pkgpath), $cwd
set-content ".\.coverage" -value $data
}
if (test-path $schedule_target) {
remove-item $schedule_target -force
}
if (!$success) {
throw
}