def test_missing_config_file_then_create_with_defaults( self, tmp_path: Path): # Given: A config path to a non-existing file config_path = tmp_path / 'config.yaml' assert not config_path.exists() # When: Instanciating ConfigRepo actual_file_reader = FileReader() actual_file_writer = FileWriter() config_repo = ConfigRepo(config_path, actual_file_reader, actual_file_writer) # Then: # - Config exist w/ defaults assert config_path.exists() with config_path.open('r') as config_file: assert yaml.load(config_file) == defaults.DEFAULT_CONFIG # - Defaults are loaded assert config_repo.get_kata_grepo_username( ) == defaults.DEFAULT_CONFIG['KataGRepo']['User'] assert config_repo.get_kata_grepo_reponame( ) == defaults.DEFAULT_CONFIG['KataGRepo']['Repo'] for language_name in defaults.DEFAULT_CONFIG['HasTemplateAtRoot']: assert config_repo.has_template_at_root(KataLanguage(language_name)) \ is defaults.DEFAULT_CONFIG['HasTemplateAtRoot'][language_name] assert config_repo.has_template_at_root( KataLanguage('fakelanguagethatdoesntexist')) is None assert not config_repo.should_skip_not_logged_in_warning()
def test_valid_case(self, init_kata_service: InitKataService, kata_language_repo: HardCoded.KataLanguageRepo): kata_language_repo.available_languages = ['java', 'python'] available_languages = init_kata_service.list_available_languages() assert available_languages == [ KataLanguage('java'), KataLanguage('python') ]
def test_return_all_directory_as_kata_languages( self, mock_api: MagicMock, kata_language_repo: KataLanguageRepo): mock_api.contents.return_value = [ mock_file_entry('some_random_file.txt'), mock_dir_entry('java'), mock_file_entry('README.md'), mock_dir_entry('rust') ] all_languages = kata_language_repo.get_all() assert all_languages == [ KataLanguage('java'), KataLanguage('rust') ]
def test_valid_language( self, init_kata_service: InitKataService, kata_language_repo: HardCoded.KataLanguageRepo, kata_template_repo: HardCoded.KataTemplateRepo): kata_language_repo.available_languages = ['java'] kata_template_repo.available_templates = { 'java': ['junit5', 'hamcrest'] } available_templates_for_java = init_kata_service.list_available_templates( 'java') assert available_templates_for_java == [ KataTemplate(KataLanguage('java'), 'junit5'), KataTemplate(KataLanguage('java'), 'hamcrest') ]
def test_request_contents_of_language_directory( self, mock_api: MagicMock, kata_template_repo: KataTemplateRepo): kata_template_repo.get_for_language(KataLanguage('javascript')) mock_api.contents.assert_called_with( DEFAULT_CONFIG['KataGRepo']['User'], DEFAULT_CONFIG['KataGRepo']['Repo'], 'javascript')
def test_template_name_isnt_given_but_multiple_options_are_available( self, tmp_path: Path, init_kata_service: InitKataService, kata_language_repo: HardCoded.KataLanguageRepo, kata_template_repo: HardCoded.KataTemplateRepo): with pytest.raises( KataTemplateNotFound) as template_not_found_error: kata_language_repo.available_languages = ['java'] kata_template_repo.available_templates = { 'java': ['junit5', 'hamcrest'] } init_kata_service.init_kata(tmp_path, VALID_KATA_NAME, 'java', None) assert template_not_found_error.value.available_templates == \ [KataTemplate(KataLanguage('java'), 'junit5'), KataTemplate(KataLanguage('java'), 'hamcrest')]
def test_template_is_not_at_root(self, mock_api: MagicMock, kata_template_repo: KataTemplateRepo): # Given: There are multiple directories at root of the language dir mock_api.contents.return_value = [ mock_dir_entry('java/junit5'), mock_dir_entry('java/hamcrest') ] # When: Fetching the available templates for java available_java_templates = kata_template_repo.get_for_language( KataLanguage('java')) # Then: All available templates are returned assert available_java_templates == [ KataTemplate(KataLanguage('java'), 'junit5'), KataTemplate(KataLanguage('java'), 'hamcrest') ]
def test_valid_language_name(self, mock_api: MagicMock, kata_language_repo: KataLanguageRepo): mock_api.contents.return_value = [ mock_file_entry('some_random_file.txt'), mock_dir_entry('java'), mock_file_entry('README.md'), mock_dir_entry('rust') ] assert kata_language_repo.get('java') == KataLanguage('java')
def test_template_name_doesnt_exist( self, tmp_path: Path, init_kata_service: InitKataService, kata_language_repo: HardCoded.KataLanguageRepo, kata_template_repo: HardCoded.KataTemplateRepo): with pytest.raises( KataTemplateNotFound) as template_not_found_error: kata_language_repo.available_languages = ['java'] kata_template_repo.available_templates = { 'java': ['junit5', 'hamcrest'] } init_kata_service.init_kata( tmp_path, VALID_KATA_NAME, 'java', 'some_framework_thats_not_junit5') assert template_not_found_error.value.available_templates == \ [KataTemplate(KataLanguage('java'), 'junit5'), KataTemplate(KataLanguage('java'), 'hamcrest')]
def test_template_at_root( self, mock_api: MagicMock, config_repo, kata_template_repo: KataTemplateRepo): # Given: 'rust' is explicitly stated as having the template at its root config_repo.config['HasTemplateAtRoot'] = {'rust': True} mock_api.contents.return_value = [ mock_file_entry('rust/some_random_file.txt'), mock_dir_entry('rust/some_dir') ] # When: Fetching the available templates for rust rust_available_templates = kata_template_repo.get_for_language( KataLanguage('rust')) # Then: Only one is available and it is the root template (template_name == None) assert rust_available_templates == [ KataTemplate(KataLanguage('rust'), template_name=None) ]
def test_check_if_has_readme( self, mock_api: MagicMock, config_repo, kata_template_repo: KataTemplateRepo): # If there's no information in the config whether this language has its template at root, # try to guess by checking if there's a 'README.md' in the language root dir # Given: # - 'golang' isn't included in the list of languages w/ a template at root # - there's a 'README.md' at 'golang' dir root config_repo.config['HasTemplateAtRoot'] = {} mock_api.contents.return_value = [ mock_file_entry('golang/README.md') ] # When: Fetching the available templates for java available_java_templates = kata_template_repo.get_for_language( KataLanguage('golang')) # Then: Only one is available and it is the root template (template_name == None) assert len(available_java_templates) == 1 assert available_java_templates[0] == KataTemplate( KataLanguage('golang'), template_name=None)
def test_valid_config(self, tmp_path: Path): def write_config(config_contents): with config_file.open('w') as f: f.write(textwrap.dedent(config_contents)) def create_config_repo(): actual_file_reader = FileReader() actual_file_writer = FileWriter() return ConfigRepo(config_file, actual_file_reader, actual_file_writer) # Given: A valid config in a real file config_file = tmp_path / 'config.yml' write_config("""\ KataGRepo: User: frank Repo: 'awesome-repo' HasTemplateAtRoot: java: False elixir: True Auth: SkipNotLoggedInWarning: True """) # When: Creating a repo with real dependencies config_repo = create_config_repo() # Then: Config is read as expected assert config_repo.get_kata_grepo_username() == 'frank' assert config_repo.get_kata_grepo_reponame() == 'awesome-repo' assert config_repo.has_template_at_root( KataLanguage('java')) is False assert config_repo.has_template_at_root( KataLanguage('elixir')) is True assert config_repo.has_template_at_root( KataLanguage('csharp')) is None assert config_repo.should_skip_not_logged_in_warning()
def test_missing_hastemplateatroot_entry( self, valid_config, assert_given_config_raises_when_calling_given_method): def config_wo_hastemplateatroot(): conf = valid_config conf.pop('HasTemplateAtRoot') return conf assert_given_config_raises_when_calling_given_method( config=config_wo_hastemplateatroot(), method_to_call='has_template_at_root', method_args=(KataLanguage('java')), regexes_to_match=[r'HasTemplateAtRoot', r'Missing'])
def test_language_doesnt_exist( self, init_kata_service: InitKataService, kata_language_repo: HardCoded.KataLanguageRepo, kata_template_repo: HardCoded.KataTemplateRepo): kata_language_repo.available_languages = ['java'] kata_template_repo.available_templates = { 'java': ['junit5', 'hamcrest'] } with pytest.raises( KataLanguageNotFound) as language_not_found_error: init_kata_service.list_available_templates('python') assert language_not_found_error.value.available_languages == [ KataLanguage('java') ]
def test_template_not_at_root( self, mock_api: MagicMock, config_repo, kata_template_repo: KataTemplateRepo): # Given: 'java' is explicitly stated as not having the template at its root config_repo.config['HasTemplateAtRoot'] = {'java': False} mock_api.contents.return_value = [ mock_file_entry('java/some_random_file.txt'), mock_file_entry('java/README.md'), mock_dir_entry('java/template1'), mock_dir_entry('java/template2') ] # When: Fetching the available templates for java java_available_templates = kata_template_repo.get_for_language( KataLanguage('java')) # Then: Only one is available and it is the root template (template_name == None) assert java_available_templates == [ KataTemplate(KataLanguage('java'), template_name='template1'), KataTemplate(KataLanguage('java'), template_name='template2') ]
def test_language_doesnt_exist( self, tmp_path: Path, init_kata_service: InitKataService, kata_language_repo: HardCoded.KataLanguageRepo, kata_template_repo: HardCoded.KataTemplateRepo): with pytest.raises( KataLanguageNotFound) as language_not_found_error: kata_language_repo.available_languages = ['java'] kata_template_repo.available_templates = { 'java': ['junit5', 'hamcrest'] } init_kata_service.init_kata(tmp_path, VALID_KATA_NAME, 'python', NOT_USED) assert language_not_found_error.value.available_languages == [ KataLanguage('java') ]
def _all_sub_directories_mapped_to_languages(contents_of_dir): for file_or_dir in contents_of_dir: if file_or_dir['type'] == 'dir': sub_dir_name_interpreted_as_available_kata_language_name = file_or_dir['path'] yield KataLanguage(name=sub_dir_name_interpreted_as_available_kata_language_name)
def get_all(self) -> List[KataLanguage]: return [KataLanguage(lang_name) for lang_name in self.available_languages]
def get(self, language_name: str) -> Optional[KataLanguage]: for available_language_name in self.available_languages: if available_language_name == language_name: return KataLanguage(language_name) if language_name not in self.available_languages: return None
def test_has_template(self, config_repo: ConfigRepo): assert config_repo.has_template_at_root( KataLanguage('java')) is False
def test_no_information_whether_or_not_template_is_located_at_root( self, config_repo: ConfigRepo): assert config_repo.has_template_at_root( KataLanguage('csharp')) is None
def test_doesnt_have_template(self, config_repo: ConfigRepo): assert config_repo.has_template_at_root( KataLanguage('elixir')) is True