def test_set_global_vars(tmpdir): template = """ [main] version=5.1 """ target_dir = tmpdir.strpath + "/collective.foo" os.mkdir(target_dir) with open(os.path.join(target_dir + "/bobtemplate.cfg"), "w") as f: f.write(template) configurator = Configurator( template="bobtemplates.plone:addon", target_directory=target_dir, variables={ "year": 1970, "plone.version": "5.1-latest" }, ) base.set_global_vars(configurator) configurator = Configurator( template="bobtemplates.plone:addon", target_directory=target_dir, variables={"year": 1970}, ) base.set_global_vars(configurator)
def test_post_plone_version(): configurator = Configurator(template="bobtemplates.plone:addon", target_directory="collective.foo") base.post_plone_version(configurator, None, "4.3") configurator = Configurator(template="bobtemplates.plone:addon", target_directory="collective.foo") base.post_plone_version(configurator, None, "4-latest") configurator = Configurator(template="bobtemplates.plone:addon", target_directory="collective.foo") base.post_plone_version(configurator, None, "5.1") configurator = Configurator(template="bobtemplates.plone:addon", target_directory="collective.foo") base.post_plone_version(configurator, None, "5-latest") configurator = Configurator( template="bobtemplates.plone:addon", target_directory="collective.foo", variables={ "plone.is_plone5": True, "plone.minor_version": "5.0" }, ) base.post_plone_version(configurator, None, "5.0.1")
def test_set_plone_version_variables(tmpdir): template = """ [main] version=5.1 """ target_dir = tmpdir.strpath + "/collective.foo" os.mkdir(target_dir) with open(os.path.join(target_dir + "/bobtemplate.cfg"), "w") as f: f.write(template) configurator = Configurator( template="bobtemplates.plone:addon", target_directory=target_dir, variables={"plone.version": "5"}, ) base.set_plone_version_variables(configurator) assert configurator.variables.get("plone.is_plone5") assert not configurator.variables.get("plone.is_plone51") assert not configurator.variables.get("plone.is_plone52") assert configurator.variables.get("plone.minor_version") == "5" configurator = Configurator( template="bobtemplates.plone:addon", target_directory=target_dir, variables={"plone.version": "5.2"}, ) base.set_plone_version_variables(configurator) assert configurator.variables.get("plone.is_plone5") assert not configurator.variables.get("plone.is_plone51") assert configurator.variables.get("plone.is_plone52") assert configurator.variables.get("plone.minor_version") == "5.2" configurator = Configurator( template="bobtemplates.plone:addon", target_directory=target_dir, variables={"plone.version": "5.1"}, ) base.set_plone_version_variables(configurator) assert configurator.variables.get("plone.is_plone5") assert configurator.variables.get("plone.is_plone51") assert not configurator.variables.get("plone.is_plone52") assert configurator.variables.get("plone.minor_version") == "5.1" configurator = Configurator( template="bobtemplates.plone:addon", target_directory=target_dir, variables={"plone.version": "4.3"}, ) base.set_plone_version_variables(configurator) assert not configurator.variables.get("plone.is_plone5") assert not configurator.variables.get("plone.is_plone51") assert not configurator.variables.get("plone.is_plone52") assert configurator.variables.get("plone.minor_version") == "4.3"
def test_update_indexers_configure_zcml(tmpdir): target_path = tmpdir.strpath + "/collective.sample" package_path = target_path + "/src/collective/sample" indexers_path = package_path + "/indexers/" os.makedirs(target_path) os.makedirs(package_path) os.makedirs(indexers_path) template = """<configure xmlns="http://namespaces.zope.org/zope" i18n_domain="{{{ package.dottedname }}}"> -*- extra stuff goes here -*- </configure> """ with open(os.path.join(indexers_path + "configure.zcml"), "w") as f: f.write(template) configurator = Configurator( template="bobtemplates.plone:indexer", target_directory="collective.sample", bobconfig={"non_interactive": True}, variables={ "indexer_name": "my_cool_index", "package_folder": package_path, }, ) indexer._update_indexers_configure_zcml(configurator) with open(os.path.join(indexers_path + "configure.zcml"), "r") as f: content = f.read() if content != template: pytest.raises(ValidationError)
def test_check_name(tmpdir): target_path = tmpdir.strpath + '/collective.todo' question = Question(name="svelte_app_name", question="Name of your Svelte app", default=None) configurator = Configurator( template='bobtemplates.plone:svelte_app', target_directory=target_path, bobconfig={"non_interactive": True}, ) with pytest.raises(ValidationError): svelte_app.check_name(configurator, question, 'My-Svelteapp') with pytest.raises(ValidationError): svelte_app.check_name(configurator, question, 'MySvelteApp') with pytest.raises(ValidationError): svelte_app.check_name(configurator, question, 'mysvelteapp') with pytest.raises(ValidationError): svelte_app.check_name(configurator, question, 'my_svelteapp') with pytest.raises(ValidationError): svelte_app.check_name(configurator, question, 'my_svelte_app') assert svelte_app.check_name(configurator, question, 'my-app') == 'my-app' assert svelte_app.check_name(configurator, question, 'my-svelte-app') == 'my-svelte-app' assert svelte_app.check_name(configurator, question, 'my-cool-svelte-app') == 'my-cool-svelte-app' assert svelte_app.check_name( configurator, question, 'my-cool-svelte-app-one') == 'my-cool-svelte-app-one'
def test_pre_email(): configurator = Configurator( template="bobtemplates.plone:addon", bobconfig={"non_interactive": True}, target_directory="collective.foo", ) base.pre_email(configurator, None)
def test_read_bobtemplate_ini(tmpdir): configurator = Configurator(template="bobtemplates.plone:addon", target_directory="collective.todo") base.read_bobtemplates_ini(configurator) template = """[main] version=5.1 """ target_dir = tmpdir.strpath + "/collective.foo" os.mkdir(target_dir) with open(os.path.join(target_dir + "/bobtemplate.cfg"), "w") as f: f.write(template) configurator = Configurator(template="bobtemplates.plone:addon", target_directory=target_dir) base.read_bobtemplates_ini(configurator)
def create_package(opts): package_name = opts.package capitalized_package_name = package_name.capitalize() variables = { 'project.name': package_name, 'entity.example_name': 'My%sEntity' % capitalized_package_name, 'entity.base_name': '%sEntity' % capitalized_package_name, 'created.year': datetime.now().year, 'canari.version': canari.__version__ } defaults = {'project.create_example': True, 'author.name': getuser()} if not path.exists(package_name): print('creating skeleton in %s' % package_name) configurator = Configurator( u'canari.resources.templates:create_package', package_name, { 'non_interactive': False, 'remember_answers': True }, variables=variables, defaults=defaults) configurator.ask_questions() configurator.render() else: print('A directory with the name %s already exists... exiting' % package_name) exit(-1) print('done!')
def create_transform(project, module_name): transform_name = stringcase.pascalcase(module_name) module_name = module_name.lower() target = project.root_dir transform_directory = project.transforms_dir if os.path.exists(os.path.join(transform_directory, '%s.py' % module_name)): click.echo('Transform %r already exists... quitting' % module_name, err=True) exit(-1) variables = parse_config(os.path.join(target, '.mrbob.ini'))['variables'] variables.update({ 'transform.module': module_name, 'transform.name': transform_name }) configurator = Configurator('canari.resources.templates:create_transform', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() click.echo('Creating transform %r...' % module_name, err=True) configurator.render() click.echo('done!', err=True)
def test_delete_python_file(tmpdir): """Test to remove unwanted files.""" target_path = tmpdir.strpath + '/collective.sample' package_path = target_path + '/src/collective/sample' views_path = package_path + '/views/' os.makedirs(target_path) os.makedirs(package_path) os.makedirs(views_path) file_name = u'configure.zcml.example' file_path = views_path + file_name open(file_path, 'a').close() file_name = u'myview.py' file_path = views_path + file_name open(file_path, 'a').close() configurator = Configurator( template='bobtemplates.plone:view', target_directory='collective.sample', bobconfig={ 'non_interactive': True, }, variables={ 'view_name': 'myview', 'view_python_class': False, 'view_python_file_name': 'myview', 'view_template': True, 'package_folder': package_path, }, ) view._delete_unwanted_files(configurator) if os.path.isfile(file_path): pytest.raises(ValidationError)
def test_read_source_version(tmpdir): package_root = tmpdir.strpath + "/collective.todo" package_path = init_package_base_structure(package_root) target_path = os.path.join(package_path, 'profiles/default') template = """<?xml version='1.0' encoding='UTF-8'?> <metadata> <version>1004</version> <dependencies> <dependency>profile-plone.app.dexterity:default</dependency> </dependencies> </metadata> """ with open(os.path.join(target_path, 'metadata.xml'), 'w') as f: f.write(template) configurator = Configurator( template='bobtemplates.plone:upgrade_step', target_directory=package_path, bobconfig={ 'non_interactive': True, }, variables={ 'plone.version': '5.1', 'package_folder': package_path, 'upgrade_step_title': 'Add cool index and reindex it', 'upgrade_step_description': 'We add an index and reindex it with existing content.', }, ) assert configurator assert upgrade_step._read_source_version(configurator) == 1004
def create_package(package_name, author, email, description, create_example): capitalized_package_name = package_name.capitalize() variables = { 'project.name': package_name, 'project.description': description, 'project.create_example': create_example, 'entity.example_name': 'My%sEntity' % capitalized_package_name, 'entity.base_name': '%sEntity' % capitalized_package_name, 'created.year': datetime.now().year, 'canari.version': canari.version, 'canari.major_version': StrictVersion(canari.version).version[0], 'author.name': author, 'author.email': email } if not path.exists(package_name): click.echo('creating skeleton in %s' % package_name, err=True) configurator = Configurator( 'canari.resources.templates:create_package%s' % ('' if create_example else '_no_example'), package_name, {'non_interactive': True, 'remember_answers': True}, variables=variables ) configurator.ask_questions() configurator.render() else: click.echo('A directory with the name %s already exists... exiting' % package_name, err=True) exit(-1) click.echo('done!', err=True)
def test_delete_template_file_false(tmpdir): """Test to not remove template file.""" target_path = tmpdir.strpath + '/collective.sample' package_path = target_path + '/src/collective/sample' viewlets_path = package_path + '/viewlets/' os.makedirs(target_path) os.makedirs(package_path) os.makedirs(viewlets_path) file_name = u'configure.zcml.example' file_path = viewlets_path + file_name open(file_path, 'a').close() file_name = u'my_viewlet.pt' file_path = viewlets_path + file_name open(file_path, 'a').close() configurator = Configurator( template='bobtemplates.plone:viewlet', target_directory='collective.sample', bobconfig={ 'non_interactive': True, }, variables={ 'viewlet_name_normalized': 'my_viewlet', 'viewlet_template': True, 'viewlet_template_name': 'my_viewlet', 'package_folder': package_path, }, ) viewlet._delete_unwanted_files(configurator) if not os.path.isfile(file_path): pytest.raises(ValidationError)
def test_remove_unwanted_files(tmpdir): files_to_remove = [ '/api/configure.zcml.example', '/api/services/configure.zcml.example', ] target_path = tmpdir.strpath + '/collective.todo' package_path = target_path + '/src/collective/todo' os.makedirs(package_path + '/api/services/') configurator = Configurator( template='bobtemplates.plone:restapi_service', target_directory=tmpdir.strpath, variables={ 'package_folder': package_path, }, ) for file_to_remove in files_to_remove: with open( os.path.join(package_path + file_to_remove, ), 'w', ) as f: f.write(u'dummy') restapi_service._remove_unwanted_files(configurator) for file_to_remove in files_to_remove: assert not os.path.isfile( os.path.join(package_path + file_to_remove), )
def test_pre_renderer(tmpdir): target_path = tmpdir.strpath + '/collective.todo' package_path = target_path + '/src/collective/todo' os.makedirs(target_path) os.makedirs(package_path) template = """ [main] version=5.1 """ with open(os.path.join(target_path + '/bobtemplate.cfg'), 'w') as f: f.write(template) template = """ dummy '-*- Extra requirements: -*-' """ with open(os.path.join(target_path + '/setup.py'), 'w') as f: f.write(template) configurator = Configurator( template='bobtemplates.plone:restapi_service', target_directory=target_path, variables={ 'service_class_name': 'SomeRelatedThings', 'service_name': 'some-related-things', 'package_folder': package_path, }, ) restapi_service.pre_renderer(configurator)
def test_prepare_renderer(): configurator = Configurator( template='bobtemplates.plone:buildout', target_directory='collective.foo', ) buildout.prepare_renderer(configurator) assert configurator.variables['template_id'] == 'buildout'
def test_plonecli_config_with_comments(tmpdir): template = """[mr.bob] verbose = False [variables] author.name = The Plone Collective author.email = [email protected] #author.github.user = collective package.git.init = y package.git.autocommit = y package.git.disabled = y plone.version = 5.1 """ with open(os.path.join(tmpdir.strpath, '.mrbob'), 'w') as f: f.write(template) configurator = Configurator( template='plonecli:configure_mrbob', target_directory=tmpdir.strpath, bobconfig={ 'non_interactive': True, }, variables={ 'configure_mrbob.author.name': 'XYZ', 'configure_mrbob.author.email': '*****@*****.**', 'configure_mrbob.author.github.user': '******', 'configure_mrbob.package.git.init': 'y', 'configure_mrbob.package.git.autocommit': 'y', 'configure_mrbob.package.git.disabled': 'y', 'configure_mrbob.plone.version': '5.1', }, ) post_render(configurator, target_directory=tmpdir.strpath) with open(os.path.join(tmpdir.strpath, '.mrbob'), 'r') as f: content = f.read() assert '#author.github.user' in content
def test_post_renderer(tmpdir): target_path = tmpdir.strpath + '/collective.foo' package_path = target_path + '/src/collective/foo' os.makedirs(target_path) os.makedirs(package_path) template = """ dummy '-*- Extra requirements: -*-' """ with open(os.path.join(target_path + '/setup.py'), 'w') as f: f.write(template) template = """ <configure xmlns="http://namespaces.zope.org/zope" xmlns:genericsetup="http://namespaces.zope.org/genericsetup" xmlns:i18n="http://namespaces.zope.org/i18n" xmlns:plone="http://namespaces.plone.org/plone"> <!-- -*- extra stuff goes here -*- --> </configure> """ with open(os.path.join(package_path + '/configure.zcml'), 'w') as f: f.write(template) configurator = Configurator( template='bobtemplates.cs:cs_migration', target_directory=package_path, bobconfig={'non_interactive': True}, ) migration.prepare_renderer(configurator) configurator.render() migration.post_renderer(configurator)
def test_pre_theme_name(): configurator = Configurator( template="bobtemplates.plone:theme_barceloneta", target_directory="collective.theme", ) question = Question("package", "type") theme_barceloneta.pre_theme_name(configurator, question) theme_barceloneta.pre_theme_name(configurator, question)
def test_validate_packagename(tmpdir): base_path = tmpdir.strpath # step 1: test None with pytest.raises(AttributeError): base.validate_packagename(None) # step 2: test base namespace (level 2) configurator = Configurator( template="bobtemplates.plone:addon", target_directory=os.path.join(base_path, "collective.foo"), ) base.validate_packagename(configurator) # step 3: test nested namespace (level 3) configurator = Configurator( template="bobtemplates.plone:addon", target_directory=os.path.join(base_path, "collective.foo.bar"), ) base.validate_packagename(configurator) # step 4: test without namespace (level 1) with pytest.raises(SystemExit): configurator = Configurator( template="bobtemplates.plone:addon", target_directory=os.path.join(base_path, "foo"), ) base.validate_packagename(configurator) # step 5: test deep nested namespace (level 4) with pytest.raises(SystemExit): configurator = Configurator( template="bobtemplates.plone:addon", target_directory=os.path.join(base_path, "collective.foo.bar.spam"), ) base.validate_packagename(configurator) # step 6: test leading dot with pytest.raises(SystemExit): configurator = Configurator( template="bobtemplates.plone:addon", target_directory=os.path.join(base_path, ".collective.foo"), ) base.validate_packagename(configurator) # step 7: test ending dot with pytest.raises(SystemExit): configurator = Configurator( template="bobtemplates.plone:addon", target_directory=os.path.join(base_path, "collective.foo."), ) base.validate_packagename(configurator) # step 8: test invalid char with pytest.raises(SystemExit): configurator = Configurator( template="bobtemplates.plone:addon", target_directory=os.path.join(base_path, "collective.$SPAM"), ) base.validate_packagename(configurator)