File indexing completed on 2024-05-19 16:49:24

0001 #!/usr/bin/env ruby
0002 
0003 # SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0004 #
0005 # SPDX-License-Identifier: GPL-2.0-or-later
0006 
0007 require_relative 'test_helper'
0008 
0009 require 'tmpdir'
0010 
0011 class ServiceMenuInstallationTest < Test::Unit::TestCase
0012   def setup
0013     @tmpdir = Dir.mktmpdir("dolphintest-#{self.class.to_s.tr(':', '_')}")
0014     @pwdir = Dir.pwd
0015     Dir.chdir(@tmpdir)
0016 
0017     ENV['XDG_DATA_HOME'] = File.join(@tmpdir, 'data')
0018   end
0019 
0020   def teardown
0021     Dir.chdir(@pwdir)
0022     FileUtils.rm_rf(@tmpdir)
0023 
0024     ENV.delete('XDG_DATA_HOME')
0025   end
0026 
0027   def test_run_install
0028     service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
0029     FileUtils.mkpath(service_dir)
0030     archive = "#{service_dir}/foo.tar"
0031 
0032     archive_dir = 'foo' # relative so tar cf is relative without fuzz
0033     FileUtils.mkpath(archive_dir)
0034     File.write("#{archive_dir}/install-it.sh", <<-INSTALL_IT_SH)
0035 #!/bin/sh
0036 touch #{@tmpdir}/install-it.sh-run
0037 INSTALL_IT_SH
0038     File.write("#{archive_dir}/install.sh", <<-INSTALL_SH)
0039 #!/bin/sh
0040 touch #{@tmpdir}/install.sh-run
0041     INSTALL_SH
0042     assert(system('tar', '-cf', archive, archive_dir))
0043 
0044     assert(system('servicemenuinstaller', 'install', archive))
0045 
0046     tar_dir = "#{service_dir}/foo.tar-dir"
0047     tar_extract_dir = "#{service_dir}/foo.tar-dir/foo"
0048     assert_path_exist(tar_dir)
0049     assert_path_exist(tar_extract_dir)
0050     assert_path_exist("#{tar_extract_dir}/install-it.sh")
0051     assert_path_exist("#{tar_extract_dir}/install.sh")
0052   end
0053 
0054   def test_run_install_with_arg
0055     service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
0056     FileUtils.mkpath(service_dir)
0057     archive = "#{service_dir}/foo.tar"
0058 
0059     archive_dir = 'foo' # relative so tar cf is relative without fuzz
0060     FileUtils.mkpath(archive_dir)
0061     File.write("#{archive_dir}/install.sh", <<-INSTALL_SH)
0062 #!/bin/sh
0063 if [ "$@" = "--install" ]; then
0064   touch #{@tmpdir}/install.sh-run
0065   exit 0
0066 fi
0067 exit 1
0068     INSTALL_SH
0069     assert(system('tar', '-cf', archive, archive_dir))
0070 
0071     assert(system('servicemenuinstaller', 'install', archive))
0072 
0073     tar_dir = "#{service_dir}/foo.tar-dir"
0074     tar_extract_dir = "#{service_dir}/foo.tar-dir/foo"
0075     assert_path_exist(tar_dir)
0076     assert_path_exist(tar_extract_dir)
0077     assert_path_not_exist("#{tar_extract_dir}/install-it.sh")
0078     assert_path_exist("#{tar_extract_dir}/install.sh")
0079   end
0080 
0081   def test_run_fail
0082     service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
0083     FileUtils.mkpath(service_dir)
0084     archive = "#{service_dir}/foo.tar"
0085 
0086     archive_dir = 'foo' # relative so tar cf is relative without fuzz
0087     FileUtils.mkpath(archive_dir)
0088     assert(system('tar', '-cf', archive, archive_dir))
0089 
0090     refute(system('servicemenuinstaller', 'install', archive))
0091   end
0092 
0093   def test_run_desktop
0094     service_dir = File.join(Dir.pwd, 'share/servicemenu-download')
0095     downloaded_file = "#{service_dir}/foo.desktop"
0096     FileUtils.mkpath(service_dir)
0097     FileUtils.touch(downloaded_file)
0098 
0099     installed_file = "#{ENV['XDG_DATA_HOME']}/kservices5/ServiceMenus/foo.desktop"
0100 
0101     assert(system('servicemenuinstaller', 'install', downloaded_file))
0102 
0103     assert_path_exist(downloaded_file)
0104     assert_path_exist(installed_file)
0105   end
0106 end