Passed
Pull Request — master (#2)
by William
01:35
created

test_track_save_conflict()   B

Complexity

Conditions 5

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 15
rs 8.5454
c 0
b 0
f 0
cc 5
nop 3
1
# coding=utf-8
2
3
4
import os
5
6
import git
7
import pytest
8
from click.testing import CliRunner
9
10
from module_renamer.cli import track
11
12
13
@pytest.fixture()
14
def repo(tmpdir):
15
    """
16
    Creates a empty repository on the temp dir
17
    """
18
    repo = git.Repo.init(path=str(tmpdir))
19
    yield repo
20
    repo.close()
21
22
23
@pytest.fixture()
24
def create_scenario():
25
    """
26
    Commits the content from file_master and file_working_branch on the
27
    given git repository
28
    """
29
30
    def _create_scenario(repo, file_master, file_working_branch):
31
        file_path = os.path.join(repo.working_dir, 'file_a.py')
32
33
        with open(file_path, mode='w') as file:
34
            file.writelines(file_master)
35
        repo.index.add(['file_a.py'])
36
        repo.index.commit("commit on master")
37
        repo.heads.master.checkout(b='new_branch')
38
39
        with open(file_path, mode='w') as file:
40
            file.writelines(file_working_branch)
41
        repo.index.add(['file_a.py'])
42
        repo.index.commit("commit on working branch")
43
44
    return _create_scenario
45
46
47
@pytest.fixture()
48
def run_cli():
49
    def _run_cli(repo, input=None):
50
        runner = CliRunner()
51
52
        output_file = _output_file(repo)
53
        output_arg = '--output_file={0}'.format(output_file)
54
55
        return runner.invoke(track, [repo.working_dir, output_arg], input=input)
56
57
    return _run_cli
58
59
60 View Code Duplication
def test_track_without_modification(repo, create_scenario, run_cli):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
61
    imports_for_file_a = ['from a.b import c\n', 'from d.e import f\n']
62
    imports_for_file_b = ['from g.h import i\n', 'from j.k import l\n']
63
64
    create_scenario(repo, imports_for_file_a, imports_for_file_b)
65
66
    result = run_cli(repo)
67
68
    assert result.exit_code == 0
69
    assert "test_list_output.py" in result.output
70
71
    output_file = _output_file(repo)
72
    with open(output_file, mode='r') as file:
73
        assert "imports_to_move = []" in file.read()
74
75
76 View Code Duplication
def test_track_with_modification(repo, create_scenario, run_cli):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
77
    imports_for_file_a = ['from a.b import c\n', 'from d.e import f\n']
78
    imports_for_file_b = ['from x.x import c\n', 'from j.k import l\n']
79
    create_scenario(repo, imports_for_file_a, imports_for_file_b)
80
81
    result = run_cli(repo)
82
83
    assert result.exit_code == 0
84
    assert "test_list_output.py" in result.output
85
86
    output_file = _output_file(repo)
87
    with open(output_file, mode='r') as file:
88
        assert "imports_to_move = [('a.b.c', 'x.x.c')]" in file.read()
89
90
91
def test_track_with_conflict(repo, create_scenario, run_cli):
92
    imports_for_file_a = ['from a.b import c\n', 'from b import c\n']
93
    imports_for_file_b = ['from x.x import c\n', 'from y import c\n']
94
    create_scenario(repo, imports_for_file_a, imports_for_file_b)
95
96
    result = run_cli(repo)
97
98
    assert result.exit_code == 1
99
    assert ("a.b.c" and "b.c") in result.output
100
101
102
def test_track_save_conflict(repo, create_scenario, run_cli):
103
    imports_for_file_a = ['from a.b import c\n', 'from b import c\n',
104
                          'from m import n\n']
105
    imports_for_file_b = ['from x.x import c\n', 'from y import c\n',
106
                          'from w import n\n']
107
    create_scenario(repo, imports_for_file_a, imports_for_file_b)
108
109
    result = run_cli(repo, input="y\n")
110
111
    assert result.exit_code == 0
112
    assert "test_list_output.py" in result.output
113
114
    output_file = _output_file(repo)
115
    with open(output_file, mode='r') as file:
116
        assert "imports_to_move = [('m.n', 'w.n')]" in file.read()
117
118
119
def _output_file(repo):
120
    return os.path.join(repo.working_dir, "test_list_output.py")
121