┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/settings/services/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/settings/services/test')
-rw-r--r--src/settings/services/test/service_menu_deinstallation_test.rb121
-rw-r--r--src/settings/services/test/service_menu_installation_test.rb122
-rw-r--r--src/settings/services/test/test_helper.rb78
-rwxr-xr-xsrc/settings/services/test/test_run.rb24
4 files changed, 345 insertions, 0 deletions
diff --git a/src/settings/services/test/service_menu_deinstallation_test.rb b/src/settings/services/test/service_menu_deinstallation_test.rb
new file mode 100644
index 000000000..2b3514a65
--- /dev/null
+++ b/src/settings/services/test/service_menu_deinstallation_test.rb
@@ -0,0 +1,121 @@
+#!/usr/bin/env ruby
+
+# 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
+
+require_relative 'test_helper'
+
+require 'tmpdir'
+
+class ServiceMenuDeinstallationTest < Test::Unit::TestCase
+ def setup
+ @tmpdir = Dir.mktmpdir("dolphintest-#{self.class.to_s.tr(':', '_')}")
+ @pwdir = Dir.pwd
+ Dir.chdir(@tmpdir)
+
+ ENV['XDG_DATA_HOME'] = File.join(@tmpdir, 'data')
+ end
+
+ def teardown
+ Dir.chdir(@pwdir)
+ FileUtils.rm_rf(@tmpdir)
+
+ ENV.delete('XDG_DATA_HOME')
+ end
+
+ def test_run_deinstall
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ archive_base = "#{service_dir}/foo.zip"
+ archive_dir = "#{archive_base}-dir/foo-1.1/"
+ FileUtils.mkpath(archive_dir)
+ File.write("#{archive_dir}/deinstall.sh", <<-DEINSTALL_SH)
+#!/bin/sh
+touch #{@tmpdir}/deinstall.sh-run
+ DEINSTALL_SH
+ File.write("#{archive_dir}/install.sh", <<-INSTALL_SH)
+#!/bin/sh
+touch #{@tmpdir}/install.sh-run
+ INSTALL_SH
+
+ assert(covered_system('servicemenudeinstallation', archive_base))
+
+ # deinstaller should be run
+ # installer should not be run
+ # archive_dir should have been correctly removed
+
+ assert_path_exist('deinstall.sh-run')
+ assert_path_not_exist('install.sh-run')
+ assert_path_not_exist(archive_dir)
+ end
+
+ def test_run_install_with_arg
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ archive_base = "#{service_dir}/foo.zip"
+ archive_dir = "#{archive_base}-dir/foo-1.1/"
+ FileUtils.mkpath(archive_dir)
+
+ File.write("#{archive_dir}/install.sh", <<-INSTALL_SH)
+#!/bin/sh
+if [ "$@" = "--uninstall" ]; then
+ touch #{@tmpdir}/install.sh-run
+ exit 0
+fi
+exit 1
+ INSTALL_SH
+
+ assert(covered_system('servicemenudeinstallation', archive_base))
+
+ assert_path_not_exist('deinstall.sh-run')
+ assert_path_exist('install.sh-run')
+ assert_path_not_exist(archive_dir)
+ end
+
+ # no scripts in sight
+ def test_run_fail
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ archive_base = "#{service_dir}/foo.zip"
+ archive_dir = "#{archive_base}-dir/foo-1.1/"
+ FileUtils.mkpath(archive_dir)
+
+ refute(covered_system('servicemenudeinstallation', archive_base))
+
+ # I am unsure if deinstallation really should keep the files around. But
+ # that's how it behaved originally so it's supposedly intentional
+ # - sitter, 2019
+ assert_path_exist(archive_dir)
+ end
+
+ # For desktop files things are a bit special. There is one in .local/share/servicemenu-download
+ # and another in the actual ServiceMenus dir. The latter gets removed by the
+ # script, the former by KNS.
+ def test_run_desktop
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ downloaded_file = "#{service_dir}/foo.desktop"
+ FileUtils.mkpath(service_dir)
+ FileUtils.touch(downloaded_file)
+
+ menu_dir = "#{ENV['XDG_DATA_HOME']}/kservices5/ServiceMenus/"
+ installed_file = "#{menu_dir}/foo.desktop"
+ FileUtils.mkpath(menu_dir)
+ FileUtils.touch(installed_file)
+
+ assert(covered_system('servicemenudeinstallation', downloaded_file))
+
+ assert_path_exist(downloaded_file)
+ assert_path_not_exist(installed_file)
+ end
+end
diff --git a/src/settings/services/test/service_menu_installation_test.rb b/src/settings/services/test/service_menu_installation_test.rb
new file mode 100644
index 000000000..3c9fc90ef
--- /dev/null
+++ b/src/settings/services/test/service_menu_installation_test.rb
@@ -0,0 +1,122 @@
+#!/usr/bin/env ruby
+
+# 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
+
+require_relative 'test_helper'
+
+require 'tmpdir'
+
+class ServiceMenuInstallationTest < Test::Unit::TestCase
+ def setup
+ @tmpdir = Dir.mktmpdir("dolphintest-#{self.class.to_s.tr(':', '_')}")
+ @pwdir = Dir.pwd
+ Dir.chdir(@tmpdir)
+
+ ENV['XDG_DATA_HOME'] = File.join(@tmpdir, 'data')
+ end
+
+ def teardown
+ Dir.chdir(@pwdir)
+ FileUtils.rm_rf(@tmpdir)
+
+ ENV.delete('XDG_DATA_HOME')
+ end
+
+ def test_run_install
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ FileUtils.mkpath(service_dir)
+ archive = "#{service_dir}/foo.tar"
+
+ archive_dir = 'foo' # relative so tar cf is relative without fuzz
+ FileUtils.mkpath(archive_dir)
+ File.write("#{archive_dir}/install-it.sh", <<-INSTALL_IT_SH)
+#!/bin/sh
+touch #{@tmpdir}/install-it.sh-run
+INSTALL_IT_SH
+ File.write("#{archive_dir}/install.sh", <<-INSTALL_SH)
+#!/bin/sh
+touch #{@tmpdir}/install.sh-run
+ INSTALL_SH
+ assert(system('tar', '-cf', archive, archive_dir))
+
+ assert(covered_system('servicemenuinstallation', archive))
+
+ tar_dir = "#{service_dir}/foo.tar-dir"
+ tar_extract_dir = "#{service_dir}/foo.tar-dir/foo"
+ assert_path_exist(tar_dir)
+ assert_path_exist(tar_extract_dir)
+ assert_path_exist("#{tar_extract_dir}/install-it.sh")
+ assert_path_exist("#{tar_extract_dir}/install.sh")
+ end
+
+ def test_run_install_with_arg
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ FileUtils.mkpath(service_dir)
+ archive = "#{service_dir}/foo.tar"
+
+ archive_dir = 'foo' # relative so tar cf is relative without fuzz
+ FileUtils.mkpath(archive_dir)
+ File.write("#{archive_dir}/install.sh", <<-INSTALL_SH)
+#!/bin/sh
+if [ "$@" = "--install" ]; then
+ touch #{@tmpdir}/install.sh-run
+ exit 0
+fi
+exit 1
+ INSTALL_SH
+ assert(system('tar', '-cf', archive, archive_dir))
+
+ assert(covered_system('servicemenuinstallation', archive))
+
+ tar_dir = "#{service_dir}/foo.tar-dir"
+ tar_extract_dir = "#{service_dir}/foo.tar-dir/foo"
+ assert_path_exist(tar_dir)
+ assert_path_exist(tar_extract_dir)
+ assert_path_not_exist("#{tar_extract_dir}/install-it.sh")
+ assert_path_exist("#{tar_extract_dir}/install.sh")
+ end
+
+ def test_run_fail
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ FileUtils.mkpath(service_dir)
+ archive = "#{service_dir}/foo.tar"
+
+ archive_dir = 'foo' # relative so tar cf is relative without fuzz
+ FileUtils.mkpath(archive_dir)
+ assert(system('tar', '-cf', archive, archive_dir))
+
+ refute(covered_system('servicemenuinstallation', archive))
+ end
+
+ def test_run_desktop
+ service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
+ downloaded_file = "#{service_dir}/foo.desktop"
+ FileUtils.mkpath(service_dir)
+ FileUtils.touch(downloaded_file)
+
+ menu_dir = "#{ENV['XDG_DATA_HOME']}/kservices5/ServiceMenus/"
+ installed_file = "#{menu_dir}/foo.desktop"
+ FileUtils.mkpath(menu_dir)
+ FileUtils.touch(installed_file)
+
+ assert(covered_system('servicemenuinstallation', downloaded_file))
+
+ assert_path_exist(downloaded_file)
+ assert_path_exist(installed_file)
+ end
+end
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'
diff --git a/src/settings/services/test/test_run.rb b/src/settings/services/test/test_run.rb
new file mode 100755
index 000000000..b2149bbe0
--- /dev/null
+++ b/src/settings/services/test/test_run.rb
@@ -0,0 +1,24 @@
+#!/usr/bin/env ruby
+
+# 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
+# This is a fancy wrapper around test_helper to prevent the collector from
+# loading the helper twice as it would occur if we ran the helper directly.
+
+require_relative 'test_helper'
+
+Test::Unit::AutoRunner.run(true, File.absolute_path(__dir__))