def setUp(self): """ make root directory to conduct tests with checking os.listdir output with expected output. most checks here are redundant as os.listdir is essentially covered by cpython's more-than-competent testing suite """ # name and create root directory self.root_directory = 'lsx_root_dir' os.mkdir(self.root_directory) # generate random file names # and create them in self.root_directory self.file_names = [random_string() for _ in range(5)] for file_name in self.file_names: pathlib.Path(full_path(self.root_directory, file_name)).touch() # generate random directory names # and create them in self.root_directory self.dir_names = [random_string() for _ in range(5)] for dir_name in self.dir_names: os.mkdir(full_path(self.root_directory, dir_name))
def test_text_tab_3_to_space_with_listdir(self): before_tshift_files = os.listdir(self.root_directory) expected_files = sorted( os.listdir(self.root_directory) + ['post_tshift_test_3.txt']) path = full_path(self.root_directory, self.files[2]) tshift(path, 6, full_path(self.root_directory, 'post_tshift_test_3.txt')) after_tshift_files = sorted(os.listdir(self.root_directory)) self.assertEqual(after_tshift_files, expected_files)
def test_negative_space_number_fail(self): success, error = tshift(full_path(self.root_directory, self.files[1]), -2) self.assertFalse(success) self.assertEqual('space number should be a non zero positive number', error)
def test_text_tab_2_to_space(self): path = full_path(self.root_directory, self.files[1]) tshift(path, 3) after_tshift_file = open(path) after_tshift_text = after_tshift_file.read() after_tshift_file.close() self.assertEqual(after_tshift_text, text_space_2)
def test_files_check(self): before_lsx_files = os.listdir(self.root_directory) # basic redundant check self.assertEqual(before_lsx_files, lsx(self.root_directory)) # create a file and then check pathlib.Path(full_path(self.root_directory, 'test_1.txt')).touch() self.assertEqual(sorted(before_lsx_files + ['test_1.txt']), sorted(lsx(self.root_directory)))
def setUp(self): """ create files for testing and compare them to results of """ self.root_directory = 'tshift_root_dir' os.mkdir(self.root_directory) self.files = ('test_1.py', 'test_2.txt', 'test_3.txt') with open(full_path(self.root_directory, 'test_1.py'), 'w+') as f: f.write(text_tab_1) f.close() with open(full_path(self.root_directory, 'test_2.txt'), 'w+') as f: f.write(text_tab_2) f.close() with open(full_path(self.root_directory, 'test_3.txt'), 'w+') as f: f.write(text_tab_3) f.close()
def setUp(self): """ make root directory to conduct tests with test directories and file assertions will be checked by different output from os.listdir against results from betterx.rme and known output """ # name and create root directory self.root_directory = 'rme_root_dir' os.mkdir(self.root_directory) # name and create files in root directory self.file_names = ['profile.txt', 'posts.json', 'important.md'] for file_name in self.file_names: pathlib.Path(full_path(self.root_directory, file_name)).touch() # name and create directories in root directory self.directory_names = ['projects', 'topics', 'research'] for directory_name in self.directory_names: os.mkdir(full_path(self.root_directory, directory_name)) # create files in existing directories for some test cases self.sub_dir_name = random.choice(self.directory_names) self.sub_file_names = ['sub_politics.txt', 'sub_texts.ini'] for sub_file_name in self.sub_file_names: pathlib.Path( full_path(self.root_directory, sub_file_name, self.sub_dir_name)).touch() # create directory to populate and test deletion later self.sub_dir_to_delete = 'users' os.mkdir(full_path(self.root_directory, self.sub_dir_to_delete)) for index in range(1, 10): pathlib.Path( full_path(self.root_directory, '{}.txt'.format(index), self.sub_dir_to_delete)).touch()
def test_file_exist_fail(self): success, error = tshift(full_path(self.root_directory, 'test_4.txt')) self.assertFalse(success) self.assertRegex(error, r"file ([\"'])(?:(?=(\\?))\2.)*?\1 does not exist")