- Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcpp_library_spec.rb
319 lines (276 loc) · 11.5 KB
/
cpp_library_spec.rb
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
require"spec_helper"
require"pathname"
require'tmpdir'
require'fake_lib_dir'
sampleproj_path=Pathname.new(__dir__).parent + "SampleProjects"
defverified_install(backend,path)
ret=backend.install_local_library(path)
raise"backend.install_local_library from '#{path}' failed: #{backend.last_msg}"ifret.nil?
ret
end
RSpec.describe"ExcludeSomething C++"do
nextifskip_cpp_tests
# we will need to install some dummy libraries into a fake location, so do that on demand
fld=FakeLibDir.new
backend=fld.backend
test_lib_name="ExcludeSomething"
cpp_lib_path=sampleproj_path + test_lib_name
around(:example){ |example| fld.in_pristine_fake_libraries_dir(example)}
before(:each)do
@base_dir=fld.libraries_dir
@cpp_library=verified_install(backend,cpp_lib_path)
end
context"without excludes"do
context"cpp_files"do
it"finds cpp files in directory"do
expect(@cpp_library).to_notbe(nil)
excludesomething_cpp_files=[
Pathname.new("ExcludeSomething/src/exclude-something.cpp"),
Pathname.new("ExcludeSomething/src/excludeThis/exclude-this.cpp")
]
relative_paths=@cpp_library.cpp_files.map{ |f| f.relative_path_from(@base_dir)}
expect(relative_paths).tomatch_array(excludesomething_cpp_files)
end
end
context"unit tests"do
it"can't build due to files that should have been excluded"do
@cpp_library=verified_install(backend,cpp_lib_path)
config=ArduinoCI::CIConfig.default.from_example(cpp_lib_path)
path=config.allowable_unittest_files(@cpp_library.test_files).first
compiler=config.compilers_to_use.first
result=@cpp_library.build_shared_library([],compiler,config.gcc_config("uno"))
expect(result).tobenil
result=@cpp_library.build_for_test(path,compiler)
expect(result).tobenil
end
end
end
context"with excludes"do
context"cpp_files"do
it"finds cpp files in directory"do
@cpp_library=verified_install(backend,cpp_lib_path)
@cpp_library.exclude_dirs=["src/excludeThis"].map(&Pathname.method(:new))
excludesomething_cpp_files=[
Pathname.new("ExcludeSomething/src/exclude-something.cpp")
]
relative_paths=@cpp_library.cpp_files.map{ |f| f.relative_path_from(@base_dir)}
expect(relative_paths).tomatch_array(excludesomething_cpp_files)
end
end
end
end
RSpec.describeArduinoCI::CppLibrarydo
nextifskip_ruby_tests
context"compiler flags"do
config=ArduinoCI::CIConfig.new
config.load_yaml(File.join(File.dirname(__FILE__),"yaml","o1.yaml"))
bogo_config=config.gcc_config("bogo")
fld=FakeLibDir.new
backend=fld.backend
cpp_lib_path=sampleproj_path + "DoSomething"
around(:example){ |example| fld.in_pristine_fake_libraries_dir(example)}
before(:each){@cpp_library=verified_install(backend,cpp_lib_path)}
# the keys are the methods of cpp_library to call
# the results are what we expect to see based on the config we loaded
methods_and_results={
feature_args: ["-fa","-fb"],
warning_args: ["-We","-Wf"],
define_args: ["-Dc","-Dd"],
flag_args: ["g","h"]
}
methods_and_results.eachdo |m,expected|
it"Creates #{m} from config"do
expect(expected).toeq(@cpp_library.send(m,bogo_config))
end
end
end
context"arduino-library-specification detection"do
answers={
DoSomething: {
one_five: false,
library_properties: true,
cpp_files: [Pathname.new("DoSomething") + "do-something.cpp"],
cpp_files_libraries: [],
header_dirs: [Pathname.new("DoSomething")],
arduino_library_src_dirs: [],
test_files: [
"DoSomething/test/bad-errormessages.cpp",
"DoSomething/test/bad-null.cpp",
"DoSomething/test/good-assert.cpp",
"DoSomething/test/good-library.cpp",
"DoSomething/test/good-null.cpp",
].map{ |f| Pathname.new(f)}
},
OnePointOhDummy: {
one_five: false,
library_properties: false,
cpp_files: [
"OnePointOhDummy/YesBase.cpp",
"OnePointOhDummy/utility/YesUtil.cpp",
].map{ |f| Pathname.new(f)},
cpp_files_libraries: [],
header_dirs: [
"OnePointOhDummy",
"OnePointOhDummy/utility"
].map{ |f| Pathname.new(f)},
arduino_library_src_dirs: [],
test_files: [
"OnePointOhDummy/test/null.cpp",
].map{ |f| Pathname.new(f)}
},
OnePointFiveMalformed: {
one_five: false,
library_properties: false,
cpp_files: [
"OnePointFiveMalformed/YesBase.cpp",
"OnePointFiveMalformed/utility/YesUtil.cpp",
].map{ |f| Pathname.new(f)},
cpp_files_libraries: [],
header_dirs: [
"OnePointFiveMalformed",
"OnePointFiveMalformed/utility"
].map{ |f| Pathname.new(f)},
arduino_library_src_dirs: [],
test_files: []
},
OnePointFiveDummy: {
one_five: true,
library_properties: true,
cpp_files: [
"OnePointFiveDummy/src/YesSrc.cpp",
"OnePointFiveDummy/src/subdir/YesSubdir.cpp",
].map{ |f| Pathname.new(f)},
cpp_files_libraries: [],
header_dirs: [
"OnePointFiveDummy/src",
"OnePointFiveDummy/src/subdir",
].map{ |f| Pathname.new(f)},
arduino_library_src_dirs: [],
test_files: [
"OnePointFiveDummy/test/null.cpp",
].map{ |f| Pathname.new(f)}
}
}
# easier to construct this one from the other test cases
answers[:DependOnSomething]={
one_five: true,
library_properties: true,
cpp_files: ["DependOnSomething/src/YesDeps.cpp"].map{ |f| Pathname.new(f)},
cpp_files_libraries: answers[:OnePointOhDummy][:cpp_files] + answers[:OnePointFiveDummy][:cpp_files],
header_dirs: ["DependOnSomething/src"].map{ |f| Pathname.new(f)},# this is not recursive!
arduino_library_src_dirs: answers[:OnePointOhDummy][:header_dirs] + answers[:OnePointFiveDummy][:header_dirs],
test_files: [
"DependOnSomething/test/null.cpp",
].map{ |f| Pathname.new(f)}
}
answers.freeze
answers.eachdo |sampleproject,expected|
# we will need to install some dummy libraries into a fake location, so do that on demand
fld=FakeLibDir.new
backend=fld.backend
context"#{sampleproject}"do
cpp_lib_path=sampleproj_path + sampleproject.to_s
around(:example){ |example| fld.in_pristine_fake_libraries_dir(example)}
before(:each)do
@base_dir=fld.libraries_dir
@cpp_library=verified_install(backend,cpp_lib_path)
end
it"is a sane test env"do
expect(sampleproject.to_s).toeq(@cpp_library.name)
end
it"detects 1.5 format"do
expect(@cpp_library.one_point_five?).toeq(expected[:one_five])
end
it"detects library.properties"do
expect(@cpp_library.library_properties?).toeq(expected[:library_properties])
end
context"cpp_files"do
it"finds cpp files in directory"do
relative_paths=@cpp_library.cpp_files.map{ |f| f.relative_path_from(@base_dir)}
expect(relative_paths.map(&:to_s)).tomatch_array(expected[:cpp_files].map(&:to_s))
end
end
context"cpp_files_libraries"do
it"finds cpp files in directories of dependencies"do
@cpp_library.all_arduino_library_dependencies!# side effect: installs them
dependencies=@cpp_library.arduino_library_dependencies.nil? ? [] : @cpp_library.arduino_library_dependencies
dependencies.each{ |d| verified_install(backend,sampleproj_path + d)}
relative_paths=@cpp_library.cpp_files_libraries(dependencies).map{ |f| f.relative_path_from(@base_dir)}
expect(relative_paths.map(&:to_s)).tomatch_array(expected[:cpp_files_libraries].map(&:to_s))
end
end
context"header_dirs"do
it"finds directories containing h files"do
relative_paths=@cpp_library.header_dirs.map{ |f| f.relative_path_from(@base_dir)}
expect(relative_paths.map(&:to_s)).tomatch_array(expected[:header_dirs].map(&:to_s))
end
end
context"tests_dir"do
it"locates the tests directory"do
# since we don't know where the CI system will install this stuff,
# we need to go looking for a relative path to the SampleProjects directory
# just to get our "expected" value
relative_path=@cpp_library.tests_dir.relative_path_from(@base_dir)
expect(relative_path.to_s).toeq("#{sampleproject}/test")
end
end
context"examples_dir"do
it"locates the examples directory"do
relative_path=@cpp_library.examples_dir.relative_path_from(@base_dir)
expect(relative_path.to_s).toeq("#{sampleproject}/examples")
end
end
context"test_files"do
it"finds cpp files in directory"do
relative_paths=@cpp_library.test_files.map{ |f| f.relative_path_from(@base_dir)}
expect(relative_paths.map(&:to_s)).tomatch_array(expected[:test_files].map(&:to_s))
end
end
context"arduino_library_src_dirs"do
it"finds src dirs from dependent libraries"do
# we explicitly feed in the internal dependencies
dependencies=@cpp_library.arduino_library_dependencies.nil? ? [] : @cpp_library.arduino_library_dependencies
dependencies.each{ |d| verified_install(backend,sampleproj_path + d)}
relative_paths=@cpp_library.arduino_library_src_dirs(dependencies).map{ |f| f.relative_path_from(@base_dir)}
expect(relative_paths.map(&:to_s)).tomatch_array(expected[:arduino_library_src_dirs].map(&:to_s))
end
end
end
end
end
context"test"do
# we will need to install some dummy libraries into a fake location, so do that on demand
fld=FakeLibDir.new
backend=fld.backend
cpp_lib_path=sampleproj_path + "DoSomething"
config=ArduinoCI::CIConfig.default
around(:example){ |example| fld.in_pristine_fake_libraries_dir(example)}
before(:each){@cpp_library=verified_install(backend,cpp_lib_path)}
after(:each)do |example|
ifexample.exception
puts"Last command: #{@cpp_library.last_cmd}"
puts"========== Stdout:"
puts@cpp_library.last_out
puts"========== Stderr:"
puts@cpp_library.last_err
end
end
it"is going to test more than one library"do
test_files=@cpp_library.test_files
expect(test_files.empty?).tobefalse
end
test_files=Pathname.glob(Pathname.new(cpp_lib_path) + "test" + "*.cpp")
test_files.eachdo |path|
expected=path.basename.to_s.include?("good")
config.compilers_to_use.eachdo |compiler|
it"tests #{File.basename(path)} with #{compiler} expecting #{expected}"do
exe=@cpp_library.build_shared_library([],compiler,config.gcc_config("uno"))
expect(exe).not_tobenil
exe=@cpp_library.build_for_test(path,compiler)
expect(exe).not_tobenil
expect(@cpp_library.run_test_file(exe)).toeq(expected)
end
end
end
end
end