def test_one(): """ main.py: Test defaults with local directory as input. """ main.main([get_anchorhub_path() + get_path_separator() + '../sample/multi-file', get_anchorhub_path() + get_path_separator() + 'tests/anchorhub-out', '-r'])
def test_one(): """ main.py: Test defaults with local directory as input. """ main.main([ get_anchorhub_path() + get_path_separator() + '../sample/multi-file', get_anchorhub_path() + get_path_separator() + 'tests/anchorhub-out', '-r' ])
def test_get_path_separator(): """ compatibility.py: Test get_path_separator() Test to make sure get_path_separator() returns the proper path separator. """ if os.name == 'nt': assert compat.get_path_separator() == '\\' else: assert compat.get_path_separator() == '/'
def test_abs_path_directories(): """ normalize_opts.py: Test add_abs_path_directories() """ a = {'input': '.', 'output': 'anchorhub-out', 'is_dir': True} n.add_abs_path_directories(a) assert 'abs_input' in a assert 'abs_output' in a assert a['abs_input'] == path.abspath('.') + get_path_separator() assert a['abs_output'] == path.abspath( 'anchorhub-out') + get_path_separator()
def test_abs_path_directories(): """ normalize_opts.py: Test add_abs_path_directories() """ a = {'input': '.', 'output': 'anchorhub-out', 'is_dir': True} n.add_abs_path_directories(a) assert 'abs_input' in a assert 'abs_output' in a assert a['abs_input'] == path.abspath('.') + get_path_separator() assert a['abs_output'] == path.abspath('anchorhub-out')+get_path_separator()
def test_normalize(): """ normalize_opts.py: Test normalize() """ a = NormObj('.', 'anchorhub-out', '{ }', ['.md'], False) a = n.normalize(a) assert a.input == '.' + get_path_separator() assert a.output == 'anchorhub-out' + get_path_separator() assert a.open == '{' assert a.close == '}' assert a.abs_input == path.abspath(a.input) + get_path_separator() assert a.abs_output == path.abspath(a.output) + get_path_separator()
def add_abs_path_directories(opts_dict): """ Adds 'abs_input' and 'abs_output' to opts_dict :param opts_dict: dictionary that will be modified """ assert_has_input_output(opts_dict) opts_dict["abs_input"] = path.abspath(opts_dict["input"]) if opts_dict["is_dir"]: # Only add path separator to input if it is a directory opts_dict["abs_input"] += get_path_separator() opts_dict["abs_output"] = path.abspath(opts_dict["output"]) + get_path_separator()
def ensure_directories_end_in_separator(opts_dict): """ Adds a path separator to the end of the input and output directories, if they don't already have them. :param opts_dict: dictionary that will be modified """ assert_has_input_output(opts_dict) assert "is_dir" in opts_dict if opts_dict["is_dir"]: opts_dict["input"] = add_suffix(opts_dict["input"], get_path_separator()) opts_dict["output"] = add_suffix(opts_dict["output"], get_path_separator())
def add_abs_path_directories(opts_dict): """ Adds 'abs_input' and 'abs_output' to opts_dict :param opts_dict: dictionary that will be modified """ assert_has_input_output(opts_dict) opts_dict['abs_input'] = path.abspath(opts_dict['input']) if opts_dict['is_dir']: # Only add path separator to input if it is a directory opts_dict['abs_input'] += get_path_separator() opts_dict['abs_output'] = path.abspath(opts_dict['output']) + \ get_path_separator()
def ensure_directories_end_in_separator(opts_dict): """ Adds a path separator to the end of the input and output directories, if they don't already have them. :param opts_dict: dictionary that will be modified """ assert_has_input_output(opts_dict) assert 'is_dir' in opts_dict if opts_dict['is_dir']: opts_dict['input'] = add_suffix(opts_dict['input'], get_path_separator()) opts_dict['output'] = add_suffix(opts_dict['output'], get_path_separator())
def test_get_anchorhub_path_directory(): """ getanchorhubpath.py: Make sure directory given is named 'anchorhub' """ path = get_anchorhub_path() dir = path[path.rfind(get_path_separator()) + 1:] assert dir == 'anchorhub'
def get_files(dir, exts, exclude=None, recursive=False): """ Get a list of files within a directory with given extensions. Exclude/black list directories from the list if specified. By default, the search is recursive, looking in all subdirectories of 'dir' :param dir: String root directory to search under. All subdirectories are also searched by default :param exts: List of string file extensions. Files inside of dir with names ending with this string will be included in the list. Note: the string does not strictly need to be a file extension (beginging with a '.' dot), it could be the entire name of the file, or a common suffix to files. :param exclude: List of strings specifying directories that should not be included in the output list :param recursive: When True, search in all subdirectories, otherwise just look in the current directory :return: List of string directories """ file_paths = [] if recursive: for root, _, _ in os.walk(dir): # os.walk() does not add path separator by default to end of path root = add_suffix(root, get_path_separator()) if exclude is not None and is_dir_inside(root, exclude): # Skip directories that are in the exclude list continue file_paths.extend(get_files_in_dir(root, *exts)) else: file_paths.extend(get_files_in_dir(dir, *exts)) return file_paths
def get_files(dir, exts, exclude=None, recursive=False): """ Get a list of files within a directory with given extensions. Exclude/black list directories from the list if specified. By default, the search is recursive, looking in all subdirectories of 'dir' :param dir: String root directory to search under. All subdirectories are also searched by default :param exts: List of string file extensions. Files inside of dir with names ending with this string will be included in the list. Note: the string does not strictly need to be a file extension (beginging with a '.' dot), it could be the entire name of the file, or a common suffix to files. :param exclude: List of strings specifying directories that should not be included in the output list :param recursive: When True, search in all subdirectories, otherwise just look in the current directory :return: List of string directories """ file_paths =[] if recursive: for root, _, _ in os.walk(dir): # os.walk() does not add path separator by default to end of path root = add_suffix(root, get_path_separator()) if exclude is not None and is_dir_inside(root, exclude): # Skip directories that are in the exclude list continue file_paths.extend(get_files_in_dir(root, *exts)) else: file_paths.extend(get_files_in_dir(dir, *exts)) return file_paths
def test_validate_same_input_output_relative_dirs(): """ validate_overwrite.py: input/output are same, using only relative paths """ cwd = os.getcwd() # Find last path separator in cwd; dir is everything after that dir = cwd[cwd.rfind(get_path_separator()):] a = OverObj('.', '..' + dir, False) assert v.validate(a)
def test_add_is_file(): """ normalize_opts.py: Test add_is_file() """ dir = get_anchorhub_path() a = {'input': dir, 'output': 'anchorhub-out'} n.add_is_dir(a) assert 'is_dir' in a assert a['is_dir'] == True file = get_anchorhub_path() + get_path_separator() + 'main.py' b = {'input': file, 'output': 'anchorhub-out'} n.add_is_dir(b) assert 'is_dir' in b assert b['is_dir'] == False
def test_ensure_directories_end_in_separator(): """ normalize_opts.py: Test ensure_directories_end_in_separator() """ a = {'input': '.', 'output': 'anchorhub-out', 'is_dir': True} n.ensure_directories_end_in_separator(a) assert a['input'] == '.' + get_path_separator() assert a['output'] == 'anchorhub-out' + get_path_separator() b = { 'input': 'hello' + get_path_separator(), 'output': 'dolly' + get_path_separator(), 'is_dir': True } n.ensure_directories_end_in_separator(b) assert b['input'] == 'hello' + get_path_separator() assert b['output'] == 'dolly' + get_path_separator() c = {'input': 'fakefile.md', 'output': 'anchorhub-out', 'is_dir': False} n.ensure_directories_end_in_separator(c) assert c['input'] == 'fakefile.md' assert c['output'] == 'anchorhub-out' + get_path_separator()
def test_ensure_directories_end_in_separator(): """ normalize_opts.py: Test ensure_directories_end_in_separator() """ a = {'input': '.', 'output': 'anchorhub-out', 'is_dir': True} n.ensure_directories_end_in_separator(a) assert a['input'] == '.' + get_path_separator() assert a['output'] == 'anchorhub-out' + get_path_separator() b = {'input': 'hello' + get_path_separator(), 'output': 'dolly' + get_path_separator(), 'is_dir': True} n.ensure_directories_end_in_separator(b) assert b['input'] == 'hello' + get_path_separator() assert b['output'] == 'dolly' + get_path_separator() c = {'input': 'fakefile.md', 'output': 'anchorhub-out', 'is_dir': False} n.ensure_directories_end_in_separator(c) assert c['input'] == 'fakefile.md' assert c['output'] == 'anchorhub-out' + get_path_separator()
""" Tests for fileparse.py fileparse.py: http://www.github.com/samjabrahams/anchorhub/anchorhub/fileparse.py """ import os.path from anchorhub.fileparse import get_file_list from anchorhub.util.getanchorhubpath import get_anchorhub_path from anchorhub.compatibility import get_path_separator sep = get_path_separator() class FileObj(object): """ Helper class for testing out fileparse.get_file_list """ def __init__(self, abs_input=None, abs_output=None, extensions=None, recursive=None, is_dir=None, input=None): if abs_input is not None: self.abs_input = abs_input if abs_output is not None: self.abs_output = abs_output if extensions is not None: self.extensions = extensions if recursive is not None: self.recursive = recursive if is_dir is not None: self.is_dir = is_dir if input is not None:
def test_file_to_list_basic(): sep = get_path_separator() path = get_anchorhub_path() + sep + 'lib' + sep + 'tests' + sep + \ 'test_data' + sep + 'filelist' assert FileToList.to_list(path) == ['Hello!\n', 'My name\n', 'is\n', \ 'AnchorHub']
""" Tests for fileparse.py fileparse.py: http://www.github.com/samjabrahams/anchorhub/anchorhub/fileparse.py """ import os.path from anchorhub.fileparse import get_file_list from anchorhub.util.getanchorhubpath import get_anchorhub_path from anchorhub.compatibility import get_path_separator sep = get_path_separator() class FileObj(object): """ Helper class for testing out fileparse.get_file_list """ def __init__(self, abs_input=None, abs_output=None, extensions=None, recursive=None, is_dir=None, input=None): if abs_input is not None: self.abs_input = abs_input if abs_output is not None: self.abs_output = abs_output if extensions is not None: