Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Jan 18, 2018
1 parent 25fc0b9 commit 9003d90
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 30 deletions.
2 changes: 1 addition & 1 deletion automergetool/amt.py
Expand Up @@ -164,9 +164,9 @@ def merge_with_tool(tool: str,
try:
invocation_result = launcher.invoke(cmd)
except Exception as err:
invocation_result = ERROR_INVOCATION
if verbose:
print(" [AMT] ✗ {0} error running command {1}\n $ {2}".format(tool, err, cmd))
return ERROR_INVOCATION

# Check result
trust_exit_code = launcher.get_tool_trust(tool)
Expand Down
12 changes: 6 additions & 6 deletions automergetool/amt_import_solver.py
Expand Up @@ -162,10 +162,10 @@ def __merge_imports_shallow(self,
def __is_in_imports(self, imp: str, imports: List[str]) -> bool:
for test_imp in imports:
if self.are_imports_the_same(imp, test_imp):
if self.are_imports_equals(imp, test_imp):
return True
else:
if self.are_imports_incompatible(imp, test_imp):
raise RuntimeError("✗ Imports conflict between\n" + imp + "\n and\n" + test_imp)
else:
return True
return False

def __read_imports(self, path: str) -> List[str]:
Expand Down Expand Up @@ -285,14 +285,14 @@ def are_imports_the_same(self, imp: str, other_imp: str) -> bool:
return imp == other_imp

# noinspection PyMethodMayBeStatic
def are_imports_equals(self, imp: str, other_imp: str) -> bool:
def are_imports_incompatible(self, imp: str, other_imp: str) -> bool:
"""
:param imp: an import statement
:param other_imp: another import statement
:return: true if both statements imports the same logical element, with no logical differnence (same alias...)
:return: true if both statements imports the same logical element, with incompatible logical differnence (same alias...)
When invoked, inputs are guaranteed to represent the same import (are_imports_the_same returned true)
"""
return imp == other_imp
return False

@abstractmethod
def is_import_line(self, line: str) -> bool:
Expand Down
12 changes: 6 additions & 6 deletions automergetool/solvers/gen_additions.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
from argparse import ArgumentParser, Namespace
import sys

from automergetool.amt_utils import REPORT_NONE, REPORT_SOLVED, REPORT_UNSOLVED, REPORT_FULL, ConflictsWalker
Expand All @@ -14,16 +14,16 @@
ORDER_NONE = ""


def parse_arguments():
def parse_arguments(args: list) -> Namespace:
"""Parses the arguments passed on invocation in a dict and return it"""
parser = argparse.ArgumentParser(description="A tool to resolve addition conflicts")
parser = ArgumentParser(description="A tool to resolve addition conflicts")

parser.add_argument('-m', '--merged', required=True)
parser.add_argument(
'-o',
'--order',
choices=[ORDER_LOCAL_FIRST, ORDER_REMOTE_FIRST, ORDER_ASK],
default=ORDER_REMOTE_FIRST,
default=ORDER_ASK,
required=False)
parser.add_argument(
'-r',
Expand All @@ -34,7 +34,7 @@ def parse_arguments():
parser.add_argument('-w', '--whitespace', required=False, action='store_true')
parser.add_argument('-v', '--verbose', required=False, action='store_true')

return parser.parse_args()
return parser.parse_args(args)


def is_same_addition(local: str, remote: str):
Expand Down Expand Up @@ -99,7 +99,7 @@ def get_order(conflict, choice, user_input=lambda msg: input(msg)):


if __name__ == '__main__':
args = parse_arguments()
args = parse_arguments(sys.argv[1:])
walker = ConflictsWalker(args.merged, 'adds', args.report, args.verbose)
while walker.has_more_conflicts():
handle_conflict(walker.next_conflict(), lambda c: get_order(c, args.order),
Expand Down
10 changes: 5 additions & 5 deletions automergetool/solvers/gen_deletions.py
@@ -1,15 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
from argparse import ArgumentParser, Namespace
import sys

from automergetool.amt_utils import REPORT_NONE, REPORT_SOLVED, REPORT_UNSOLVED, REPORT_FULL, ConflictsWalker


def parse_arguments():
def parse_arguments(args: list) -> Namespace:
"""Parses the arguments passed on invocation in a dict and return it"""
parser = argparse.ArgumentParser(description="A tool to resolve woven conflicts")
parser = ArgumentParser(description="A tool to resolve woven conflicts")

parser.add_argument('-m', '--merged', required=True)
parser.add_argument(
Expand All @@ -20,7 +20,7 @@ def parse_arguments():
required=False)
parser.add_argument('-v', '--verbose', required=False, action='store_true')

return parser.parse_args()
return parser.parse_args(args)


def handle_conflict(conflict):
Expand All @@ -37,7 +37,7 @@ def handle_conflict(conflict):


if __name__ == '__main__':
args = parse_arguments()
args = parse_arguments(sys.argv[1:])
walker = ConflictsWalker(args.merged, 'dels', args.report, args.verbose)
while walker.has_more_conflicts():
handle_conflict(walker.next_conflict())
Expand Down
10 changes: 5 additions & 5 deletions automergetool/solvers/gen_simplify.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
from argparse import ArgumentParser, Namespace
import sys

from automergetool.amt_lcs import LCSAnalyser, ListSequencer
Expand All @@ -10,9 +10,9 @@
ConflictsWalker


def parse_arguments():
def parse_arguments(args: list) -> Namespace:
"""Parses the arguments passed on invocation in a dict and return it"""
parser = argparse.ArgumentParser(description="A tool to resolve dummy conflicts")
parser = ArgumentParser(description="A tool to resolve woven conflicts")

parser.add_argument('-m', '--merged', required=True)
parser.add_argument(
Expand All @@ -23,7 +23,7 @@ def parse_arguments():
required=False)
parser.add_argument('-v', '--verbose', required=False, action='store_true')

return parser.parse_args()
return parser.parse_args(args)


def handle_conflict(conflict):
Expand Down Expand Up @@ -91,7 +91,7 @@ def handle_conflict(conflict):


if __name__ == '__main__':
args = parse_arguments()
args = parse_arguments(sys.argv[1:])
walker = ConflictsWalker(args.merged, 'simplify', args.report, args.verbose)
while walker.has_more_conflicts():
handle_conflict(walker.next_conflict())
Expand Down
10 changes: 5 additions & 5 deletions automergetool/solvers/gen_woven.py
@@ -1,15 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
from argparse import ArgumentParser, Namespace
import sys

from automergetool.amt_utils import REPORT_NONE, REPORT_SOLVED, REPORT_UNSOLVED, REPORT_FULL, ConflictsWalker


def parse_arguments():
def parse_arguments(args: list) -> Namespace:
"""Parses the arguments passed on invocation in a dict and return it"""
parser = argparse.ArgumentParser(description="A tool to resolve woven conflicts")
parser = ArgumentParser(description="A tool to resolve woven conflicts")

parser.add_argument('-m', '--merged', required=True)
parser.add_argument(
Expand All @@ -20,7 +20,7 @@ def parse_arguments():
required=False)
parser.add_argument('-v', '--verbose', required=False, action='store_true')

return parser.parse_args()
return parser.parse_args(args)


def handle_conflict(conflict):
Expand Down Expand Up @@ -49,7 +49,7 @@ def handle_conflict(conflict):


if __name__ == '__main__':
args = parse_arguments()
args = parse_arguments(sys.argv[1:])
walker = ConflictsWalker(args.merged, 'woven', args.report, args.verbose)
while walker.has_more_conflicts():
handle_conflict(walker.next_conflict())
Expand Down
77 changes: 75 additions & 2 deletions tests/_amt_test.py
Expand Up @@ -5,6 +5,8 @@
from configparser import ConfigParser
from unittest.mock import *

import tempfile

from automergetool.amt import *
from automergetool.amt_utils import *

Expand Down Expand Up @@ -201,6 +203,32 @@ def test_merge_with_tool_untrusted_unsolved(self):
self.assertEqual(result, ERROR_CONFLICTS)
launcher.invoke.assert_called_with('MY_CMD ' + args.merged)


def test_merge_with_tool_failing(self):
# Given
tool = FAKE_TOOL
cfg = ConfigParser()
cfg.add_section(SECT_AMT)
cfg.set(SECT_AMT, OPT_VERBOSE, 'true')
args = create_args()
launcher_args = {
'get_tool_extensions.return_value': None,
'get_tool_ignored_extensions.return_value': None,
'get_tool_cmd.return_value': 'MY_CMD $MERGED',
'invoke.side_effect': Exception("Oops"),
'get_tool_trust.return_value': True
}
launcher = Mock(**launcher_args)
analyser_args = {'has_remaining_conflicts.return_value': True}
analyser = Mock(**analyser_args)

# When
result = merge_with_tool(tool, cfg, args, launcher, analyser)

# Then
self.assertEqual(result, ERROR_INVOCATION)
launcher.invoke.assert_called_with('MY_CMD ' + args.merged)

def test_merge_with_tools_all_fail(self):
# Given
cfg = ConfigParser()
Expand Down Expand Up @@ -240,7 +268,7 @@ def test_merge_with_tools_first_succeeds(self):
'get_tool_extensions.return_value': None,
'get_tool_ignored_extensions.return_value': None,
'get_tool_cmd.side_effect':
['MY_CMD1 $MERGED', 'MY_CMD2 --out $MERGED', 'MY_CMD3 $BASE $MERGED'],
['MY_CMD1 $MERGED', 'MY_CMD2 --out $MERGED', 'MY_CMD3 $BASE $MERGED'],
'invoke.return_value': 0
}
launcher = Mock(**launcher_args)
Expand Down Expand Up @@ -286,14 +314,58 @@ def test_merge_not_configured(self):

def test_find_local_config_not_a_git_folder(self):
# Given
path = '/dev/null'
path = '/foo/bar/spam'

# When
config = find_local_config_path(path)

# Then
self.assertEqual(config, None)

def test_find_local_config_not_a_real_folder(self):
# Given
path = '/foo/bar/spam'

# When
config = find_local_config_path(path)

# Then
self.assertEqual(config, None)

def test_find_local_config_in_current_repo_git_folder(self):
# Given
parent = tempfile.mkdtemp()
gitDir = parent + os.sep + ".git"
os.mkdir(gitDir)
configFile = gitDir + os.sep + "config"
open(configFile, 'a').close()

path = parent + os.sep + "fakedir" + os.sep + "fakefile"

# When
config = find_local_config_path(path)

# Then
self.assertEqual(config, configFile)


def test_no_local_config_in_current_repo_git_folder(self):
# Given
parent = tempfile.mkdtemp()
gitDir = parent + os.sep + ".git"
os.mkdir(gitDir)
indexFile = gitDir + os.sep + "index"
open(indexFile, 'a').close()

path = parent + os.sep + "fakedir" + os.sep + "fakefile"

# When
config = find_local_config_path(path)

# Then
self.assertEqual(config, None)

# noinspection PyUnresolvedReferences
def test_path_arguments_shorts(self):
# Given
b = "b"
Expand All @@ -310,6 +382,7 @@ def test_path_arguments_shorts(self):
self.assertEqual(parsed.remote, base_path + os.sep + r)
self.assertEqual(parsed.merged, base_path + os.sep + m)

# noinspection PyUnresolvedReferences
def test_path_arguments_long(self):
# Given
b = "b"
Expand Down

0 comments on commit 9003d90

Please sign in to comment.