コード例 #1
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_write_data_with_handle():
    """Tests writing to a File with a handle passed in."""
    the_file = File(DEFAULT_ARGS)
    mock_handle = MockHandle()
    data = 'Some mock data...'
    the_file.write(data=data, handle=mock_handle)
    assert mock_handle.called_once_with(data)
コード例 #2
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_read_file_with_data(mock_exists):
    """Tests file read() a file that has data altered in memory."""
    mock_exists.return_value = True
    the_file = File(DEFAULT_ARGS)
    the_file.data = 'Test data'
    assert the_file.read() == the_file.data
    assert the_file.read() == 'Test data'
    the_file.data = 'New data'
    assert the_file.read() == the_file.data
    assert the_file.read() == 'New data'
コード例 #3
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_write_data_without_handle():
    """Tests writing to a file without a handle."""
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        the_file = File(DEFAULT_ARGS)
        data = 'Some mock data...'
        assert the_file.write(data, False)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().write.assert_called_once_with(data)
        m_open().close.assert_called_once_with()
コード例 #4
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_create_already_existing_file_replacing(mock_exists, mock_prompt, mock_chmod, mock_chown):
    """Tests file creation of an already existing file (replacing the file)."""
    mock_exists.return_value = True
    mock_chown.return_value = mock_chmod.return_value = True
    mock_prompt.return_value = True # Answer yes, replace
    the_file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        assert the_file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().close.assert_called_once_with()
コード例 #5
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_read_file_force_flush_memory(mock_exists):
    """Tests file read method forcing read from memory."""
    mock_exists.return_value = True
    the_file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        # pylint: disable=no-member
        m_open.return_value.read.return_value = data_on_disk = 'The data on disk..'
        the_file.data = 'The data...'
        assert the_file.read(True) == data_on_disk
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'r')
        m_open().close.assert_called_once_with()
コード例 #6
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_create_and_create_parent_dirs(mock_exists, mock_chmod, mock_chown):
    """Tests file creation while creating parent dirs."""
    mock_parent_dir = MockParentDir(False)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    the_file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        assert the_file.create()
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        m_open().close.assert_called_once_with()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 1
コード例 #7
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_create_with_data(mock_write, mock_exists, mock_chmod, mock_chown):
    """Tests file creation with data."""
    mock_parent_dir = MockParentDir(True)
    mock_exists.return_value = False
    mock_chown.return_value = mock_chmod.return_value = True
    File.parent_dir = mock_parent_dir
    the_file = File(DEFAULT_ARGS)
    m_open = mock_open()
    with patch(BUILTINS + '.open', m_open, create=True):
        data = 'The data...'
        assert the_file.create(data)
        m_open.assert_called_once_with(DEFAULT_ARGS['path'], 'w')
        mock_write.assert_called_once_with(data, False, m_open())
        m_open().close.assert_called_once_with()
        assert mock_parent_dir.exists_called == 1
        assert mock_parent_dir.create_called == 0
コード例 #8
0
ファイル: test_website.py プロジェクト: hbradleyiii/ww
def test_website_pack_unpack():
    """Integration test: initializes, installs, verifies, packs, removes,
    unpacks and removes website."""
    with patch(_INPUT, return_value="y"):
        website = Website("example.com")

    assert not website.is_installed(), "Website 'example.com' should not exist on this server."
    assert not website.verify(), "Verification on a non-existing website should fail."

    with patch(_INPUT, return_value="y"):
        website.install()

    # Create a test file in htdocs
    the_file = File({"path": website.htdocs + "/index.html"})
    the_file.data = "Test file."
    the_file.create()

    # Pack the site
    website.pack()

    website.remove(ask=False)
    assert not website.is_installed()
    assert not website.verify(), "Verification on a non-existing website should fail."
    assert not the_file.exists()

    with patch(_INPUT, return_value="y"):
        website.unpack()

    assert website.is_installed()

    def verify(website):
        """Verify function to wrap with localhost decorator."""
        assert website.verify()

    localhost(verify)(website)
    assert the_file.exists()

    # Remove and check again for good measure
    website.remove(ask=False)
    assert not website.is_installed()
    assert not website.verify(), "Verification on a non-existing website should fail."
    assert not the_file.exists()
コード例 #9
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_create_already_existing_file_not_replacing(mock_exists, mock_prompt):
    """Tests file creation of an already existing file (NOT replacing the file)."""
    mock_exists.return_value = True
    mock_prompt.return_value = False # Answer no, don't replace
    the_file = File(DEFAULT_ARGS)
    assert not the_file.create()
コード例 #10
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_nonexisting_file_read_data(mock_exists):
    """Tests file read() a file that has data altered in memory."""
    mock_exists.return_value = False
    the_file = File(DEFAULT_ARGS)
    the_file.read()
    assert the_file.data == ''
コード例 #11
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_read_nonexisting_file_with_data_in_memory(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    the_file = File(DEFAULT_ARGS)
    the_file.data = 'Data is in memory...'
    assert the_file.read() == 'Data is in memory...'
コード例 #12
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_read_nonexisting_file(mock_exists):
    """Tests File read method."""
    mock_exists.return_value = False
    the_file = File(DEFAULT_ARGS)
    assert the_file.read() == ''
コード例 #13
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_remove(mock_remove, *args):  # pylint: disable=unused-argument
    """Tests File remove method."""
    the_file = File(DEFAULT_ARGS)
    assert the_file.remove(False)
    mock_remove.assert_called_once_with(DEFAULT_ARGS['path'])
コード例 #14
0
ファイル: test_file.py プロジェクト: hbradleyiii/ext_pylib
def test_file_write_no_data():
    """Tests writing to a File with no data passed in."""
    the_file = File(DEFAULT_ARGS)
    with pytest.raises(UnboundLocalError):
        the_file.write()
コード例 #15
0
def test_section_file_apply_to_file():
    """[Integration Test] Test apply_to_file() method of Section class."""
    # Setup a root dir to use to test
    root_dir = Dir({"path": "/tmp/ext_pylib/"})
    assert root_dir.remove(False)  # If it already exists, remove it.
    assert root_dir.create()
    assert root_dir.exists()

    file_without_section = File({"path": "/tmp/ext_pylib/file"})
    assert file_without_section.create()
    assert file_without_section.overwrite(FILE_CONTENTS_WITHOUT_SECTION)
    assert file_without_section.exists()

    class SectionFile(Section, File):
        """Dummy Class extending Section and File."""

    section = SectionFile({"path": "/tmp/ext_pylib/section"})
    assert section.create()
    assert section.overwrite(SECTION_FILE_CONTENTS)
    assert section.exists()

    file_with_section = File({"path": "/tmp/ext_pylib/file_with_section"})
    assert file_with_section.create()
    assert file_with_section.overwrite(FILE_CONTENTS_WITH_SECTION)
    assert file_with_section.exists()

    assert section.is_in(file_with_section.read())
    print("Without:")
    print(file_without_section.read())
    print("Without (Applied):")
    print(section.apply_to(file_without_section.read()))
    print("With:")
    print(file_with_section.read())
    assert section.apply_to(file_without_section.read()) == file_with_section.read()

    # Cleanup
    assert root_dir.remove(False)
    assert not root_dir.exists()