def test_disabled_errors_are_parsed_from_config_file(tmpdir: py.path.local): """Test that when an error is disbled in the config file it gets parsed correctly TODO: parametrize test """ config_file = tmpdir.join('config_file.toml') config_file.write(""" [check] disable_math_error = true """) c = Config() opts = c.parse( [f'--config-file={config_file.strpath}', 'check', 'some_docs'], ) assert 'disabled_errors' in opts['check'] assert len(opts['check']['disabled_errors']) == len(ALL_ERRORS) found = False for error in opts['check']['disabled_errors']: if error['dest'] == 'd-math-error': found = True assert error['value'] == True break # Makes sure that the error whas actually present assert found
def test_gen_argparse_args(options: dict, expected: tuple): """Test that _gen_argparse_args generates the correct arguments""" c = Config() args, kwargs = c._gen_argparse_args(options) assert args == expected[0] assert kwargs == expected[1]
def test_config_file_flag_sets_the_config_file(tmpdir: py.path.local): """Test that the --config-file sets the internal config file path""" config_file = tmpdir.join('test.toml') c = Config() c.parse([f'--config-file={config_file.strpath}', 'check', 'some_docs']) assert c._config_file_path == str(config_file.strpath)
def test_default_config_file_locations_are_used(tmpdir: py.path.local): config_file = tmpdir.join('.notesystem.toml') config_file.write(""" [general] no_visual=true """) folder_to_check = tmpdir.strpath c = Config('.notesystem.toml', [folder_to_check]) opts = c.parse(('check', 'docs')) assert opts['general']['no_visual']['value'] == True
def test_correct_mode_returned(): """Test that the correct mode is returned""" c = Config() cov_ret = c.parse(['convert', 'in', 'uot']) assert 'general' in cov_ret assert 'convert' in cov_ret assert 'check' not in cov_ret check_ret = c.parse(['check', 'in']) assert 'general' in check_ret assert 'check' in check_ret assert 'convert' not in check_ret
def test_commandline_arguments_overwrite_config_file( tmpdir: py.path.local, config_file_content: str, args: tuple, section_option_value: tuple, ): config_file = tmpdir.join('config.toml') config_file.write(config_file_content) c = Config() largs: List[str] = list(args) largs[0] = f'--config-file={config_file.strpath}' opt = c.parse(largs) section = section_option_value[0] option = section_option_value[1] value = section_option_value[2] assert opt[section][option]['value'] == value
def test_not_known_options_are_ingored(tmpdir): config_file = tmpdir.join('cf.toml') config_file.write(""" [general] verbose=true does_not_exist = true """) c = Config() c._config_file_path = config_file.strpath c._parse_config_file() found_existing = False for o in c.OPTIONS['general']: assert o != 'does_not_exist' if o == 'verbose': found_existing = True # Makes sure it parses correctly aka finds availible options assert found_existing
def test_option_lists_raises_assertion_error(): """Test that _gen_argparse_args raises an assertion error when there is no group_name and group_desc (which are required to create a subparser) """ c = Config() c.OPTIONS['check']['test_options'] = [ { 'value': None, 'flags': ['--test-flag'], 'help': 'this is some help', 'dest': 'test-dest', }, { 'value': None, 'flags': ['--test-flag-2'], 'help': 'this is some help', 'dest': 'test-dest-2', }, ] with pytest.raises(AssertionError): _ = c._create_parser()
def test_disabled_errors_are_parsed_from_the_command_lined(): """Test that when an error is disabled using a flag it gets parsed correctly. TODO: parametrize test """ c = Config() opts = c.parse(['check', 'some_docs', '--disable-math-error']) assert 'disabled_errors' in opts['check'] assert len(opts['check']['disabled_errors']) == len(ALL_ERRORS) found = False for error in opts['check']['disabled_errors']: if error['dest'] == 'd-math-error': found = True assert error['value'] == True break # Makes sure that the error whas actually present assert found pass
def test_config_file_parses_config_file( tmpdir: py.path.local, config_file_content: str, mode: str, section_option_value: List[tuple], ): """Test that the options set in the config file are added to the options""" config_file = tmpdir.join('config.toml') config_file.write(config_file_content) c = Config() if mode == 'convert': opt = c.parse( [f'--config-file={config_file.strpath}', mode, 'some_docs', 'out' ], ) else: opt = c.parse( [f'--config-file={config_file.strpath}', mode, 'some_docs'], ) for sov in section_option_value: section = sov[0] option = sov[1] value = sov[2] assert opt[section][option]['value'] == value
def test_throw_error_when_no_mode(): """Test that SystemExit is thrown when no mode is given""" c = Config() with pytest.raises(SystemExit): c.parse((''))
def test_disabled_errors_are_in_options(): """Test that ALL_ERRORS are pressent in the options dict""" c = Config() assert len(c.OPTIONS['check']['disabled_errors']) == len(ALL_ERRORS)
def main(argv: Optional[Sequence[str]] = None): if argv is None: argv = sys.argv[1:] # Create logger logging.basicConfig( format='[%(levelname)s:%(name)s] @ %(asctime)s: %(message)s', datefmt='%H:%M:%S', ) logger = logging.getLogger(__name__) logger.setLevel(logging.ERROR) c = Config() config = c.parse(argv) # Set the general settings if config['general']['verbose']['value']: logging.getLogger().setLevel(logging.INFO) mode: Optional[BaseMode] = None options: Optional[ModeOptions] = None if 'check' in config: # start check mode mode = CheckMode() disabled_errors = [] for disabled_error in config['check']['disabled_errors']: if disabled_error['value'] == True: disabled_errors.append(disabled_error['dest'][2:]) options = { 'visual': not config['general']['no_visual']['value'], 'args': { 'in_path': config['check']['in_path']['value'], 'fix': config['check']['fix']['value'], 'simple_errors': config['check']['simple_errors']['value'], 'disabled_errors': disabled_errors, }, } elif 'convert' in config: # start convert mode mode = ConvertMode() pandoc_options: PandocOptions = { 'arguments': config['convert']['pandoc_args']['value'], 'template': config['convert']['pandoc_template']['value'], 'output_format': ( 'pdf' if config['convert']['to_pdf']['value'] else 'html' ), 'ignore_warnings': config['convert']['ignore_warnings']['value'], } options = { 'visual': not config['general']['no_visual']['value'], 'args': { 'in_path': config['convert']['in_path']['value'], 'out_path': config['convert']['out_path']['value'], 'watch': config['convert']['watch']['value'], 'pandoc_options': pandoc_options, }, } elif 'search' in config: mode = SearchMode() search_args = { 'pattern': config['search']['pattern']['value'], 'path': config['search']['path']['value'], 'tag_str': config['search']['tags']['value'], 'tag_delimiter': config['search']['tag_delimiter']['value'], 'topic': config['search']['topic']['value'], 'case_insensitive': config['search']['case_insensitive']['value'], 'title': config['search']['title']['value'], 'full_path': config['search']['full_path']['value'], } options = { # TODO: Extract visual into variable 'visual': not config['general']['no_visual']['value'], 'args': search_args, } elif 'upload' in config: mode = UploadMode() upload_args = { 'path': config['upload']['path']['value'], 'url': config['upload']['url']['value'], 'username': config['upload']['username']['value'], 'save_credentials': config['upload']['save_credentials']['value'], } options = { 'visual': not config['general']['no_visual']['value'], 'args': upload_args, } else: raise SystemExit(1) mode.start(options)