test_dvcs.py 2.76 KB
Newer Older
1 2 3 4
import os

import pytest

Loïc Dachary's avatar
Loïc Dachary committed
5
from . import factory
Loïc Dachary's avatar
Loïc Dachary committed
6 7 8 9 10


@pytest.fixture
def dvcs(dvcs_factory, tmpdir):
    (DVCS, repository) = dvcs_factory
11 12
    directory = f"{tmpdir}/testrepository"
    origin = f"{tmpdir}/origin"
13
    repository(origin).prepare_for_test()
14 15 16 17 18 19
    return DVCS(dir=directory, url=origin)


def test_dvcs_properties(dvcs):
    assert dvcs.directory
    assert dvcs.url
20 21
    assert dvcs.clone("master") is True
    assert dvcs.hash
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39


def test_dvcs_clone_master(dvcs):
    assert dvcs.clone("master") is True
    assert os.path.exists(f"{dvcs.directory}/README.md")
    assert dvcs.clone("master") is False


def test_dvcs_clone_not_master(dvcs):
    assert dvcs.clone("other") is True
    assert not os.path.exists(f"{dvcs.directory}/README.md")
    assert dvcs.clone("other") is False


def test_dvcs_commit(dvcs, tmpdir):
    assert dvcs.clone("master") is True
    content = "C"
    open(f"{dvcs.directory}/README.md", "w").write(content)
Loïc Dachary's avatar
Loïc Dachary committed
40
    dvcs.commit("message")
41 42 43 44 45 46 47 48
    dvcs.push("master")

    DVCS = type(dvcs)
    other_dvcs = DVCS(dir=f"{tmpdir}/other", url=dvcs.url)
    assert other_dvcs.clone("master") is True
    assert open(f"{other_dvcs.directory}/README.md").read() == content


Loïc Dachary's avatar
Loïc Dachary committed
49 50 51 52 53
def test_dvcs_log(dvcs, tmpdir):
    assert dvcs.clone("master") is True
    hash1 = dvcs.hash
    content = "C"
    open(f"{dvcs.directory}/README.md", "w").write(content)
Loïc Dachary's avatar
Loïc Dachary committed
54
    dvcs.commit("message")
Loïc Dachary's avatar
Loïc Dachary committed
55 56 57 58 59 60 61
    dvcs.push("master")
    hash0 = dvcs.hash
    log = dvcs.log()
    assert log[0] == hash0
    assert log[1] == hash1


62 63 64 65 66 67 68 69
def test_dvcs_push_pull(dvcs, tmpdir):
    assert dvcs.clone("master") is True

    DVCS = type(dvcs)
    other_dvcs = DVCS(dir=f"{tmpdir}/other", url=dvcs.url)
    assert other_dvcs.clone("master") is True
    content = "C"
    open(f"{other_dvcs.directory}/README.md", "w").write(content)
Loïc Dachary's avatar
Loïc Dachary committed
70
    other_dvcs.commit("message")
71 72 73 74
    other_dvcs.push("master")

    dvcs.pull("master")
    assert open(f"{dvcs.directory}/README.md").read() == content
75 76 77 78 79 80 81 82


def test_dvcs_factory(dvcs):
    DVCS = type(dvcs)
    assert dvcs.clone("master") is True
    other_dvcs = factory(dvcs.directory)
    assert type(other_dvcs) == DVCS
    assert other_dvcs.url == dvcs.url
Loïc Dachary's avatar
Loïc Dachary committed
83 84


Loïc Dachary's avatar
Loïc Dachary committed
85
def test_dvcs_fetch_reset(dvcs_factory, dvcs, tmpdir):
Loïc Dachary's avatar
Loïc Dachary committed
86 87
    (DVCS, repository) = dvcs_factory
    other_origin = f"{tmpdir}/other_origin"
88
    repository(other_origin).prepare_for_test()
Loïc Dachary's avatar
Loïc Dachary committed
89 90 91 92 93
    other_dvcs = DVCS(dir=f"{tmpdir}/other", url=other_origin)
    assert other_dvcs.clone("master") is True
    other_hash = other_dvcs.hash

    assert dvcs.clone("master") is True
Loïc Dachary's avatar
Loïc Dachary committed
94
    hash = dvcs.hash
Loïc Dachary's avatar
Loïc Dachary committed
95
    assert dvcs.fetch(other_origin, other_hash) is True
Loïc Dachary's avatar
Loïc Dachary committed
96
    assert dvcs.hash == hash
Loïc Dachary's avatar
Loïc Dachary committed
97
    assert dvcs.fetch(other_origin, other_hash) is False
Loïc Dachary's avatar
Loïc Dachary committed
98 99 100
    assert dvcs.hash == hash
    assert dvcs.reset("master", other_hash) is True
    assert dvcs.hash == other_hash