|
| 1 | +import io |
| 2 | +import os |
| 3 | + |
| 4 | +from contextlib import redirect_stdout |
| 5 | + |
| 6 | +from antsibull.cli.antsibull_docs import run |
| 7 | + |
| 8 | + |
| 9 | +def write_file(path, content): |
| 10 | + with open(path, 'wb') as f: |
| 11 | + f.write(content) |
| 12 | + |
| 13 | + |
| 14 | +def test_docsite_linting_success(tmp_path_factory): |
| 15 | + dir = tmp_path_factory.mktemp('foobar') |
| 16 | + write_file(dir / 'galaxy.yml', b''' |
| 17 | +namespace: foo |
| 18 | +name: bar |
| 19 | +''') |
| 20 | + docsite_dir = dir / 'docs' / 'docsite' |
| 21 | + docsite_rst_dir = docsite_dir / 'rst' |
| 22 | + os.makedirs(docsite_rst_dir) |
| 23 | + write_file(docsite_dir / 'extra-docs.yml', b''' |
| 24 | +sections: |
| 25 | + - title: Foo |
| 26 | + toctree: |
| 27 | + - foo |
| 28 | +
|
| 29 | +''') |
| 30 | + write_file(docsite_dir / 'links.yml', b''' |
| 31 | +edit_on_github: |
| 32 | + repository: ansible-collections/foo.bar |
| 33 | + branch: main |
| 34 | + path_prefix: '' |
| 35 | +
|
| 36 | +extra_links: |
| 37 | + - description: Submit an issue |
| 38 | + url: https://github.com/ansible-collections/foo.bar/issues/new |
| 39 | +
|
| 40 | +communication: |
| 41 | + matrix_rooms: |
| 42 | + - topic: General usage and support questions |
| 43 | + room: '#users:ansible.im' |
| 44 | + irc_channels: |
| 45 | + - topic: General usage and support questions |
| 46 | + network: Libera |
| 47 | + channel: '#ansible' |
| 48 | + mailing_lists: |
| 49 | + - topic: Ansible Project List |
| 50 | + url: https://groups.google.com/g/ansible-project |
| 51 | +''') |
| 52 | + write_file(docsite_rst_dir / 'foo.rst', b''' |
| 53 | +_ansible_collections.foo.bar.docsite.bla: |
| 54 | +
|
| 55 | +Foo bar |
| 56 | +======= |
| 57 | +
|
| 58 | +Baz bam :ref:`myself <ansible_collections.foo.bar.docsite.bla>`. |
| 59 | +''') |
| 60 | + |
| 61 | + stdout = io.StringIO() |
| 62 | + with redirect_stdout(stdout): |
| 63 | + rc = run(['antsibull-docs', 'lint-collection-docs', str(dir)]) |
| 64 | + stdout = stdout.getvalue().splitlines() |
| 65 | + print('\n'.join(stdout)) |
| 66 | + assert rc == 0 |
| 67 | + assert stdout == [] |
| 68 | + |
| 69 | + |
| 70 | +def test_docsite_linting_failure(tmp_path_factory): |
| 71 | + dir = tmp_path_factory.mktemp('foo.bar') |
| 72 | + write_file(dir / 'galaxy.yml', b''' |
| 73 | +namespace: foo |
| 74 | +name: bar |
| 75 | +''') |
| 76 | + docsite_dir = dir / 'docs' / 'docsite' |
| 77 | + docsite_rst_dir = docsite_dir / 'rst' |
| 78 | + os.makedirs(docsite_rst_dir) |
| 79 | + extra_docs = docsite_dir / 'extra-docs.yml' |
| 80 | + write_file(extra_docs, b''' |
| 81 | +sections: |
| 82 | + - title: Foo |
| 83 | + toctree: |
| 84 | + - foo |
| 85 | + - fooooo |
| 86 | + - foo: bar |
| 87 | + toctree: |
| 88 | + baz: bam |
| 89 | +''') |
| 90 | + links = docsite_dir / 'links.yml' |
| 91 | + write_file(links, b''' |
| 92 | +foo: bar |
| 93 | +
|
| 94 | +edit_on_github: |
| 95 | + repository: ansible-collections/foo.bar |
| 96 | + path_prefix: 1 |
| 97 | +
|
| 98 | +extra_links: |
| 99 | + - description: Submit an issue |
| 100 | + url: https://github.com/ansible-collections/foo.bar/issues/new |
| 101 | + - url: bar |
| 102 | +
|
| 103 | +communication: |
| 104 | + matrix_rooms: |
| 105 | + - topic: General usage and support questions |
| 106 | + room: '#users:ansible.im' |
| 107 | + irc_channel: |
| 108 | + - topic: General usage and support questions |
| 109 | + network: Libera |
| 110 | + channel: '#ansible' |
| 111 | + mailing_lists: |
| 112 | + - topic: Ansible Project List |
| 113 | + url: https://groups.google.com/g/ansible-project |
| 114 | +''') |
| 115 | + write_file(docsite_rst_dir / 'foo.rst', b''' |
| 116 | +_ansible_collections.foo.bar.docsite.bla: |
| 117 | +
|
| 118 | +Foo bar |
| 119 | +======= |
| 120 | +
|
| 121 | +Baz bam :ref:`myself <ansible_collections.foo.bar.docsite.bla>`. |
| 122 | +''') |
| 123 | + |
| 124 | + stdout = io.StringIO() |
| 125 | + with redirect_stdout(stdout): |
| 126 | + rc = run(['antsibull-docs', 'lint-collection-docs', str(dir)]) |
| 127 | + stdout = stdout.getvalue().splitlines() |
| 128 | + print('\n'.join(stdout)) |
| 129 | + assert rc == 3 |
| 130 | + assert stdout == [ |
| 131 | + f'{extra_docs}:0:0: Section #1 has no "title" entry', |
| 132 | + f'{links}:0:0: communication -> irc_channel: extra fields not permitted (type=value_error.extra)', |
| 133 | + f'{links}:0:0: edit_on_github -> branch: field required (type=value_error.missing)', |
| 134 | + f'{links}:0:0: extra_links -> 1 -> description: field required (type=value_error.missing)', |
| 135 | + f'{links}:0:0: foo: extra fields not permitted (type=value_error.extra)', |
| 136 | + ] |
0 commit comments