def test_fragment_loading_fail(tmp_path):
    paths = PathsConfig.force_ansible(str(tmp_path))
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    p = tmp_path / 'test.yaml'
    p.write_text('test: [')
    with pytest.raises(ChangelogError):
        load_fragments(paths, config, [str(p)])
Esempio n. 2
0
def test_is_release_version_ansible_fail(version):
    paths = PathsConfig.force_ansible('.')
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    config.release_tag_re = r'(v(?:[\d.ab\-]|rc)+)'
    config.pre_release_tag_re = r'(?P<pre_release>(?:[ab]|rc)+\d*)$'
    with pytest.raises(ChangelogError):
        print(is_release_version(config, version))
Esempio n. 3
0
def test_config_store_ansible(ansible_config_path):
    ansible_config_path.write_text('')
    paths = PathsConfig.detect()
    collection_details = CollectionDetails(paths)

    config = ChangelogConfig.default(paths, collection_details)
    config.always_refresh = 'none'
    config.store()
    config = ChangelogConfig.load(paths, collection_details)
    assert config.always_refresh == 'none'

    config.always_refresh = False
    config.store()
    config = ChangelogConfig.load(paths, collection_details)
    assert config.always_refresh == 'none'

    config.always_refresh = 'full'
    config.store()
    config = ChangelogConfig.load(paths, collection_details)
    assert config.always_refresh == 'full'

    config.always_refresh = True
    config.store()
    config = ChangelogConfig.load(paths, collection_details)
    assert config.always_refresh == 'full'
Esempio n. 4
0
def test_config_loading_bad_keep_fragments(ansible_config_path):
    ansible_config_path.write_text(
        'changes_format: classic\nkeep_fragments: false')
    paths = PathsConfig.detect()
    collection_details = CollectionDetails(paths)
    with pytest.raises(ChangelogError):
        ChangelogConfig.load(paths, collection_details)
Esempio n. 5
0
def test_config_store_collection(collection_config_path):
    collection_config_path.write_text('')
    paths = PathsConfig.detect()
    collection_details = CollectionDetails(paths)

    config = ChangelogConfig.default(paths, collection_details)
    assert config.flatmap is None

    config.store()
    config = ChangelogConfig.load(paths, collection_details)
    assert config.flatmap is None

    config.always_refresh = True
    config.flatmap = True
    config.store()
    config = ChangelogConfig.load(paths, collection_details)
    assert config.always_refresh is True
    assert config.flatmap is True

    config.always_refresh = False
    config.flatmap = False
    config.store()
    config = ChangelogConfig.load(paths, collection_details)
    assert config.always_refresh is False
    assert config.flatmap is False
Esempio n. 6
0
 def __init__(self, base_path: pathlib.Path):
     super().__init__(base_path,
                      PathsConfig.force_ansible(base_dir=str(base_path)))
     self.mkdir(
         os.path.join(self.paths.base_dir, 'lib', 'ansible', 'modules'))
     self.mkdir(
         os.path.join(self.paths.base_dir, 'lib', 'ansible', 'plugins'))
def test_good_changelog_yaml_files(yaml_filename):
    paths = PathsConfig.force_collection(base_dir='/')
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    data = load_yaml(yaml_filename)
    result = sanitize_changes(data, config)
    if 'ancestor' not in data and result['ancestor'] is None:
        result.pop('ancestor')
    assert result == data
Esempio n. 8
0
 def __init__(self, base_path: pathlib.Path, namespace: str, collection: str):
     collection_path = base_path / 'ansible_collections' / namespace / collection
     collection_path.mkdir(parents=True, exist_ok=True)
     super().__init__(base_path,
                      PathsConfig.force_collection(base_dir=str(collection_path)))
     self.namespace = namespace
     self.collection = collection
     self.collection_name = '{0}.{1}'.format(namespace, collection)
Esempio n. 9
0
 def ansible_base(cls,
                  changelog_data: t.Optional[t.Any] = None
                  ) -> 'ChangelogData':
     paths = PathsConfig.force_ansible('')
     collection_details = CollectionDetails(paths)
     config = ChangelogConfig.default(paths, collection_details)
     return cls(paths,
                config,
                ChangesData(config, '', changelog_data),
                flatmap=False)
Esempio n. 10
0
 def collection(cls, collection_name: str, version: str,
                changelog_data: t.Optional[t.Any] = None) -> 'ChangelogData':
     paths = PathsConfig.force_collection('')
     collection_details = CollectionDetails(paths)
     collection_details.namespace, collection_details.name = collection_name.split('.', 1)
     collection_details.version = version
     collection_details.flatmap = False  # TODO!
     config = ChangelogConfig.default(paths, collection_details)
     return cls(paths,
                config,
                ChangesData(config, '', changelog_data),
                flatmap=True)  # TODO!
Esempio n. 11
0
def test_detect_ansible_no_doc_binary(cwd_tmp_path):
    d = cwd_tmp_path / 'lib'
    d.mkdir()
    d = d / 'ansible'
    d.mkdir()
    d = cwd_tmp_path / 'bin'
    d.mkdir()
    d = cwd_tmp_path / 'changelogs'
    d.mkdir()
    (d / 'config.yaml').write_text('---')
    c = PathsConfig.detect()
    assert c.ansible_doc_path == 'ansible-doc'
def test_bad_changelog_yaml_files(yaml_filename, json_filename):
    paths = PathsConfig.force_collection(base_dir='/')
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    try:
        data = load_yaml(yaml_filename)
        if not STORE_RESULT:
            sanitized_data = load_yaml(yaml_filename + '-sanitized')
    except Exception:
        # We are only interested in parsable YAML
        return
    result = sanitize_changes(data, config)
    if STORE_RESULT:
        store_yaml(yaml_filename + '-sanitized', result)
    else:
        assert result == sanitized_data
Esempio n. 13
0
    def ansible(cls, directory: t.Optional[str],
                output_directory: t.Optional[str] = None) -> 'ChangelogData':
        paths = PathsConfig.force_ansible('')

        config = ChangelogConfig.default(paths, CollectionDetails(paths), 'Ansible')
        # TODO: adjust the following lines once Ansible switches to semantic versioning
        config.use_semantic_versioning = False
        config.release_tag_re = r'''(v(?:[\d.ab\-]|rc)+)'''
        config.pre_release_tag_re = r'''(?P<pre_release>(?:[ab]|rc)+\d*)$'''

        changelog_path = ''
        if directory is not None:
            changelog_path = os.path.join(directory, 'changelog.yaml')
        changes = ChangesData(config, changelog_path)
        if output_directory is not None:
            changes.path = os.path.join(output_directory, 'changelog.yaml')
        return cls(paths, config, changes, flatmap=True)
def test_fragments_filename_ignore(tmp_path):
    '''Ensure we don't load files we mean to ignore'''
    paths = PathsConfig.force_ansible(str(tmp_path))
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    test_filenames = [
        '.test.yaml', 'test.yaml~', 'test.yml~', 'test', 'valid.yml',
        'valid.yaml'
    ]

    for fn in test_filenames:
        p = tmp_path / fn
        p.write_text('minor_changes: ["foo"]')

    loaded = load_fragments(paths, config, [], None, tmp_path)
    assert sorted([x.name for x in loaded]) == ['valid.yaml', 'valid.yml']

    config.ignore_other_fragment_extensions = False
    loaded = load_fragments(paths, config, [], None, tmp_path)
    assert sorted([x.name for x in loaded]) == [
        'test', 'test.yaml~', 'test.yml~', 'valid.yaml', 'valid.yml'
    ]
Esempio n. 15
0
def test_is_release_version_ansible(version, is_release):
    paths = PathsConfig.force_ansible('.')
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    config.release_tag_re = r'(v(?:[\d.ab\-]|rc)+)'
    config.pre_release_tag_re = r'(?P<pre_release>(?:[ab]|rc)+\d*)$'
    assert is_release_version(config, version) == is_release
Esempio n. 16
0
def test_config_loading_bad_changes_format(collection_config_path):
    collection_config_path.write_text('changes_format: other')
    paths = PathsConfig.detect()
    collection_details = CollectionDetails(paths)
    with pytest.raises(ChangelogError):
        ChangelogConfig.load(paths, collection_details)
Esempio n. 17
0
def test_is_release_version_collection(version, is_release):
    paths = PathsConfig.force_collection('.')
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    assert is_release_version(config, version) == is_release
Esempio n. 18
0
def test_detect_complete_fail(root):
    with pytest.raises(ChangelogError):
        PathsConfig.detect()
Esempio n. 19
0
def test_collection_details(tmp_path):
    paths = PathsConfig.force_ansible(str(tmp_path))
    details = CollectionDetails(paths)
    with pytest.raises(Exception) as exc:
        details.get_namespace()
    assert str(
        exc.value
    ) == 'Internal error: cannot get collection details for non-collection'
    with pytest.raises(Exception) as exc:
        details.get_name()
    assert str(
        exc.value
    ) == 'Internal error: cannot get collection details for non-collection'
    with pytest.raises(Exception) as exc:
        details.get_version()
    assert str(
        exc.value
    ) == 'Internal error: cannot get collection details for non-collection'
    with pytest.raises(Exception) as exc:
        details.get_flatmap()
    assert str(
        exc.value
    ) == 'Internal error: cannot get collection details for non-collection'

    paths = PathsConfig.force_collection(str(tmp_path))
    details = CollectionDetails(paths)
    with pytest.raises(ChangelogError) as exc:
        details.get_namespace()
    assert 'Cannot find galaxy.yml' in str(exc.value)
    with pytest.raises(ChangelogError) as exc:
        details.get_name()
    assert 'Cannot find galaxy.yml' in str(exc.value)
    with pytest.raises(ChangelogError) as exc:
        details.get_version()
    assert 'Cannot find galaxy.yml' in str(exc.value)
    with pytest.raises(ChangelogError) as exc:
        details.get_flatmap()
    assert 'Cannot find galaxy.yml' in str(exc.value)

    galaxy_path = tmp_path / 'galaxy.yml'
    galaxy_path.write_text('---\na: b\n')
    paths = PathsConfig.force_collection(str(tmp_path))
    details = CollectionDetails(paths)
    with pytest.raises(ChangelogError) as exc:
        details.get_namespace()
    assert 'Cannot find "namespace" field in galaxy.yaml' in str(exc.value)
    with pytest.raises(ChangelogError) as exc:
        details.get_name()
    assert 'Cannot find "name" field in galaxy.yaml' in str(exc.value)
    with pytest.raises(ChangelogError) as exc:
        details.get_version()
    assert 'Cannot find "version" field in galaxy.yaml' in str(exc.value)
    assert details.get_flatmap() is None

    galaxy_path = tmp_path / 'galaxy.yml'
    galaxy_path.write_text('---\nnamespace: 1\nname: 2\nversion: 3\ntype: 4')
    paths = PathsConfig.force_collection(str(tmp_path))
    details = CollectionDetails(paths)
    with pytest.raises(ChangelogError) as exc:
        details.get_namespace()
    assert 'Cannot find "namespace" field in galaxy.yaml' in str(exc.value)
    with pytest.raises(ChangelogError) as exc:
        details.get_name()
    assert 'Cannot find "name" field in galaxy.yaml' in str(exc.value)
    with pytest.raises(ChangelogError) as exc:
        details.get_version()
    assert 'Cannot find "version" field in galaxy.yaml' in str(exc.value)
    assert details.get_flatmap() is False

    galaxy_path = tmp_path / 'galaxy.yml'
    galaxy_path.write_text(
        '---\nnamespace: a\nname: b\nversion: c\ntype: flatmap')
    paths = PathsConfig.force_collection(str(tmp_path))
    details = CollectionDetails(paths)
    assert details.get_namespace() == 'a'
    assert details.get_name() == 'b'
    assert details.get_version() == 'c'
    assert details.get_flatmap() is True

    galaxy_path = tmp_path / 'galaxy.yml'
    galaxy_path.write_text('---\ntype: other')
    paths = PathsConfig.force_collection(str(tmp_path))
    details = CollectionDetails(paths)
    assert details.get_flatmap() is False

    galaxy_path = tmp_path / 'galaxy.yml'
    galaxy_path.write_text(
        '---\nnamespace: a\nname: b\nversion: c\ntype: flatmap')
    paths = PathsConfig.force_collection(str(tmp_path))
    details = CollectionDetails(paths)
    details.namespace = 'test'
    details.name = 'asdf'
    details.version = '1.0.0'
    details.flatmap = False
    assert details.get_namespace() == 'test'
    assert details.get_name() == 'asdf'
    assert details.get_version() == '1.0.0'
    assert details.get_flatmap() is False
Esempio n. 20
0
def test_is_release_version_collection_fail(version):
    paths = PathsConfig.force_collection('.')
    config = ChangelogConfig.default(paths, CollectionDetails(paths))
    with pytest.raises(ChangelogError):
        is_release_version(config, version)
Esempio n. 21
0
def test_changes_data():
    paths = PathsConfig.force_collection('/')
    details = CollectionDetails(paths)
    config = ChangelogConfig.default(paths, details)

    data = {
        'ancestor': None,
        'releases': {},
    }

    changes = ChangesData(config,
                          os.path.join(config.paths.changelog_dir,
                                       config.changes_file),
                          data_override=data)
    changes.ancestor = '0.1.0'

    assert not changes.has_release
    assert sorted(changes.releases.keys()) == []

    changes.add_release('1.1.0', None, datetime.date(2020, 2, 1))

    assert changes.has_release
    assert sorted(changes.releases.keys()) == ['1.1.0']
    assert changes.latest_version == '1.1.0'

    changes.add_release('1.0.0', None, datetime.date(2020, 1, 1))

    assert changes.has_release
    assert sorted(changes.releases.keys()) == ['1.0.0', '1.1.0']
    assert changes.latest_version == '1.1.0'

    changes.add_release('1.2.0', None, datetime.date(2020, 3, 1))

    assert changes.has_release
    assert sorted(changes.releases.keys()) == ['1.0.0', '1.1.0', '1.2.0']
    assert changes.latest_version == '1.2.0'

    changes.add_release('1.2.1', None, datetime.date(2020, 3, 2))
    changes.add_release('1.3.0-alpha', None, datetime.date(2020, 3, 3))
    changes.add_release('1.3.0-beta', None, datetime.date(2020, 3, 4))
    changes.add_release('1.3.0', None, datetime.date(2020, 3, 5))
    changes.add_release('1.3.1-alpha', None, datetime.date(2020, 3, 6))

    assert sorted(changes.releases.keys()) == [
        '1.0.0',
        '1.1.0',
        '1.2.0',
        '1.2.1',
        '1.3.0',
        '1.3.0-alpha',
        '1.3.0-beta',
        '1.3.1-alpha',
    ]

    changes2 = ChangesData(config,
                           os.path.join(config.paths.changelog_dir,
                                        config.changes_file),
                           data_override=ChangesBase.empty())
    changes2.ancestor = '1.3.1-alpha'
    changes2.add_release('1.3.2', None, datetime.date(2020, 3, 10))
    changes2.add_release('1.3.3', None, datetime.date(2020, 3, 10))
    assert sorted(changes2.releases.keys()) == [
        '1.3.2',
        '1.3.3',
    ]

    changes3 = ChangesData(config,
                           os.path.join(config.paths.changelog_dir,
                                        config.changes_file),
                           data_override=ChangesBase.empty())
    changes3.add_release('0.1.0', None, datetime.date(2019, 7, 30))
    changes3.add_release('0.2.0', None, datetime.date(2019, 12, 31))
    assert sorted(changes3.releases.keys()) == [
        '0.1.0',
        '0.2.0',
    ]

    for order in [
        [changes, changes2],
        [changes2, changes],
    ]:
        changes_concat = ChangesData.concatenate(order)
        assert sorted(changes_concat.releases.keys()) == [
            '1.0.0',
            '1.1.0',
            '1.2.0',
            '1.2.1',
            '1.3.0',
            '1.3.0-alpha',
            '1.3.0-beta',
            '1.3.1-alpha',
            '1.3.2',
            '1.3.3',
        ]
        assert changes_concat.ancestor == '0.1.0'

    for order in [
        [changes, changes2, changes3],
        [changes, changes3, changes2],
        [changes2, changes, changes3],
        [changes2, changes3, changes],
        [changes3, changes, changes2],
        [changes3, changes2, changes],
    ]:
        changes_concat = ChangesData.concatenate(order)
        assert sorted(changes_concat.releases.keys()) == [
            '0.1.0',
            '0.2.0',
            '1.0.0',
            '1.1.0',
            '1.2.0',
            '1.2.1',
            '1.3.0',
            '1.3.0-alpha',
            '1.3.0-beta',
            '1.3.1-alpha',
            '1.3.2',
            '1.3.3',
        ]
        assert changes_concat.ancestor is None

    changes_concat.ancestor = '0.0.1'

    changes_concat.prune_versions(versions_after='0.1.0', versions_until=None)
    assert sorted(changes_concat.releases.keys()) == [
        '0.2.0',
        '1.0.0',
        '1.1.0',
        '1.2.0',
        '1.2.1',
        '1.3.0',
        '1.3.0-alpha',
        '1.3.0-beta',
        '1.3.1-alpha',
        '1.3.2',
        '1.3.3',
    ]
    assert changes_concat.ancestor == '0.1.0'

    changes_concat.prune_versions(versions_after=None, versions_until='1.3.2')
    assert sorted(changes_concat.releases.keys()) == [
        '0.2.0',
        '1.0.0',
        '1.1.0',
        '1.2.0',
        '1.2.1',
        '1.3.0',
        '1.3.0-alpha',
        '1.3.0-beta',
        '1.3.1-alpha',
        '1.3.2',
    ]
    assert changes_concat.ancestor == '0.1.0'

    changes_concat.prune_versions(versions_after='1.1.0',
                                  versions_until='1.3.0')
    assert sorted(changes_concat.releases.keys()) == [
        '1.2.0',
        '1.2.1',
        '1.3.0',
        '1.3.0-alpha',
        '1.3.0-beta',
    ]
    assert changes_concat.ancestor == '1.1.0'