def test_app_parse_ebnf(bundle): """ Ensures App.parse supports specifying an ebnf """ App.parse('path', ebnf='ebnf') bt = Bundle.from_path().bundle_trees bt.assert_called_with(ebnf='ebnf', lower=False)
def test_app_compile_ebnf(patch, bundle): """ Ensures App.compile supports specifying an ebnf file """ patch.object(json, 'dumps') App.compile('path', ebnf='ebnf') Bundle.from_path().bundle.assert_called_with(ebnf='ebnf')
def test_app_compile_first_error(patch, bundle): """ Ensures that the App throws an error for --first with more than one story """ Bundle.from_path().bundle.return_value = {'stories': { 'my_story': 42, 'another_story': 43, }} patch.object(json, 'dumps') with raises(StoryError) as e: App.compile('path', first=True) assert e.value.message() == \ 'The option `--first`/-`f` can only be used if one story is complied.' Bundle.from_path.assert_called_with('path', ignored_path=None) Bundle.from_path().bundle.assert_called_with(ebnf=None)
def compile_app(app_name_for_analytics, debug) -> dict: """ Compiles, prints pretty info, and returns the compiled tree. :return: The compiled tree """ from storyscript.App import App click.echo(click.style('Compiling Stories...', bold=True)) with click_spinner.spinner(): try: stories = json.loads(App.compile(os.getcwd())) except BaseException: import traceback traceback.print_exc() click.echo('Failed', err=True) stories = None result = 'Success' count = 0 if stories is None: result = 'Failed' else: count = len(stories.get('stories', {})) cli.track('App Compiled', { 'App name': app_name_for_analytics, 'Result': result, 'Stories': count }) return stories
def test_cli_compile_json(runner, echo, app, option): """ Ensures --json outputs json """ runner.invoke(Cli.compile, [option]) App.compile.assert_called_with(os.getcwd(), ebnf=None, ignored_path=None) click.echo.assert_called_with(App.compile())
def test_app_compile(patch, bundle): patch.object(json, 'dumps') result = App.compile('path') Bundle.from_path.assert_called_with('path', ignored_path=None) Bundle.from_path().bundle.assert_called_with(ebnf=None) json.dumps.assert_called_with(Bundle.from_path().bundle(), indent=2) assert result == json.dumps()
def test_app_parse(bundle): """ Ensures App.parse returns the parsed bundle """ result = App.parse('path') Bundle.from_path.assert_called_with('path', ignored_path=None) Bundle.from_path().bundle_trees.assert_called_with(ebnf=None) assert result == Bundle.from_path().bundle_trees()
def test_cli_compile_output_file(patch, runner, app): """ Ensures the compile command supports specifying an output file. """ patch.object(io, 'open') runner.invoke(Cli.compile, ['/path', 'hello.story', '-j']) io.open.assert_called_with('hello.story', 'w') io.open().__enter__().write.assert_called_with(App.compile())
def test_cli_compile_output_file(patch, runner, app): """ Ensures the compile command supports specifying an output file. """ patch.object(io, "open") runner.invoke(Cli.compile, ["/path", "hello.story", "-j"]) io.open.assert_called_with("hello.story", "w") io.open().__enter__().write.assert_called_with(App.compile().results)
def test_app_compile_concise(patch, bundle): patch.object(json, 'dumps') patch.object(AppModule, '_clean_dict') result = App.compile('path', concise=True) Bundle.from_path.assert_called_with('path', ignored_path=None) Bundle.from_path().bundle.assert_called_with(ebnf=None) AppModule._clean_dict.assert_called_with(Bundle.from_path().bundle()) json.dumps.assert_called_with(AppModule._clean_dict(), indent=2) assert result == json.dumps()
def test_compile_app(runner, patch, init_sample_app_in_cwd, pre_init_cli_runner, nested_dir, force_compilation_error, compilation_exc): app_name_for_analytics = 'my_special_app' patch.object(cli, 'get_asyncy_yaml', return_value='asyncy_yml_content') patch.object(cli, 'track') patch.object(click, 'echo') if force_compilation_error: patch.object(App, 'compile', side_effect=compilation_exc) with runner.runner.isolated_filesystem(): init_sample_app_in_cwd() actual_project_root = os.getcwd() pre_init_cli_runner() from story.commands import test # nested_dir is used to specifically ensure that when compile_app # is executed in a sub directory under the main project, all stories # in the project are compiled, and not just the ones that were # in that sub directory. if nested_dir: os.chdir(f'{os.getcwd()}/src/b') actual_compilation_result = test.compile_app(app_name_for_analytics, False) if not force_compilation_error: os.chdir(actual_project_root) expected_compilation_result = json.loads(App.compile(os.getcwd())) expected_compilation_result['yaml'] = 'asyncy_yml_content' # Ugly assert, I know. Can't help it since we're not running this via # the runner. assert 'Compiling Stories' in click.echo.mock_calls[0][1][0] if force_compilation_error: assert actual_compilation_result is None assert 'Failed to compile project' in click.echo.mock_calls[1][1][0] err_line = click.echo.mock_calls[2][1][0] if isinstance(compilation_exc, StoryError): assert 'Please report at ' \ 'https://github.com/storyscript' \ '/storyscript/issues' in err_line else: assert str(compilation_exc) in err_line else: assert actual_compilation_result == expected_compilation_result cli.track.assert_called_with( 'App Compiled', { 'App name': app_name_for_analytics, 'Result': 'Failed' if force_compilation_error else 'Success', 'Stories': 0 if force_compilation_error else 2 })
def test_app_compile_first(patch, bundle): """ Ensures that the App only returns the first story """ Bundle.from_path().bundle.return_value = {'stories': {'my_story': 42}} patch.object(json, 'dumps') result = App.compile('path', first=True) Bundle.from_path.assert_called_with('path', ignored_path=None) Bundle.from_path().bundle.assert_called_with(ebnf=None) json.dumps.assert_called_with(42, indent=2) assert result == json.dumps()
def test_app_parse_lower(patch, bundle, magic): """ Ensures App.parse applies the loweror """ story = magic() Bundle.from_path().bundle_trees.return_value = {'foo.story': story} result = App.parse('path', lower=True) Bundle.from_path.assert_called_with('path', ignored_path=None) bt = Bundle.from_path().bundle_trees bt.assert_called_with(ebnf=None, lower=True) assert result == Bundle.from_path().bundle_trees(story)
def test_cli_compile_json(runner, echo, app, option): """ Ensures --json outputs json """ runner.invoke(Cli.compile, [option]) App.compile.assert_called_with('.', ebnf=None, ignored_path=None, concise=False, first=False, features={}) click.echo.assert_called_with(App.compile())
def test_app_parse_preprocess(patch, bundle, magic): """ Ensures App.parse applies the preprocessor """ patch.object(Preprocessor, 'process') story = magic() Bundle.from_path().bundle_trees.return_value = {'foo.story': story} result = App.parse('path', preprocess=True) assert Preprocessor.process.call_count == 1 Bundle.from_path.assert_called_with('path', ignored_path=None) Bundle.from_path().bundle_trees.assert_called_with(ebnf=None) assert result == {'foo.story': Preprocessor.process(story)}
def compile_app(app_name_for_analytics, debug) -> dict: """Compiles, prints pretty info, and returns the compiled tree. :return: The compiled tree """ from storyscript.App import App click.echo(click.style('Compiling Stories… ', bold=True)) old_cwd = os.getcwd() try: os.chdir(utils.get_project_root_dir()) stories = json.loads(App.compile(utils.get_project_root_dir())) except StoryError as e: click.echo('Failed to compile project:\n', err=True) click.echo(click.style(str(e.message()), fg='red'), err=True) stories = None except BaseException as e: click.echo('Failed to compile project:\n', err=True) click.echo(click.style(str(e), fg='red'), err=True) stories = None finally: os.chdir(old_cwd) result = 'Success' count = 0 if stories is None: result = 'Failed' else: count = len(stories.get('stories', {})) stories['yaml'] = cli.get_asyncy_yaml() cli.track( 'App Compiled', { 'App name': app_name_for_analytics, 'Result': result, 'Stories': count, }, ) return stories
def test_app_grammar(patch): patch.init(Grammar) patch.object(Grammar, 'build') assert App.grammar() == Grammar().build()
def test_app_lex_ebnf(bundle): App.lex('/path', ebnf='my.ebnf') Bundle.from_path().lex.assert_called_with(ebnf='my.ebnf')
def test_app_parse_ignored_path(bundle): App.parse('path', ignored_path='ignored') Bundle.from_path.assert_called_with('path', ignored_path='ignored')
def test_app_lex(bundle): result = App.lex('/path') Bundle.from_path.assert_called_with('/path') Bundle.from_path().lex.assert_called_with(ebnf=None) assert result == Bundle.from_path().lex()
def test_app_compile_ignored_path(patch, bundle): patch.object(json, 'dumps') App.compile('path', ignored_path='ignored') Bundle.from_path.assert_called_with('path', ignored_path='ignored')