def test_quickstart_defaults(tempdir): answers = { 'Root path': tempdir, 'Project name': 'Sphinx Test', 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) conffile = tempdir / 'conf.py' assert conffile.isfile() ns = {} exec(conffile.read_text(), ns) assert ns['extensions'] == [] assert ns['templates_path'] == ['_templates'] assert ns['project'] == 'Sphinx Test' assert ns['copyright'] == '%s, Georg Brandl' % time.strftime('%Y') assert ns['version'] == '0.1' assert ns['release'] == '0.1' assert ns['html_static_path'] == ['_static'] assert (tempdir / '_static').isdir() assert (tempdir / '_templates').isdir() assert (tempdir / 'index.rst').isfile() assert (tempdir / 'Makefile').isfile() assert (tempdir / 'make.bat').isfile()
def test_quickstart_defaults(tempdir): answers = { 'Root path': tempdir, 'Project name': 'Sphinx Test', 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) conffile = tempdir / 'conf.py' assert conffile.isfile() ns = {} execfile_(conffile, ns) assert ns['extensions'] == [] assert ns['templates_path'] == ['_templates'] assert ns['source_suffix'] == '.rst' assert ns['master_doc'] == 'index' assert ns['project'] == 'Sphinx Test' assert ns['copyright'] == '%s, Georg Brandl' % time.strftime('%Y') assert ns['version'] == '0.1' assert ns['release'] == '0.1' assert ns['html_static_path'] == ['_static'] assert ns['latex_documents'] == [ ('index', 'SphinxTest.tex', 'Sphinx Test Documentation', 'Georg Brandl', 'manual')] assert (tempdir / '_static').isdir() assert (tempdir / '_templates').isdir() assert (tempdir / 'index.rst').isfile() assert (tempdir / 'Makefile').isfile() assert (tempdir / 'make.bat').isfile()
def test_quickstart_all_answers(tempdir): answers = { 'Root path': tempdir, 'Separate source and build': 'y', 'Name prefix for templates': '.', 'Project name': u'STASI™'.encode('utf-8'), 'Author name': u'Wolfgang Schäuble & G\'Beckstein'.encode('utf-8'), 'Project version': '2.0', 'Project release': '2.0.1', 'Project language': 'de', 'Source file suffix': '.txt', 'Name of your master document': 'contents', 'autodoc': 'y', 'doctest': 'yes', 'intersphinx': 'no', 'todo': 'y', 'coverage': 'no', 'imgmath': 'N', 'mathjax': 'no', 'ifconfig': 'no', 'viewcode': 'no', 'githubpages': 'no', 'Create Makefile': 'no', 'Create Windows command file': 'no', 'Do you want to use the epub builder': 'yes', } qs.term_input = mock_input(answers, needanswer=True) qs.TERM_ENCODING = 'utf-8' d = {} qs.ask_user(d) qs.generate(d) conffile = tempdir / 'source' / 'conf.py' assert conffile.isfile() ns = {} execfile_(conffile, ns) assert ns['extensions'] == [ 'sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.todo' ] assert ns['templates_path'] == ['.templates'] assert ns['source_suffix'] == '.txt' assert ns['master_doc'] == 'contents' assert ns['project'] == u'STASI™' assert ns['copyright'] == u'%s, Wolfgang Schäuble & G\'Beckstein' % \ time.strftime('%Y') assert ns['version'] == '2.0' assert ns['release'] == '2.0.1' assert ns['todo_include_todos'] is True assert ns['html_static_path'] == ['.static'] assert ns['latex_documents'] == [ ('contents', 'STASI.tex', u'STASI™ Documentation', u'Wolfgang Schäuble \\& G\'Beckstein', 'manual') ] assert (tempdir / 'build').isdir() assert (tempdir / 'source' / '.static').isdir() assert (tempdir / 'source' / '.templates').isdir() assert (tempdir / 'source' / 'contents.txt').isfile()
def ask_user(d): """Wrap sphinx.cmd.quickstart.ask_user, and add additional questions.""" # Print welcome message msg = bold('Welcome to the Hieroglyph %s quickstart utility.') % ( version(), ) print(msg) msg = """ This will ask questions for creating a Hieroglyph project, and then ask some basic Sphinx questions. """ print(msg) # set a few defaults that we don't usually care about for Hieroglyph d.update({ 'version': datetime.date.today().strftime('%Y.%m.%d'), 'release': datetime.date.today().strftime('%Y.%m.%d'), 'make_mode': True, }) if 'project' not in d: print(''' The presentation title will be included on the title slide.''') d['project'] = sphinx_quickstart.do_prompt('Presentation title') if 'author' not in d: d['author'] = sphinx_quickstart.do_prompt('Author name(s)') # slide_theme theme_entrypoints = pkg_resources.iter_entry_points('hieroglyph.theme') themes = [t.load() for t in theme_entrypoints] msg = """ Available themes: """ for theme in themes: msg += '\n'.join([ bold(theme['name']), theme['desc'], '', '', ]) msg += """Which theme would you like to use?""" print(msg) d['slide_theme'] = sphinx_quickstart.do_prompt( 'Slide Theme', themes[0]['name'], sphinx_quickstart.choice(*[t['name'] for t in themes]), ) # Ask original questions print("") sphinx_quickstart.ask_user(d)
def test_quickstart_all_answers(tempdir): answers = { 'Root path': tempdir, 'Separate source and build': 'y', 'Name prefix for templates': '.', 'Project name': u'STASI™'.encode('utf-8'), 'Author name': u'Wolfgang Schäuble & G\'Beckstein'.encode('utf-8'), 'Project version': '2.0', 'Project release': '2.0.1', 'Project language': 'de', 'Source file suffix': '.txt', 'Name of your master document': 'contents', 'autodoc': 'y', 'doctest': 'yes', 'intersphinx': 'no', 'todo': 'y', 'coverage': 'no', 'imgmath': 'N', 'mathjax': 'no', 'ifconfig': 'no', 'viewcode': 'no', 'githubpages': 'no', 'Create Makefile': 'no', 'Create Windows command file': 'no', 'Do you want to use the epub builder': 'yes', } qs.term_input = mock_input(answers, needanswer=True) qs.TERM_ENCODING = 'utf-8' d = {} qs.ask_user(d) qs.generate(d) conffile = tempdir / 'source' / 'conf.py' assert conffile.isfile() ns = {} execfile_(conffile, ns) assert ns['extensions'] == [ 'sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.todo' ] assert ns['templates_path'] == ['.templates'] assert ns['source_suffix'] == '.txt' assert ns['master_doc'] == 'contents' assert ns['project'] == u'STASI™' assert ns['copyright'] == u'%s, Wolfgang Schäuble & G\'Beckstein' % \ time.strftime('%Y') assert ns['version'] == '2.0' assert ns['release'] == '2.0.1' assert ns['todo_include_todos'] is True assert ns['html_static_path'] == ['.static'] assert ns['latex_documents'] == [ ('contents', 'STASI.tex', u'STASI™ Documentation', u'Wolfgang Schäuble \\& G\'Beckstein', 'manual')] assert (tempdir / 'build').isdir() assert (tempdir / 'source' / '.static').isdir() assert (tempdir / 'source' / '.templates').isdir() assert (tempdir / 'source' / 'contents.txt').isfile()
def test_exits_when_existing_confpy(monkeypatch): # The code detects existing conf.py with path.isfile() # so we mock it as True with pytest's monkeypatch def mock_isfile(path): return True monkeypatch.setattr(path, 'isfile', mock_isfile) qs.term_input = mock_input( {'Please enter a new root path (or just Enter to exit)': ''}) d = {} with pytest.raises(SystemExit): qs.ask_user(d)
def test_default_filename(tempdir): answers = { 'Root path': tempdir, 'Project name': '\u30c9\u30a4\u30c4', # Fullwidth characters only 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) conffile = tempdir / 'conf.py' assert conffile.isfile() ns = {} exec(conffile.read_text(), ns)
def test_default_filename(tempdir): answers = { 'Root path': tempdir, 'Project name': '\u30c9\u30a4\u30c4', # Fullwidth characters only 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) conffile = tempdir / 'conf.py' assert conffile.isfile() ns = {} execfile_(conffile, ns) assert ns['latex_documents'][0][1] == 'sphinx.tex'
def test_default_filename(tempdir): answers = { 'Root path': tempdir, 'Project name': u'\u30c9\u30a4\u30c4', # Fullwidth characters only 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) conffile = tempdir / 'conf.py' assert conffile.isfile() ns = {} execfile_(conffile, ns) assert ns['latex_documents'][0][1] == 'sphinx.tex'
def test_generated_files_eol(tempdir): answers = { 'Root path': tempdir, 'Project name': 'Sphinx Test', 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) def assert_eol(filename, eol): content = filename.read_bytes().decode() assert all([l[-len(eol):] == eol for l in content.splitlines(True)]) assert_eol(tempdir / 'make.bat', '\r\n') assert_eol(tempdir / 'Makefile', '\n')
def test_generated_files_eol(tempdir): answers = { 'Root path': tempdir, 'Project name': 'Sphinx Test', 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) def assert_eol(filename, eol): content = filename.bytes().decode('unicode-escape') assert all([l[-len(eol):] == eol for l in content.splitlines(True)]) assert_eol(tempdir / 'make.bat', '\r\n') assert_eol(tempdir / 'Makefile', '\n')
def test_quickstart_and_build(tempdir): answers = { 'Root path': tempdir, 'Project name': 'Fullwidth characters: \u30c9\u30a4\u30c4', 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) app = application.Sphinx( tempdir, # srcdir tempdir, # confdir (tempdir / '_build' / 'html'), # outdir (tempdir / '_build' / '.doctree'), # doctreedir 'html', # buildername status=StringIO(), warning=warnfile) app.builder.build_all() warnings = warnfile.getvalue() assert not warnings
def test_quickstart_and_build(tempdir): answers = { 'Root path': tempdir, 'Project name': u'Fullwidth characters: \u30c9\u30a4\u30c4', 'Author name': 'Georg Brandl', 'Project version': '0.1', } qs.term_input = mock_input(answers) d = {} qs.ask_user(d) qs.generate(d) app = application.Sphinx( tempdir, # srcdir tempdir, # confdir (tempdir / '_build' / 'html'), # outdir (tempdir / '_build' / '.doctree'), # doctreedir 'html', # buildername status=StringIO(), warning=warnfile) app.builder.build_all() warnings = warnfile.getvalue() assert not warnings
def quickstart( project: Optional[str] = typer.Option( None, '-p', '--project', metavar='PROJECT_NAME', help="project name. \ndefault is basename of PROJECT_DIR."), author: str = typer.Option(SphinxExpress.default_user, '-a', '--author', metavar='AUTHOR_NAME', help='author name. default is "{}"'.format( SphinxExpress.default_user)), ver: str = typer.Option('0.0.1', '-v', '--ver', metavar='VERSION', help="version of project."), lang: str = typer.Option('ja', '-l', '--lang', metavar='LANG', help="document language."), templatedir: Optional[pathlib. Path] = typer.Option( SphinxExpress.default_templatedir, '-t', '--templatedir', metavar='TEMPLATE_DIR', file_okay=False, resolve_path=True, exists=True, help="template directory for template files." ), define_value: List[ str] = typer.Option(None, '-d', '--define', metavar='NAE=VALUE', help="define a template variable."), configfile: Optional[pathlib. Path] = typer.Option( SphinxExpress.default_configfile, '-c', '--configfile', metavar='CONFIG_FILEPATH', dir_okay=False, exists=True, help="sphinx-express configfile."), new: bool = typer.Option(False, '-N', '--new', help="Ignore least configures."), setup: bool = typer.Option(False, '--setup', callback=startup_callback, help="Copy templates and exit."), project_dir: str = typer.Argument(...), version: bool = typer.Option(False, '--version', callback=version_callback, is_eager=True, help="Show version and exit."), debug: bool = typer.Option(False, '--debug', hidden=True), ): def parse_variables(variable_list): dummy = dict() for variable in variable_list: try: name, value = variable.split('=') dummy[name] = value except ValueError: typer.echo(f'Invalid template variable: {variable}') return [f'{k}={v}' for k, v in dummy.items()] try: dir = pathlib.Path(project_dir) except TypeError: click.echo("\nError: Missing argument 'PROJECT_DIR'.\n\n", err=True) raise typer.Exit() if not dir.exists(): dir.mkdir() elif dir.is_dir() is not True: click.echo( "\nError: Your select project root is already exists. file: {}". format(project_dir), err=True) raise typer.Exit() d = DEFAULTS.copy() d["path"] = project_dir d["project"] = project or pathlib.Path(project_dir).parent if lang not in ['en', 'ja']: try: test_import = f"from sphinx.locale import {lang}" eval(test_import) except ImportError: click.echo( f"{lang} is not supported language, using 'en' instead.") lang = 'en' if new: templatedir = None d["author"] = author d["version"] = ver d["lang"] = lang d["variables"] = parse_variables(list(define_value)) else: config = SphinxExpress(configfile) least_config = config.load_config() least_variable = set(d.get("variables", [])) define_value = set(define_value) d["variables"] = parse_variables(list(least_variable | define_value)) d.update(**least_config) ask_user(d) generate(d, templatedir=templatedir) config.save_config(d)