Skip to content

Commit bce5e4d

Browse files
authored
Merge pull request #134 from ianfixes/2019-08-19_exclude_dirs
Add support to exclude directories
2 parents 12102bb + c875a2a commit bce5e4d

12 files changed

+100
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99
### Added
10+
- Unit testing configuration now allows `exclude_dirs` to be set, which prevents stray source files from as part of unit testing allows
1011

1112
### Changed
1213

REFERENCE.md

+8
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,16 @@ For your unit tests, in addition to setting specific libraries and platforms, yo
112112

113113
Filtering your unit tests may help speed up targeted testing locally, but it is intended primarily as a means to temporarily disable tests between individual commits.
114114

115+
Furthermore, you can filter the files that will be included in the compilation step by specifying `exclude_dirs`. All cpp and header files in those directories will not be included in the compilation step, before the unittests are run.
116+
115117
```yaml
116118
unittest:
119+
120+
# Exclude these directories from compilation
121+
exclude_dirs:
122+
- someDirectory
123+
- someOtherDirectory
124+
117125
# Perform unit tests with these compilers (these are the binaries that will be called via the shell)
118126
compilers:
119127
- g++ # default

SampleProjects/TestSomething/.arduino-ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
unittest:
2+
exclude_dirs:
3+
- excludeThis
24
platforms:
35
- uno
46
- due
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This file intentionally contains syntactically incorrect code
2+
to break unit test compilation. If arduino_ci is working
3+
properly, it should exclude this file (as per .arduino-ci.yml
4+
configuration) and unit test compilation should succeed.
5+
6+
~!@#$%^&*()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This file intentionally contains syntactically incorrect code
2+
to break unit test compilation. If arduino_ci is working
3+
properly, it should exclude this file (as per .arduino-ci.yml
4+
configuration) and unit test compilation should succeed.
5+
6+
~!@#$%^&*()

arduino_ci.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
2828
spec.add_dependency "os", "~> 1.0"
2929
spec.add_dependency "rubyzip", "~> 1.2"
3030

31-
spec.add_development_dependency "bundler", "~> 1.15"
31+
spec.add_development_dependency "bundler", "> 1.15"
3232
spec.add_development_dependency "keepachangelog_manager", "~> 0.0.2"
3333
spec.add_development_dependency "rspec", "~> 3.0"
3434
spec.add_development_dependency 'rubocop', '~>0.59.0'

exe/arduino_ci_remote.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ def perform_unit_tests(file_config)
183183
return
184184
end
185185
config = file_config.with_override_config(@cli_options[:ci_config])
186-
cpp_library = ArduinoCI::CppLibrary.new(Pathname.new("."), @arduino_cmd.lib_dir)
186+
cpp_library = ArduinoCI::CppLibrary.new(Pathname.new("."),
187+
@arduino_cmd.lib_dir,
188+
config.exclude_dirs.map(&Pathname.method(:new)))
187189

188190
# check GCC
189191
compilers = config.compilers_to_use

lib/arduino_ci/ci_config.rb

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
compilers: Array,
2929
platforms: Array,
3030
libraries: Array,
31+
exclude_dirs: Array,
3132
testfiles: {
3233
select: Array,
3334
reject: Array,
@@ -256,6 +257,14 @@ def compilers_to_use
256257
@unittest_info[:compilers]
257258
end
258259

260+
# paths to exclude all files in for building and unitttests
261+
# @return [Array<String>] The directories (relative to base dir) to exclude
262+
def exclude_dirs
263+
return [] if @unittest_info[:exclude_dirs].nil?
264+
265+
@unittest_info[:exclude_dirs]
266+
end
267+
259268
# platforms to build [the examples on]
260269
# @return [Array<String>] The platforms to build
261270
def platforms_to_build

lib/arduino_ci/cpp_library.rb

+27-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ class CppLibrary
3737

3838
# @param base_dir [Pathname] The path to the library being tested
3939
# @param arduino_lib_dir [Pathname] The path to the libraries directory
40-
def initialize(base_dir, arduino_lib_dir)
40+
# @param exclude_dirs [Array<Pathname>] Directories that should be excluded from compilation
41+
def initialize(base_dir, arduino_lib_dir, exclude_dirs)
4142
raise ArgumentError, 'base_dir is not a Pathname' unless base_dir.is_a? Pathname
4243
raise ArgumentError, 'arduino_lib_dir is not a Pathname' unless arduino_lib_dir.is_a? Pathname
44+
raise ArgumentError, 'exclude_dir is not an array of Pathnames' unless exclude_dirs.is_a?(Array)
45+
raise ArgumentError, 'exclude_dir array contains non-Pathname elements' unless exclude_dirs.all? { |p| p.is_a? Pathname }
4346

4447
@base_dir = base_dir
48+
@exclude_dirs = exclude_dirs
4549
@arduino_lib_dir = arduino_lib_dir.expand_path
4650
@artifacts = []
4751
@last_err = ""
@@ -115,6 +119,19 @@ def in_tests_dir?(path)
115119
false
116120
end
117121

122+
# Guess whether a file is part of any @excludes_dir dir (indicating library compilation should ignore it).
123+
#
124+
# @param path [Pathname] The path to check
125+
# @return [bool]
126+
def in_exclude_dir?(path)
127+
# we could do this but some rubies don't return an enumerator for ascend
128+
# path.ascend.any? { |part| tests_dir_aliases.include?(part) }
129+
path.ascend do |part|
130+
return true if exclude_dir.any? { |p| p.realpath == part }
131+
end
132+
false
133+
end
134+
118135
# Check whether libasan (and by extension -fsanitizer=address) is supported
119136
#
120137
# This requires compilation of a sample program, and will be cached
@@ -150,7 +167,7 @@ def cpp_files_in(some_dir)
150167
# CPP files that are part of the project library under test
151168
# @return [Array<Pathname>]
152169
def cpp_files
153-
cpp_files_in(@base_dir).reject { |p| vendor_bundle?(p) || in_tests_dir?(p) }
170+
cpp_files_in(@base_dir).reject { |p| vendor_bundle?(p) || in_tests_dir?(p) || in_exclude_dir?(p) }
154171
end
155172

156173
# CPP files that are part of the arduino mock library we're providing
@@ -172,6 +189,12 @@ def cpp_files_libraries(aux_libraries)
172189
arduino_library_src_dirs(aux_libraries).map { |d| cpp_files_in(d) }.flatten.uniq
173190
end
174191

192+
# Returns the Pathnames for all paths to exclude from testing and compilation
193+
# @return [Array<Pathname>]
194+
def exclude_dir
195+
@exclude_dirs.map { |p| Pathname.new(@base_dir) + p }.select(&:exist?)
196+
end
197+
175198
# The directory where we expect to find unit test defintions provided by the user
176199
# @return [Pathname]
177200
def tests_dir
@@ -190,7 +213,8 @@ def header_dirs
190213
real = @base_dir.realpath
191214
all_files = Find.find(real).map { |f| Pathname.new(f) }.reject(&:directory?)
192215
unbundled = all_files.reject { |path| vendor_bundle?(path) }
193-
files = unbundled.select { |path| HPP_EXTENSIONS.include?(path.extname.downcase) }
216+
unexcluded = unbundled.reject { |path| in_exclude_dir?(path) }
217+
files = unexcluded.select { |path| HPP_EXTENSIONS.include?(path.extname.downcase) }
194218
files.map(&:dirname).uniq
195219
end
196220

spec/ci_config_spec.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157

158158
context "allowable_unittest_files" do
159159
cpp_lib_path = Pathname.new(__dir__) + "fake_library"
160-
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path, Pathname.new("my_fake_arduino_lib_dir"))
160+
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path, Pathname.new("my_fake_arduino_lib_dir"), [])
161161

162162
it "starts with a known set of files" do
163163
expect(cpp_library.test_files.map { |f| File.basename(f) }).to match_array([
@@ -177,4 +177,3 @@
177177
end
178178

179179
end
180-

spec/cpp_library_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def get_relative_dir(sampleprojects_tests_dir)
1313
RSpec.describe ArduinoCI::CppLibrary do
1414
next if skip_ruby_tests
1515
cpp_lib_path = sampleproj_path + "DoSomething"
16-
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path, Pathname.new("my_fake_arduino_lib_dir"))
16+
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path, Pathname.new("my_fake_arduino_lib_dir"), [])
1717
context "cpp_files" do
1818
it "finds cpp files in directory" do
1919
dosomething_cpp_files = [Pathname.new("DoSomething") + "do-something.cpp"]

spec/testsomething_unittests_spec.rb

+35-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,44 @@ def get_relative_dir(sampleprojects_tests_dir)
1010
sampleprojects_tests_dir.relative_path_from(base_dir)
1111
end
1212

13+
RSpec.describe "TestSomething C++ without excludes" do
14+
next if skip_cpp_tests
15+
cpp_lib_path = sampleproj_path + "TestSomething"
16+
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path,
17+
Pathname.new("my_fake_arduino_lib_dir"),
18+
[])
19+
context "cpp_files" do
20+
it "finds cpp files in directory" do
21+
testsomething_cpp_files = [
22+
Pathname.new("TestSomething/test-something.cpp"),
23+
Pathname.new("TestSomething/excludeThis/exclude-this.cpp")
24+
]
25+
relative_paths = cpp_library.cpp_files.map { |f| get_relative_dir(f) }
26+
expect(relative_paths).to match_array(testsomething_cpp_files)
27+
end
28+
end
29+
30+
context "unit tests" do
31+
it "can't build due to files that should have been excluded" do
32+
config = ArduinoCI::CIConfig.default.from_example(cpp_lib_path)
33+
path = config.allowable_unittest_files(cpp_library.test_files).first
34+
compiler = config.compilers_to_use.first
35+
result = cpp_library.build_for_test_with_configuration(path,
36+
[],
37+
compiler,
38+
config.gcc_config("uno"))
39+
expect(result).to be nil
40+
end
41+
end
42+
43+
end
44+
1345
RSpec.describe "TestSomething C++" do
1446
next if skip_cpp_tests
1547
cpp_lib_path = sampleproj_path + "TestSomething"
16-
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path, Pathname.new("my_fake_arduino_lib_dir"))
48+
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path,
49+
Pathname.new("my_fake_arduino_lib_dir"),
50+
["excludeThis"].map(&Pathname.method(:new)))
1751
context "cpp_files" do
1852
it "finds cpp files in directory" do
1953
testsomething_cpp_files = [Pathname.new("TestSomething/test-something.cpp")]

0 commit comments

Comments
 (0)