Changeset View
Changeset View
Standalone View
Standalone View
src/settings/services/test/test_helper.rb
Show All 9 Lines | |||||
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | # GNU General Public License for more details. | 11 | # GNU General Public License for more details. | ||
12 | # | 12 | # | ||
13 | # You should have received a copy of the GNU General Public License | 13 | # You should have received a copy of the GNU General Public License | ||
14 | # along with this program; if not, write to the | 14 | # along with this program; if not, write to the | ||
15 | # Free Software Foundation, Inc., | 15 | # Free Software Foundation, Inc., | ||
16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | 17 | | |||
18 | GLOBAL_COVERAGE_ROOT = File.dirname(__dir__) # ../ | | |||
19 | | ||||
20 | # Simplecov is a bit meh and expects src and coverage to be under the | | |||
21 | # same root. Since we get run through cmake that assumption absolutely | | |||
22 | # doesn't hold true, so we'll need to figure out the coverage_dir relative | | |||
23 | # to the root and the root must always be the source :/ | | |||
24 | # The relativity only works because internally the path gets expanded, this | | |||
25 | # isn't fully reliable, but oh well... | | |||
26 | # https://github.com/colszowka/simplecov/issues/716 | | |||
27 | GLOBAL_COVERAGE_DIR = begin | | |||
28 | require 'pathname' | | |||
29 | src_path = Pathname.new(GLOBAL_COVERAGE_ROOT) | | |||
30 | coverage_path = Pathname.new(File.join(Dir.pwd, 'coverage')) | | |||
31 | coverage_path.relative_path_from(src_path).to_s | | |||
32 | end | | |||
33 | | ||||
34 | begin | | |||
35 | require 'simplecov' | | |||
36 | | ||||
37 | SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new( | | |||
38 | [ | | |||
39 | SimpleCov::Formatter::HTMLFormatter | | |||
40 | ] | | |||
41 | ) | | |||
42 | | ||||
43 | SimpleCov.start do | | |||
44 | root GLOBAL_COVERAGE_ROOT | | |||
45 | coverage_dir GLOBAL_COVERAGE_DIR | | |||
46 | end | | |||
47 | rescue LoadError | | |||
48 | warn 'SimpleCov not loaded' | | |||
49 | end | | |||
50 | | ||||
51 | # FIXME: add coverage report for jenkins? | | |||
52 | | ||||
53 | $LOAD_PATH.unshift(File.absolute_path('../', __dir__)) # ../ | 18 | $LOAD_PATH.unshift(File.absolute_path('../', __dir__)) # ../ | ||
54 | 19 | | |||
55 | def __test_method_name__ | 20 | def __test_method_name__ | ||
56 | return @method_name if defined?(:@method_name) | 21 | return @method_name if defined?(:@method_name) | ||
57 | index = 0 | 22 | index = 0 | ||
58 | caller = '' | 23 | caller = '' | ||
59 | until caller.start_with?('test_') | 24 | until caller.start_with?('test_') | ||
60 | caller = caller_locations(index, 1)[0].label | 25 | caller = caller_locations(index, 1)[0].label | ||
61 | index += 1 | 26 | index += 1 | ||
62 | end | 27 | end | ||
63 | caller | 28 | caller | ||
64 | end | 29 | end | ||
65 | 30 | | |||
66 | # system() variant which sets up merge-coverage. simplecov supports merging | | |||
67 | # of multiple coverage sets. we use this to get coverage metrics on the | | |||
68 | # binaries without having to refactor the script into runnable classes. | | |||
69 | def covered_system(cmd, *argv) | | |||
70 | pid = fork do | | |||
71 | Kernel.module_exec do | | |||
72 | alias_method(:real_system, :system) | | |||
73 | define_method(:system) do |*args| | | |||
74 | return true if args.include?('kdialog') # disable kdialog call | | |||
75 | real_system(*args) | | |||
76 | end | | |||
77 | end | | |||
78 | | ||||
79 | begin | | |||
80 | require 'simplecov' | | |||
81 | SimpleCov.start do | | |||
82 | root GLOBAL_COVERAGE_ROOT | | |||
83 | coverage_dir GLOBAL_COVERAGE_DIR | | |||
84 | command_name "#{cmd}_#{__test_method_name__}" | | |||
85 | end | | |||
86 | rescue LoadError | | |||
87 | warn 'SimpleCov not loaded' | | |||
88 | end | | |||
89 | | ||||
90 | ARGV.replace(argv) | | |||
91 | load "#{__dir__}/../#{cmd}" | | |||
92 | puts 'all good, fork ending!' | | |||
93 | exit 0 | | |||
94 | end | | |||
95 | waitedpid, status = Process.waitpid2(pid) | | |||
96 | assert_equal(pid, waitedpid) | | |||
97 | status.success? # behave like system and return the success only | | |||
98 | end | | |||
99 | | ||||
100 | require 'test/unit' | 31 | require 'test/unit' |