123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import pytest
- import osex
- import os
- def test_new(tmpdir):
- os.chdir(tmpdir.strpath)
- osex.symlink("source", "link")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source"
- def test_absolute1(tmpdir):
- os.chdir(tmpdir.strpath)
- os.makedirs(os.path.join("1", "2"))
- link = os.path.join("1", "2", "link")
- source = os.path.join(os.getcwd(), "1", "source")
- osex.symlink(source, link)
- assert os.path.lexists(link)
- assert os.readlink(link) == source
- def test_absolute2(tmpdir):
- os.chdir(tmpdir.strpath)
- os.makedirs(os.path.join("1", "2"))
- link = os.path.join("1", "2", "link")
- source = os.path.join(os.getcwd(), "1", "source")
- osex.symlink(source, link, relative = False)
- assert os.path.lexists(link)
- assert os.readlink(link) == source
- def test_relative(tmpdir):
- os.chdir(tmpdir.strpath)
- os.makedirs(os.path.join("1", "2"))
- link = os.path.join("1", "2", "link")
- source = os.path.join(os.getcwd(), "1", "source")
- osex.symlink(source, link, relative = True)
- assert os.path.lexists(link)
- assert os.readlink(link) == "../source"
- def test_suitable_link_exists(tmpdir):
- os.chdir(tmpdir.strpath)
- osex.symlink("source", "link")
- assert os.path.lexists("link")
- osex.symlink("source", "link")
- osex.symlink("source", "link", override = False)
- assert os.listdir(tmpdir.strpath) == ["link"]
- def test_override_suitable_link(tmpdir):
- os.chdir(tmpdir.strpath)
- osex.symlink("source", "link")
- assert os.path.lexists("link")
- osex.symlink("source", "link", override = True, backup = True)
- # no backup should have been created since the existing link
- # already had the proper target
- assert os.listdir(tmpdir.strpath) == ["link"]
- def test_divergent_link_exists(tmpdir):
- os.chdir(tmpdir.strpath)
- osex.symlink("source1", "link")
- assert os.path.lexists("link")
- with pytest.raises(OSError):
- osex.symlink("source2", "link")
- with pytest.raises(OSError):
- osex.symlink("source3", "link", override = False)
- def test_dir_exists(tmpdir):
- os.chdir(tmpdir.strpath)
- os.mkdir("dir")
- assert os.path.exists("dir")
- with pytest.raises(OSError):
- osex.symlink("source", "dir")
- with pytest.raises(OSError):
- osex.symlink("source", "dir", override = False)
- def test_override_link(tmpdir):
- os.chdir(tmpdir.strpath)
- osex.symlink("source1", "link")
- assert os.path.lexists("link")
- osex.symlink("source2", "link", override = True, backup = False, backup_suffix = "~")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source2"
- assert not os.path.lexists("link~")
- def test_override_dir(tmpdir):
- os.chdir(tmpdir.strpath)
- os.mkdir("link")
- assert os.path.exists("link")
- osex.symlink("source", "link", override = True, backup = False, backup_suffix = "-")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source"
- assert not os.path.isdir("link-")
- def test_backup_link(tmpdir):
- os.chdir(tmpdir.strpath)
- osex.symlink("source1", "link")
- assert os.path.lexists("link")
- osex.symlink("source2", "link", override = True, backup = True, backup_suffix = "~")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source2"
- assert os.path.lexists("link~")
- assert os.readlink("link~") == "source1"
- def test_backup_dir(tmpdir):
- os.chdir(tmpdir.strpath)
- os.mkdir("link")
- assert os.path.exists("link")
- osex.symlink("source", "link", override = True, backup = True, backup_suffix = "-")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source"
- assert os.path.isdir("link-")
- def test_backup_multiple_links(tmpdir):
- os.chdir(tmpdir.strpath)
- # source 1
- osex.symlink("source1", "link")
- assert os.path.lexists("link")
- # source 2
- osex.symlink("source2", "link", override = True, backup = True, backup_suffix = "~")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source2"
- assert os.path.lexists("link~")
- assert os.readlink("link~") == "source1"
- # source 3
- osex.symlink("source3", "link", override = True, backup = True, backup_suffix = "~")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source3"
- assert os.path.lexists("link~")
- assert os.readlink("link~") == "source1"
- assert os.path.lexists("link~1")
- assert os.readlink("link~1") == "source2"
- # source 4
- osex.symlink("source4", "link", override = True, backup = True, backup_suffix = "~")
- assert os.path.lexists("link")
- assert os.readlink("link") == "source4"
- assert os.path.lexists("link~")
- assert os.readlink("link~") == "source1"
- assert os.path.lexists("link~1")
- assert os.readlink("link~1") == "source2"
- assert os.path.lexists("link~2")
- assert os.readlink("link~2") == "source3"
|