┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/settings/contextmenu/test/service_menu_installation_test.rb
blob: 7c05a40e37c02f116cbb4cfc7bb1e543e54a8e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby

# SPDX-FileCopyrightText: 2019 Harald Sitter <[email protected]>
#
# SPDX-License-Identifier: GPL-2.0-or-later

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(system('servicemenuinstaller', 'install', 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(system('servicemenuinstaller', 'install', 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(system('servicemenuinstaller', 'install', 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)

    installed_file = "#{ENV['XDG_DATA_HOME']}/kservices5/ServiceMenus/foo.desktop"

    assert(system('servicemenuinstaller', 'install', downloaded_file))

    assert_path_exist(downloaded_file)
    assert_path_exist(installed_file)
  end
end