Ejemplo n.º 1
0
def test_source_links():
  for ext in ('.tar.gz', '.tar', '.tgz', '.zip', '.tar.bz2'):
    sl = SourceLink('a_p_r-3.1.3' + ext)
    assert sl._name == 'a_p_r'
    assert sl.name == 'a-p-r'
    assert sl.raw_version == '3.1.3'
    assert sl.version == parse_version(sl.raw_version)
    for req in ('a_p_r', 'a_p_r>2', 'a_p_r>3', 'a_p_r>=3.1.3', 'a_p_r==3.1.3', 'a_p_r>3,<3.5'):
      assert sl.satisfies(req)
      assert sl.satisfies(Requirement.parse(req))
    for req in ('foo', 'a_p_r==4.0.0', 'a_p_r>4.0.0', 'a_p_r>3.0.0,<3.0.3', 'a==3.1.3'):
      assert not sl.satisfies(req)
  sl = SourceLink('python-dateutil-1.5.tar.gz')
  assert sl.name == 'python-dateutil'
  assert sl.raw_version == '1.5'

  with temporary_dir() as td:
    dateutil_base = 'python-dateutil-1.5'
    dateutil = '%s.zip' % dateutil_base
    with contextlib.closing(ZipFile(os.path.join(td, dateutil), 'w')) as zf:
      zf.writestr(os.path.join(dateutil_base, 'file1.txt'), 'junk1')
      zf.writestr(os.path.join(dateutil_base, 'file2.txt'), 'junk2')
    sl = SourceLink('file://' + os.path.join(td, dateutil), opener=Web())
    with temporary_dir() as td2:
      sl.fetch(location=td2)
      print(os.listdir(td2))
      assert set(os.listdir(os.path.join(td2, dateutil_base))) == set(['file1.txt', 'file2.txt'])
Ejemplo n.º 2
0
def test_maybe_local():
  maybe_local = Web().maybe_local_url
  assert maybe_local('http://www.google.com') == 'http://www.google.com'
  assert maybe_local('https://www.google.com/whatever') == 'https://www.google.com/whatever'
  assert maybe_local('tmp/poop.txt') == 'file://' + os.path.realpath('tmp/poop.txt')
  assert maybe_local('/tmp/poop.txt') == 'file://' + os.path.realpath('/tmp/poop.txt')
  assert maybe_local('www.google.com') == 'file://' + os.path.realpath('www.google.com')
Ejemplo n.º 3
0
def test_unreachable_error(create_connection_mock, gethostbyname_mock):
  gethostbyname_mock.return_value = '1.2.3.4'
  create_connection_mock.side_effect = socket.error(errno.ENETUNREACH,
      'Could not reach network.')
  with pytest.raises(FetchError):
    Web().open('http://www.google.com')
  gethostbyname_mock.assert_called_once_with('www.google.com')
Ejemplo n.º 4
0
def test_local_open():
  m = mox.Mox()
  m.StubOutWithMock(urllib_request, 'urlopen')
  urllib_request.urlopen('file:///local/filename').AndReturn('data')
  m.ReplayAll()
  assert Web().open('/local/filename') == 'data'
  m.UnsetStubs()
  m.VerifyAll()
Ejemplo n.º 5
0
def test_open_resolve_failure():
  m = mox.Mox()
  m.StubOutWithMock(socket, 'gethostbyname')
  socket.gethostbyname('www.google.com').AndRaise(
      socket.gaierror(errno.EADDRNOTAVAIL, 'Could not resolve host.'))
  m.ReplayAll()
  with pytest.raises(urllib_error.URLError):
    Web().open('http://www.google.com')
  m.UnsetStubs()
  m.VerifyAll()
Ejemplo n.º 6
0
def test_unreachable_error():
  m = mox.Mox()
  m.StubOutWithMock(socket, 'gethostbyname')
  m.StubOutWithMock(socket, 'create_connection')
  socket.gethostbyname('www.google.com').AndReturn('1.2.3.4')
  socket.create_connection(('www.google.com', 80), timeout=mox.IgnoreArg()).AndRaise(
      socket.error(errno.ENETUNREACH, 'Could not reach network.'))
  m.ReplayAll()
  with pytest.raises(urllib_error.URLError):
    Web().open('http://www.google.com')
  m.UnsetStubs()
  m.VerifyAll()
Ejemplo n.º 7
0
def test_egg_packages():
  el = EggPackage('psutil-0.4.1-py2.6-macosx-10.7-intel.egg')
  assert el.name == 'psutil'
  assert el.raw_version == '0.4.1'
  assert el.py_version == '2.6'
  assert el.platform == 'macosx-10.7-intel'
  for req in ('psutil', 'psutil>0.4', 'psutil==0.4.1', 'psutil>0.4.0,<0.4.2'):
    assert el.satisfies(req)
  for req in ('foo', 'bar==0.4.1'):
    assert not el.satisfies(req)

  el = EggPackage('pytz-2012b-py2.6.egg')
  assert el.name == 'pytz'
  assert el.raw_version == '2012b'
  assert el.py_version == '2.6'
  assert el.platform is None

  # Eggs must have their own version and a python version.
  with pytest.raises(EggPackage.InvalidLink):
    EggPackage('bar.egg')

  with pytest.raises(EggPackage.InvalidLink):
    EggPackage('bar-1.egg')

  with pytest.raises(EggPackage.InvalidLink):
    EggPackage('bar-py2.6.egg')

  dateutil = 'python_dateutil-1.5-py2.6.egg'
  with create_layout([dateutil]) as td:
    el = EggPackage('file://' + os.path.join(td, dateutil), opener=Web())

    with temporary_dir() as td2:
      # local file fetch w/o location will always remain same
      loc1 = el.fetch()
      assert loc1 == os.path.join(td, dateutil)

      el.fetch(location=td2)
      assert os.listdir(td2) == [dateutil]
Ejemplo n.º 8
0
def test_local_open(urlopen_mock):
  urlopen_mock.return_value = b'data'
  assert Web().open('/local/filename') == b'data'
Ejemplo n.º 9
0
def test_open_resolve_failure(gethostbyname_mock):
  gethostbyname_mock.side_effect = socket.gaierror(errno.EADDRNOTAVAIL, 'Could not resolve host.')
  with pytest.raises(FetchError):
    Web().open('http://www.google.com')