def test_build_docs_blob_contents(get_readme_doc_file, get_html): get_readme_doc_file.return_value.name = 'README.md' get_html.return_value = '<p>A detailed guide</p>' collection_loader = CollectionLoader( '/tmpdir', 'filename', cfg=SimpleNamespace(run_ansible_doc=True)) collection_loader.content_objs = [ schema.Content(name='my_module', content_type=ContentType.MODULE), schema.Content(name='my_role', content_type=ContentType.ROLE), ] res = collection_loader._build_docs_blob() assert attr.asdict(res) == { 'collection_readme': { 'name': 'README.md', 'html': '<p>A detailed guide</p>' }, 'documentation_files': [], 'contents': [ { 'content_name': 'my_module', 'content_type': 'module', 'doc_strings': {}, 'readme_file': None, 'readme_html': None, }, { 'content_name': 'my_role', 'content_type': 'role', 'doc_strings': {}, 'readme_file': None, 'readme_html': None, }, ], }
def test_build_docs_blob_no_readme(get_readme_doc_file): get_readme_doc_file.return_value = None collection_loader = CollectionLoader( '/tmpdir', 'filename', cfg=SimpleNamespace(run_ansible_doc=True)) collection_loader.content_objs = [] with pytest.raises(exc.ImporterError): collection_loader._build_docs_blob()
def test_build_docs_blob_doc_files(get_doc_files, get_readme, get_html): get_readme.return_value.name = 'README.md' get_html.return_value = '<p>A detailed guide</p>' get_doc_files.return_value = [ markup_utils.DocFile(name='INTRO.md', text='Intro text', mimetype='text/markdown', hash=''), markup_utils.DocFile(name='INTRO2.md', text='Intro text', mimetype='text/markdown', hash=''), ] collection_loader = CollectionLoader('/tmpdir', 'filename') collection_loader.content_objs = [] res = collection_loader._build_docs_blob() assert attr.asdict(res) == { 'collection_readme': { 'name': 'README.md', 'html': '<p>A detailed guide</p>' }, 'documentation_files': [ { 'name': 'INTRO.md', 'html': '<p>A detailed guide</p>', }, { 'name': 'INTRO2.md', 'html': '<p>A detailed guide</p>', }, ], 'contents': [], }
def test_build_contents_blob(): collection_loader = CollectionLoader('/tmpdir', 'filename') collection_loader.content_objs = [ schema.Content(name='my_module', content_type=ContentType.MODULE), schema.Content(name='my_role', content_type=ContentType.ROLE), ] res = collection_loader._build_contents_blob() assert [attr.asdict(item) for item in res] == [ {'content_type': 'module', 'description': None, 'name': 'my_module'}, {'content_type': 'role', 'description': None, 'name': 'my_role'} ]
def test_manifest_success(): with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) data = CollectionLoader( temp_dir, 'my_namespace-my_collection-2.0.2.tar.gz').load() assert data['metadata']['namespace'] == 'my_namespace' assert data['metadata']['name'] == 'my_collection' assert data['metadata']['version'] == '2.0.2' assert data['metadata']['authors'] == ['John Doe'] assert data['metadata']['readme'] == 'README.md' assert data['metadata']['tags'] == ['deployment', 'fedora'] assert data['metadata']['description'] == \ 'A collection with various roles and plugins' assert data['metadata']['license_file'] is None assert data['metadata']['dependencies'] == { 'my_namespace.collection_nginx': '>=0.1.6', 'network_user.collection_inspect': '2.0.0', 'dave.deploy': '*' } assert data['metadata']['repository'] is None assert data['metadata']['homepage'] is None assert data['metadata']['issues'] is None assert data['error'] is None assert data['result'] == 'completed'
def test_missing_readme(): with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) with pytest.raises(exc.ManifestValidationError, match=r"Could not find file README.md"): CollectionLoader(temp_dir, filename=None).load()
def test_manifest_success(_build_docs_blob, tmp_collection_root): _build_docs_blob.return_value = {} with open(os.path.join(tmp_collection_root, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) with open(os.path.join(tmp_collection_root, 'README.md'), 'w'): pass filename = CollectionFilename('my_namespace', 'my_collection', '2.0.2') data = CollectionLoader( tmp_collection_root, filename, cfg=SimpleNamespace(run_ansible_doc=True), ).load() assert data.metadata.namespace == 'my_namespace' assert data.metadata.name == 'my_collection' assert data.metadata.version == '2.0.2' assert data.metadata.authors == ['John Doe'] assert data.metadata.readme == 'README.md' assert data.metadata.tags == ['deployment', 'fedora'] assert data.metadata.description == \ 'A collection with various roles and plugins' assert data.metadata.license_file is None assert data.metadata.dependencies == { 'my_namespace.collection_nginx': '>=0.1.6', 'network_user.collection_inspect': '2.0.0', 'dave.deploy': '*' } assert data.metadata.repository == 'http://example.com/repository' assert data.metadata.homepage is None assert data.metadata.issues is None
def test_filename_not_match_metadata(tmp_collection_root): with open(os.path.join(tmp_collection_root, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) filename = CollectionFilename('diff_ns', 'my_collection', '2.0.2') with pytest.raises(exc.ManifestValidationError): CollectionLoader(tmp_collection_root, filename).load()
def test_manifest_success(_build_docs_blob): _build_docs_blob.return_value = {} with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) filename = CollectionFilename('my_namespace', 'my_collection', '2.0.2') data = CollectionLoader(temp_dir, filename).load() assert data.metadata.namespace == 'my_namespace' assert data.metadata.name == 'my_collection' assert data.metadata.version == '2.0.2' assert data.metadata.authors == ['John Doe'] assert data.metadata.readme == 'README.md' assert data.metadata.tags == ['deployment', 'fedora'] assert data.metadata.description == \ 'A collection with various roles and plugins' assert data.metadata.license_file is None assert data.metadata.dependencies == { 'my_namespace.collection_nginx': '>=0.1.6', 'network_user.collection_inspect': '2.0.0', 'dave.deploy': '*' } assert data.metadata.repository is None assert data.metadata.homepage is None assert data.metadata.issues is None
def test_filename_not_match_metadata(): with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) filename = CollectionFilename('diff_ns', 'my_collection', '2.0.2') with pytest.raises(exc.ManifestValidationError): CollectionLoader(temp_dir, filename).load()
def test_missing_readme(tmp_collection_root): with open(os.path.join(tmp_collection_root, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) with pytest.raises( exc.ManifestValidationError, match=r"Could not find file README.md" ): CollectionLoader(tmp_collection_root, filename=None).load()
def test_manifest_fail(manifest_text, new_text, error_subset): manifest_edited = MANIFEST_JSON.replace(manifest_text, new_text) with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, 'MANIFEST.json'), 'w') as fh: fh.write(manifest_edited) with pytest.raises(ManifestValidationError, match=error_subset): CollectionLoader(temp_dir, 'my_namespace-my_collection-2.0.2.tar.gz').load()
def test_manifest_fail(manifest_text, new_text, error_subset, tmp_collection_root): manifest_edited = MANIFEST_JSON.replace(manifest_text, new_text) with open(os.path.join(tmp_collection_root, 'MANIFEST.json'), 'w') as fh: fh.write(manifest_edited) with pytest.raises(exc.ManifestValidationError, match=error_subset): CollectionLoader(tmp_collection_root, 'my_namespace-my_collection-2.0.2.tar.gz').load()
def test_filename_empty_value(_build_docs_blob): _build_docs_blob.return_value = {} with tempfile.TemporaryDirectory() as temp_dir: with open(os.path.join(temp_dir, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) filename = CollectionFilename(namespace='my_namespace', name='my_collection', version=None) data = CollectionLoader(temp_dir, filename).load() assert data.metadata.namespace == 'my_namespace' assert data.metadata.name == 'my_collection' assert data.metadata.version == '2.0.2' filename = None data = CollectionLoader(temp_dir, filename).load() assert data.metadata.namespace == 'my_namespace' assert data.metadata.name == 'my_collection' assert data.metadata.version == '2.0.2'
def test_filename_none(_build_docs_blob, tmp_collection_root): _build_docs_blob.return_value = {} with open(os.path.join(tmp_collection_root, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) with open(os.path.join(tmp_collection_root, 'README.md'), 'w'): pass filename = None data = CollectionLoader(tmp_collection_root, filename).load() assert data.metadata.namespace == 'my_namespace' assert data.metadata.name == 'my_collection' assert data.metadata.version == '2.0.2'
def test_license_file(tmp_collection_root): with open(os.path.join(tmp_collection_root, 'MANIFEST.json'), 'w') as fh: manifest = json.loads(MANIFEST_JSON) manifest['collection_info']['license'] = [] manifest['collection_info']['license_file'] = 'my_license.txt' fh.write(json.dumps(manifest)) with open(os.path.join(tmp_collection_root, 'README.md'), 'w'): pass with open(os.path.join(tmp_collection_root, 'my_license.txt'), 'w'): pass data = CollectionLoader(tmp_collection_root, filename=None).load() assert data.metadata.license_file == 'my_license.txt'
def test_filename_empty_value(_build_docs_blob, tmp_collection_root): _build_docs_blob.return_value = {} with open(os.path.join(tmp_collection_root, 'MANIFEST.json'), 'w') as fh: fh.write(MANIFEST_JSON) with open(os.path.join(tmp_collection_root, 'README.md'), 'w'): pass filename = CollectionFilename(namespace='my_namespace', name='my_collection', version=None) data = CollectionLoader( tmp_collection_root, filename, cfg=SimpleNamespace(run_ansible_doc=True), ).load() assert data.metadata.namespace == 'my_namespace' assert data.metadata.name == 'my_collection' assert data.metadata.version == '2.0.2'
def test_build_docs_blob_no_readme(get_readme_doc_file): get_readme_doc_file.return_value = None collection_loader = CollectionLoader('/tmpdir', 'filename') collection_loader.content_objs = [] with pytest.raises(exc.ImporterError): collection_loader._build_docs_blob()