def test_evaluate_command_line_arguments_when_blank_source_export_in_args(
        caplog):
    config.yanom_globals.logger_level = logging.DEBUG
    args = {'silent': False, 'ini': False, 'source': '', 'export': ''}
    cd = config_data.ConfigData(f"{config.yanom_globals.data_dir}/config.ini",
                                'gfm',
                                allow_no_value=True)
    nc = notes_converter.NotesConvertor(args, cd)
    nc.conversion_settings = conversion_settings.ConversionSettings()

    with patch(
            'notes_converter.NotesConvertor.configure_for_ini_settings',
            spec=True,
    ) as mock_configure_for_ini_settings:
        with patch(
                'notes_converter.NotesConvertor.run_interactive_command_line_interface',
                spec=True,
        ) as mock_run_interactive_command_line_interface:
            caplog.clear()
            nc.evaluate_command_line_arguments()

            mock_configure_for_ini_settings.assert_called_once()
            mock_run_interactive_command_line_interface.assert_called_once()

            assert nc.conversion_settings.export_folder == 'notes'
            assert nc.conversion_settings.source == ''

            assert 'Starting interactive command line tool' in caplog.messages
def test_evaluate_command_line_arguments_when_will_be_interactive_command_line_used(
        caplog):
    test_source_path = str(Path(__file__).parent.absolute())
    config.yanom_globals.logger_level = logging.DEBUG
    args = {
        'silent': False,
        'ini': False,
        'source': test_source_path,
        'export': 'hello'
    }
    cd = config_data.ConfigData(f"{config.yanom_globals.data_dir}/config.ini",
                                'gfm',
                                allow_no_value=True)
    nc = notes_converter.NotesConvertor(args, cd)
    nc.conversion_settings = conversion_settings.ConversionSettings()

    with patch(
            'notes_converter.NotesConvertor.configure_for_ini_settings',
            spec=True,
    ) as mock_configure_for_ini_settings:
        with patch(
                'notes_converter.NotesConvertor.run_interactive_command_line_interface',
                spec=True,
        ) as mock_run_interactive_command_line_interface:
            caplog.clear()
            nc.evaluate_command_line_arguments()

            mock_configure_for_ini_settings.assert_called_once()
            mock_run_interactive_command_line_interface.assert_called_once()

            assert nc.conversion_settings.export_folder == Path('hello')
            assert nc.conversion_settings.source == Path(test_source_path)

            assert 'Starting interactive command line tool' in caplog.messages
def test_evaluate_command_line_arguments_when_going_to_use_ini_file(
        caplog, silent, ini):
    test_source_path = str(Path(__file__).parent.absolute())
    config.yanom_globals.logger_level = logging.DEBUG
    args = {
        'silent': silent,
        'ini': ini,
        'source': test_source_path,
        'export': 'hello'
    }
    cd = config_data.ConfigData(f"{config.yanom_globals.data_dir}/config.ini",
                                'gfm',
                                allow_no_value=True)
    nc = notes_converter.NotesConvertor(args, cd)
    nc.conversion_settings = conversion_settings.ConversionSettings()

    with patch(
            'notes_converter.NotesConvertor.configure_for_ini_settings',
            spec=True,
    ) as mock_configure_for_ini_settings:
        with patch(
                'notes_converter.NotesConvertor.run_interactive_command_line_interface',
                spec=True,
        ) as mock_run_interactive_command_line_interface:
            caplog.clear()
            nc.evaluate_command_line_arguments()

            mock_configure_for_ini_settings.assert_called_once()
            mock_run_interactive_command_line_interface.assert_not_called()
def test_process_files_copy_attachments_source_and_export_same_folder(
        tmp_path, silent):
    config.yanom_globals.is_silent = silent
    args = {'source': tmp_path}

    cd = config_data.ConfigData(f"{config.yanom_globals.data_dir}/config.ini",
                                'gfm',
                                allow_no_value=True)
    nc = notes_converter.NotesConvertor(args, cd)
    nc.conversion_settings = conversion_settings.ConversionSettings()
    nc.conversion_settings.export_folder = Path(tmp_path)

    touch(Path(tmp_path, 'file1.html'))
    file1_content = '<a href="attachments/a-file.pdf">an attachment</a>'
    Path(tmp_path, 'file1.html').write_text(file1_content)
    Path(tmp_path, 'attachments').mkdir()
    Path(tmp_path, 'attachments', 'a-file.pdf').touch()

    nc.conversion_settings._source = Path(tmp_path, 'file1.html')
    nc.conversion_settings._source_absolute_root = Path(tmp_path)

    files_to_convert = [Path(tmp_path, 'file1.html')]
    file_converter = file_converter_HTML_to_MD.HTMLToMDConverter(
        nc.conversion_settings, files_to_convert)

    nc.process_files(files_to_convert, file_converter)

    assert nc._note_page_count == 1
    assert Path(tmp_path, 'file1.md').exists()
def test_exit_if_no_files_found_with_no_file(tmp_path, caplog, silent_mode,
                                             expected_out):
    config.yanom_globals.is_silent = silent_mode
    test_source_path = tmp_path
    args = {'source': test_source_path}
    touch(Path(tmp_path, 'file1.html'))
    cd = config_data.ConfigData(f"{config.yanom_globals.data_dir}/config.ini",
                                'gfm',
                                allow_no_value=True)
    nc = notes_converter.NotesConvertor(args, cd)
    nc.conversion_settings = conversion_settings.ConversionSettings()
    nc.conversion_settings._source = Path(tmp_path)

    files_to_convert = None
    extension = 'html'

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        caplog.clear()
        nc.exit_if_no_files_found(files_to_convert, extension)

    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 0

    assert len(caplog.records) == 1
    assert expected_out in caplog.records[0].message
def test_convert_notes_nsx_file_type(tmp_path, capsys, caplog):
    test_source_path = tmp_path
    input_file = 'file1.nsx'
    args = {'silent': True, 'ini': False, 'source': test_source_path}
    touch(Path(tmp_path, input_file))
    cd = config_data.ConfigData(f"{config.yanom_globals.data_dir}/config.ini",
                                'gfm',
                                allow_no_value=True)
    nc = notes_converter.NotesConvertor(args, cd)
    nc.conversion_settings = conversion_settings.ConversionSettings()
    nc.conversion_settings._source = Path(tmp_path)
    nc.conversion_settings._source_absolute_root = Path(tmp_path)
    nc.conversion_settings.conversion_input = 'nsx'

    with patch('notes_converter.NotesConvertor.process_nsx_files',
               spec=True) as mock_process_nsx_files:
        with patch(
                'notes_converter.NotesConvertor.evaluate_command_line_arguments',
                spec=True):
            caplog.clear()
            nc.convert_notes()

    mock_process_nsx_files.assert_called_once()
    assert len(nc._nsx_backups) == 1

    captured = capsys.readouterr()

    assert 'Processing Completed' in caplog.records[-1].message
    assert 'Found pandoc' in captured.out
def test_convert_html_rename_existing_file(tmp_path):
    test_source_path = tmp_path
    args = {'source': test_source_path}
    cd = config_data.ConfigData(f"{config.yanom_globals.data_dir}/config.ini",
                                'gfm',
                                allow_no_value=True)
    nc = notes_converter.NotesConvertor(args, cd)
    nc.conversion_settings = conversion_settings.ConversionSettings()
    nc.conversion_settings.orphans = 'orphan'
    nc.conversion_settings._working_directory = Path(tmp_path)
    nc.conversion_settings.export_folder = Path(tmp_path, 'notes')
    nc.conversion_settings.orphans = 'orphan'

    touch(Path(tmp_path, 'file1.html'))
    # add link to existing file so it does not get moved to orphans
    content_for_source = f'<a href="file1.md">existing file</a><a href="{Path(tmp_path, "notes", "file1.md")}">existing file</a>'
    Path(tmp_path, 'file1.html').write_text(content_for_source)
    # create the notes export folder and create the existing file
    Path(tmp_path, 'notes').mkdir()
    touch(Path(tmp_path, 'notes', 'file1.md'))

    nc.conversion_settings._source = Path(tmp_path)
    nc.conversion_settings._source_absolute_root = Path(tmp_path)

    nc.convert_html()

    assert Path(tmp_path, 'file1.html').exists()
    assert Path(tmp_path, 'notes', 'file1.md').exists()
    assert Path(tmp_path, 'notes', 'file1-old-1.md').exists()
    assert Path(tmp_path, 'notes', 'file1-old-1.md').stat().st_size == 0
    assert Path(tmp_path, 'notes', 'file1.md').stat().st_size > 0
def test_convert_notes(tmp_path, input_file, file_converter_type,
                       export_format, conversion_input):
    test_source_path = tmp_path
    args = {'silent': True, 'ini': False, 'source': test_source_path}
    touch(Path(tmp_path, input_file))
    nc = notes_converter.NotesConvertor(args, 'config_data')
    nc.conversion_settings = conversion_settings.ConversionSettings()
    nc.conversion_settings._source = Path(tmp_path)
    nc.conversion_settings._working_directory = Path(tmp_path)
    nc.conversion_settings._source_absolute_root = Path(tmp_path)
    nc.conversion_settings.export_format = export_format
    nc.conversion_settings.conversion_input = conversion_input

    with patch('notes_converter.NotesConvertor.process_files',
               spec=True) as mock_process_files:
        with patch('notes_converter.NotesConvertor.evaluate_command_line_arguments', spec=True) \
                as mock_evaluate_command_line_arguments:
            nc.convert_notes()

            mock_evaluate_command_line_arguments.assert_called_once()
            mock_evaluate_command_line_arguments.assert_called_once()
            mock_process_files.assert_called_once()
            args = mock_process_files.call_args.args
            assert args[0] == {Path(tmp_path, input_file)}
            assert isinstance(args[1], file_converter_type)
Beispiel #9
0
def test_export_folder_setting_default_data_dir(tmp_path):
    cs = conversion_settings.ConversionSettings()
    cs.working_directory = tmp_path
    Path(tmp_path, config.yanom_globals.data_dir, ).mkdir(parents=True)
    cs.export_folder = str(Path(tmp_path, config.yanom_globals.data_dir))

    assert cs.export_folder == Path(tmp_path, config.yanom_globals.data_dir)
    assert cs.export_folder_absolute == Path(tmp_path, config.yanom_globals.data_dir)
Beispiel #10
0
def test_export_folder_setting_absolute_path_not_in_data_dir(tmp_path):
    cs = conversion_settings.ConversionSettings()
    cs.working_directory = tmp_path
    Path(tmp_path.parent, "somewhere_else/my-target").mkdir(parents=True)
    cs.export_folder = str(Path(tmp_path.parent, "somewhere_else/my-target"))

    assert cs.export_folder == Path(tmp_path.parent, "somewhere_else/my-target")
    assert cs.export_folder_absolute == Path(tmp_path.parent, "somewhere_else/my-target")
Beispiel #11
0
def test_source_setting_valid_absolute_path_not_in_data_dir(tmp_path):
    cs = conversion_settings.ConversionSettings()
    cs.working_directory = tmp_path
    Path(tmp_path.parent, "somewhere_else/my-source").mkdir(parents=True)
    cs.source = str(Path(tmp_path.parent, "somewhere_else/my-source"))

    assert cs.source == Path(tmp_path.parent, "somewhere_else/my-source")
    assert cs._source_absolute_root == Path(tmp_path.parent, "somewhere_else/my-source")
Beispiel #12
0
def test_source_setting_valid_absolute_path(tmp_path):
    cs = conversion_settings.ConversionSettings()
    cs.working_directory = tmp_path
    Path(tmp_path, config.yanom_globals.data_dir, "my-source").mkdir(parents=True)
    cs.source = str(Path(tmp_path, config.yanom_globals.data_dir, "my-source"))

    assert cs.source == Path("my-source")
    assert cs._source_absolute_root == Path(tmp_path, config.yanom_globals.data_dir, "my-source")
Beispiel #13
0
def test_metadata_schema_invalid_value(caplog):
    cs = conversion_settings.ConversionSettings()
    cs.metadata_schema = 1

    assert len(caplog.records) > 0

    for record in caplog.records:
        assert record.levelname == "WARNING"
Beispiel #14
0
def test_attachment_export_folder_setter(string_to_test, expected, tmp_path):
    cs = conversion_settings.ConversionSettings()
    cs._working_directory = tmp_path
    export_path = Path(string_to_test)
    cs.export_folder = export_path

    assert cs.export_folder == Path(expected)
    assert cs.export_folder_absolute == Path(tmp_path, config.yanom_globals.data_dir, expected)
Beispiel #15
0
def test_set_common_quick_settings_defaults_for_nsx_input():
    cs = conversion_settings.ConversionSettings()
    cs.conversion_input = 'nsx'
    cs.metadata_schema = ['hello']

    cs.set_common_quick_settings_defaults()

    assert cs.metadata_schema == ['title', 'ctime', 'mtime', 'tag']
Beispiel #16
0
def test_source_setting_empty_string_data_dir_not_exist(tmp_path):
    cs = conversion_settings.ConversionSettings()
    cs.working_directory = tmp_path

    # THis will raise error as dat dir does not exist.

    # set source with blank entry and test is using default data dir
    with pytest.raises(SystemExit):
        cs.source = ''
Beispiel #17
0
def test_unrecognised_tag_format_setter_invalid_value():
    cs = conversion_settings.ConversionSettings()
    cs.unrecognised_tag_format = 'html'
    with pytest.raises(ValueError) as exc:
        cs.unrecognised_tag_format = 'invalid value'

    assert 'Invalid value provided for for unrecognised tag format option. Attempted to use invalid value -' in exc.value.args[0]

    assert cs.unrecognised_tag_format == 'html'
Beispiel #18
0
def test_keep_nimbus_row_and_column_headers_setter():
    cs = conversion_settings.ConversionSettings()
    cs._keep_nimbus_row_and_column_headers = True

    assert cs.keep_nimbus_row_and_column_headers

    cs.keep_nimbus_row_and_column_headers = False

    assert not cs.keep_nimbus_row_and_column_headers
Beispiel #19
0
def test_orphans_setter_invalid_value():
    cs = conversion_settings.ConversionSettings()
    cs.orphans = 'ignore'
    with pytest.raises(ValueError) as exc:
        cs.orphans = 'invalid value'

    assert 'Invalid value provided for for orphan file option. Attempted to use invalid value -' in exc.value.args[0]

    assert cs.orphans == 'ignore'
Beispiel #20
0
def test_exit_if_path_is_invalid(tmp_path, capsys, silent, expected_screen_output):
    config.yanom_globals.is_silent = silent
    cs = conversion_settings.ConversionSettings()

    with pytest.raises(SystemExit):
        cs.exit_if_path_is_invalid(str(Path(tmp_path, "no\0where")), str(Path(tmp_path, "no:where")))

    captured = capsys.readouterr()
    assert expected_screen_output in captured.out
Beispiel #21
0
def test_read_invalid_settings_from_dictionary(caplog, silent):
    cs = conversion_settings.ConversionSettings()
    config.yanom_globals.is_silent = silent
    cs.set_from_dictionary({'invalid': True})

    assert len(caplog.records) > 0

    for record in caplog.records:
        assert record.levelname == "WARNING"
Beispiel #22
0
def test_front_matter_setter_invalid():
    cs = conversion_settings.ConversionSettings()
    cs.front_matter_format = 'toml'
    with pytest.raises(ValueError) as exc:
        cs.front_matter_format = 'invalid'

    assert 'Invalid value provided for for front matter format. ' in exc.value.args[0]

    assert cs.front_matter_format == 'toml'
Beispiel #23
0
def test_set_common_quick_settings_defaults_for_nimbus_input():
    cs = conversion_settings.ConversionSettings()
    cs.conversion_input = 'nimbus'
    cs.metadata_schema = ['hello']

    cs.set_common_quick_settings_defaults()

    assert cs.metadata_schema == ['title', 'tag']
    assert cs.first_column_as_header is False
Beispiel #24
0
def test_checklist_ptr_processing_nsx_to_html_with_nsx_html_markdown_output(
        html, expected):
    checklist_processor = checklist_processing.NSXInputHTMLOutputChecklistProcessor(
        html)
    cs = conversion_settings.ConversionSettings()

    result = checklist_processor.processed_html

    assert result == expected
Beispiel #25
0
def test_export_folder_setting_empty_string(tmp_path):
    cs = conversion_settings.ConversionSettings()
    cs.working_directory = tmp_path
    cs.export_folder = ''

    assert cs.export_folder == Path(config.yanom_globals.default_export_folder)
    assert cs.export_folder_absolute == Path(tmp_path,
                                             config.yanom_globals.data_dir,
                                             config.yanom_globals.default_export_folder
                                             )
Beispiel #26
0
def test_export_format_setter_invalid_value():
    cs = conversion_settings.ConversionSettings()
    cs.export_format = 'html'

    with pytest.raises(ValueError) as exc:
        cs.export_format = 'invalid'

    assert 'Invalid value provided for for export format. ' in exc.value.args[0]

    assert cs.export_format == 'html'
Beispiel #27
0
def test_quick_setting_setter_invalid_value():
    cs = conversion_settings.ConversionSettings()
    cs.quick_setting = 'obsidian'

    with pytest.raises(ValueError) as exc:
        cs.quick_setting = 'invalid'

    assert 'Invalid value provided for for quick setting. ' in exc.value.args[0]

    assert cs.quick_setting == 'obsidian'
Beispiel #28
0
 def __init__(self):
     self.attachments = {}
     self.attachment_count = 0
     self.nsx_file = 'hello'
     self.note_json = {}
     self.notebook_folder_name = 'notebook_folder_name'
     self.conversion_settings = conversion_settings.ConversionSettings()
     self.image_count = 0
     self.title = 'title'
     self.parent_notebook = 'parent'
Beispiel #29
0
def test_markdown_conversion_input_setter_invalid_value():
    cs = conversion_settings.ConversionSettings()
    cs.markdown_conversion_input = 'gfm'

    with pytest.raises(ValueError) as exc:
        cs.markdown_conversion_input = 'invalid'

    assert 'Invalid value provided for for markdown conversion input. ' in exc.value.args[0]

    assert cs.markdown_conversion_input == 'gfm'
Beispiel #30
0
def test_read_settings_from_dictionary():
    cs = conversion_settings.ConversionSettings()
    cs.attachment_folder_name = 'old_folder_name'
    cs._creation_time_in_exported_file_name = False
    settings_dict = {'attachment_folder_name': 'new_folder_name', 'creation_time_in_exported_file_name': True}

    cs.set_from_dictionary(settings_dict)

    assert cs.attachment_folder_name == Path('new_folder_name')
    assert cs._creation_time_in_exported_file_name