#
# Copyright (C) 2013 Ash (Tuxdude) <tuxdude.github@gmail.com>
#
# This file is part of repobuddy.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys as _sys
if _sys.version_info < (2, 7):
import unittest2 as _unittest # pylint: disable=F0401
else:
import unittest as _unittest # pylint: disable=F0401
from repobuddy.tests.common import TestCaseBase
from repobuddy.manifest_parser import ClientSpec, Manifest, Repo, \
ManifestParser, ManifestParserError
from repobuddy.utils import ResourceHelper
[docs]class ManifestParserTestCase(TestCaseBase):
def _parse_manifest(self, manifest_file):
manifest_stream = ResourceHelper.open_data_file(
'repobuddy.tests.manifests',
manifest_file)
manifest_parser = ManifestParser()
manifest_parser.parse(manifest_stream)
return manifest_parser.get_manifest()
[docs] def __init__(self, methodName='runTest'):
super(ManifestParserTestCase, self).__init__(methodName)
return
[docs] def test_repo_str_repr(self):
repo = Repo('https://github.com/git/git.git',
'master',
'repos/git')
expected_repo_str = '<Repo url:https://github.com/git/git.git ' + \
'branch:master dest:repos/git>'
self.assertEqual(str(repo), expected_repo_str)
self.assertEqual(repr(repo), expected_repo_str)
return
[docs] def test_client_spec_str_repr(self):
client_spec = ClientSpec(
'Spec1',
[
Repo('https://github.com/git/git.git',
'master',
'repos/git'),
Repo('https://github.com/github/linguist.git',
'master',
'repos/linguist')])
expected_client_spec_str = \
'<ClientSpec name:Spec1 repo_list:' + \
'[<Repo url:https://github.com/git/git.git branch:master ' + \
'dest:repos/git>, ' + \
'<Repo url:https://github.com/github/linguist.git ' + \
'branch:master dest:repos/linguist>]>'
self.assertEqual(str(client_spec), expected_client_spec_str)
self.assertEqual(repr(client_spec), expected_client_spec_str)
return
[docs] def test_manifest_str_repr(self):
manifest = Manifest(
'Spec1',
[
ClientSpec(
'Spec1',
[
Repo('https://github.com/git/git.git',
'master',
'repos/git'),
Repo('https://github.com/github/linguist.git',
'master',
'repos/linguist')])])
expected_manifest_str = \
'<Manifest default_client_spec:Spec1 client_spec_list:' + \
'[<ClientSpec name:Spec1 repo_list:[<Repo ' + \
'url:https://github.com/git/git.git branch:master ' + \
'dest:repos/git>, ' + \
'<Repo url:https://github.com/github/linguist.git ' + \
'branch:master dest:repos/linguist>]>]>'
self.assertEqual(str(manifest), expected_manifest_str)
self.assertEqual(repr(manifest), expected_manifest_str)
return
[docs] def test_parse_invalid_file_handle(self):
manifest_parser = ManifestParser()
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: file_handle cannot be None$'):
manifest_parser.parse(None)
manifest_stream = ResourceHelper.open_data_file(
'repobuddy.tests.manifests',
'valid.xml')
manifest_stream.close()
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: I/O Operation on closed file_handle$'):
manifest_parser.parse(manifest_stream)
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: file_handle cannot be a string$'):
manifest_parser.parse('dummy')
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: file_handle is not a stream object$'):
manifest_parser.parse([])
return
[docs] def test_valid_manifest(self):
manifest = self._parse_manifest('valid.xml')
expected_manifest = Manifest(
'Spec1',
[
ClientSpec(
'Spec1',
[
Repo(
'https://gist.github.com/08e8481e9d43646eb942.git',
'master',
'repos/gist-test-repo1'),
Repo(
'https://gist.github.com/157762e334f517a1062e.git',
'master',
'repos/gist-test-repo3')]),
ClientSpec(
'Spec2',
[
Repo(
'https://gist.github.com/08e8481e9d43646eb942.git',
'master',
'gist-test-repo2'),
Repo(
'git://github.com/github/linguist.git',
'master',
'linguist',)]),
ClientSpec(
'Spec3',
[
Repo(
'https://gist.github.com/08e8481e9d43646eb942.git',
'master',
'repos/gist-test-repo1'),
Repo(
'https://gist.github.com/08e8481e9d43646eb942.git',
'master',
'repos/gist-test-repo2'),
Repo(
'https://gist.github.com/157762e334f517a1062e.git',
'master',
'repos/gist-test-repo3')])])
self.assertEqual(manifest, expected_manifest)
return
[docs] def test_no_client_spec(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: There should be at least one valid Client Spec$'):
self._parse_manifest('no-clientspec.xml')
return
[docs] def test_empty_client_spec(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec2\' should have at least ' +
r'one repo$'):
self._parse_manifest('empty-clientspec.xml')
return
[docs] def test_client_spec_no_name(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: No name specified for ClientSpec$'):
self._parse_manifest('clientspec-no-name.xml')
return
[docs] def test_empty_repo(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec1\' has no info about the Repo$'):
self._parse_manifest('empty-repo.xml')
return
[docs] def test_repo_no_url(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec1\' has a Repo with no ' +
r'\'Url\' info$'):
self._parse_manifest('repo-no-url.xml')
return
[docs] def test_repo_empty_url(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec1\' has an empty Repo \'Url\'$'):
self._parse_manifest('repo-empty-url.xml')
return
[docs] def test_repo_no_branch(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec1\' has a Repo with no ' +
r'\'Branch\' info$'):
self._parse_manifest('repo-no-branch.xml')
return
[docs] def test_repo_empty_branch(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec1\' has an empty Repo '
r'\'Branch\'$'):
self._parse_manifest('repo-empty-branch.xml')
return
[docs] def test_repo_no_dest(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec1\' has a Repo with no '
r'\'Destination\' info$'):
self._parse_manifest('repo-no-dest.xml')
return
[docs] def test_repo_empty_dest(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Client Spec \'Spec1\' has an empty Repo '
r'\'Destination\'$'):
self._parse_manifest('repo-empty-dest.xml')
return
[docs] def test_no_default_client_spec(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: No default_client_spec found$'):
self._parse_manifest('no-default-clientspec.xml')
return
[docs] def test_empty_default_client_spec(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: default_client_spec cannot be empty$'):
self._parse_manifest('empty-default-clientspec.xml')
return
[docs] def test_nonexistent_default_client_spec(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Unable to find the Client Spec \'Spec1\' in '
r'the list of Repos$'):
self._parse_manifest('nonexistent-default-clientspec.xml')
return
[docs] def test_duplicate_client_spec(self):
with self.assertRaisesRegexp(
ManifestParserError,
r'^Error: Duplicate Client Spec \'Spec1\' found$'):
self._parse_manifest('duplicate-clientspec.xml')
return
[docs]class ManifestParserTestSuite: # pylint: disable=W0232
@classmethod
[docs] def get_test_suite(cls):
tests = [
'test_repo_str_repr',
'test_client_spec_str_repr',
'test_manifest_str_repr',
'test_parse_invalid_file_handle',
'test_valid_manifest',
'test_malformed',
'test_no_client_spec',
'test_empty_client_spec',
'test_client_spec_no_name',
'test_empty_repo',
'test_repo_no_url',
'test_repo_empty_url',
'test_repo_no_branch',
'test_repo_empty_branch',
'test_repo_no_dest',
'test_repo_empty_dest',
'test_empty_default_client_spec',
'test_no_default_client_spec',
'test_nonexistent_default_client_spec',
'test_duplicate_client_spec']
return _unittest.TestSuite(map(ManifestParserTestCase, tests))