def __init__(self, projectdirectory, overwrite_exists=False, skip_exists=False, dry_run=False): """Provide configurations that are common to all DevOpsTemplate actions Params: projectdirectory: String with a (relative) path to the directory that contains the instance of the template. overwrite_exists: Boolean specifying if existing files should be overwritten. An error is raised otherwise. skip_exists: Boolean specifying if existing files should be skipped/ignores. An error is raised otherwise. dry_run: Boolean specifying whether to not perform any actions in order to see (in the log) what would have happend. """ self.__projectdir = projectdirectory self.__overwrite = overwrite_exists self.__skip = skip_exists self.__dry_run = dry_run with pkg.stream('template.json') as handle: self.__template_dict = json.load(handle) self.__template_dname = 'template' # ATTENTION: using __package__ may only work as long as this module # (template.py) is located in the top-level import directory loader = PackageLoader(__package__, self.__template_dname) self.__env = Environment(loader=loader, autoescape=select_autoescape(default=True)) # Create project base directory if not present self.__mkdir(projectdirectory)
def test_manage(self): # Define test project context = {'project_name': 'project'} components = ['git', 'sonar', 'mongo'] # Generate reference data project_file_list = [] with pkg.stream('template.json') as fh: template_dict = json.load(fh) for fpath in itertools.chain(*(template_dict[comp] for comp in components)): # Render path fpath_template = Template(fpath) fpath_rendered = fpath_template.render(**context) project_file_list.append(fpath_rendered) # Create test project with tempfile.TemporaryDirectory() as tmpdirname: template = DevOpsTemplate(projectdirectory=tmpdirname) # Create 'make' component which is required to test 'manage' template._DevOpsTemplate__install('make', context) # Run 'manage' template.manage(context, components) # Make sure all files exist for fpath in project_file_list: fpath = os.path.join(tmpdirname, fpath) self.assertTrue(os.path.exists(fpath))
def test_template_json(self): with pkg.stream('template.json') as fh: template_dict = json.load(fh) filestr_list = list(itertools.chain(*template_dict.values())) self.assertTrue(pkg.isdir('template')) for fpath in filestr_list: self.assertTrue(pkg.exists(f'template/{fpath}'))
def test_stream(self): refstr_head = '\n'.join(self.__ref_template_index_head) with pkg.stream('template/MANIFEST.in') as fh: buffer = fh.read(2) b_list = [buffer] while buffer: buffer = fh.read(2) b_list.append(buffer) filestr = b''.join(b_list) filestr = filestr.decode() filestr_head = filestr[:len(refstr_head)] self.assertEqual(filestr_head, refstr_head)
def test_create(self): # Define test project context = { 'project_name': 'project', 'project_slug': 'project', 'project_version': '0.1.0', 'project_url': '', 'project_description': '', 'author_name': 'full name', 'author_email': '*****@*****.**', 'add_scripts_dir': True, 'add_docs_dir': True, 'no_gitignore_file': False, 'no_readme_file': False, 'no_sonar': False } components = [ 'src', 'tests', 'make', 'setuptools', 'readme', 'docker', 'git', 'sonar' ] # Generate reference data project_file_list = [] with pkg.stream('template.json') as fh: template_dict = json.load(fh) for fpath in itertools.chain(*(template_dict[comp] for comp in components)): # Render path fpath_template = Template(fpath) fpath_rendered = fpath_template.render(**context) project_file_list.append(fpath_rendered) # Create test project with tempfile.TemporaryDirectory() as tmpdirname: template = DevOpsTemplate(projectdirectory=tmpdirname) template.create(context, components) # Make sure all files exist for fpath in project_file_list: fpath = os.path.join(tmpdirname, fpath) self.assertTrue(os.path.exists(fpath))
def test_cookiecutter(self): # Define test project project_slug = ''.join([ "{{ ", "cookiecutter.project_name.lower()", ".replace(' ', '_')", ".replace('-', '_')", " }}" ]) context = { 'project_name': '', 'project_slug': project_slug, 'project_version': '0.1.0', 'project_url': '', 'project_description': '', 'author_name': 'full name', 'author_email': '*****@*****.**' } components = [ 'src', 'tests', 'make', 'setuptools', 'readme', 'docker', 'git', 'sonar' ] # Generate reference data cookiecutterconfig = { key: '{{cookiecutter.%s}}' % key for key in context.keys() } project_file_list = [] with pkg.stream('template.json') as fh: template_dict = json.load(fh) for fpath in itertools.chain(*(template_dict[comp] for comp in components)): # Render path fpath_template = Template(fpath) fpath_rendered = fpath_template.render(**cookiecutterconfig) project_file_list.append(fpath_rendered) # Create test cookiecutter template with tempfile.TemporaryDirectory() as tmpdirname: template = DevOpsTemplate(projectdirectory=tmpdirname) template.cookiecutter(context, components) # Make sure all files exist projectdirname = os.path.join(tmpdirname, '{{cookiecutter.project_slug}}') for fpath in project_file_list: fpath = os.path.join(projectdirname, fpath) self.assertTrue(os.path.exists(fpath)) # Check template __init__.py pkgdirname = os.path.join(projectdirname, '{{cookiecutter.project_slug}}') init_fpath = os.path.join(pkgdirname, '__init__.py') with open(init_fpath, 'r') as fh: init_contents = fh.read() tests_dpath = os.path.dirname(__file__) init_ref_fpath = os.path.join(tests_dpath, 'template_init.ref') with open(init_ref_fpath, 'r') as fh: init_ref_contents = fh.read() self.assertEqual(init_contents, init_ref_contents) # Check cookiecutter files cookiecutter_json_fpath = os.path.join(tmpdirname, 'cookiecutter.json') self.assertTrue(os.path.exists(cookiecutter_json_fpath)) with open(cookiecutter_json_fpath, 'r') as fh: cookiecutter_json = json.load(fh) self.assertEqual(cookiecutter_json, context) self.assertTrue( os.path.exists(os.path.join(tmpdirname, 'README.md'))) # Check that the DevOps template project directory is the unittest # tmp directory self.assertEqual(template._DevOpsTemplate__projectdir, tmpdirname)