diff options
| author | Harald Sitter <[email protected]> | 2019-02-26 13:48:08 +0100 |
|---|---|---|
| committer | Harald Sitter <[email protected]> | 2019-03-18 14:02:16 +0100 |
| commit | 644d6f85223d486a412af9aaf4b40ef3d5d6705b (patch) | |
| tree | 3c9e99afe3f7d65007e7cc20185d932834f28437 /src/settings/services/test/test_helper.rb | |
| parent | 363fd1afa9dc68ffdee4d55a8c8bde56a13f0969 (diff) | |
redo service menu ruby helpers from ground up more or less
Summary:
- apply ruby community style guidelines
- full rewrite fixing, among other things:
- inefficient/unreadable String#end_with reimplementation
- inefficient use of Kernel.system (forked shell to fork a shell)
- inefficient/unreadable Dir.glob reimplementation
- inefficient File initialization for single chmod
- invocation conditions are now actually readable
- invocation conditions now also force +x on argless scripts, not just
scripts that need arguments
- repetitive conditions are now expressed as loops on argument arrays
- mime detection now uses xdg-mime instead of file (xdg-mime internally
may fall back to mime but will prefer higher level tools such as
kmimetypefinder5; giving better results overall)
- return values of "backtick forks" are now checked and will produce
suitable errors on stderr
- fail now takes a log_msg argument which is printed to stderr. this
is in addition to the error raised as notification for the user, as
that is unfortunately not so useful for diagnostics
- overall error handling and logging of problem causes is much improved
- add license headers. the original code was actually fairly exhaustive, so
this really should have had a header to begin with. the code was
originally introduced in svn r1045663 on Nov 6 14:56:35 2009 UTC
- add blackbox tests. in the interest of keeping the scripts actually
simple scripts (as opposed to a bunch of classes used by even simpler
scirpts) they are now also covered by test rigging which runs them as
scripts (again, as opposed to individual unit testing of distinct units)
- the tests optionally can use simplecov to gather coverage metrics
- also wired up to ctest so it actually gets run
structurally there is actually a fair amount of overlap between the two
scripts, but again, in the interest of keeping things simple I think it's
better to live with that instead of refactoring a shared library out of
it and then use heavy-duty meta-programming
Reviewers: #dolphin, elvisangelaccio
Reviewed By: #dolphin, elvisangelaccio
Subscribers: elvisangelaccio, kfm-devel
Tags: #dolphin
Differential Revision: https://phabricator.kde.org/D19334
Diffstat (limited to 'src/settings/services/test/test_helper.rb')
| -rw-r--r-- | src/settings/services/test/test_helper.rb | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/settings/services/test/test_helper.rb b/src/settings/services/test/test_helper.rb new file mode 100644 index 000000000..98c29c139 --- /dev/null +++ b/src/settings/services/test/test_helper.rb @@ -0,0 +1,78 @@ +# Copyright (C) 2019 Harald Sitter <[email protected]> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +begin + require 'simplecov' + SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new( + [ + SimpleCov::Formatter::HTMLFormatter + ] + ) + SimpleCov.start +rescue LoadError + warn 'SimpleCov not loaded' +end + +# FIXME: add coverage report for jenkins? + +$LOAD_PATH.unshift(File.absolute_path('../', __dir__)) # ../ + +def __test_method_name__ + return @method_name if defined?(:@method_name) + index = 0 + caller = '' + until caller.start_with?('test_') + caller = caller_locations(index, 1)[0].label + index += 1 + end + caller +end + +# system() variant which sets up merge-coverage. simplecov supports merging +# of multiple coverage sets. we use this to get coverage metrics on the +# binaries without having to refactor the script into runnable classes. +def covered_system(cmd, *argv) + pid = fork do + Kernel.module_exec do + alias_method(:real_system, :system) + define_method(:system) do |*args| + return true if args.include?('kdialog') # disable kdialog call + real_system(*args) + end + end + + begin + require 'simplecov' + SimpleCov.start do + command_name "#{cmd}_#{__test_method_name__}" + merge_timeout 16 + end + rescue LoadError + warn 'SimpleCov not loaded' + end + + ARGV.replace(argv) + load "#{__dir__}/../#{cmd}" + puts 'all good, fork ending!' + exit 0 + end + waitedpid, status = Process.waitpid2(pid) + assert_equal(pid, waitedpid) + status.success? # behave like system and return the success only +end + +require 'test/unit' |
