def test_file_creation(self): '''Test that all files are created where they should be for all child classes of the ProjectBase class. ''' # Loop through all classes available in entree.projects for project_cls in CLASSES: # Create temporary rootdir with TMPFile() as rootdir: # Create temporary project directory with TMPFile(root=rootdir) as project: print_header('Testing file creation for class ' '`{0}`'.format(project_cls.__name__)) project_cls.create_all(rootdir, project) gendir = os.path.join(rootdir, project) tpath = project_cls.template_path() _, files = entree.utils.get_all_dirs_and_files(tpath) filmap = entree.utils.filemap(files, replace=project_cls.replace, modname=project) for _, fname in filmap.items(): six.print_('- Testing file `{0}`'.format(fname)) path = os.path.join(gendir, fname) try: self.assertTrue(os.path.exists(path)) except AssertionError: six.print_('\nERROR: Path does not exist: ' '{0}'.format(path)) raise
def test_file_content(self): '''Test file content for all files for all child classes of the ProjectBase class. ''' for project_cls in CLASSES: with TMPFile() as rootdir: with TMPFile(root=rootdir) as project: print_header('Testing file content for class ' '`{0}`'.format(project_cls.__name__)) project_cls.create_all(rootdir, project) gendir = os.path.join(rootdir, project) tpath = project_cls.template_path() _, files = entree.utils.get_all_dirs_and_files(tpath) filmap = entree.utils.filemap(files, replace=project_cls.replace, modname=project) for tname, fname in filmap.items(): six.print_('- Testing file content for ' '`{0}`'.format(fname)) filepath = os.path.join(gendir, fname) templatepath = project_cls.template_path() templatepath = os.path.join(templatepath, tname) content1, content2 = get_file_content( project, filepath, templatepath, project_cls=project_cls) self.assertEqual(content1, content2)
def test_partial_file_creation(self): '''Test that all files are created where they should be for all child classes of the ProjectBase class. ''' # Loop through all classes available in entree.projects for project_cls in CLASSES: # Create temporary rootdir config = project_cls.get_config() if 'partial_builds' in config: for partial_build in config['partial_builds']: with TMPFile() as rootdir: # Create temporary project directory with TMPFile(root=rootdir) as project: print_header('Testing partial file creation for ' 'class ' '`{0}`'.format(project_cls.__name__)) project_cls.create_all(rootdir, project, partial=partial_build) gendir = os.path.join(rootdir, project) tpath = project_cls.template_path() files = entree.utils.get_all_dirs_and_files(tpath) filmap = entree.utils.filemap( files[1], replace=project_cls.replace, modname=project) partials = config['partial_builds'][partial_build] for tname, fname in filmap.items(): path = os.path.join(gendir, fname) if tname in partials: six.print_('- Testing file ' '`{0}`: IN'.format(fname)) try: self.assertTrue(os.path.exists(path)) except AssertionError: six.print_('\nERROR: Path does not ' 'exist: {0}'.format(path)) raise else: six.print_('- Testing file ' '`{0}`: OUT'.format(fname)) try: self.assertFalse(os.path.exists(path)) except AssertionError: six.print_('\nERROR: Path does ' 'exist: {0}'.format(path)) raise
def test_cfs_with_blah_dictionary(self): '''Test copy_file_structure() ''' with TMPFile() as rootdir: path_a = os.path.join(rootdir, 'a') path_b = os.path.join(rootdir, 'b') path_c = os.path.join(rootdir, 'c') file_a = os.path.join(path_a, 'a.txt') file_b = os.path.join(path_b, 'b.md') file_c = os.path.join(path_c, 'c.py') entree.utils.copy_file_structure(rootdir, self.template_dir, blah={'age': 19}) self.assertTrue(os.path.exists(path_a)) self.assertTrue(os.path.exists(path_b)) self.assertTrue(os.path.exists(path_c)) self.assertTrue(os.path.exists(file_a)) self.assertTrue(os.path.exists(file_b)) self.assertTrue(os.path.exists(file_c)) with open(file_a) as fil: content = fil.read() self.assertEqual(content, "My name is") with open(file_b) as fil: content = fil.read() self.assertEqual(content, "") with open(file_c) as fil: content = fil.read() self.assertEqual(content, "I'm 19 years old.")
def test_cfs_with_replace_dir_templ(self): '''Test copy_file_structure() ''' with TMPFile() as rootdir: path_a = os.path.join(rootdir, 'a') path_b = os.path.join(rootdir, 'b') path_c = os.path.join(rootdir, 'Lily') file_a = os.path.join(path_a, 'new.txt') file_b = os.path.join(path_b, 'b.md') file_c = os.path.join(path_c, 'c.py') entree.utils.copy_file_structure(rootdir, self.template_dir, replace={'a.txt': 'new.txt', 'c': '{{ name }}'}, blah={'age': 19}, name="Lily") self.assertTrue(os.path.exists(path_a)) self.assertTrue(os.path.exists(path_b)) self.assertTrue(os.path.exists(path_c)) self.assertTrue(os.path.exists(file_a)) self.assertTrue(os.path.exists(file_b)) self.assertTrue(os.path.exists(file_c)) with open(file_a) as fil: content = fil.read() self.assertEqual(content, "My name is") with open(file_b) as fil: content = fil.read() self.assertEqual(content, "Lily") with open(file_c) as fil: content = fil.read() self.assertEqual(content, "I'm 19 years old.")
def test_no_projtype_dir(self): '''check status code of `entree blah.py -d blah` ''' with TMPFile() as rootdir: modname = random_string(16) code = run_command('entree {0} -d {1}'.format(modname, rootdir)) self.assertEqual(code, 0) if os.path.exists(modname): os.remove(modname)
def test_create_general_file(self): '''Test create_dirs() ''' with TMPFile() as rootdir: path_a = os.path.join(rootdir, 'a') entree.utils.create_general_file(path_a, "AAAAA") self.assertTrue(os.path.exists(path_a)) with open(path_a) as fil: content = fil.read() self.assertEqual(content, "AAAAA")
def test_add(self): '''check status code of `entree python -a -d rootdir blah` ''' for project_cls in CLSNAMES: modname = random_string(16) with TMPFile() as rootdir: cmd = 'entree {0} -a -d {1} {2}'.format( project_cls, rootdir, modname) code = run_command(cmd) self.assertEqual(code, 0)
def test_create_dirs(self): '''Test create_dirs() ''' with TMPFile() as rootdir: path_a = os.path.join(rootdir, 'a') path_b = os.path.join(rootdir, 'b') path_c = os.path.join(rootdir, 'c') entree.utils.create_dirs(rootdir, path_a, path_b, path_c) self.assertTrue(os.path.exists(rootdir)) self.assertTrue(os.path.exists(path_a)) self.assertTrue(os.path.exists(path_b)) self.assertTrue(os.path.exists(path_c))
def test_partial_file_creation_noconfig(self): '''Test that all files are created where they should be for all child classes of the ProjectBase class. ''' # Loop through all classes available in entree.projects for project_cls in CLASSES: # Create temporary rootdir config = project_cls.get_config() with TMPFile() as rootdir: # Create temporary project directory with TMPFile(root=rootdir) as project: if 'partial_builds' not in config: with self.assertRaises(ValueError): project_cls.create_all(rootdir, project, partial='blah') else: with self.assertRaises(ValueError): project_cls.create_all(rootdir, project, partial=project)
def test_single_file_creation(self): '''Test file creation in single-file mode for all child classes of the ProjectBase class. ''' # Loop through all classes available in entree.projects for project_cls in CLASSES: if project_cls.single_file: # Create temporary rootdir with TMPFile() as rootdir: # Create temporary project directory with TMPFile(root=rootdir) as project: print_header('Testing single-file creation for class ' '`{0}`'.format(project_cls.__name__)) gendir = os.path.join(rootdir, project) project_cls.create_one(gendir, 'somefile.txt') path = os.path.join(gendir, 'somefile.txt') try: self.assertTrue(os.path.exists(path)) except AssertionError: six.print_('\nERROR: Path does not exist: ' '{0}'.format(path)) raise
def test_directory_creation(self): '''Test that all directories are created where they should be for all child classes of the ProjectBase class. ''' # Loop through all classes available in entree.projects for project_cls in CLASSES: # Create temporary rootdir with TMPFile() as rootdir: # Create temporary project directory with TMPFile(root=rootdir) as project: print_header('Testing directory creation for class ' '`{0}` with files to ' 'ignore'.format(project_cls.__name__)) project_cls.create_all(rootdir, project) config = project_cls.get_config() files_to_ignore = [] if 'files_to_ignore' in config: files_to_ignore = config['files_to_ignore'] six.print_('Files to ignore: ', files_to_ignore) gendir = os.path.join(rootdir, project) tpath = project_cls.template_path() dirs, _ = entree.utils.get_all_dirs_and_files( tpath, files_to_ignore=files_to_ignore) dirmap = entree.utils.filemap(dirs, replace=project_cls.replace, modname=project) for _, dname in dirmap.items(): six.print_('- Testing directory `{0}`'.format(dname)) path = os.path.join(gendir, dname) try: self.assertTrue(os.path.exists(path)) except AssertionError: six.print_('\nERROR: Path does not exist: ' '{0}'.format(path)) raise
def test_single_file_content(self): '''Test file content in single-file mode for all child classes of the ProjectBase class. ''' # Loop through all classes available in entree.projects for project_cls in CLASSES: if project_cls.single_file: # Create temporary rootdir with TMPFile() as rootdir: # Create temporary project directory with TMPFile(root=rootdir) as project: print_header('Testing single-file content for class ' '`{0}`'.format(project_cls.__name__)) gendir = os.path.join(rootdir, project) project_cls.create_one(gendir, 'somefile.txt') filepath = os.path.join(gendir, 'somefile.txt') templatepath = project_cls.single_file_path() content1, content2 = get_file_content( 'somefile', filepath, templatepath, project_cls=project_cls) self.assertEqual(content1, content2)
def test_add(self): '''check output of `entree python -a -d rootdir blah` ''' with TMPFile() as rootdir: cmd = 'entree python -a -d {0} {1}'.format(rootdir, self.modname) run_command(cmd) files = [ self.modname, os.path.join(self.modname, '__init__.py'), 'tests', os.path.join('tests', 'test_{0}.py'.format(self.modname)), 'setup.py', 'License.md', 'requirements.txt', 'README.md', '.gitignore' ] for name in files: self.assertTrue(os.path.exists(os.path.join(rootdir, name)))
def test_render_template(self): '''Test render_template() ''' with TMPFile() as rootdir: path_a = os.path.join(rootdir, 'a') template_string = ("My name is {{ name }}, I'm {{ blah['age'] }} " "years old. This is year " "{{ date.strftime('%Y') }}.") entree.utils.create_general_file(path_a, template_string) date = datetime.datetime(2018, 1, 1) content = entree.utils.render_template(path_a, name="Lily", blah={'age': 19}, date=date) self.assertEqual(content, 'My name is Lily, I\'m 19 years old. ' 'This is year 2018.')
def test_partial2(self): '''check status code of `entree python -d dir -p partial blah` Status should be 1 if there is no `partial_builds` in the project config or if the partial name does not exist in `partial_builds`, should be 0 otherwise. ''' for project_cls in CLASSES: clsname = project_cls.__name__.lower() modname = random_string(16) with TMPFile() as rootdir: config = project_cls.get_config() if 'partial_builds' in config: cmd = 'entree {0} -d {1} -p {2} {2}'.format( clsname, rootdir, modname) code = run_command(cmd) self.assertEqual(code, 1)
def test_create_single_file(self): '''Test create_single_file() ''' with TMPFile() as rootdir: newfilename = 'newfilename' template_path = os.path.join(rootdir, 'template_path') template_string = ("My name is {{ name }}, I'm {{ blah['age'] }} " "years old") entree.utils.create_general_file(template_path, template_string) entree.utils.create_single_file(rootdir, newfilename, template_path, name="Lily", blah={'age': 19}) self.assertTrue(os.path.exists(os.path.join(rootdir, newfilename))) with open(os.path.join(rootdir, newfilename)) as fil: content = fil.read() self.assertEqual(content, 'My name is Lily, I\'m 19 years old')
def test_cfs_ignore4(self): '''Test copy_file_structure() ''' with TMPFile() as rootdir: path_a = os.path.join(rootdir, 'a') path_b = os.path.join(rootdir, 'b') path_c = os.path.join(rootdir, 'c') file_a = os.path.join(path_a, 'a.txt') file_b = os.path.join(path_b, 'b.md') file_c = os.path.join(path_c, 'c.py') ignore = ['a'] entree.utils.copy_file_structure(rootdir, self.template_dir, files_to_ignore=ignore, blah={'age': 19}) self.assertFalse(os.path.exists(path_a)) self.assertTrue(os.path.exists(path_b)) self.assertTrue(os.path.exists(path_c)) self.assertFalse(os.path.exists(file_a)) self.assertTrue(os.path.exists(file_b)) self.assertTrue(os.path.exists(file_c))
def test_cfs_partial(self): '''Test copy_file_structure() ''' with TMPFile() as rootdir: path_a = os.path.join(rootdir, 'a') path_b = os.path.join(rootdir, 'b') path_c = os.path.join(rootdir, 'c') file_a = os.path.join(path_a, 'a.txt') file_b = os.path.join(path_b, 'b.md') file_c = os.path.join(path_c, 'c.py') partial = ['a', 'a/a.txt', 'c'] partial = [os.path.join(self.template_dir, name) for name in partial] entree.utils.copy_file_structure(rootdir, self.template_dir, partial=partial, blah={'age': 19}) self.assertTrue(os.path.exists(path_a)) self.assertFalse(os.path.exists(path_b)) self.assertTrue(os.path.exists(path_c)) self.assertTrue(os.path.exists(file_a)) self.assertFalse(os.path.exists(file_b)) self.assertFalse(os.path.exists(file_c)) with open(file_a) as fil: content = fil.read() self.assertEqual(content, "My name is")
def test_cfs_missing_dictionary(self): '''Test copy_file_structure() ''' with TMPFile() as rootdir: with self.assertRaises(UndefinedError): entree.utils.copy_file_structure(rootdir, self.template_dir)