blob: 454f3ccf33140727285eb90e0d81d39fb4e31d19 [file] [log] [blame]
Antoine Mussobfd8f2a2014-09-23 15:31:40 +02001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4# Copyright 2014 Wikimedia Foundation Inc.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18import logging
19import os
20
21import git
22
23from zuul.merger.merger import Repo
24from tests.base import ZuulTestCase
25
26logging.basicConfig(level=logging.DEBUG,
27 format='%(asctime)s %(name)-32s '
28 '%(levelname)-8s %(message)s')
29
30
31class TestMergerRepo(ZuulTestCase):
32
33 log = logging.getLogger("zuul.test.merger.repo")
34 workspace_root = None
35
36 def setUp(self):
37 super(TestMergerRepo, self).setUp()
38 self.workspace_root = os.path.join(self.test_root, 'workspace')
39
40 def test_ensure_cloned(self):
41 parent_path = os.path.join(self.upstream_root, 'org/project1')
42
43 # Forge a repo having a submodule
44 parent_repo = git.Repo(parent_path)
45 parent_repo.git.submodule('add', os.path.join(
46 self.upstream_root, 'org/project2'), 'subdir')
47 parent_repo.index.commit('Adding project2 as a submodule in subdir')
48 # git 1.7.8 changed .git from being a directory to a file pointing
49 # to the parent repository /.git/modules/*
50 self.assertTrue(os.path.exists(
51 os.path.join(parent_path, 'subdir', '.git')),
52 msg='.git file in submodule should be a file')
53
54 work_repo = Repo(parent_path, self.workspace_root,
55 'none@example.org', 'User Name')
56 self.assertTrue(
57 os.path.isdir(os.path.join(self.workspace_root, 'subdir')),
58 msg='Cloned repository has a submodule placeholder directory')
59 self.assertFalse(os.path.exists(
60 os.path.join(self.workspace_root, 'subdir', '.git')),
61 msg='Submodule is not initialized')
62
63 sub_repo = Repo(
64 os.path.join(self.upstream_root, 'org/project2'),
65 os.path.join(self.workspace_root, 'subdir'),
66 'none@example.org', 'User Name')
67 self.assertTrue(os.path.exists(
68 os.path.join(self.workspace_root, 'subdir', '.git')),
69 msg='Cloned over the submodule placeholder')
70
71 self.assertEquals(
72 os.path.join(self.upstream_root, 'org/project1'),
73 work_repo.createRepoObject().remotes[0].url,
74 message="Parent clone still point to upstream project1")
75
76 self.assertEquals(
77 os.path.join(self.upstream_root, 'org/project2'),
78 sub_repo.createRepoObject().remotes[0].url,
79 message="Sub repository points to upstream project2")