def test_python_error_on_illustrate(self): """ Handle exceptions inside the Python module on __call__(). """ self.create_score() material_path = self.create_material('test_material') definition_path = material_path.joinpath('definition.py') with open(str(definition_path), 'w') as file_pointer: file_pointer.write( stringtools.normalize(r''' # -*- coding: utf-8 -*- from abjad.tools import abctools class Foo(object): def __illustrate__(self): raise TypeError('This is fake.') test_material = Foo() ''')) script = commandlinetools.ManageMaterialScript() command = ['--illustrate', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): with self.assertRaises(SystemExit) as context_manager: script(command) assert context_manager.exception.code == 1 self.compare_captured_output(r''' Illustration candidates: 'test_material' ... Illustrating test_score/materials/test_material/ Importing test_score.materials.test_material.definition '''.replace('/', os.path.sep))
def test_success_one_material(self, open_file_mock): self.create_score() self.create_material('test_material') script = commandlinetools.ManageMaterialScript() command = ['--illustrate', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: script(command) except SystemExit as e: raise RuntimeError('SystemExit: {}'.format(e.code)) self.compare_captured_output(r''' Illustration candidates: 'test_material' ... Illustrating test_score/materials/test_material/ Importing test_score.materials.test_material.definition Abjad runtime: ... second... Writing test_score/materials/test_material/illustration.ly ... OK! Writing test_score/materials/test_material/illustration.pdf ... OK! LilyPond runtime: ... second... Illustrated test_score/materials/test_material/ '''.replace('/', os.path.sep)) self.compare_path_contents(self.materials_path, self.expected_files) illustration_path = self.materials_path.joinpath( 'test_material', 'illustration.ly') self.compare_lilypond_contents( illustration_path, self.expected_illustration_contents, )
def test_python_cannot_illustrate(self): """ Handle un-illustrables. """ self.create_score() material_path = self.create_material('test_material') definition_path = material_path.joinpath('definition.py') with open(str(definition_path), 'w') as file_pointer: file_pointer.write( stringtools.normalize(r''' # -*- coding: utf-8 -*- test_material = None ''')) script = commandlinetools.ManageMaterialScript() command = ['--illustrate', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): with self.assertRaises(SystemExit) as context_manager: script(command) assert context_manager.exception.code == 1 self.compare_captured_output(r''' Illustration candidates: 'test_material' ... Illustrating test_score/materials/test_material/ Importing test_score.materials.test_material.definition Cannot illustrate material of type NoneType. '''.replace('/', os.path.sep))
def create_material( self, material_name='test_material', force=False, expect_error=False, ): script = commandlinetools.ManageMaterialScript() command = ['--new', material_name] if force: command.insert(0, '-f') with systemtools.TemporaryDirectoryChange(str(self.score_path)): if expect_error: with self.assertRaises(SystemExit) as context_manager: script(command) assert context_manager.exception.code == 1 else: try: script(command) except SystemExit: raise RuntimeError('SystemExit') return self.score_path.joinpath( self.score_path.name, 'materials', material_name, )
def illustrate_material(self, material_name): script = commandlinetools.ManageMaterialScript() command = ['--illustrate', material_name] with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: script(command) except SystemExit as e: raise RuntimeError('SystemExit: {}'.format(e.code))
def test_materials(material_name): materials = importlib.import_module('{}.materials'.format(root_name)) material = getattr(materials, material_name, None) if not material: pytest.skip('No nominative material.') elif not hasattr(material, '__illustrate__'): pytest.skip('No illustratable material.') with systemtools.TemporaryDirectoryChange(str(test_path)): script = commandlinetools.ManageMaterialScript() command = ['--illustrate', material_name] with mock.patch('abjad.systemtools.IOManager.open_file'): script(command)
def test_list_materials_no_materials(self): self.create_score() script = commandlinetools.ManageMaterialScript() command = ['--list'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): with self.assertRaises(SystemExit) as context_manager: script(command) assert context_manager.exception.code == 2 self.compare_captured_output(r''' Available materials: No materials available. ''')
def test_success_all_materials(self, open_file_mock): self.create_score() self.create_material('material_one') self.create_material('material_two') self.create_material('material_three') script = commandlinetools.ManageMaterialScript() command = ['--illustrate', '*'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: script(command) except SystemExit as e: raise RuntimeError('SystemExit: {}'.format(e.code)) self.compare_captured_output(r''' Illustration candidates: '*' ... Illustrating test_score/materials/material_one/ Importing test_score.materials.material_one.definition Abjad runtime: ... second... Writing test_score/materials/material_one/illustration.ly ... OK! Writing test_score/materials/material_one/illustration.pdf ... OK! LilyPond runtime: ... second... Illustrated test_score/materials/material_one/ Illustrating test_score/materials/material_three/ Importing test_score.materials.material_three.definition Abjad runtime: ... second... Writing test_score/materials/material_three/illustration.ly ... OK! Writing test_score/materials/material_three/illustration.pdf ... OK! LilyPond runtime: ... second... Illustrated test_score/materials/material_three/ Illustrating test_score/materials/material_two/ Importing test_score.materials.material_two.definition Abjad runtime: ... second... Writing test_score/materials/material_two/illustration.ly ... OK! Writing test_score/materials/material_two/illustration.pdf ... OK! LilyPond runtime: ... second... Illustrated test_score/materials/material_two/ '''.replace('/', os.path.sep)) assert self.materials_path.joinpath( 'material_one', 'illustration.pdf', ).exists() assert self.materials_path.joinpath( 'material_two', 'illustration.pdf', ).exists() assert self.materials_path.joinpath( 'material_three', 'illustration.pdf', ).exists()
def test_lilypond_error(self): """ Handle failing LilyPond rendering. """ self.create_score() material_path = self.create_material('test_material') definition_path = material_path.joinpath('definition.py') with open(str(definition_path), 'w') as file_pointer: file_pointer.write( stringtools.normalize(r''' # -*- coding: utf-8 -*- from abjad.tools import lilypondfiletools test_material = lilypondfiletools.make_basic_lilypond_file() test_material.items.append(r'\this-does-not-exist') ''')) script = commandlinetools.ManageMaterialScript() command = ['--illustrate', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): with self.assertRaises(SystemExit) as context_manager: script(command) assert context_manager.exception.code == 1 self.compare_captured_output(r''' Illustration candidates: 'test_material' ... Illustrating test_score/materials/test_material/ Importing test_score.materials.test_material.definition Abjad runtime: ... second... Writing test_score/materials/test_material/illustration.ly ... OK! Writing test_score/materials/test_material/illustration.pdf ... Failed! '''.replace('/', os.path.sep)) illustration_ly_path = material_path.joinpath('illustration.ly') assert illustration_ly_path.exists() self.compare_lilypond_contents( illustration_ly_path, stringtools.normalize(r''' \language "english" \header {} \layout {} \paper {} \this-does-not-exist '''))
def test_success(self): self.create_score() script = commandlinetools.ManageMaterialScript() command = ['--new', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: script(command) except SystemExit: raise RuntimeError('SystemExit') self.compare_captured_output(r''' Creating material subpackage 'test_material' ... Reading test_score/metadata.json ... OK! Created test_score/materials/test_material/ '''.replace('/', os.path.sep)) assert self.materials_path.joinpath('test_material').exists() self.compare_path_contents(self.materials_path, self.expected_files)
def test_missing_definition(self): """ Handle missing definition. """ self.create_score() material_path = self.create_material('test_material') definition_path = material_path.joinpath('definition.py') definition_path.unlink() script = commandlinetools.ManageMaterialScript() command = ['--illustrate', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): with self.assertRaises(SystemExit) as context_manager: script(command) assert context_manager.exception.code == 1 self.compare_captured_output(r''' Illustration candidates: 'test_material' ... Illustrating test_score/materials/test_material/ Importing test_score.materials.test_material.definition '''.replace('/', os.path.sep))
def test_python_error_on_import(self): """ Handle exceptions inside the Python module on import. """ self.create_score() material_path = self.create_material('test_material') definition_path = material_path.joinpath('definition.py') with open(str(definition_path), 'a') as file_pointer: file_pointer.write('\n\nfailure = 1 / 0\n') script = commandlinetools.ManageMaterialScript() command = ['--illustrate', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): with self.assertRaises(SystemExit) as context_manager: script(command) assert context_manager.exception.code == 1 self.compare_captured_output(r''' Illustration candidates: 'test_material' ... Illustrating test_score/materials/test_material/ Importing test_score.materials.test_material.definition '''.replace('/', os.path.sep))
def test_success(self, call_subprocess_mock): call_subprocess_mock.return_value = 0 self.create_score() material_path = self.create_material('test_material') script = commandlinetools.ManageMaterialScript() command = ['--edit', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: script(command) except SystemExit as e: raise RuntimeError('SystemExit: {}'.format(e.code)) self.compare_captured_output(r''' Edit candidates: 'test_material' ... ''') definition_path = material_path.joinpath('definition.py') command = '{} {!s}'.format( abjad_configuration.get_text_editor(), definition_path, ) call_subprocess_mock.assert_called_with(command)
def test_success_one_material(self, open_file_mock): self.create_score() material_path = self.create_material('test_material') self.illustrate_material('test_material') pdf_path = material_path.joinpath('illustration.pdf') assert pdf_path.exists() pdf_path.unlink() script = commandlinetools.ManageMaterialScript() command = ['--render', 'test_material'] with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: script(command) except SystemExit as e: raise RuntimeError('SystemExit: {}'.format(e.code)) self.compare_captured_output(r''' Rendering candidates: 'test_material' ... Rendering test_score/materials/test_material/ Writing test_score/materials/test_material/illustration.pdf ... OK! LilyPond runtime: ... second... Rendered test_score/materials/test_material/ '''.replace('/', os.path.sep)) self.compare_path_contents(self.materials_path, self.expected_files)