File: test_hook.py.in

package info (click to toggle)
click 0.5.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,372 kB
  • sloc: python: 7,135; ansic: 857; makefile: 441; sh: 236; perl: 26; xml: 11
file content (80 lines) | stat: -rw-r--r-- 2,947 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2014 Canonical Ltd.
# Author: Michael Vogt <michael.vogt@ubuntu.com>

# 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; version 3 of the License.
#
# 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, see <http://www.gnu.org/licenses/>.

"""Integration tests for the click hook feature."""

import os
import subprocess
from textwrap import dedent

from .helpers import (
    ClickTestCase,
    require_root,
)


class TestHook(ClickTestCase):

    @classmethod
    def setUpClass(cls):
        super(TestHook, cls).setUpClass()
        require_root()

    def _make_hook(self, name):
        hook_fname = "/usr/share/click/hooks/%s.hook" % name
        canary_fname = os.path.join(self.temp_dir, "canary.sh")
        canary_log = os.path.join(self.temp_dir, "canary.log")
        with open(hook_fname, "w") as f:
            f.write(dedent("""\
            Pattern: ${home}/${id}.test-hook
            User-Level: yes
            Exec: %s
            Hook-Name: %s
            """ % (canary_fname, name)))
        with open(canary_fname, "w") as f:
            f.write(dedent("""\
            #!/bin/sh
            echo "i-hook-you-up" >> %s
            """ % canary_log))
        os.chmod(canary_fname, 0o755)
        return hook_fname, canary_log

    def test_hook_install_user(self):
        # build/install the hook
        hook_name = "clicktesthook"
        hook_file, hook_log = self._make_hook(hook_name)
        self.addCleanup(os.unlink, hook_file)
        subprocess.check_call(
            [self.click_binary, "hook", "install", hook_name])
        self.addCleanup(
            subprocess.check_call, [self.click_binary, "hook", "remove",
                                    hook_name])
        # make click that uses the hook
        hooks = {'app1': {hook_name: 'README'}}
        click_pkg_name = "com.example.hook-1"
        click_pkg = self._make_click(
            click_pkg_name, framework="", hooks=hooks)
        user = os.environ.get("USER", "root")
        self.click_install(click_pkg, click_pkg_name, user)
        # ensure we have the hook
        generated_hook_file = os.path.expanduser(
            "~/com.example.hook-1_app1_1.0.test-hook")
        self.assertTrue(os.path.exists(generated_hook_file))
        self.assertEqual(
            os.path.realpath(generated_hook_file),
            "@DEFAULT_ROOT@/com.example.hook-1/1.0/README")
        with open(hook_log) as f:
            hook_log_content = f.read().strip()
        self.assertEqual("i-hook-you-up", hook_log_content)