import pytest import os import subprocess def test_new(tmpdir): os.chdir(tmpdir.strpath) subprocess.check_call(["symlink", "source", "link"]) assert os.path.lexists("link") assert os.readlink("link") == "source" def test_absolute(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") subprocess.check_call(["symlink", source, link]) 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") subprocess.check_call(["symlink", source, link, "--relative"]) assert os.path.lexists(link) assert os.readlink(link) == "../source" def test_suitable_link_exists(tmpdir): os.chdir(tmpdir.strpath) subprocess.check_call(["symlink", "source", "link"]) assert os.path.lexists("link") subprocess.check_call(["symlink", "source", "link"]) def test_override_suitable_link(tmpdir, **kwargs): os.chdir(tmpdir.strpath) subprocess.check_call(["symlink", "source", "link"]) assert os.path.lexists("link") subprocess.check_call(["symlink", "--override", "source", "link"]) # 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) subprocess.check_call(["symlink", "source", "link"]) assert os.path.lexists("link") with pytest.raises(subprocess.CalledProcessError): subprocess.check_call(["symlink", "source2", "link"]) def test_dir_exists(tmpdir): os.chdir(tmpdir.strpath) os.mkdir("dir") assert os.path.exists("dir") with pytest.raises(subprocess.CalledProcessError): subprocess.check_call(["symlink", "source", "dir"]) def test_override_link(tmpdir): os.chdir(tmpdir.strpath) subprocess.check_call(["symlink", "source1", "link"]) assert os.path.lexists("link") subprocess.check_call(["symlink", "source2", "link", "--override", "--no-backup"]) 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") subprocess.check_call(["symlink", "source", "link", "--override", "--no-backup"]) 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) subprocess.check_call(["symlink", "source1", "link"]) assert os.path.lexists("link") subprocess.check_call(["symlink", "source2", "link", "--override", "--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") subprocess.check_call(["symlink", "source", "link", "--override", "--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 subprocess.check_call(["symlink", "source1", "link"]) assert os.path.lexists("link") # source 2 subprocess.check_call(["symlink", "source2", "link", "--override", "--backup-suffix", "~"]) assert os.path.lexists("link") assert os.readlink("link") == "source2" assert os.path.lexists("link~") assert os.readlink("link~") == "source1" # source 3 subprocess.check_call(["symlink", "source3", "link", "--override", "--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 subprocess.check_call(["symlink", "source4", "link", "--override", "--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"