def rename_element(): if request.method == 'POST': project = request.form['project'] elem_type = request.form['elemType'] full_filename = request.form['fullFilename'] new_full_filename = request.form['newFullFilename'] error = '' old_filename, old_parents = utils.separate_file_from_parents( full_filename) new_filename, new_parents = utils.separate_file_from_parents( new_full_filename) if len(new_filename) == 0: error = 'File name cannot be empty' else: for c in new_full_filename.replace('.', ''): if not c.isalnum() and not c in ['-', '_']: error = 'Only letters, numbers, \'-\' and \'_\' are allowed' break dir_type_name = '' if not error: if elem_type == 'test': dir_type_name = 'tests' elif elem_type == 'page': dir_type_name = 'pages' elif elem_type == 'suite': dir_type_name = 'suites' old_path = os.path.join(root_path, 'projects', project, dir_type_name, os.sep.join(old_parents)) new_path = os.path.join(root_path, 'projects', project, dir_type_name, os.sep.join(new_parents)) error = file_manager.rename_file(old_path, old_filename + '.py', new_path, new_filename + '.py') if not error and elem_type == 'test': # try to rename data file in /data/ folder # TODO, data files in /data/ will be deprecated old_path = os.path.join(root_path, 'projects', project, 'data', os.sep.join(old_parents)) new_path = os.path.join(root_path, 'projects', project, 'data', os.sep.join(new_parents)) if os.path.isfile(os.path.join(old_path, old_filename + '.csv')): error = file_manager.rename_file(old_path, old_filename + '.csv', new_path, new_filename + '.csv') # try to rename data file in /tests/ folder old_path = os.path.join(root_path, 'projects', project, 'tests', os.sep.join(old_parents)) new_path = os.path.join(root_path, 'projects', project, 'tests', os.sep.join(new_parents)) if os.path.isfile(os.path.join(old_path, old_filename + '.csv')): error = file_manager.rename_file(old_path, old_filename + '.csv', new_path, new_filename + '.csv') return json.dumps(error)
def test_rename_file_source_does_not_exist(self, dir_function): path = dir_function.path oldpath = os.path.join(path, 'testfile.txt') newpath = os.path.join(path, 'subfolder', 'newtestfile.txt') errors = file_manager.rename_file(oldpath, newpath) assert errors == [f'File {oldpath} does not exist'] assert not os.path.isfile(newpath)
def test_rename_file_source_does_not_exist(self, dir_function): path = dir_function.path filename = 'testfile.txt' new_filename = 'newtestfile.txt' error = file_manager.rename_file(path, filename, path, new_filename) expected_error = ('File {} does not exist'.format( os.path.join(path, filename))) assert error == expected_error
def test_rename_file_destination_exist(self, dir_function): path = dir_function.path filename = 'testfile.txt' new_filename = 'newtestfile.txt' open(filename, 'w').close() open(new_filename, 'w+').close() error = file_manager.rename_file(path, filename, path, new_filename) assert error == 'A file with that name already exists'
def test_rename_file_source_does_not_exist(self, dir_function): path = dir_function.path oldpath = os.path.join(path, 'testfile.txt') newpath = os.path.join(path, 'subfolder', 'newtestfile.txt') errors = file_manager.rename_file(oldpath, newpath) expected_error = ('File {} does not exist'.format(oldpath)) assert errors == [expected_error] assert not os.path.isfile((newpath))
def rename_test(project_name, test_name, new_test_name): errors = [] project = Project(project_name) if test_name not in project.tests(): errors.append('Test {} does not exist'.format(test_name)) else: errors = validate_project_element_name(new_test_name) if not errors: old_path = Test(project_name, test_name).path new_path = Test(project_name, new_test_name).path project.create_packages_for_element(new_test_name, project.file_types.TEST) errors = file_manager.rename_file(old_path, new_path) # try to rename data file in /tests/ folder old_data_path = os.path.splitext(old_path)[0] + '.csv' new_data_path = os.path.splitext(new_path)[0] + '.csv' if os.path.isfile(old_data_path): errors = file_manager.rename_file(old_data_path, new_data_path) return errors
def test_rename_file(self, dir_function): path = dir_function.path oldpath = os.path.join(path, 'testfile.txt') open(oldpath, 'w+').close() newpath = os.path.join(path, 'subfolder', 'newtestfile.txt') errors = file_manager.rename_file(oldpath, newpath) assert errors == [] assert not os.path.isfile(oldpath) assert os.path.isfile(newpath)
def test_rename_file_destination_exist(self, dir_function): path = dir_function.path oldpath = os.path.join(path, 'testfile.txt') open(oldpath, 'w+').close() newpath = os.path.join(path, 'newtestfile.txt') open(newpath, 'w+').close() errors = file_manager.rename_file(oldpath, newpath) assert os.path.isfile(oldpath) assert os.path.isfile(newpath) assert errors == ['A file with that name already exists']
def test_rename_file_destination_exist(self, dir_function): basedir = dir_function.path filename = 'testfile.txt' new_filename = 'newtestfile.txt' open(filename, 'w').close() open(new_filename, 'w+').close() error = file_manager.rename_file(basedir, filename, basedir, new_filename) expected_error = ('File {} already exists'.format( os.path.join(basedir, new_filename))) assert error == expected_error
def test_rename_file(self, dir_function): basedir = dir_function.path filename = 'testfile.txt' open(filename, 'w+').close() new_path = os.path.join(basedir, 'subfolder') new_filename = 'newtestfile.txt' error = file_manager.rename_file(basedir, filename, new_path, new_filename) new_full_path = os.path.join(new_path, new_filename) assert os.path.isfile(new_full_path) assert error == ''
def test_rename_file_destination_exist(self, random_project_fixture): project = random_project_fixture['name'] testdir = random_project_fixture['testdir'] page_object.new_page_object(testdir, project, [], 'page_two') page_object.new_page_object(testdir, project, [], 'destination') base_dir = page_object.pages_base_dir(testdir, project) error = file_manager.rename_file(base_dir, 'page_one.py', base_dir, 'destination.py') expected_error = ('File {} already exists' .format(os.path.join(base_dir, 'destination.py'))) assert error == expected_error
def test_rename_file(self, random_project_fixture): project = random_project_fixture['name'] testdir = random_project_fixture['testdir'] page_object.new_page_object(testdir, project, [], 'page_one') base_dir = page_object.pages_base_dir(testdir, project) new_path = os.path.join(base_dir, 'submodule') error = file_manager.rename_file(base_dir, 'page_one.py', new_path, 'page_one_edit.py') new_full_path = os.path.join(new_path, 'page_one_edit.py') assert os.path.isfile(new_full_path) assert error == ''
def rename_page(project_name, page_name, new_page_name): errors = [] project = Project(project_name) if page_name not in project.pages(): errors.append('Page {} does not exist'.format(page_name)) else: errors = validate_project_element_name(new_page_name) if not errors: old_path = Page(project_name, page_name).path new_path = Page(project_name, new_page_name).path project.create_packages_for_element(new_page_name, project.file_types.PAGE) errors = file_manager.rename_file(old_path, new_path) return errors
def rename_suite(project, suite_name, new_suite_name): errors = [] project_obj = Project(project) if suite_name not in project_obj.suites(): errors.append(f'Suite {suite_name} does not exist') else: errors = validate_project_element_name(new_suite_name) if not errors: old_path = Suite(project, suite_name).path new_path = Suite(project, new_suite_name).path project_obj.create_packages_for_element(new_suite_name, Project.file_types.SUITE) errors = file_manager.rename_file(old_path, new_path) return errors
def rename_page(project_name, page_name, new_page_name): errors = [] project = Project(project_name) new_page_name = new_page_name.strip().replace(' ', '_') if page_name not in project.pages(): errors.append('Page {} does not exist'.format(page_name)) else: errors = validate_project_element_name(new_page_name) if not errors: old_path = Page(project_name, page_name).path new_path = Page(project_name, new_page_name).path project.create_packages_for_element(new_page_name, 'page') errors = file_manager.rename_file(old_path, new_path) return errors
def rename_suite(project, suite_name, new_suite_name): errors = [] project_obj = Project(project) if suite_name not in project_obj.suites(): errors.append('Suite {} does not exist'.format(suite_name)) else: new_suite_name = new_suite_name.strip().replace(' ', '_') errors = validate_project_element_name(new_suite_name) if not errors: old_path = Suite(project, suite_name).path new_path = Suite(project, new_suite_name).path project_obj.create_packages_for_element(new_suite_name, 'suite') errors = file_manager.rename_file(old_path, new_path) return errors