summaryrefslogtreecommitdiff
path: root/morphlib/workspace_tests.py
blob: 9eef1053144164bfa27b32f6d2af1136bae46813 (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
107
108
109
110
111
# Copyright (C) 2013-2014  Codethink Limited
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# =*= License: GPL-2 =*=


import os
import shutil
import tempfile
import unittest

import morphlib


class WorkspaceTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.workspace_dir = os.path.join(self.tempdir, 'workspace')

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def assertIsWorkspace(self, dirname):
        self.assertTrue(os.path.isdir(dirname))
        self.assertTrue(os.path.isdir(os.path.join(dirname, '.morph')))

    def create_it(self):
        morphlib.workspace.create(self.workspace_dir)

    def test_creates_workspace(self):
        ws = morphlib.workspace.create(self.workspace_dir)
        self.assertIsWorkspace(self.workspace_dir)
        self.assertEqual(ws.root, self.workspace_dir)

    def test_create_initialises_existing_but_empty_directory(self):
        os.mkdir(self.workspace_dir)
        ws = morphlib.workspace.create(self.workspace_dir)
        self.assertIsWorkspace(self.workspace_dir)
        self.assertEqual(ws.root, self.workspace_dir)

    def test_fails_to_create_workspace_when_dir_exists_and_is_not_empty(self):
        os.mkdir(self.workspace_dir)
        os.mkdir(os.path.join(self.workspace_dir, 'somedir'))
        self.assertRaises(
            morphlib.workspace.WorkspaceDirExists,
            morphlib.workspace.create, self.workspace_dir)

    def test_fails_to_recreate_workspace(self):
        # Create it once.
        morphlib.workspace.create(self.workspace_dir)
        # Creating it again must fail.
        self.assertRaises(
            morphlib.workspace.WorkspaceDirExists,
            morphlib.workspace.create, self.workspace_dir)

    def test_opens_workspace_when_given_its_root(self):
        self.create_it()
        ws = morphlib.workspace.open(self.workspace_dir)
        self.assertEqual(ws.root, self.workspace_dir)

    def test_opens_workspace_when_given_subdirectory(self):
        self.create_it()
        subdir = os.path.join(self.workspace_dir, 'subdir')
        os.mkdir(subdir)
        ws = morphlib.workspace.open(subdir)
        self.assertEqual(ws.root, self.workspace_dir)

    def test_fails_to_open_workspace_when_no_workspace_anywhere(self):
        self.assertRaises(
            morphlib.workspace.NotInWorkspace,
            morphlib.workspace.open, self.tempdir)

    def test_invents_appropriate_name_for_system_branch_directory(self):
        self.create_it()
        ws = morphlib.workspace.open(self.workspace_dir)
        branch = 'foo/bar'
        self.assertEqual(
            ws.get_default_system_branch_directory_name(branch),
            os.path.join(self.workspace_dir, branch))

    def test_creates_system_branch_directory(self):
        self.create_it()
        ws = morphlib.workspace.open(self.workspace_dir)
        url = 'test:morphs'
        branch = 'my/new/thing'
        sb = ws.create_system_branch_directory(url, branch)
        self.assertEqual(type(sb), morphlib.sysbranchdir.SystemBranchDirectory)

    def test_lists_created_system_branches(self):
        self.create_it()
        ws = morphlib.workspace.open(self.workspace_dir)

        branches = ["branch/1", "branch/2"]
        for branch in branches:
            ws.create_system_branch_directory('test:morphs', branch)
        self.assertEqual(sorted(sb.system_branch_name
                                for sb in ws.list_system_branches()),
                         branches)