def test_find_known_words_from_pack_ignore_commands_name(repo): """ Given: - Pack's structure is correct and pack-ignore file is present. When: - Running DocReviewer.find_known_words_from_pack. Then: - Ensure the found path result is appropriate. - Ensure the commands names are ignored. """ pack = repo.create_pack('test_pack') pack_integration_path = os.path.join( AZURE_FEED_PACK_PATH, "Integrations/FeedAzure/FeedAzure.yml") valid_integration_yml = get_yaml(pack_integration_path, cache_clear=True) pack.create_integration(name="first_integration", yml=valid_integration_yml) rn_file = pack.create_release_notes( version='1_0_0', content='azure-hidden-command \n azure-get-indicators') doc_reviewer = DocReviewer(file_paths=[]) with ChangeCWD(repo.path): found_known_words = doc_reviewer.find_known_words_from_pack( rn_file.path)[1] assert 'azure-hidden-command' in found_known_words assert 'azure-get-indicators' in found_known_words
def test_adding_known_words_from_pack(repo, file_content, unknown_words, known_words_files_contents, packs_known_words_content, review_success): """ Given: - A release notes file with two misspelled words. - Different variations of known_words files, including pack-ignore known_words. When: - Running doc_reviewer with known_words_file_paths and load_known_words_from_pack option. Then: - Ensure the review result is appropriate. - Make sure a review has taken place. - Enusure the unknown words are as expected. """ pack = repo.create_pack('test_pack') rn_file = pack.create_release_notes(version='1_0_0', content=file_content) pack.pack_ignore.write_list(packs_known_words_content) known_words_file_paths = [] for index, known_words_file_contents in enumerate( known_words_files_contents): known_words_file = pack._create_text_based(f"known_words_{index}.txt") known_words_file.write_list(known_words_file_contents) known_words_file_paths.append(known_words_file.path) with ChangeCWD(repo.path): doc_reviewer = DocReviewer( file_paths=[rn_file.path], known_words_file_paths=known_words_file_paths, load_known_words_from_pack=True) assert doc_reviewer.run_doc_review() == review_success assert len(doc_reviewer.files) > 0 assert set(doc_reviewer.unknown_words.keys()) == unknown_words
def test_camel_case_split(): """ Given - A CamelCase word When - Running camel_case_split on it. Then - Ensure result is a list of the split words in the camel case. """ camel_1 = 'ThisIsCamelCase' result = DocReviewer.camel_case_split(camel_1) assert isinstance(result, List) assert 'This' in result assert 'Is' in result assert 'Camel' in result assert 'Case' in result camel_2 = 'thisIPIsAlsoCamel' result = DocReviewer.camel_case_split(camel_2) assert 'this' in result assert 'IP' in result assert 'Is' in result assert 'Also' in result assert 'Camel' in result
def test_find_known_words_from_pack(repo, known_words_content, expected_known_words): """ Given: - Pack's structure is correct and pack-ignore file is present. - Case A: pack-ignore file has known_words section with words. - Case B: pack-ignore file has known_words section without words. - Case C: pack-ignore file doesn't have a known_words section. When: - Running DocReviewer.find_known_words_from_pack. Then: - Ensure the found path result is appropriate. - Ensure the pack name (test_pack) is in the know words. """ pack = repo.create_pack('test_pack') rn_file = pack.create_release_notes(version='1_0_0', content='Some release note') pack.pack_ignore.write_list(known_words_content) doc_reviewer = DocReviewer(file_paths=[]) with ChangeCWD(repo.path): assert doc_reviewer.find_known_words_from_pack( rn_file.path) == ('Packs/test_pack/.pack-ignore', expected_known_words)
def pass_files_to_docs_review(files_for_review: List[str]) -> int: """ This function passes each file of the files_for_review list to the demisto-sdk docs reviewer. Args: files_for_review: the files that should be reviewed. Returns: the exit code according to the docs reviewer result. """ exit_code = 0 for file_path in files_for_review: click.secho(f'Checking file: {file_path}\n', fg="yellow") doc_reviewer = DocReviewer( file_path=file_path, release_notes_only=True, known_words_file_path='Tests/known_words.txt', no_camel_case=True) result = doc_reviewer.run_doc_review() if not result: click.secho( 'Docs review resulted in failure, the exact logs can be found above.', fg="red") exit_code = 1 return exit_code
def get_file_report_mocker(self, mocker, files_type): """ Returns a mock of the file report. Args: mocker (MockerFixture): a mocker object. files_type (str): whether mock misspelled files or valid spelled files or both are required. Returns: MagicMock: a magic mock object of the click 'secho' function. """ import click doc_reviewer = DocReviewer() if files_type == self.SpelledFileType.VALID: doc_reviewer.files_without_misspells = self.MOCKED_FILES elif files_type == self.SpelledFileType.INVALID: doc_reviewer.files_with_misspells = self.MOCKED_FILES elif files_type == self.SpelledFileType.INVALID_RELEASE_NOTES: doc_reviewer.malformed_rn_files = self.MOCKED_FILES else: doc_reviewer.files_with_misspells = self.MOCKED_FILES doc_reviewer.files_without_misspells = self.MOCKED_FILES secho_mocker = mocker.patch.object(click, 'secho') doc_reviewer.print_file_report() return secho_mocker
def test_having_two_file_paths_different_pack( repo, mocker, first_file_content, second_file_content, unknown_word_calls, known_words_files_contents, review_success, misspelled_files_num, first_packs_known_words_content, second_packs_known_words_content, load_known_words_from_pack): """ Given: - 2 release notes files with two misspelled words each. - Different variations of known_words files, including pack-ignore known_words. When: - Running doc_reviewer with known_words_file_paths. Then: - Ensure the review result is appropriate. - Make sure a review has taken place. - Enusure the unknown words are as expected for each file. """ first_pack = repo.create_pack('first_test_pack') second_pack = repo.create_pack('second_test_pack') first_rn_file = first_pack.create_release_notes(version='1_0_0', content=first_file_content) second_rn_file = second_pack.create_release_notes( version='1_0_1', content=second_file_content) first_pack.pack_ignore.write_list(first_packs_known_words_content) second_pack.pack_ignore.write_list(second_packs_known_words_content) known_words_file_paths = [] for index, known_words_file_contents in enumerate( known_words_files_contents): known_words_file = first_pack._create_text_based( f"known_words_{index}.txt") known_words_file.write_list(known_words_file_contents) known_words_file_paths.append(known_words_file.path) unknown_word_calls_with_mocker = [] for unknown_words in unknown_word_calls: unknown_word_calls_with_mocker.append( mocker.call(unknown_words=unknown_words)) print_unknown_words = mocker.patch.object(DocReviewer, 'print_unknown_words') with ChangeCWD(repo.path): doc_reviewer = DocReviewer( file_paths=[first_rn_file.path, second_rn_file.path], known_words_file_paths=known_words_file_paths, load_known_words_from_pack=load_known_words_from_pack) assert doc_reviewer.run_doc_review() == review_success assert len(doc_reviewer.files) == 2 print_unknown_words.assert_has_calls(unknown_word_calls_with_mocker, any_order=True) assert len(doc_reviewer.files_with_misspells) == misspelled_files_num
def test_find_files_from_invalid_path(self): """ Given - invalid path file. When - trying to find files to do doc-reviews on. Then - Ensure no files are found. """ doc_review = DocReviewer(file_paths=['test']) assert doc_review.run_doc_review() assert not doc_review.files
def test_invalid_misspelled_files_with_no_failure( self, invalid_spelled_content_pack): """ Given - Pack files with invalid content spelling and 'no_failure' input parameter set to True. When - Running doc-review on misspelled files. Then - Ensure doc-review still succeeds. """ pack, _ = invalid_spelled_content_pack doc_reviewer = DocReviewer(file_paths=[pack.path], no_failure=True) assert doc_reviewer.run_doc_review()
def test_find_only_supported_files(self, valid_spelled_content_pack): """ Given - valid pack directory path. When - trying to find files from a directory. Then - Ensure the files that are found are only supported files. """ doc_review = DocReviewer(file_paths=[valid_spelled_content_pack.path]) doc_review.get_files_to_run_on( file_path=valid_spelled_content_pack.path) for file in doc_review.files: assert find_type(path=file) in doc_review.SUPPORTED_FILE_TYPES
def test_find_single_file(self, valid_spelled_content_pack): """ Given - valid integration yml file path. When - trying to find the file to do the doc-review on. Then - Ensure the file that was found exist in the directory. """ doc_review = DocReviewer( file_paths=[valid_spelled_content_pack.integrations[0].yml.path]) doc_review.get_files_to_run_on( file_path=valid_spelled_content_pack.integrations[0].yml.path) for file in doc_review.files: assert path.exists(file)
def test_find_files_from_dir(self, valid_spelled_content_pack): """ Given - valid pack directory path. When - trying to find files to do doc-reviews on. Then - Ensure the files that are found exist in the directory. """ # must set file path here it otherwise use_git=True doc_review = DocReviewer(file_paths=[valid_spelled_content_pack.path]) doc_review.get_files_to_run_on( file_path=valid_spelled_content_pack.path) for file in doc_review.files: assert path.exists(file)
def test_valid_spelled_files(self, valid_spelled_content_pack): """ Given - Pack files with valid content spelling. When - Running doc-review on correctly spelled files. Then - Ensure doc-review succeed and there aren't any misspelled files found. """ pack = valid_spelled_content_pack doc_reviewer = DocReviewer(file_paths=[pack.path]) assert doc_reviewer.run_doc_review() assert not doc_reviewer.found_misspelled assert len(doc_reviewer.files_with_misspells) == 0 assert doc_reviewer.files_with_misspells == set()
def test_invalid_misspelled_files_are_correct( self, invalid_spelled_content_pack): """ Given - Pack files with invalid content spelling. When - Running doc-review on misspelled files. Then - Ensure doc-review fails and all the misspelled files are found. """ pack, misspelled_files = invalid_spelled_content_pack doc_reviewer = DocReviewer(file_paths=[pack.path]) assert not doc_reviewer.run_doc_review() assert doc_reviewer.found_misspelled assert len(doc_reviewer.files_with_misspells) == len(misspelled_files) assert doc_reviewer.files_with_misspells == misspelled_files
def test_doc_review_is_performed_only_on_release_notes( self, valid_spelled_content_pack): """ Given - a pack When - Running doc-review with release-notes only. Then - Ensure The files that were doc-reviewed are only release-notes. """ doc_reviewer = DocReviewer( file_paths=[valid_spelled_content_pack.path], release_notes_only=True) assert doc_reviewer.run_doc_review() assert set(doc_reviewer.files) == { rn.path for rn in valid_spelled_content_pack.release_notes }
def test_failure_on_malformed_rns(self, pack): """ Given - Pack files with malformed release notes but correct spelling. When - Running doc-review. Then - Ensure malformed release notes are found. Ensure no misspelled words were found. Ensure doc-review returned False to indicate failure. """ rn = pack.create_release_notes( version="release-note-0", content="\n#### Script\n##### Script Name\n- blah blah") doc_reviewer = DocReviewer(file_paths=[pack.path]) result = doc_reviewer.run_doc_review() assert rn.path in doc_reviewer.malformed_rn_files assert not doc_reviewer.found_misspelled assert not result
def test_check_word_functionality(word, is_invalid_word, no_camelcase): """ Given - Case1: A misspelled word (including CamelCase words). Case2: A valid spelled word (including CamelCase words). Case3: A valid spelled CamelCase word with no_camel_case=True When - Checking word's spelling. Then - Case1: Ensure the word is part of the 'unknown' words. Case2: Ensure the word is not part of the 'unknown' words. Case3: Ensure the CamelCase word is part of the 'unknown' words (which means it's a misspelled word!). """ doc_reviewer = DocReviewer(no_camel_case=no_camelcase) doc_reviewer.check_word(word=word) if is_invalid_word: assert word in doc_reviewer.unknown_words else: assert word not in doc_reviewer.unknown_words
def test_doc_review_with_release_notes_is_skipped_on_invalid_json_file( self, malformed_incident_field: JSONBased): """ Given - malformed json incident field. When - Calling doc-review with --release-notes. Then - Ensure that no exception/error is raised and that the malformed files were not added to the files to review. """ _path = malformed_incident_field.path try: doc_reviewer = DocReviewer(file_paths=[_path], release_notes_only=True) assert doc_reviewer.run_doc_review() assert not doc_reviewer.files except ValueError as err: assert False, str(err)
def test_find_files_from_git(self, mocker, valid_spelled_content_pack): """ Given - valid pack directory path. When - trying to find files to do doc-reviews on from git. Then - Ensure the files that git reports are the same found that are meant to be doc-reviewed. """ changed_files_mock = [ valid_spelled_content_pack.integrations[0].yml.path, valid_spelled_content_pack.scripts[0].yml.path ] + [rn.path for rn in valid_spelled_content_pack.release_notes] mocker.patch.object(DocReviewer, 'gather_all_changed_files', return_value=changed_files_mock) doc_review = DocReviewer(use_git=True) doc_review.get_files_to_run_on(file_path='') assert set(doc_review.files) == set(changed_files_mock)
def test_supported_files_are_only_release_notes(self): """ Given - doc-reviewer with release-notes only. When - initializing doc-reviewer. Then - Ensure supported files contain only release-notes. """ assert DocReviewer(release_notes_only=True).SUPPORTED_FILE_TYPES == [ FileType.RELEASE_NOTES ]
def test_find_known_words_from_pack_ignore_integrations_name(repo): """ Given: - Pack's structure is correct and pack-ignore file is present. When: - Running DocReviewer.find_known_words_from_pack. Then: - Ensure the found path result is appropriate. - Ensure the integrations name are ignored. """ pack = repo.create_pack('test_pack') integration1 = pack.create_integration(name="first_integration") integration2 = pack.create_integration(name="second_integration") rn_file = pack.create_release_notes( version='1_0_0', content=f'{integration1.name}\n{integration2.name}') doc_reviewer = DocReviewer(file_paths=[]) with ChangeCWD(repo.path): found_known_words = doc_reviewer.find_known_words_from_pack( rn_file.path)[1] assert integration1.name in found_known_words assert integration2.name in found_known_words
def test_find_known_words_from_pack_ignore_commons_scripts_name(repo): """ Given: - Pack's structure is correct and pack-ignore file is present. - The scripts are in the old version (JS code), no Scripts' dir exists (only yml amd md files). When: - Running DocReviewer.find_known_words_from_pack. Then: - Ensure the found path result is appropriate. - Ensure the scripts names are ignored. - Ensure script readme name is not handled (bla.md) """ pack = repo.create_pack('test_pack') script1_name = 'script-first_script' # add a yml script directly into Scripts folder pack._create_yaml_based(name=script1_name, dir_path=f'{pack.path}//Scripts', content={'name': script1_name}) # add a .md file script directly into Scripts folder pack._create_text_based('bla.md', '', dir_path=Path(f'{pack.path}//Scripts')) # add a script into second_script folder script2 = pack.create_script(name='second_script') rn_file = pack.create_release_notes( version='1_0_0', content=f'{script1_name}\n{script2.name}') doc_reviewer = DocReviewer(file_paths=[]) with ChangeCWD(repo.path): found_known_words = doc_reviewer.find_known_words_from_pack( rn_file.path)[1] assert script1_name in found_known_words assert script2.name in found_known_words assert 'bla.md' not in found_known_words
def test_get_invalid_files_from_git_with_release_notes( self, mocker, malformed_integration_yml, malformed_incident_field): """ Given - malformed json/yml. When - Collecting files from git and release-notes is set to True. Then - Ensure that no exception/error is raised and that the malformed files were not added to the files for review. """ mocker.patch.object(DocReviewer, 'gather_all_changed_files', return_value=[ malformed_integration_yml.path, malformed_incident_field.path ]) try: doc_reviewer = DocReviewer(release_notes_only=True) doc_reviewer.get_files_from_git() assert not doc_reviewer.files except ValueError as err: assert False, str(err)