blob: 5cde19c702f4c4184b0cc34cc2a86b068dd791a6 [file] [log] [blame]
Jens Geyera4d458f2024-10-10 23:03:57 +02001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9# *
10# http://www.apache.org/licenses/LICENSE-2.0
11# *
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20#---- failing tests --------------------------------------------
21
22# expected to fail at Thrift Compiler
23$FAIL_THRIFT = @(
24 "BrokenConstants.thrift", # intended to break
25 "DuplicateImportsTest.thrift", # subdir includes don't work here
26 "Include.thrift") # subdir includes don't work here
27
28# expected to fail at Delphi Compiler
29$FAIL_DELPHI = @()
30
31# unexpected but known bugs (TODO: fix them)
Jens Geyera5d84e52024-10-17 22:56:48 +020032$KNOWN_BUGS = @( "ConstOptionalField.thrift"
33 , "IgnoreInitialismsTest.thrift"
34 , "JavaTypes.thrift"
35 , "JavaDefinitionOrderB.thrift"
36 , "JavaDeepCopyTest.thrift"
37 , "NameConflictTest.thrift"
38 , "Thrift5320.thrift" # this conflicts with Delphi scopes, but it's a bad practice testcase anyway
39 )
Jens Geyera4d458f2024-10-10 23:03:57 +020040
41
42
43#---- functions --------------------------------------------
44
45function FindThriftExe() {
46 # prefer debug over release over path
47 write-host -nonewline Looking for thrift.exe ...
48 $exe = "thrift.exe"
49
50 # if we have a freshly compiled one it might be a better choice
51 @("Release","Debug") | foreach{
52 if( test-path "$ROOTDIR\compiler\cpp\$_\thrift.exe") { $exe = "$ROOTDIR\compiler\cpp\$_\thrift.exe" }
53 if( test-path "$ROOTDIR\compiler\cpp\compiler\$_\thrift.exe") { $exe = "$ROOTDIR\compiler\cpp\$_\compiler\thrift.exe" }
54 }
55
56 return $exe
57}
58
59
60function FindDcc32Exe() {
61 # prefer debug over release over path
62 write-host -nonewline Looking for dcc32.exe ...
63 $exe = "dcc32.exe"
64
65 # TODO: add arbitraily complex code to locate a suitable dcc32.exe if it is not in the path
66
67 return $exe
68}
69
70
71function InitializeFolder([string] $folder, [string] $pattern) {
72 #write-host $folder\$pattern
73 if(-not (test-path $folder)) {
74 new-item $folder -type directory | out-null
75 }
76 pushd $folder
77 remove-item $pattern #-recurse
78 popd
79}
80
81
82function CopyFilesFrom([string] $source, $text) {
83 #write-host "$source"
84 if( ($source -ne "") -and (test-path $source)) {
85 if( $text -ne $null) {
86 write-host -foregroundcolor yellow Copying $text ...
87 }
88
89 pushd $source
90 # recurse dirs
91 gci . -directory | foreach {
92 CopyFilesFrom "$_"
93 }
94 # files within
95 gci *.thrift -file | foreach {
96 #write-host $_
97 $name = $_.name
98 copy-item $_ "$TARGET\$name"
99 }
100 popd
101 }
102}
103
104function TestIdlFile([string] $idl) {
105 # expected to fail at Thrift Compiler
106 $filter = $false
107 $FAIL_THRIFT | foreach {
108 if( $idl -eq $_) {
109 $filter = $true
110 write-host "Skipping $_"
111 }
112 }
113 if( $filter) { return $true }
114
115 # compile IDL
116 #write-host -nonewline " $idl"
117 InitializeFolder "$TARGET\gen-delphi" "*.pas"
118 &$THRIFT_EXE $VERBOSE -r --gen delphi:register_types,constprefix,events,xmldoc $idl | out-file "$TARGET\thrift.log"
119 if( -not $?) {
120 get-content "$TARGET\thrift.log" | out-default
121 write-host -foregroundcolor red "Thrift compilation failed: $idl"
122 return $false
123 }
124
125 # generate project dile
126 $units = gci "$TARGET\gen-delphi\*.pas"
127 $lines = @()
128 $lines += "program $TESTAPP;"
129 $lines += "{`$APPTYPE CONSOLE}"
130 $lines += ""
131 $lines += "uses"
132 $units | foreach { $name = $_.name.Replace(".pas",""); $lines += " $name," }
133 $lines += " Windows, Classes, SysUtils;"
134 $lines += ""
135 $lines += "begin"
136 $lines += " Writeln('Successfully compiled!');"
137 $lines += " Writeln('List of units:');"
138 $units | foreach { $name = $_.name.Replace(".pas",""); $lines += " Writeln('- $name');" }
139 $lines += " Writeln;"
140 $lines += ""
141 $lines += "end."
142 $lines += ""
143 $lines | set-content "$TARGET\gen-delphi\$TESTAPP.dpr"
144
145 # try to compile the DPR
146 # this should not throw any errors, warnings or hints
147 $exe = "$TARGET\gen-delphi\$TESTAPP.exe"
148 if( test-path $exe) { remove-item $exe }
149 &$DCC32_EXE -B "$TARGET\gen-delphi\$TESTAPP" -U"$UNITSEARCH" -I"$UNITSEARCH" -N"$OUTDCU" -E"$TARGET\gen-delphi" | out-file "$TARGET\compile.log"
150 if( -not (test-path $exe)) {
151
152 # expected to fail at Thrift Compiler
153 $filter = $false
154 $FAIL_DELPHI | foreach {
155 if( $idl -eq $_) {
156 $filter = $true
157 write-host ("Delphi compilation failed at "+$idl+" - as expected")
158 }
159 }
160 $KNOWN_BUGS | foreach {
161 if( $idl -eq $_) {
162 $filter = $true
163 write-host ("Delphi compilation failed at "+$idl+" - known issue (TODO)")
164 }
165 }
166 if( $filter) { return $true }
167
168 get-content "$TARGET\compile.log" | out-default
169 write-host -foregroundcolor red "Delphi compilation failed: $idl"
170 return $false
171 }
172
173 # The compiled program is now executed. If it hangs or crashes, we
174 # have a serious problem with the generated code. Expected output
175 # is "Successfully compiled:" followed by a list of generated units.
176 &"$exe" | out-file "$TARGET\exec.log"
177 if( -not $?) {
178 get-content "$TARGET\exec.log" | out-default
179 write-host -foregroundcolor red "Test execution failed: $idl"
180 return $false
181 }
182 return $true
183}
184
185#---- main -------------------------------------------------
186# CONFIGURATION BEGIN
187# configuration settings, adjust as necessary to meet your system setup
188$MY_THRIFT_FILES = ""
189$VERBOSE = "" # set any Thrift compiler debug/verbose flag you want
190
191# init
192$ROOTDIR = $PSScriptRoot + "\..\..\..\.."
193
194# try to find thrift.exe
195$THRIFT_EXE = FindThriftExe
196&$THRIFT_EXE -version
197if( -not $?) {
198 write-host -foregroundcolor red Missing thrift.exe
199 exit 1
200}
201
202# try to find dcc32.exe
203$DCC32_EXE = FindDcc32Exe
204&$DCC32_EXE --version
205if( -not $?) {
206 write-host -foregroundcolor red Missing dcc32.exe
207 exit 1
208}
209
210
211# some helpers
212$TARGET = "$ROOTDIR\..\thrift-testing"
213$TESTAPP = "TestProject"
214$UNITSEARCH = "$ROOTDIR\lib\pas\src;$ROOTDIR\lib\delphi\src"
215$OUTDCU = "$TARGET\dcu"
216
217# create and/or empty target dirs
218InitializeFolder "$TARGET" "*.thrift"
219InitializeFolder "$TARGET\gen-delphi" "*.pas"
220InitializeFolder "$OUTDCU" "*.dcu"
221
222# recurse through thrift WC and "my thrift files" folder
223# copies all .thrift files into thrift-testing
224CopyFilesFrom "$ROOTDIR" "Thrift IDL files"
225CopyFilesFrom "$MY_THRIFT_FILES" "Custom IDL files"
226
227# codegen and compile all thrift files, one by one to prevent side effects
228$count = 0
229write-host -foregroundcolor yellow Running codegen tests ..
230try {
231 pushd "$TARGET"
232 gci *.thrift -file | foreach {
233 $count += 1
234 $ok = TestIdlFile $_.name
235 if( -not $ok) {
236 throw "TEST FAILED" # automated tests
237 popd; pause; pushd "$TARGET" # interactive / debug
238 }
239 }
240 write-host -foregroundcolor green "Success ($count tests executed)"
241 exit 0
242} catch {
243 write-host -foregroundcolor red $_
244 exit 1
245} finally {
246 popd
247}
248
249#eof