def create_segment( self, segment_name='test_segment', force=False, expect_error=False, ): script = commandlinetools.ManageSegmentScript() command = ['--new', segment_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, 'segments', segment_name, )
def test_success_one_segment(self, open_file_mock): self.create_score() self.create_segment('test_segment') script = commandlinetools.ManageSegmentScript() command = ['--illustrate', 'test_segment'] 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_segment' ... Reading test_score/segments/metadata.json ... OK! Illustrating test_score/segments/test_segment/ Reading test_score/segments/metadata.json ... OK! Reading test_score/segments/test_segment/metadata.json ... JSON does not exist. Importing test_score.segments.test_segment.definition Writing test_score/segments/test_segment/metadata.json Abjad runtime: ... second... Writing test_score/segments/test_segment/illustration.ly ... OK! Writing test_score/segments/test_segment/illustration.pdf ... OK! LilyPond runtime: ... second... Illustrated test_score/segments/test_segment/ '''.replace('/', os.path.sep)) self.compare_path_contents(self.segments_path, self.expected_files) illustration_path = self.segments_path.joinpath( 'test_segment', 'illustration.ly') self.compare_lilypond_contents( illustration_path, self.expected_illustration_contents, )
def test_success(self): self.create_score() script = commandlinetools.ManageSegmentScript() try: names = script._read_segments_list_json( self.score_path, verbose=False, ) assert names == [] except SystemExit: raise RuntimeError('SystemExit') command = ['--new', 'test_segment'] 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 segment subpackage 'test_segment' ... Reading test_score/metadata.json ... OK! Reading test_score/segments/metadata.json ... JSON does not exist. Writing test_score/segments/metadata.json Created test_score/segments/test_segment/ '''.replace('/', os.path.sep)) assert self.segments_path.joinpath('test_segment').exists() self.compare_path_contents(self.segments_path, self.expected_files) try: names = script._read_segments_list_json( self.score_path, verbose=False, ) assert names == ['test_segment'] except SystemExit: raise RuntimeError('SystemExit')
def test_list_segments_unstaged(self): self.create_score() self.create_segment('segment_one') self.create_segment('segment_two') self.create_segment('segment_three') script = commandlinetools.ManageSegmentScript() segment_names = script._read_segments_list_json( self.score_path, verbose=False, ) segment_names.remove('segment_two') script._write_segments_list_json( segment_names, score_path=self.score_path, verbose=False, ) 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 segments: Reading test_score/segments/metadata.json ... OK! segment_one [1] segment_three [2] segment_two '''.replace('/', os.path.sep))
def illustrate_segment(self, segment_name): script = commandlinetools.ManageSegmentScript() command = ['--illustrate', segment_name] with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: script(command) except SystemExit as e: raise RuntimeError('SystemExit: {}'.format(e.code))
def test_success_filtered_segments(self, open_file_mock): self.create_score() self.create_segment('segment_one') self.create_segment('segment_two') self.create_segment('segment_three') script = commandlinetools.ManageSegmentScript() command = ['--illustrate', 'segment_t*'] 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: 'segment_t*' ... Reading test_score/segments/metadata.json ... OK! Illustrating test_score/segments/segment_two/ Reading test_score/segments/metadata.json ... OK! Reading test_score/segments/segment_one/metadata.json ... JSON does not exist. Reading test_score/segments/segment_two/metadata.json ... JSON does not exist. Importing test_score.segments.segment_two.definition Writing test_score/segments/segment_two/metadata.json Abjad runtime: ... second... Writing test_score/segments/segment_two/illustration.ly ... OK! Writing test_score/segments/segment_two/illustration.pdf ... OK! LilyPond runtime: ... second... Illustrated test_score/segments/segment_two/ Illustrating test_score/segments/segment_three/ Reading test_score/segments/metadata.json ... OK! Reading test_score/segments/segment_two/metadata.json ... OK! Reading test_score/segments/segment_three/metadata.json ... JSON does not exist. Importing test_score.segments.segment_three.definition Writing test_score/segments/segment_three/metadata.json Abjad runtime: ... second... Writing test_score/segments/segment_three/illustration.ly ... OK! Writing test_score/segments/segment_three/illustration.pdf ... OK! LilyPond runtime: ... second... Illustrated test_score/segments/segment_three/ '''.replace('/', os.path.sep)) assert not self.segments_path.joinpath( 'segment_one', 'illustration.pdf', ).exists() assert self.segments_path.joinpath( 'segment_two', 'illustration.pdf', ).exists() assert self.segments_path.joinpath( 'segment_three', 'illustration.pdf', ).exists()
def test_list_segments_no_segments(self): self.create_score() script = commandlinetools.ManageSegmentScript() 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 segments: Reading test_score/segments/metadata.json ... JSON does not exist. No segments available. '''.replace('/', os.path.sep))
def test_success(self, call_subprocess_mock): call_subprocess_mock.return_value = 0 self.create_score() self.create_segment('segment_a') self.create_segment('segment_b') self.create_segment('segment_c') script = commandlinetools.ManageSegmentScript() command = ['--stage'] 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''' Staging segments: Reading test_score/segments/metadata.json ... OK! Staged: segment_a segment_b segment_c '''.replace('/', os.path.sep)) call_subprocess_mock.assert_called_with( '{} segments.txt'.format(abjad_configuration.get_text_editor()), ) call_subprocess_mock.side_effect = self.side_effect self.reset_string_io() 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''' Staging segments: Reading test_score/segments/metadata.json ... OK! Writing test_score/segments/metadata.json Staged: segment_c segment_b segment_a '''.replace('/', os.path.sep)) call_subprocess_mock.assert_called_with( '{} segments.txt'.format(abjad_configuration.get_text_editor()), )
def test_internal_path(self): self.create_score() script = commandlinetools.ManageSegmentScript() command = ['--new', 'test_segment'] internal_path = self.score_path.joinpath('test_score', 'build') assert internal_path.exists() with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(internal_path)): try: script(command) except SystemExit: raise RuntimeError('SystemExit') self.compare_captured_output(r''' Creating segment subpackage 'test_segment' ... Reading test_score/metadata.json ... OK! Reading test_score/segments/metadata.json ... JSON does not exist. Writing test_score/segments/metadata.json Created test_score/segments/test_segment/ '''.replace('/', os.path.sep))
def test_python_error_on_illustrate(self): """ Handle exceptions inside the Python module on __call__(). """ self.create_score() segment_path = self.create_segment('test_segment') definition_path = segment_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 FaultySegmentMaker(abctools.AbjadObject): def __call__( self, segment_metadata=None, previous_segment_metadata=None, ): raise TypeError('This is intentionally broken.') segment_maker = FaultySegmentMaker() ''')) script = commandlinetools.ManageSegmentScript() command = ['--illustrate', 'test_segment'] 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_segment' ... Reading test_score/segments/metadata.json ... OK! Illustrating test_score/segments/test_segment/ Reading test_score/segments/metadata.json ... OK! Reading test_score/segments/test_segment/metadata.json ... JSON does not exist. Importing test_score.segments.test_segment.definition '''.replace('/', os.path.sep))
def test_success(self, call_subprocess_mock): call_subprocess_mock.return_value = 0 self.create_score() segment_path = self.create_segment('test_segment') script = commandlinetools.ManageSegmentScript() command = ['--edit', 'test_segment'] 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_segment' ... Reading test_score/segments/metadata.json ... OK! '''.replace('/', os.path.sep)) definition_path = segment_path.joinpath('definition.py') command = '{} {!s}'.format( abjad_configuration.get_text_editor(), definition_path, ) call_subprocess_mock.assert_called_with(command)
def test_missing_definition(self): """ Handle missing definition. """ self.create_score() segment_path = self.create_segment('test_segment') definition_path = segment_path.joinpath('definition.py') definition_path.unlink() script = commandlinetools.ManageSegmentScript() command = ['--illustrate', 'test_segment'] 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_segment' ... Reading test_score/segments/metadata.json ... OK! Illustrating test_score/segments/test_segment/ Reading test_score/segments/metadata.json ... OK! Reading test_score/segments/test_segment/metadata.json ... JSON does not exist. Importing test_score.segments.test_segment.definition '''.replace('/', os.path.sep))
def test_python_error_on_import(self): """ Handle exceptions inside the Python module on import. """ self.create_score() segment_path = self.create_segment('test_segment') definition_path = segment_path.joinpath('definition.py') with open(str(definition_path), 'a') as file_pointer: file_pointer.write('\n\nfailure = 1 / 0\n') script = commandlinetools.ManageSegmentScript() command = ['--illustrate', 'test_segment'] 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_segment' ... Reading test_score/segments/metadata.json ... OK! Illustrating test_score/segments/test_segment/ Reading test_score/segments/metadata.json ... OK! Reading test_score/segments/test_segment/metadata.json ... JSON does not exist. Importing test_score.segments.test_segment.definition '''.replace('/', os.path.sep))
def test_success_one_segment(self, open_file_mock): self.create_score() segment_path = self.create_segment('test_segment') self.illustrate_segment('test_segment') pdf_path = segment_path.joinpath('illustration.pdf') assert pdf_path.exists() pdf_path.unlink() script = commandlinetools.ManageSegmentScript() command = ['--render', 'test_segment'] 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_segment' ... Reading test_score/segments/metadata.json ... OK! Rendering test_score/segments/test_segment/ Writing test_score/segments/test_segment/illustration.pdf ... OK! LilyPond runtime: ... second... Rendered test_score/segments/test_segment/ '''.replace('/', os.path.sep)) self.compare_path_contents(self.segments_path, self.expected_files)
def collect_segments(self): script = commandlinetools.ManageSegmentScript() command = ['--collect'] with systemtools.TemporaryDirectoryChange(str(self.score_path)): script(command)
def illustrate_segments(self): script = commandlinetools.ManageSegmentScript() command = ['--illustrate', '*'] with systemtools.TemporaryDirectoryChange(str(self.score_path)): script(command)
def test_success(self, open_file_mock): self.create_score() self.create_segment('segment_one') self.create_segment('segment_two') self.create_segment('segment_three') self.illustrate_segments() collect_script = commandlinetools.ManageSegmentScript() with systemtools.RedirectedStreams(stdout=self.string_io): with systemtools.TemporaryDirectoryChange(str(self.score_path)): try: collect_script(['--collect']) except SystemExit as e: raise RuntimeError('SystemExit: {}'.format(e.code)) self.compare_captured_output(r''' Collecting segments: segments/segment_one/illustration.ly --> build/segments/segment-one.ily segments/segment_three/illustration.ly --> build/segments/segment-three.ily segments/segment_two/illustration.ly --> build/segments/segment-two.ily Reading test_score/segments/metadata.json ... OK! '''.replace('/', os.path.sep)) self.compare_path_contents(self.build_path, self.expected_files) path = self.build_path.joinpath('segments.ily') self.compare_lilypond_contents( path, r''' { \include "../segments/segment-one.ily" \include "../segments/segment-two.ily" \include "../segments/segment-three.ily" } '''.replace('/', os.path.sep)) path = self.build_path.joinpath('segments', 'segment-one.ily') self.compare_lilypond_contents( path, r''' \context Score = "Example Score" << \context Staff = "Example Staff" { \context Voice = "Example Voice" { c'4 ( d'4 e'4 f'4 ) } } >> ''') path = self.build_path.joinpath('segments', 'segment-two.ily') self.compare_lilypond_contents( path, r''' \context Score = "Example Score" << \context Staff = "Example Staff" { \context Voice = "Example Voice" { c'4 ( d'4 e'4 f'4 ) } } >> ''') path = self.build_path.joinpath('segments', 'segment-three.ily') self.compare_lilypond_contents( path, r''' \context Score = "Example Score" << \context Staff = "Example Staff" { \context Voice = "Example Voice" { c'4 ( d'4 e'4 f'4 ) } } >> ''')
def test_lilypond_error(self): """ Handle failing LilyPond rendering. """ self.create_score() segment_path = self.create_segment('test_segment') definition_path = segment_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 from abjad.tools import lilypondfiletools from abjad.tools import scoretools class FaultySegmentMaker(abctools.AbjadObject): def __call__( self, segment_metadata=None, previous_segment_metadata=None, ): lilypond_file = lilypondfiletools.make_basic_lilypond_file( scoretools.Staff("c'4 ( d'4 e'4 f'4 )") ) lilypond_file.items.append(r'\this-does-not-exist') return lilypond_file, segment_metadata segment_maker = FaultySegmentMaker() ''')) script = commandlinetools.ManageSegmentScript() command = ['--illustrate', 'test_segment'] 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_segment' ... Reading test_score/segments/metadata.json ... OK! Illustrating test_score/segments/test_segment/ Reading test_score/segments/metadata.json ... OK! Reading test_score/segments/test_segment/metadata.json ... JSON does not exist. Importing test_score.segments.test_segment.definition Writing test_score/segments/test_segment/metadata.json Abjad runtime: ... second... Writing test_score/segments/test_segment/illustration.ly ... OK! Writing test_score/segments/test_segment/illustration.pdf ... Failed! '''.replace('/', os.path.sep)) illustration_ly_path = segment_path.joinpath('illustration.ly') assert illustration_ly_path.exists() self.compare_lilypond_contents( illustration_ly_path, stringtools.normalize(r''' \language "english" \header {} \layout {} \paper {} \score { \new Staff { c'4 ( d'4 e'4 f'4 ) } } \this-does-not-exist '''))
def test_segments(segment_name): with systemtools.TemporaryDirectoryChange(str(test_path)): script = commandlinetools.ManageSegmentScript() command = ['--illustrate', segment_name] with mock.patch('abjad.systemtools.IOManager.open_file'): script(command)