Skip to content

Commit

Permalink
green_mode.py: Run BEAR_DEPS bears while testing
Browse files Browse the repository at this point in the history
This patch makes the bears from BEAR_DEPS run and test for green
output before running the actual independent bear.
Some test bears are also added and tests are added for the changes.
  • Loading branch information
ishanSrt committed Aug 14, 2018
1 parent 9e91285 commit a725f01
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
23 changes: 22 additions & 1 deletion coala_quickstart/green_mode/green_mode.py
Expand Up @@ -7,6 +7,7 @@

from coala_quickstart.generation.Utilities import (
contained_in,
get_all_args,
get_extensions,
get_yaml_contents,
peek,
Expand Down Expand Up @@ -289,13 +290,33 @@ def local_bear_test(bear, file_dict, file_names, lang, kwargs,
values = []

for vals in itertools.product(*kwargs.values()):

flag = 0
for dep in bear.BEAR_DEPS:
section = Section('dep-bear')
bear_obj = dep(section, None)
arguments = dict(zip(kwargs, vals))
dep_args = get_all_args(dep.run)
new_arguments = {}
for arg_ in arguments.keys():
if arg_ in dep_args.keys(): # pragma: no cover
new_arguments[arg_] = arguments[arg_]
arguments = new_arguments
ret_val = bear_obj.run(**arguments)
ret_val = [] if not ret_val else list(ret_val)
dep_res = check_bear_results(ret_val, ignore_ranges)
if not dep_res:
flag = 1
if flag == 1:
continue

print_val = dict(zip(kwargs, vals))
print_val.pop('file', None)
values.append(vals)
section = Section('test-section-local-bear')
bear_obj = bear(section, None)
ret_val = bear_obj.run(**dict(zip(kwargs, vals)))
ret_val = list(ret_val)
ret_val = [] if not ret_val else list(ret_val)
# FIXME: Multiprocessing not working on windows.
if os.name == 'nt': # pragma posix: no cover
results.append(check_bear_results(ret_val, ignore_ranges))
Expand Down
45 changes: 45 additions & 0 deletions tests/green_mode/green_modeTest.py
Expand Up @@ -42,8 +42,10 @@
from tests.test_bears.AllKindsOfSettingsDependentBear import (
AllKindsOfSettingsBaseBear,
)
from tests.test_bears.AnotherTestLocalDepBear import AnotherTestLocalDepBear
from tests.test_bears.TestGlobalBear import TestGlobalBear
from tests.test_bears.TestLocalBear import TestLocalBear
from tests.test_bears.TestLocalDepBear import TestLocalDepBear

settings_key = 'green_mode_infinite_value_settings'

Expand Down Expand Up @@ -374,6 +376,49 @@ def test_bear_test_fun_2(self):
test_non_op_results[0][TestLocalBear])
self.assertCountEqual(unified_results, [None, None])

def test_bear_test_fun_3(self):
from pyprint.ConsolePrinter import ConsolePrinter
printer = ConsolePrinter()
bears = {'Python': [TestLocalDepBear]}
relevant_bears = {'test':
{TestLocalDepBear}}
bear_settings_obj = collect_bear_settings(relevant_bears)
file_dict = {'A.py': {'a\n', 'b\n'}, 'C.py': {'c\n', 'd\n'}}
dir_path = str(Path(__file__).parent) + os.sep
contents = initialize_project_data(dir_path, [])
file_names = ['A.py', 'C.py']
non_op_results, unified_results = bear_test_fun(
bears, bear_settings_obj, file_dict, [], contents,
file_names, 1, 1, printer)
print('nonop:', non_op_results)
print('op:', unified_results)
test_results = [{TestLocalDepBear: []}]
self.assertCountEqual(non_op_results[0][TestLocalDepBear],
test_results[0][TestLocalDepBear])
self.assertCountEqual(unified_results, [None])

def test_bear_test_fun_4(self):
from pyprint.ConsolePrinter import ConsolePrinter
printer = ConsolePrinter()
bears = {'Python': [AnotherTestLocalDepBear]}
relevant_bears = {'test':
{AnotherTestLocalDepBear}}
bear_settings_obj = collect_bear_settings(relevant_bears)
file_dict = {'A.py': {'a\n', 'b\n'}, 'C.py': {'c\n', 'd\n'}}
dir_path = str(Path(__file__).parent) + os.sep
contents = initialize_project_data(dir_path, [])
file_names = ['A.py', 'C.py']
non_op_results, unified_results = bear_test_fun(
bears, bear_settings_obj, file_dict, [], contents,
file_names, 1, 1, printer)
print('nonop:', non_op_results)
print('op:', unified_results)
test_results = [{AnotherTestLocalDepBear: [{'filename': 'A.py'},
{'filename': 'C.py'}]}]
self.assertCountEqual(non_op_results[0][AnotherTestLocalDepBear],
test_results[0][AnotherTestLocalDepBear])
self.assertCountEqual(unified_results, [None])

def test_write_coafile(self):
from pyprint.ConsolePrinter import ConsolePrinter
printer = ConsolePrinter()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_bears/AnotherTestLocalDepBear.py
@@ -0,0 +1,11 @@
from coalib.bears.LocalBear import LocalBear
from tests.test_bears.AnotherTestLocalIndepBear import AnotherTestLocalIndepBear


class AnotherTestLocalDepBear(LocalBear):
CAN_FIX = {}
LANGUAGES = {}
BEAR_DEPS = {AnotherTestLocalIndepBear}

def run(self, filename, file, yield_results=False):
pass
9 changes: 9 additions & 0 deletions tests/test_bears/AnotherTestLocalIndepBear.py
@@ -0,0 +1,9 @@
from coalib.bears.LocalBear import LocalBear


class AnotherTestLocalIndepBear(LocalBear):
CAN_FIX = {}
LANGUAGES = {}

def run(self, filename, file, x=2):
pass
12 changes: 12 additions & 0 deletions tests/test_bears/TestLocalDepBear.py
@@ -0,0 +1,12 @@
from coalib.bears.LocalBear import LocalBear
from tests.test_bears.TestLocalIndepBear import TestLocalIndepBear


class TestLocalDepBear(LocalBear):
CAN_FIX = {}
LANGUAGES = {}
BEAR_DEPS = {TestLocalIndepBear}

def run(self, filename, file, yield_results=False):
if yield_results:
yield 1
9 changes: 9 additions & 0 deletions tests/test_bears/TestLocalIndepBear.py
@@ -0,0 +1,9 @@
from coalib.bears.LocalBear import LocalBear


class TestLocalIndepBear(LocalBear):
CAN_FIX = {}
LANGUAGES = {}

def run(self, filename, file, x=2):
yield 1

0 comments on commit a725f01

Please sign in to comment.