~seb128/click/initctl-not-there

« back to all changes in this revision

Viewing changes to tests/integration/helpers.py

  • Committer: CI bot
  • Author(s): Colin Watson
  • Date: 2014-06-23 11:31:40 UTC
  • mfrom: (425.1.47 devel)
  • Revision ID: ps-jenkins@lists.canonical.com-20140623113140-e129f85zscm2uqrs
Click 0.4.27: Integration test improvements; handle renaming of qtsensors5-dev to libqt5sensors5-dev in Qt 5.3. Fixes: 1328486

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2014 Canonical Ltd.
 
2
# Author: Michael Vogt <michael.vogt@ubuntu.com>
 
3
 
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; version 3 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
"""Integration tests helper for the click CLI interface."""
 
17
 
 
18
import contextlib
 
19
import glob
 
20
import os
 
21
import random
 
22
import shutil
 
23
import string
 
24
import subprocess
 
25
import tempfile
 
26
import unittest
 
27
 
 
28
 
 
29
@contextlib.contextmanager
 
30
def chdir(target):
 
31
    curdir = os.getcwd()
 
32
    os.chdir(target)
 
33
    try:
 
34
        yield
 
35
    finally:
 
36
        os.chdir(curdir)
 
37
 
 
38
 
 
39
class TestCase(unittest.TestCase):
 
40
 
 
41
    @classmethod
 
42
    def setUpClass(cls):
 
43
        cls.click_binary = os.environ.get("CLICK_BINARY", "/usr/bin/click")
 
44
 
 
45
    def _create_manifest(self, target, name, version, framework):
 
46
        with open(target, "w") as f:
 
47
            f.write("""{
 
48
            "name": "%s",
 
49
            "version": "%s",
 
50
            "maintainer": "Foo Bar <foo@example.org>",
 
51
            "title": "test title",
 
52
            "framework": "%s"
 
53
            }""" % (name, version, framework))
 
54
 
 
55
    def _make_click(self, name=None, version=1.0, framework="ubuntu-sdk-13.10"):
 
56
        if name is None:
 
57
            name = "com.ubuntu.%s" % "".join(
 
58
                random.choice(string.ascii_lowercase) for i in range(10))
 
59
        tmpdir = tempfile.mkdtemp()
 
60
        self.addCleanup(lambda: shutil.rmtree(tmpdir))
 
61
        clickdir = os.path.join(tmpdir, name)
 
62
        os.makedirs(clickdir)
 
63
        self._create_manifest(os.path.join(clickdir, "manifest.json"),
 
64
                              name, version, framework)
 
65
        with open(os.path.join(clickdir, "README"), "w") as f:
 
66
            f.write("hello world!")
 
67
        with chdir(tmpdir), open(os.devnull, "w") as devnull:
 
68
            subprocess.call(
 
69
                [self.click_binary, "build", clickdir], stdout=devnull)
 
70
        generated_clicks = glob.glob(os.path.join(tmpdir, "*.click"))
 
71
        self.assertEqual(len(generated_clicks), 1)
 
72
        return generated_clicks[0]