def test_starter_interface_basic(tmpdir, fixtures_settings): """ Project starter with "basic" template should create expected structure. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) sample_name = "basic_sample" template_name = "basic" basedir = tmpdir destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, template_name) project_path = os.path.join(destination, "project") sources_path = os.path.join(project_path, "sources") localedir_path = os.path.join(project_path, "locale") created = starter_interface(template_path, sample_name, basedir) # Expected directories according to destination and template content assert created == destination assert os.path.exists(project_path) is True assert os.path.exists(sources_path) is True assert os.path.exists(localedir_path) is True
def startproject_command(context, name, template, destination): """ Create a new project from a template Expect one argument 'NAME' that will be the project name and its directory name. The name must be a valid Python module name. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) logger = logging.getLogger("optimus") # Valid that all characters from the name are : "_" character, letters, # identifier ::= (letter|"_") (letter | digit | "_")* # This is not fully safe, user can create a project name using an installed # Python module that will override it and make some troubles in some case if name: if name[0] not in ascii_letters: logger.error("Project name must start with a letter") return for k in name[1:]: if k not in ascii_letters and k not in digits and k != "_": logger.error(("Project name must only contains letters, " "digits or '_' character")) return # Resolve possible internal template alias to a path template = resolve_internal_template(template) try: starter_interface(template, name, destination) except RepositoryNotFound: raise click.Abort()
def test_po_interface_all(tmpdir, fixtures_settings, starter_basic_settings): """ All modes combined should create the POT and langages structure, then update it and compile the MO files. Note this is not really useful since the compile and update always involve initialization first. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) basedir = tmpdir sample_name = "basic" destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, sample_name) project_path = os.path.join(destination, "project") localedir_path = os.path.join(project_path, "locale") settings = starter_basic_settings(project_path) starter_interface(template_path, sample_name, basedir) # Remove existing locale directory for test needs shutil.rmtree(localedir_path) # Compile MO files po_interface(settings, init=True, update=True, compile_opt=True) # Expected directories and files assert os.path.exists(localedir_path) is True assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.po", )) is True) assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.mo", )) is True) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.po", )) is True) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.mo", )) is True)
def test_po_interface_init(tmpdir, fixtures_settings, starter_basic_settings): """ Init mode should creates the POT file and the enabled langages structure with their PO files. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) basedir = tmpdir sample_name = "basic" destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, sample_name) project_path = os.path.join(destination, "project") localedir_path = os.path.join(project_path, "locale") settings = starter_basic_settings(project_path) starter_interface(template_path, sample_name, basedir) # Remove existing locale directory for test needs shutil.rmtree(localedir_path) # Start catalog po_interface(settings, init=True) # Expected directories and files assert os.path.exists(localedir_path) is True assert os.path.exists(os.path.join(localedir_path, "messages.pot")) is True assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.po", )) is True) assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.mo", )) is False) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.po", )) is True) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.mo", )) is False)
def test_cli_runserver( caplog, tmpdir, fixtures_settings, flush_settings, reset_syspath ): """ Command should proceed without any error. We only test the command is running and finish with a valid exit code (0), but cherrypy server is disabled. """ # Mute every loggers related to "starter_interface" which are not from optimus # namespace set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) sample_name = "basic_sample" template_name = "basic" basedir = tmpdir destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, template_name) project_path = os.path.join(destination, "project") builddir_path = os.path.join(project_path, "_build") builddir_dev_path = os.path.join(builddir_path, "dev") starter_interface(template_path, sample_name, basedir) # Create expected publish directory os.makedirs(builddir_dev_path) runner = CliRunner() result = runner.invoke( cli_frontend, [ "--test-env", # "--verbose=5", "runserver", "--settings-name=settings.base", "--basedir={}".format(project_path), "localhost:8001", ], ) # print("result.exit_code:", result.exit_code) # print("result.exc_info:", result.exc_info) # if result.exit_code > 0: # import traceback # klass, error, error_tb = result.exc_info # print(error) # traceback.print_tb(error_tb, limit=None) assert result.exit_code == 0 # Cleanup sys.path for next tests reset_syspath(project_path)
def test_watcher_interface(tmpdir, fixtures_settings, starter_basic_settings): """ Watcher interface should return a correct observer with expected handlers. This is pretty almost code from the builder interface test with additional watcher parts. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) basedir = tmpdir sample_name = "basic" destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, sample_name) project_path = os.path.join(destination, "project") # Get basic settings and its computed path settings = starter_basic_settings(project_path) builddir_path = settings.PUBLISH_DIR # Create sample project from basic starter starter_interface(template_path, sample_name, basedir) # Make a dummy module views = DummyViewsModule() # Process to build with given settings and page module build_env = builder_interface(settings, views) assert os.path.exists(builddir_path) is True assert (os.path.exists(os.path.join( builddir_path, "index.html", )) is True) assert (os.path.exists(os.path.join( builddir_path, "index_fr_FR.html", )) is True) assert (os.path.exists( os.path.join(builddir_path, "static", "css", "app.css")) is True) # Make observer observer = watcher_interface(settings, views, build_env) # Get configured handlers to check them observed = {} for path, handlers in observer._handlers.items(): observed[path.path] = [type(item).__name__ for item in handlers] assert observed == { os.path.join(project_path, "sources"): ["AssetsWatchEventHandler"], os.path.join(project_path, "sources/templates"): ["TemplatesWatchEventHandler"], }
def test_cli_builder(tmpdir, fixtures_settings, flush_settings, reset_syspath): """ Builder CLI should correctly build pages from project settings and map. """ # Mute every loggers related to "starter_interface" which are not from optimus # namespace set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) sample_name = "basic_sample" template_name = "basic" basedir = tmpdir destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, template_name) project_path = os.path.join(destination, "project") builddir_path = os.path.join(project_path, "_build", "dev") starter_interface(template_path, sample_name, basedir) runner = CliRunner() result = runner.invoke( cli_frontend, [ "--test-env", # "--verbose=5", "build", "--settings-name=settings.base", "--basedir={}".format(project_path), ], ) # print("result.exit_code:", result.exit_code) # print("result.exc_info:", result.exc_info) # if result.exit_code > 0: # import traceback # klass, error, error_tb = result.exc_info # print(error) # traceback.print_tb(error_tb, limit=None) assert result.exit_code == 0 # Check structure has been created assert os.path.exists(os.path.join(builddir_path)) is True assert os.path.exists(os.path.join(builddir_path, "index.html")) is True assert os.path.exists(os.path.join(builddir_path, "index_fr_FR.html")) is True assert (os.path.exists( os.path.join(builddir_path, "static", "css", "app.css")) is True) # Cleanup sys.path for next tests reset_syspath(project_path)
def test_po_interface_update(tmpdir, fixtures_settings, starter_basic_settings): """ Update mode should just updates (or create it again if missing) the PO files for all enabled langages. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) basedir = tmpdir sample_name = "basic" destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, sample_name) project_path = os.path.join(destination, "project") localedir_path = os.path.join(project_path, "locale") settings = starter_basic_settings(project_path) starter_interface(template_path, sample_name, basedir) # Remove catalog files from sample os.remove(os.path.join(localedir_path, "en_US/LC_MESSAGES/messages.po")) os.remove(os.path.join(localedir_path, "en_US/LC_MESSAGES/messages.mo")) os.remove(os.path.join(localedir_path, "fr_FR/LC_MESSAGES/messages.po")) os.remove(os.path.join(localedir_path, "fr_FR/LC_MESSAGES/messages.mo")) # Update catalog (it should create again PO files which will use for assertions) po_interface(settings, update=True) # Expected directories and files assert os.path.exists(localedir_path) is True assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.po", )) is True) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.po", )) is True)
def test_po_interface_compile(tmpdir, fixtures_settings, starter_basic_settings): """ Compile mode should compiles the PO files to MO files. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) basedir = tmpdir sample_name = "basic" destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, sample_name) project_path = os.path.join(destination, "project") localedir_path = os.path.join(project_path, "locale") settings = starter_basic_settings(project_path) starter_interface(template_path, sample_name, basedir) # Remove compiled files from sample os.remove(os.path.join(localedir_path, "en_US/LC_MESSAGES/messages.mo")) os.remove(os.path.join(localedir_path, "fr_FR/LC_MESSAGES/messages.mo")) # Compile MO files po_interface(settings, compile_opt=True) # Expected directories and files assert os.path.exists(localedir_path) is True assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.mo", )) is True) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.mo", )) is True)
def test_starter_interface_fail(tmpdir, fixtures_settings): """ Failing project creation should emit a critical log and raise the original exception. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) sample_name = "basic_sample" template_name = "nope" basedir = tmpdir destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, template_name) with pytest.raises(RepositoryNotFound): starter_interface(template_path, sample_name, basedir) # Nothing should have been created assert os.path.exists(destination) is False
def test_build_interface(tmpdir, fixtures_settings, starter_basic_settings): """ Build interface should correctly build pages. """ # Mute all other loggers from cookiecutter and its dependancies set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) basedir = tmpdir sample_name = "basic" destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, sample_name) project_path = os.path.join(destination, "project") # Get basic settings and its computed path settings = starter_basic_settings(project_path) builddir_path = settings.PUBLISH_DIR # Create sample project from basic starter starter_interface(template_path, sample_name, basedir) # Make a dummy module views = DummyViewsModule() # Process to build with given settings and page module builder_interface(settings, views) assert os.path.exists(builddir_path) is True assert (os.path.exists(os.path.join( builddir_path, "index.html", )) is True) assert (os.path.exists(os.path.join( builddir_path, "index_fr_FR.html", )) is True) assert (os.path.exists( os.path.join(builddir_path, "static", "css", "app.css")) is True)
def test_cli_po(tmpdir, fixtures_settings, flush_settings, reset_syspath): """ Testing all CLI mode in single execution """ # Mute every loggers related to "starter_interface" which are not from optimus # namespace set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) sample_name = "basic_sample" template_name = "basic" basedir = tmpdir destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, template_name) project_path = os.path.join(destination, "project") localedir_path = os.path.join(project_path, "locale") starter_interface(template_path, sample_name, basedir) # Remove existing locale directory for test needs shutil.rmtree(localedir_path) runner = CliRunner() result = runner.invoke( cli_frontend, [ "--test-env", # "--verbose=5", "po", "--init", "--update", "--compile", "--settings-name=settings.base", "--basedir={}".format(project_path), ], ) # print("result.exit_code:", result.exit_code) # print("result.exc_info:", result.exc_info) # if result.exit_code > 0: # import traceback # klass, error, error_tb = result.exc_info # print(error) # traceback.print_tb(error_tb, limit=None) assert result.exit_code == 0 # Expected directories and files assert os.path.exists(localedir_path) is True assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.po", )) is True) assert (os.path.exists( os.path.join( localedir_path, "en_US", "LC_MESSAGES", "messages.mo", )) is True) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.po", )) is True) assert (os.path.exists( os.path.join( localedir_path, "fr_FR", "LC_MESSAGES", "messages.mo", )) is True) # Cleanup sys.path for next tests reset_syspath(project_path)
def test_cli_watcher(caplog, tmpdir, fixtures_settings, flush_settings, reset_syspath): """ This is the same test code than the builder CLI since watcher CLI share the same code except the watcher interface usage and observer run, which we can really test. """ # Mute every loggers related to "starter_interface" which are not from optimus # namespace set_loggers_level(["poyo", "cookiecutter", "binaryornot"]) sample_name = "basic_sample" template_name = "basic" basedir = tmpdir destination = os.path.join(basedir, sample_name) template_path = os.path.join(fixtures_settings.starters_path, template_name) project_path = os.path.join(destination, "project") builddir_path = os.path.join(project_path, "_build", "dev") starter_interface(template_path, sample_name, basedir) runner = CliRunner() result = runner.invoke( cli_frontend, [ "--test-env", # "--verbose=5", "watch", "--settings-name=settings.base", "--basedir={}".format(project_path), ], ) # print("result.exit_code:", result.exit_code) # print("result.exc_info:", result.exc_info) # if result.exit_code > 0: # import traceback # klass, error, error_tb = result.exc_info # print(error) # traceback.print_tb(error_tb, limit=None) assert result.exit_code == 0 # Check structure has been created assert os.path.exists(os.path.join(builddir_path)) is True assert os.path.exists(os.path.join(builddir_path, "index.html")) is True assert os.path.exists(os.path.join(builddir_path, "index_fr_FR.html")) is True assert (os.path.exists( os.path.join(builddir_path, "static", "css", "app.css")) is True) # At least ensure the warning before observer start is here assert caplog.record_tuples[-1] == ( "optimus", logging.WARNING, "Starting to watch sources, use CTRL+C to stop it", ) # Cleanup sys.path for next tests reset_syspath(project_path)