def test_conflict_dir(self): """Ignore a directory when it is in both the search and ignore lists""" parseables = fixture_dir('parsing', 'characters') ignore_me = fixture_dir('parsing', 'characters') characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) assert not len(list(characters))
def test_ignore_dir(self): parseables = fixture_dir('parsing', 'characters') ignore_me = fixture_dir('parsing', 'characters', 'Fetches') characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) for c in characters: assert c.tags('type')[0] != 'Fetch'
def test_ignore_file(self): parseables = fixture_dir('parsing', 'characters') ignore_me = fixture_dir('parsing', 'characters', 'Changelings', 'Kabana Matansa.nwod') characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) for c in characters: assert 'Kabana Matansa' not in c.tags['name']
def test_conflict_file(self, ): """Always parse a file when it is in the search and ignore lists""" parseables = fixture_dir('parsing', 'characters', 'Changelings', 'Kabana Matansa.nwod') ignore_me = fixture_dir('parsing', 'characters', 'Changelings', 'Kabana Matansa.nwod') characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) assert len(list(characters)) == 1
def test_extra_md_metadata(self, prefs, metaformat, tmpdir): """All metadata formats for the markdown type should show the extra metadata for the markdown type from the imported settings.""" outfile = tmpdir.join("output.md") prefs.load_more(fixture_dir(['listing', 'settings-metadata.json'])) search = fixture_dir(['listing', 'valid-json']) npc.commands.listing(search, fmt='markdown', metadata=metaformat, outfile=str(outfile), prefs=prefs) assert 'test-type: markdown' in outfile.read().lower()
def test_extra_md_metadata(prefs, metaformat, tmp_path): """All metadata formats for the markdown type should show the extra metadata for the markdown type from the imported settings.""" outfile = tmp_path / 'output.md' prefs.load_more(fixture_dir('listing', 'settings-metadata.json')) search = fixture_dir('listing', 'valid-json') result = npc.commands.listing.make_list(search, fmt='markdown', metadata=metaformat, outfile=str(outfile), prefs=prefs) assert result.errmsg == '' assert result.success == True assert 'test-type: markdown' in outfile.read_text().lower()
def test_valid_formats(self, format_name): listing = Listing(metadata_format=format_name, metadata=self.metadata) assert listing.result with open( fixture_dir('listing', 'html', 'metadata', "metadata-{}.txt".format(format_name)), 'r') as f: assert f.read() in listing.output
def test_md_mmd_metadata(tmp_path, metaformat): """The 'mmd' and 'multimarkdown' metadata args should prepend multi-markdown metadata tags to the markdown output.""" outfile = tmp_path / 'output.md' search = fixture_dir('listing', 'valid-json') npc.commands.listing.make_list(search, fmt='markdown', metadata=metaformat, outfile=str(outfile)) assert 'Title: NPC Listing' in outfile.read_text()
def make_list(*search_parts, outformat='json', metadata=None, prefs=prefs, title=None): tmpdir = tmp_path / 'list' tmpdir.mkdir() outfile = tmpdir / 'output.json' outfile.touch() search = fixture_dir('listing', *search_parts) npc.commands.listing.make_list(search, fmt=outformat, metadata=metadata, outfile=str(outfile), prefs=prefs, title=title) return load_json(outfile)
def test_default_table_format(): data_out = io.StringIO() tables = {'type': Counter(['human', 'human', 'elf'])} assert npc.formatters.markdown.report(tables, data_out) with open(fixture_dir('formatters', 'markdown', 'report.txt')) as table_fixture: assert data_out.getvalue() == table_fixture.read()
def test_md_mmd_metadata(self, tmpdir): """The 'mmd' metadata arg should prepend multi-markdown metadata tags to the markdown output.""" outfile = tmpdir.join("output.md") search = fixture_dir(['listing', 'valid-json']) npc.commands.listing(search, fmt='markdown', metadata='mmd', outfile=str(outfile)) assert 'Title: NPC Listing' in outfile.read()
def test_extra_json_metadata(self, list_json_output, prefs): """The extra json metdata should be included for the json output type.""" prefs.load_more(fixture_dir(['listing', 'settings-metadata.json'])) listing = list_json_output(['valid-json'], metadata='json', prefs=prefs) for c in listing: if 'meta' in c: assert c['test'] == 'yes' assert c['test-type'] == 'json'
def test_progress_callback(tmpdir, capsys): """The progress callback should be called for every character""" outfile = tmpdir.join("output.md") search = fixture_dir('listing', 'valid-json') npc.commands.listing.make_list(search, fmt='markdown', outfile=str(outfile), progress=lambda i, t: print("{} of {}".format(i, t), file=sys.stderr)) _, errtext = capsys.readouterr() assert "0 of 3\n1 of 3\n2 of 3\n3 of 3\n" == errtext
def test_default_table_format(): data_out = io.BytesIO() tables = {'type': Counter(['human', 'human', 'elf'])} assert npc.formatters.html.report(tables, data_out) with open(fixture_dir('formatters', 'html', 'report.txt')) as table_fixture: assert data_out.getvalue().decode() == table_fixture.read()
def lint_changeling(filename, **kwargs): prefs = npc.settings.Settings() char_file = fixture_dir('linter', 'changeling', filename) character = npc.parser.parse_character(char_file) problems = npc.linters.changeling.lint(character, sk_data=prefs.get('changeling'), **kwargs) return '/'.join(problems)
def test_extra_json_metadata(list_json_output, prefs): """The extra json metdata should be included for the json output type.""" prefs.load_more(fixture_dir('listing', 'settings-metadata.json')) listing = list_json_output('valid-json', metadata='json', prefs=prefs) for c in listing: if 'meta' in c: assert c['test'] == 'yes' assert c['test-type'] == 'json'
def make_report(tags, *search_parts, outformat='json', prefs=prefs): outfile = tmp_path / 'output.json' search = fixture_dir('report', *search_parts) npc.commands.report(tags, search=[search], fmt=outformat, outfile=str(outfile), prefs=prefs) return load_json(outfile)
def populate_from_fixture_dir(self, fixture_path): """Copy the files and folders from a fixture directory into the campaign root. The path given should be relative to the npc/tests/fixtures directory in this package. It will be run through `fixture_dir` first. """ srcpath = fixture_dir(fixture_path) dir_util.copy_tree(srcpath, str(self.basedir))
def test_invalid_metadata_arg(self): """Using metadata formats other than yfm and mmd is not supported for the markdown output type. The json output type ignores the format argument entirely, which is not tested. """ search = fixture_dir(['listing', 'valid-json']) result = npc.commands.listing(search, fmt='md', metadata='json') assert not result.success
def test_invalid_metadata_arg(): """Using metadata formats other than yfm and mmd is not supported for the markdown output type. The json output type ignores the format argument entirely, which is not tested. """ search = fixture_dir('listing', 'valid-json') result = npc.commands.listing.make_list(search, fmt='md', metadata='json') assert not result.success
def test_md_yfm_metadata(metaformat, tmp_path): """The 'yfm' and 'yaml' metadata args should both result in YAML front matter being prepended to markdown output.""" outfile = tmp_path / 'output.md' search = fixture_dir('listing', 'valid-json') npc.commands.listing.make_list(search, fmt='markdown', metadata=metaformat, outfile=str(outfile)) match = re.match(r'(?sm)\s*---(.*)---\s*', outfile.read_text()) assert match is not None assert 'title: NPC Listing' in match.group(1)
def test_md_yfm_metadata(self, metaformat, tmpdir): """The 'yfm' and 'yaml' metadata args should both result in YAML front matter being prepended to markdown output.""" outfile = tmpdir.join("output.md") search = fixture_dir(['listing', 'valid-json']) npc.commands.listing(search, fmt='markdown', metadata=metaformat, outfile=str(outfile)) match = re.match('(?sm)\s*---(.*)---\s*', outfile.read()) assert match is not None assert 'title: NPC Listing' in match.group(1)
def test_sort(list_json_output): """Tests that the dumped, sorted output identical to the internal sorted data""" raw_data = npc.parser.get_characters(search_paths=[fixture_dir(['dump'])]) sorted_data = npc.commands._sort_chars(raw_data) dump_data = list_json_output(sort=True) it = iter(sorted_data) with pytest.raises(StopIteration) as e: i = next(it) for x in dump_data: if x == i: i = next(it)
def populate_from_fixture_dir(self, *fixture_path): """ Copy the files and folders from a fixture directory into the campaign root. Args: *fixture_path (str): Path elements to combine. All should be under the npc/tests/fixtures directory in this package. It will be run through `util.fixture_dir` first. """ srcpath = fixture_dir(*fixture_path) dir_util.copy_tree(srcpath, str(self.basedir))
def make_character(filename): parseables = fixture_dir(['parsing', 'tags', filename]) characters = list(npc.parser.get_characters(search_paths=[parseables])) return characters[0]
def test_missing_vice(): char_file = fixture_dir('linter', 'nwod', 'Gotta Nada.nwod') with open(char_file, 'r') as char_file: problems = npc.linters.nwod.lint_vice_virtue(char_file.read()) assert 'Missing vice' in problems
def test_ignore_dir(self): parseables = fixture_dir(['parsing', 'characters']) ignore_me = fixture_dir(['parsing', 'characters', 'Fetches']) characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) for c in characters: assert c.get_first('type') != 'Fetch'
def make_list(*search_parts, outformat='json', metadata=None, prefs=prefs, title=None): tmpdir = tmpdir_factory.mktemp('list') outfile = tmpdir.join("output.json") search = fixture_dir('listing', *search_parts) npc.commands.listing.make_list(search, fmt=outformat, metadata=metadata, outfile=str(outfile), prefs=prefs, title=title) return json.load(outfile)
def test_output_to_file(argparser, tmpdir): outfile = tmpdir.join("output.json") search = fixture_dir(['dump']) npc.commands.dump(search, outfile=str(outfile)) assert outfile.read()
def make_list(search_parts, outformat='json', metadata=None, prefs=prefs, title=None): outfile = tmpdir.join("output.json") search = fixture_dir(['listing'] + search_parts) npc.commands.listing(search, fmt=outformat, metadata=metadata, outfile=str(outfile), prefs=prefs, title=title) return json.load(outfile)
def test_ignore_file(self): parseables = fixture_dir(['parsing', 'characters']) ignore_me = fixture_dir(['parsing', 'characters', 'Changelings', 'Kabana Matansa.nwod']) characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) for c in characters: assert 'Kabana Matansa' not in c['name']
def test_output_no_file(capsys, outopt): search = fixture_dir(['listing', 'valid-json']) npc.commands.listing(search, outfile=outopt) output, _ = capsys.readouterr() assert output
def test_conflict_file(self, ): """Always parse a file when it is in the search and ignore lists""" parseables = fixture_dir(['parsing', 'characters', 'Changelings', 'Kabana Matansa.nwod']) ignore_me = fixture_dir(['parsing', 'characters', 'Changelings', 'Kabana Matansa.nwod']) characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) assert len(list(characters)) == 1
def test_remove_filename_comments(): parseables = fixture_dir(['parsing', 'characters', 'Fetches']) characters = list(npc.parser.get_characters(search_paths=[parseables])) assert characters[0]['name'][0] == 'macho mannersson'
def do_lint(charname, strict=False): search = fixture_dir(['linter', 'characters', 'Humans', charname]) npc.commands.lint(search, strict=strict) output, _ = capsys.readouterr() return output
def test_allow_apostrophes_in_names(self): parseables = fixture_dir('parsing', 'characters', 'Fetches', "manny o'mann - super faker.nwod") characters = list(npc.parser.get_characters(search_paths=[parseables])) assert characters[0].tags('name')[0] == "manny o'mann"
def test_allow_commas_in_names(self): parseables = fixture_dir('parsing', 'characters', 'Fetches', "Manners Mann, Ph.D. - fakierest.nwod") characters = list(npc.parser.get_characters(search_paths=[parseables])) assert characters[0].tags('name')[0] == "Manners Mann, Ph.D."
def test_allow_hyphens_in_names(self): parseables = fixture_dir('parsing', 'characters', 'Fetches', "Manny Manly-Mann - fakiest.nwod") characters = list(npc.parser.get_characters(search_paths=[parseables])) assert characters[0].tags('name')[0] == "Manny Manly-Mann"
def test_output_to_file(tmpdir): outfile = tmpdir.join("output.json") search = fixture_dir(['listing', 'valid-json']) npc.commands.listing(search, outfile=str(outfile)) assert outfile.read()
def test_remove_filename_comments(): parseables = fixture_dir('parsing', 'characters', 'Fetches', 'macho mannersson - faker.nwod') characters = list(npc.parser.get_characters(search_paths=[parseables])) assert characters[0].tags('name')[0] == 'macho mannersson'
def make_character(filename): parseables = fixture_dir('parsing', 'tags', filename) characters = list( npc.parser.get_characters(search_paths=[parseables])) return characters[0]
def test_ignore_comments(self): filepath = fixture_dir('util', 'load_json', 'commented.json') loaded = util.load_json(filepath) assert "freedom" in loaded["data"]
def test_conflict_dir(self): """Ignore a directory when it is in both the search and ignore lists""" parseables = fixture_dir(['parsing', 'characters']) ignore_me = fixture_dir(['parsing', 'characters']) characters = npc.parser.get_characters(search_paths=[parseables], ignore_paths=[ignore_me]) assert not len(list(characters))
def test_ignore_trailing_commas(self): filepath = fixture_dir('util', 'load_json', 'trailing_commas.json') loaded = util.load_json(filepath) assert "freedom" in loaded["data"] assert ", }" in loaded['list']
def test_dump_matches_internal(list_json_output): dump_data = list_json_output() raw_data = npc.parser.get_characters(search_paths=[fixture_dir(['dump'])]) assert dump_data == list(raw_data)
def test_bad_syntax(self): filepath = fixture_dir('util', 'load_json', 'bad_syntax.json') with pytest.raises(json.decoder.JSONDecodeError) as err: loaded = util.load_json(filepath) assert "Bad syntax" in err.value.nicemsg
def test_output_no_file(capsys, outopt): search = fixture_dir(['dump']) npc.commands.dump(search, outfile=outopt) output, _ = capsys.readouterr() assert output
def test_open_files(prefs): override_path = fixture_dir('util', 'open_files', 'settings-echo.json') prefs.load_more(override_path) result = util.open_files('not a real path', 'seriously, no', prefs=prefs) assert 'not a real path' in result.args assert 'seriously, no' in result.args
def make_list(search_parts=[], metadata=False, sort=False): outfile = tmpdir.join("output.json") search = fixture_dir(['dump'] + search_parts) npc.commands.dump(search, outfile=str(outfile), metadata=metadata, sort=sort) return json.load(outfile)
def test_output_no_file(capsys, outopt): search = fixture_dir(['report', 'valid-json']) npc.commands.report('type', search=[search], outfile=outopt) output, _ = capsys.readouterr() assert output
def test_has_virtue(): char_file = fixture_dir('linter', 'nwod', 'Has Virtue.nwod') with open(char_file, 'r') as char_file: problems = npc.linters.nwod.lint_vice_virtue(char_file.read()) assert 'Missing virtue' not in problems
def test_unknown_metadata_arg(self): """Unrecognized metadata options should result in an error""" search = fixture_dir(['listing', 'valid-json']) result = npc.commands.listing(search, fmt='md', metadata='asdf') assert not result.success
def test_hides_footer(self, prefs): prefs.load_more( fixture_dir('listing', 'html', 'footer', 'settings.json')) listing = Listing(partial=True, prefs=prefs) assert listing.result assert "I'm footer content! Woohoo!" not in listing.output
def make_report(tags, search_parts, outformat='json', prefs=prefs): outfile = tmpdir.join("output.json") search = fixture_dir(['report'] + search_parts) npc.commands.report(tags, search=[search], fmt=outformat, outfile=str(outfile), prefs=prefs) return json.load(outfile)