Skip to content

Commit

Permalink
Fix broken funcs related to repo urls on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jan 30, 2014
1 parent 7ea6c9c commit 01dd6d4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/changes.rst
Expand Up @@ -21,9 +21,10 @@ To be released.
module: :func:`~libearth.parser.autodiscovery.get_format()`.
- Added :attr:`Link.html <libearth.feed.Link.html>` property.
- Added :attr:`LinkList.permalink <libearth.feed.LinkList.permalink>` property.
- Fix a :class:`~libearth.repository.FileSystemRepository` bug that conflicts
- Fixed a :class:`~libearth.repository.FileSystemRepository` bug that conflicts
reading buffer and emits broken mixed bytes when there are simultaneous
readings and writings to the same key.
- Fixed broken functions related to repository urls on Windows.


Version 0.1.2
Expand Down
14 changes: 13 additions & 1 deletion libearth/repository.py
Expand Up @@ -43,6 +43,7 @@
import os.path
import pipes
import shutil
import sys
import tempfile
import threading
try:
Expand Down Expand Up @@ -319,7 +320,14 @@ def from_url(cls, url):
elif url.netloc or url.params or url.query or url.fragment:
raise ValueError('file:// must not contain any host/port/user/'
'password/parameters/query/fragment')
return cls(url.path)
if sys.platform == 'win32':
if not url.path.startswith('/'):
raise ValueError('invalid file path: ' + repr(url.path))
parts = url.path.lstrip('/').split('/')
path = os.path.join(parts[0] + os.path.sep, *parts[1:])
else:
path = url.path
return cls(path)

def __init__(self, path, mkdir=True, atomic=IRON_PYTHON):
if not os.path.exists(path):
Expand All @@ -342,6 +350,10 @@ def __init__(self, path, mkdir=True, atomic=IRON_PYTHON):

def to_url(self, scheme):
super(FileSystemRepository, self).to_url(scheme)
if sys.platform == 'win32':
drive, path = os.path.splitdrive(self.path)
path = '/'.join(path.lstrip(os.path.sep).split(os.path.sep))
return '{0}:///{1}/{2}'.format(scheme, drive, path)
return '{0}://{1}'.format(scheme, self.path)

def read(self, key):
Expand Down
33 changes: 31 additions & 2 deletions tests/repository_test.py
@@ -1,5 +1,6 @@
import itertools
import os.path
import sys
import tempfile
import threading
try:
Expand Down Expand Up @@ -61,8 +62,9 @@ def test_not_implemented_error():
assert r2.list(['key']) == frozenset()


@mark.skipif('sys.platform == "win32"', reason='POSIX path test')
@mark.parametrize('without_pkg_resources', [True, False])
def test_from_url(without_pkg_resources, tmpdir, monkeypatch):
def test_from_url__posix(without_pkg_resources, tmpdir, monkeypatch):
if without_pkg_resources and not IRON_PYTHON:
monkeypatch.delattr('pkg_resources.iter_entry_points')
url = 'file://' + str(tmpdir)
Expand All @@ -73,7 +75,22 @@ def test_from_url(without_pkg_resources, tmpdir, monkeypatch):
from_url('unregistered-scheme://')


def test_file_from_to_url(tmpdir):
@mark.skipif('sys.platform != "win32"', reason='Windows path test')
@mark.parametrize('without_pkg_resources', [True, False])
def test_from_url__windows(without_pkg_resources, tmpdir, monkeypatch):
if without_pkg_resources and not IRON_PYTHON:
monkeypatch.delattr('pkg_resources.iter_entry_points')
url_tail = '/'.join(str(tmpdir).split(tmpdir.sep))
url = 'file:///' + url_tail
fs = from_url(url)
assert isinstance(fs, FileSystemRepository)
assert fs.path == str(tmpdir)
with raises(LookupError):
from_url('unregistered-scheme://')


@mark.skipif('sys.platform == "win32"', reason='POSIX path test')
def test_file_from_to_url__posix(tmpdir):
url = 'file://' + str(tmpdir)
parsed = urlparse.urlparse(url)
fs = FileSystemRepository.from_url(parsed)
Expand All @@ -83,6 +100,18 @@ def test_file_from_to_url(tmpdir):
assert fs.to_url('fs') == 'fs://' + str(tmpdir)


@mark.skipif('sys.platform != "win32"', reason='Windows path test')
def test_file_from_to_url__windows(tmpdir):
url_tail = '/'.join(str(tmpdir).split(tmpdir.sep))
url = 'file:///' + url_tail
parsed = urlparse.urlparse(url)
fs = FileSystemRepository.from_url(parsed)
assert isinstance(fs, FileSystemRepository)
assert fs.path == str(tmpdir)
assert fs.to_url('file') == url
assert fs.to_url('fs') == 'fs:///' + url_tail


def test_file_read(tmpdir):
f = FileSystemRepository(str(tmpdir))
with raises(RepositoryKeyError):
Expand Down

0 comments on commit 01dd6d4

Please sign in to comment.