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"