def test__invalid_yaml_syntax__raises_config_error(self, mock_load): error_message = 'Some syntax error..' mock_load.side_effect = SyntaxError(error_message) with self.assertRaises(Exception) as cm: config.get() assert_exception(self, cm, config.ConfigError, config._JSON_SYNTAX_ERROR.format(error_message))
def test__config_file_cannot_be_written__raises_config_error(self, mock_create, mock_load): mock_load.side_effect = IOError() error_message = 'Some error..' mock_create.side_effect = IOError(error_message) with self.assertRaises(Exception) as cm: config.get() assert_exception(self, cm, config.ConfigError, config._FILE_CREATION_ERROR.format(error_message))
def test__missing_mandatory_key__raises_config_error( self, mock_validate, mock_load): error_message = 'projects-path' mock_validate.side_effect = KeyError(error_message) with self.assertRaises(Exception) as cm: config.get() assert_exception(self, cm, config.ConfigError, config._MANDATORY_KEY_ERROR.format(error_message))
def test__invalid_value_for_key__raises_config_error(self, mock_validate, mock_load): dummy_config = dict(config._default_config) mock_load.return_value = dummy_config error_message = 'key' mock_validate.side_effect = ValueError(error_message) with self.assertRaises(Exception) as cm: config.get() assert_exception(self, cm, config.ConfigError, config._INVALID_VALUE_ERROR.format(error_message))
def test__config_file_cannot_be_written__raises_config_error( self, mock_create, mock_load): mock_load.side_effect = IOError() error_message = 'Some error..' mock_create.side_effect = IOError(error_message) with self.assertRaises(Exception) as cm: config.get() assert_exception(self, cm, config.ConfigError, config._FILE_CREATION_ERROR.format(error_message))
def test__invalid_key__raises_config_error(self, mock_validate, mock_load): dummy_config = dict(config._default_config) mock_load.return_value = dummy_config error_message = 'invalid-key' mock_validate.side_effect = SyntaxError(error_message) with self.assertRaises(Exception) as cm: config.get() assert_exception(self, cm, config.ConfigError, config._INVALID_KEY_ERROR.format(error_message))
def test__missing_optional_key__gets_replaced_with_the_default_value(self, mock_validate, mock_load): dummy_config = dict(config._default_config) del dummy_config['max-doc-width'] mock_load.return_value = dummy_config expected = 80 result = config.get() self.assertEqual(expected, result['max-doc-width'])
def test__missing_optional_key__gets_replaced_with_the_default_value( self, mock_validate, mock_load): dummy_config = dict(config._default_config) del dummy_config['max-doc-width'] mock_load.return_value = dummy_config expected = 80 result = config.get() self.assertEqual(expected, result['max-doc-width'])
def main(args): try: conf = config.get() except: pass if paths.inside_project(conf['projects-path']): # print('Inside') pass else: # print('Outside') pass
def test__missing_mandatory_key__raises_config_error(self, mock_validate, mock_load): error_message = 'projects-path' mock_validate.side_effect = KeyError(error_message) with self.assertRaises(Exception) as cm: config.get() assert_exception(self, cm, config.ConfigError, config._MANDATORY_KEY_ERROR.format(error_message))
def main(args): signal.signal(signal.SIGTSTP, sigterm_handle) try: conf = config.get() if not os.path.isdir(conf['projects-path']): os.mkdir(conf['projects-path']) print("Projects root was created: {}".format(conf['projects-path'])) print("You can put your projects here.") with open(os.path.join(os.path.expanduser('~'), '.p-path'), 'w+') as f: f.write(conf['projects-path']) return else: if not os.listdir(conf['projects-path']): print("Your projects directory is empty. Nothing to do..") with open(os.path.join(os.path.expanduser('~'), '.p-path'), 'w+') as f: f.write(conf['projects-path']) return args = args[2:] if len(args) == 1: if args[0] in ['-v', '--version']: print(__version__) return elif args[0] in ['-i', '--init']: if paths.inside_project(conf['projects-path']): if os.path.isfile('Projectfile'): print('You already have a Projectfile in this directory.. Nothing to do ;)') else: projectfile_content = projectfile.DEFAULT_PROJECTFILE.format(__version__) with open('Projectfile', 'w+') as f: f.write(projectfile_content) print('Projectfile created. Use the "p" command to invoke the manual.') else: print('You are not inside any of your projects. Use the "p" command to navigate into one.') return elif args[0] in ['-h', '--help']: pydoc.pager(help_text) return elif args[0] in ['-w', '--walk']: if paths.inside_project(conf['projects-path']): print(projectfile.get_walk_order(os.getcwd())) else: print('You are not inside any of your projects. Use the "p" command to navigate into one.') return elif args[0] in ['p']: handle_project_selection(conf) return elif args[0] in ['-l', '--list']: print('Command name missing after this option. Cannot list the command body..\np (-l|--list) <command>') return elif args[0] in ['-md', '--markdown']: project_root = paths.get_project_root(conf['projects-path'], os.getcwd()) data = projectfile.get_data_for_root(project_root['path']) data['name'] = project_root['name'] md_content = gui.generate_markdown(data) with open(os.path.join(project_root['path'], 'README.md'), 'w+') as f: f.write(md_content) print("README.md file was generated into your project's root.") return if len(args) == 2: if args[0] in ['-l', '--list']: command = args[1] project_root = paths.get_project_root(conf['projects-path'], os.getcwd()) data = projectfile.get_data_for_root(project_root['path']) if command in data['commands']: if 'alias' in data['commands'][command]: command = data['commands'][command]['alias'] for line in data['commands'][command]['script']: print(line) else: print('Invalid command: "{}"\nAvailable commands:'.format(command)) for c in data['commands']: print(c) return elif args[0] in ['-md', '--markdown']: name = args[1] project_root = paths.get_project_root(conf['projects-path'], os.getcwd()) data = projectfile.get_data_for_root(project_root['path']) data['name'] = project_root['name'] md_content = gui.generate_markdown(data) with open(os.path.join(project_root['path'], name), 'w+') as f: f.write(md_content) print("A markdown file named \"{}\" was generated into your project's root.".format(name)) return if paths.inside_project(conf['projects-path']): handle_inside_project(args, conf) else: handle_project_selection(conf) except projectfile.error.ProjectfileError as e: error = e.args[0] message = 'Projectfile error!\n{}'.format(error['error']) if 'path' in error: message = '{}\nPath: {}/Projectfile'.format(message, error['path']) if 'line' in error: message = '{}\nLine: {}'.format(message, error['line']) print(colored(message, 'red')) sys.exit(-1) except config.ConfigError as e: error = e.args[0] message = 'Config error!\n{}'.format(error) print(colored(message, 'red')) sys.exit(-1)
def test__loaded_config_gets_validated(self, mock_validate, mock_load): dummy_config = dict(config._default_config) mock_load.return_value = dummy_config config.get() mock_validate.assert_called_with(dummy_config)
def test__config_file_not_exits__creates_new_one(self, mock_create, mock_load): mock_load.side_effect = [IOError(), dict(config._default_config)] config.get() mock_create.assert_called_with()
def test__loaded_config_returned(self, mock_load): dummy_config = dict(config._default_config) mock_load.return_value = dummy_config result = config.get() self.assertEqual(dummy_config, result)
def test__loaded_projects_path_expanded(self, mock_os, mock_load): dummy_config = dict(config._default_config) mock_load.return_value = dummy_config config.get() mock_os.path.expanduser.assert_called_with(dict(config._default_config)['projects-path'])
def test__loaded_projects_path_expanded(self, mock_os, mock_load): dummy_config = dict(config._default_config) mock_load.return_value = dummy_config config.get() mock_os.path.expanduser.assert_called_with( dict(config._default_config)['projects-path'])