def test_collect_file_diffs(self): n = 10 filename = 'file{i}.py' with mock_repo() as repo: commits = make_mock_commits(repo, n=n, filename=filename) contrib_module_path = None commit_range = make_commit_range( commits[0], commits[-1]) travis_env_vars = { 'TRAVIS_BUILD_DIR': repo.working_tree_dir, 'TRAVIS_PULL_REQUEST': str(self.pr_num), 'TRAVIS_COMMIT_RANGE': commit_range, } with patch.dict('os.environ', travis_env_vars, clear=True): project_path = repo.working_tree_dir project = make_mock_project(repo, self.pr_num, project_path, contrib_module_path) change_collector = ChangeCollector(project) file_diffs = change_collector._collect_file_diffs() # checks on file_diffs self.assertEqual(len(file_diffs), n - 1) for diff in file_diffs: self.assertEqual(diff.change_type, 'A') self.assertTrue(diff.b_path.startswith('file')) self.assertTrue(diff.b_path.endswith('.py'))
def __init__(self, project: Project): self.change_collector = ChangeCollector(project) X_df, y_df = project.api.load_data() encoder = project.api.encoder y = encoder.fit_transform(y_df) self.X_df, self.y = subsample_data_for_validation(X_df, y)
class ProjectStructureValidator(BaseValidator): def __init__(self, project): self.change_collector = ChangeCollector(project) def validate(self): changes = self.change_collector.collect_changes() return not changes.inadmissible_diffs
class FeatureApiValidator(BaseValidator): def __init__(self, project): self.change_collector = ChangeCollector(project) X, y = project.load_data() self.X, self.y = subsample_data_for_validation(X, y) def validate(self): """Collect and validate all new features""" changes = self.change_collector.collect_changes() features = [] imported_okay = True for importer, modname, modpath in changes.new_feature_info: try: mod = importer() features.extend(_collect_contrib_features(mod)) except (ImportError, SyntaxError): logger.info('Failed to import module at {}'.format(modpath)) logger.exception('Exception details: ') imported_okay = False if not imported_okay: return False # if no features were added at all, reject if not features: logger.info('Failed to collect any new features.') return False return all( validate_feature_api(feature, self.X, self.y, subsample=False) for feature in features)
def test_change_collector_collect_file_diffs_custom_differ(mock_repo): repo = mock_repo n = 10 filename = 'file{i}.py' commits = make_mock_commits(repo, n=n, filename=filename) project = None differ = CustomDiffer(endpoints=(commits[0], commits[-1])) change_collector = ChangeCollector(project, differ=differ) file_diffs = change_collector._collect_file_diffs() # checks on file_diffs assert len(file_diffs) == n - 1 for diff in file_diffs: assert diff.change_type == 'A' assert diff.b_path.startswith('file') assert diff.b_path.endswith('.py')
def test_change_collector_collect_changes(quickstart): repo = quickstart.repo contrib_path = quickstart.project.config.get('contrib.module_path') path_content = [ ('something.txt', None), # invalid ('invalid.py', None), # invalid (f'{contrib_path}/foo/bar/baz.py', None), # invalid # candidate_feature, and also new_feature_info (f'{contrib_path}/user_foo/feature_bar.py', None), (f'{contrib_path}/user_foo/__init__.py', None), # valid_init ] old_head = repo.head.commit for path, content in path_content: make_mock_commit(repo, path=path, content=content) new_head = repo.head.commit differ = CustomDiffer(endpoints=(old_head, new_head)) change_collector = ChangeCollector(quickstart.project, differ=differ) changes = change_collector.collect_changes() assert len(changes.file_diffs) == 5 assert len(changes.candidate_feature_diffs) == 1 assert len(changes.valid_init_diffs) == 1 assert len(changes.inadmissible_diffs) == 3 assert len(changes.new_feature_info) == 1 actual_inadmissible = [diff.b_path for diff in changes.inadmissible_diffs] expected_inadmissible = [ 'something.txt', 'invalid.py', f'{contrib_path}/foo/bar/baz.py' ] assert set(actual_inadmissible) == set(expected_inadmissible)
def __init__(self, project): self.change_collector = ChangeCollector(project) X, y = project.load_data() self.X, self.y = subsample_data_for_validation(X, y)
def __init__(self, project): self.change_collector = ChangeCollector(project)
def test_change_collector_detect_differ_travis(mock_travis_differ, _): """Check ChangeCollector._detect_differ""" differ_instance = mock_travis_differ.return_value project = create_autospec(Project) change_collector = ChangeCollector(project) assert change_collector.differ is differ_instance
def test_change_collector_init(): project = create_autospec(Project) differ = Differ() change_collector = ChangeCollector(project, differ=differ) assert change_collector is not None