# Copyright 2015 Lars Wirzenius # # 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, either version 3 of the License, or # (at your option) any later version. # # 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 . # # =*= License: GPL-3+ =*= import os import shutil import tempfile import unittest import cliapp import icklib class GitCloneTests(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() self.origin = os.path.join(self.tempdir, 'origin.git') cliapp.runcmd(['git', 'init', self.origin]) with open(os.path.join(self.origin, 'README'), 'w') as f: f.write('This is a README\n') cliapp.runcmd(['git', 'add', '.'], cwd=self.origin) cliapp.runcmd(['git', 'commit', '-mfoo'], cwd=self.origin) def tearDown(self): shutil.rmtree(self.tempdir) def test_sets_dirname(self): clone = icklib.GitClone() self.assertEqual(clone.dirname, None) clone.set_dirname('foo') self.assertEqual(clone.dirname, 'foo') def test_sets_url(self): clone = icklib.GitClone() self.assertEqual(clone.url, None) clone.set_url('foo') self.assertEqual(clone.url, 'foo') def test_sets_subdir(self): clone = icklib.GitClone() self.assertEqual(clone.subdir, None) clone.set_subdir('foo') self.assertEqual(clone.subdir, 'foo') def test_clones_from_origin(self): clonedir = os.path.join(self.tempdir, 'clone') clone = icklib.GitClone() clone.set_dirname(clonedir) clone.set_url(self.origin) clone.clone_from_url() self.assertTrue(os.path.exists(os.path.join(clonedir, '.git'))) def test_checks_out_branch(self): clonedir = os.path.join(self.tempdir, 'clone') clone = icklib.GitClone() clone.set_dirname(clonedir) clone.set_url(self.origin) clone.clone_from_url() clone.checkout('master') output = cliapp.runcmd( ['git', 'status', '--porcelain', '--branch'], cwd=clonedir) self.assertTrue(output.startswith('## master')) def test_updates_from_origin(self): clonedir = os.path.join(self.tempdir, 'clone') clone = icklib.GitClone() clone.set_dirname(clonedir) clone.set_url(self.origin) clone.clone_from_url() with open(os.path.join(self.origin, 'README'), 'w') as f: f.write('This is a NEW README\n') cliapp.runcmd(['git', 'add', '.'], cwd=self.origin) cliapp.runcmd(['git', 'commit', '-mfoo'], cwd=self.origin) clone.update_from_url() clone.checkout('master') with open(os.path.join(clonedir, 'README')) as f: text = f.read() self.assertEqual(text, 'This is a NEW README\n') def test_syncs_from_uri(self): clonedir = os.path.join(self.tempdir, 'clone') clone = icklib.GitClone() clone.set_dirname(clonedir) clone.set_url(self.origin) clone.sync_from_url() with open(os.path.join(self.origin, 'README'), 'w') as f: f.write('This is a NEW README\n') cliapp.runcmd(['git', 'add', '.'], cwd=self.origin) cliapp.runcmd(['git', 'commit', '-mfoo'], cwd=self.origin) clone.sync_from_url() clone.checkout('master') with open(os.path.join(clonedir, 'README')) as f: text = f.read() self.assertEqual(text, 'This is a NEW README\n') def test_returns_some_commit_id_for_HEAD(self): clonedir = os.path.join(self.tempdir, 'clone') clone = icklib.GitClone() clone.set_dirname(clonedir) clone.set_url(self.origin) clone.clone_from_url() self.assertEqual(type(clone.get_HEAD_commit()), str)