Пример #1
0
class CreateGitBranchTest(GitRepoTestCase):
    def setUp(self):
        self.repo_dir = os.path.join(os.getcwd(), "test/tmp")
        self.jenkins_job = JenkinsJobMock("test-job")

    def tearDown(self):
        if os.path.exists(self.repo_dir) and os.path.isdir(self.repo_dir):
            shutil.rmtree(self.repo_dir)

    def test_plugin_creates_directory_if_not_exists(self):
        # given
        branch_name = "test-branch1"
        action = CreateGitBranch(branch=branch_name, repo_dir=self.repo_dir, from_branch="master", use_existing=False)

        # make sure that repo directory does not exist
        if os.path.exists(self.repo_dir) and os.path.isdir(self.repo_dir):
            shutil.rmtree(self.repo_dir)

        try:
            # when
            action.execute(jenkins_job=self.jenkins_job)

            # then
            assert os.path.exists(self.repo_path())
            assert os.path.isdir(self.repo_path())

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_branch_from_origin(branch_name, repo)

    def test_plugin_uses_existing_repo(self):
        # given
        branch_name = "test-branch1"
        action = CreateGitBranch(branch=branch_name, repo_dir=self.repo_path(), from_branch="master", use_existing=True)

        try:
            # when (repository should not exist when tests start
            # - we are testing in a clean environment)
            repo = git.Repo.clone_from(self.jenkins_job.get_scm_url(), self.repo_path())

            action.execute(self.jenkins_job)

            # then
            assert branch_name in repo.heads

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_branch_from_origin(branch_name, repo)

    def test_plugin_should_checkout_master_branch(self):
        # given
        branch_name = "test-branch1"
        action = CreateGitBranch(branch=branch_name, repo_dir=self.repo_dir, from_branch="master", use_existing=False)

        try:
            # when
            action.execute(jenkins_job=self.jenkins_job)
            repo = git.Repo(self.repo_path())
            head_name = repo.head.reference.name

            # then
            assert head_name == "master"

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_branch_from_origin(branch_name, repo)

    def test_plugin_should_create_branch_for_given_name(self):
        # given
        branch_name = "test-branch1"
        action = CreateGitBranch(branch=branch_name, repo_dir=self.repo_dir, from_branch="master", use_existing=False)

        try:
            # when
            action.execute(jenkins_job=self.jenkins_job)
            repo = git.Repo(self.repo_path())

            # then
            assert branch_name in repo.heads

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_branch_from_origin(branch_name, repo)

    def test_plugin_creates_remote_for_given_branch_name(self):
        # given
        branch_name = "test-branch1"
        action = CreateGitBranch(branch=branch_name, repo_dir=self.repo_dir, from_branch="master", use_existing=False)

        try:
            # when
            action.execute(jenkins_job=self.jenkins_job)
            repo = git.Repo(self.repo_path())
            origin = repo.remotes["origin"]

            # then
            ref = "origin/%s" % branch_name
            assert ref in [ref.name for ref in origin.refs]

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_branch_from_origin(branch_name, repo)
Пример #2
0
class CreateGitTagTest(GitRepoTestCase):
    def setUp(self):
        self.repo_dir = os.path.join(os.getcwd(), 'test/tmp')
        self.jenkins_job = JenkinsJobMock('test-job')

    def tearDown(self):
        repo = git.Repo(self.repo_path())
        for tag in repo.tags:
            self.remove_tag_from_origin(tag, repo)

        if os.path.exists(self.repo_dir) and os.path.isdir(self.repo_dir):
            shutil.rmtree(self.repo_dir)

    def test_plugin_creates_directory_if_not_exists(self):
        # given
        tag_name = 'hello-tag'
        action = CreateGitTag(
            tag=tag_name, repo_dir=self.repo_dir,
            from_branch='master', use_existing=False
        )

        # make sure that repo directory does not exist
        if os.path.exists(self.repo_dir) and os.path.isdir(self.repo_dir):
            shutil.rmtree(self.repo_dir)

        # when
        try:
            action.execute(jenkins_job=self.jenkins_job)

            # then
            assert os.path.exists(self.repo_path())
            assert os.path.isdir(self.repo_path())

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_tag_from_origin(tag_name, repo)

    def test_plugin_uses_existing_repo(self):
        # given
        tag_name = 'hello-tag'
        action = CreateGitTag(
            tag=tag_name, repo_dir=self.repo_path(),
            from_branch='master', use_existing=True
        )

        # (repository should not exist when tests start - we are testing
        # in a clean environment)
        repo = git.Repo.clone_from(
            self.jenkins_job.get_scm_url(),
            self.repo_path()
        )

        # when
        action.execute(self.jenkins_job)

        # then
        assert tag_name in repo.tags

        # cleanup
        self.remove_tag_from_origin(tag_name, repo)

    def test_plugin_should_checkout_master_branch(self):
        # given
        tag_name = 'hello-tag'
        action = CreateGitTag(
            tag=tag_name, repo_dir=self.repo_dir,
            from_branch='master', use_existing=False
        )

        try:
            # when
            action.execute(jenkins_job=self.jenkins_job)
            repo = git.Repo(self.repo_path())
            head_name = repo.head.reference.name

            # then
            assert head_name == 'master'

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_tag_from_origin(tag_name, repo)

    def test_plugin_should_create_tag_for_given_name(self):
        # given
        tag_name = 'hello-tag'
        action = CreateGitTag(
            tag=tag_name, repo_dir=self.repo_dir,
            from_branch='master', use_existing=False
        )

        try:
            # when
            action.execute(jenkins_job=self.jenkins_job)
            repo = git.Repo(self.repo_path())

            # then
            assert tag_name in repo.tags

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_tag_from_origin(tag_name, repo)

    def test_plugin_creates_remote_for_given_tag_name(self):
        # given
        tag_name = 'hello-tag'
        action = CreateGitTag(
            tag=tag_name, repo_dir=self.repo_dir,
            from_branch='master', use_existing=False
        )

        try:
            # when
            action.execute(jenkins_job=self.jenkins_job)
            repo = git.Repo(self.repo_path())
            origin = repo.remotes['origin']

            # then
            assert tag_name in origin.repo.refs
            assert origin.repo.refs[tag_name].__class__ is git.TagReference

        finally:
            # cleanup
            repo = git.Repo(self.repo_path())
            self.remove_tag_from_origin(tag_name, repo)