def test_move_into_too_small_gap(self): """The FileSet should recognize when moving a range into a gap that is too tight and raise an error after undoing its operation.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, t): if t == 4: raise FileSet.IndexAssignedError(t, f, "Index Assigned.") mock_change_index.side_effect = mock_change_index_side_effect with self.assertRaises( FileSet.FileCollisionError, msg= "The FileSet fails to recognize when a range will collide with other files by being moved into a tight gap." ): test_set.move_range((6, 8), 2) assertion_calls = [(mock_change_index.assert_any_call, [6, 2]), (mock_change_index.assert_any_call, [7, 3]), (mock_change_index.assert_any_call, [8, 4])] mock_assert_many_msg( assertion_calls, "The FileSet doesn't actually try to move the range into the tight gap." ) assertion_calls = [(mock_change_index.assert_any_call, [3, 7]), (mock_change_index.assert_any_call, [2, 6])] mock_assert_many_msg( assertion_calls, "The FileSet fails to undo its operations after detecting a collision when being moved into the too-tight gap." )
def test_move_gap_onto_files(self): """The FileSet should NOT find a collision when moving a gap onto other files.""" test_files = ['test (0).jpg', 'test (4).jpg'] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, t): raise FileSet.IndexUnassignedError(f, t, "Index does not exist.") mock_change_index.side_effect = mock_change_index_side_effect test_set.move_range((1, 2), 4) assertion_calls = [(mock_change_index.assert_any_call, [2, 5]), (mock_change_index.assert_any_call, [1, 4])] mock_assert_many_msg( assertion_calls, "The FileSet does not actually try to move the range.") with self.assertRaises( AssertionError, msg= "The FileSet tries to undo its operation even though there shouldn't be a problem." ): ## This assertion should fail, since the call should NOT exist! mock_assert_msg(mock_change_index.assert_any_call(5, 2))
def test_move_range_with_gaps_front_to_end_preserve_gaps(self): """The FileSet should be able to move a range with gaps from front to end when preserving the gaps.""" test_files = [ 'test (0).jpg', 'test (2).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), []) mock_add_file_set.return_value = 2 test_set.move_files((0, 1), (8, 9), preserve_gaps=True) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (-1, 0), range(0, 1 + 1), strip_gaps(False), preserve_gaps(True) ], "The FileSet fails to temporarily remove the correct files when moving a range with gaps from the front to the end, preserving the gaps." ) mock_assert_msg( mock_move_range.assert_any_call, [(2, 8), 0], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(7, range(0, 1 + 1)) mock_assert_msg( mock_move_range.assert_called_once, [], "The FileSet tries to close resulting gap-space even though there is none when moving to the end." )
def test_end(self): """The FileSet should be able to append files to the end.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (3, 4)) mock_assert_msg(mock_move_range.assert_not_called, [], "The FileSet unnecessarily tries to make space when appending the files to the end.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (4).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (5).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically append the files to the end.") assertion_calls = [ (mock_add_logically.assert_any_call, [4, 'add']), (mock_add_logically.assert_any_call, [5, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when appending to the end.") self.assertEqual(test_set.max_index, 5, "The FileSet fails to update the max_index when adding to the end of the set.")
def test_remove_into_given_filled_file_set(self): """The FileSet should append the removed file to a given filled FileSet.""" removed_file_set = FileSet( ('CustomRemoved (', ')'), ['CustomRemoved (0).jpg', 'CustomRemoved (3).png']) test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg)', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.remove_file(1, removed_file_set) mock_assert_msg( mock_add_file.assert_called_once_with, ['test (1).jpg', 4], msg= "The FileSet fails to properly remove and append the file into another FileSet." ) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 1], msg= "The FileSet fails to close the resulting gap from the remove operation when removing into another FileSet." ) self.assertEqual( removed_file_set.pattern, ('CustomRemoved (', ')'), "The set pattern of the removed_file_set is not the same as demanded." )
def test_move_if_already_tmp_files(self): """The FileSet should be able to move the files correctly even if there already are files following the tmp-files pattern.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), ['tmp1.png']) mock_add_file_set.return_value = 3 test_set.move_files((2, 4), (6, 7)) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (1, 2), range(2, 4 + 1), strip_gaps(False), preserve_gaps(False) ], "The FileSet fails to temporarily remove the correct files into the tmp FileSet if there already are tmp files." ) mock_assert_msg( mock_move_range.assert_any_call, [(5, 6), 2], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(4, range(2, 4 + 1)) mock_assert_msg( mock_move_range.assert_called_with, [(7, 8), 7], "The FileSet doesn't correctly attempt to close possibly resulting gap-space." )
def test_move_range_with_gaps_upwards(self): """The FileSet should be able to move a range with gaps upwards, preserving the gaps.""" test_files = [ 'test (0).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg' ] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, _): if f in [1, 2]: raise FileSet.IndexUnassignedError() mock_change_index.side_effect = mock_change_index_side_effect try: test_set.move_range((0, 4), 6) except FileSet.IndexUnassignedError: self.fail( "The FileSet fails to move a range upwards if it contains gaps as it doesn't handle the IndexUnassignedError." ) assertion_calls = [(mock_change_index.assert_any_call, [4, 10]), (mock_change_index.assert_any_call, [3, 9]), (mock_change_index.assert_called_with, [0, 6])] mock_assert_many_msg( assertion_calls, "The FileSet fails to move a range upwards if it contains gaps.")
def test_default_remove_set_is_active_other_set_is_given( self, mock_expand_pattern): """The CLI should be able to remove files from the default remove set if another file set to remove the files into is given.""" test_args = ['-', '-a', 'custom*set', '1-2'] mock_expand_range.return_value = (1, 2) mock_expand_pattern.return_value = ('custom', 'set') default_remove_set = FileSet(CLI.DEFAULT_REMOVE_PATTERN, ['RMVD1.jpg', 'RMVD2.jpg']) CLI.default_remove_set = default_remove_set custom_remove_set = FileSet(('custom', 'set'), []) CLI.file_set_cache = [custom_remove_set] # set already exists try: remove(default_remove_set, test_args) except CLIRuntimeError: self.fail( "The CLI fails to perform a remove operation if it's removing from the default remove set even though a range to append the removed files to is given." ) mock_assert_msg( mock_remove_files.assert_called_once_with, [[1, 2], custom_remove_set], "The CLI fails to perform a removal operation from the default remove set to a given custom remove set." )
def test_move_middle_to_end(self): """The FileSet should be able to move a range of files from middle to end.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), []) mock_add_file_set.return_value = 3 test_set.move_files((2, 4), (8, 9)) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (-1, 0), range(2, 4 + 1), strip_gaps(False), preserve_gaps(False) ], "The FileSet fails to temporarily remove the correct files when moving to the end of the set." ) mock_assert_msg( mock_move_range.assert_any_call, [(5, 8), 2], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(6, range(0, 2 + 1)) mock_assert_msg( mock_move_range.assert_called_once, [], "The FileSet attempts to close possibly resulting gap space, even though that's unnecessary when moving to the end." )
def test_front(self): """The FileSet should be able to add files to the front of the set.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (-1, 0)) mock_assert_msg(mock_move_range.assert_called_once_with, [(0, 3), 2], "The FileSet fails to make space for the new files at the front.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (0).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (1).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files to the front.") assertion_calls = [ (mock_add_logically.assert_any_call, [0, 'add']), (mock_add_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when adding to the front.")
def test_move_empty_range_downwards_preserve_gaps(self): """The FileSet should be able to move an empty range downwards when preserving the gaps.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), []) mock_add_file_set.return_value = 3 test_set.move_files((2, 4), (0, 1), preserve_gaps=True) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (-1, 0), range(2, 4 + 1), strip_gaps(False), preserve_gaps(True) ], "The FileSet fails to temporarily remove the correct files when moving an empty range downwards, preserving the gaps." ) mock_assert_msg( mock_move_range.assert_any_call, [(1, 1), 4], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(1, range(0, 2 + 1)) mock_assert_msg(mock_move_range.assert_called_with, [(5, 8), 5], "The FileSet fails to close the resulting gap-space.")
def test_wide_gap_in_foreign_add_files_strip_gaps(self): """The FileSet should still correctly strip all gaps in the foreign FileSet if strip_gaps is set to True, even if the gap is wider than just one.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (3).add'] add_set = FileSet(('add (', ')'), add_files) try: amount_added = test_set.add_file_set(add_set, (1, 2), range(0, 3+1), strip_gaps=True) except FileSet.IndexUnassignedError as e: raise AssertionError(e.args, "The FileSet raises an exception when encountering a gap even though strip_gaps was set to True.") mock_assert_msg(mock_move_range.assert_called_once_with, [(2, 3), 4], "The FileSet fails to make the correct amount of space for the newly added files when the file set to be added from contains wide gaps and strip_gaps is set to True.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (3).add', 'test (3).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files from the foreign FileSet with gaps if strip_gaps is True.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when the files to be added contain gaps with strip_gaps set to True.")
def test_too_high_max_index(self): """The FileSet should raise an error if the file set has a max_index of above 1000.""" test_files = ['test (100111).jpg'] test_set = FileSet(self.pattern, test_files) with self.assertRaises(FileSet.TooManyFilesError, msg="The FileSet fails to recognize when there are just too many indexes to scan for flaws."): test_set.find_flaws()
def test_collision_with_files(self): """THe FileSet should recognize when the moved range collides with existing files and undo its operation.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) def mock_change_index_side_effect(f, t): if t == 3: raise FileSet.IndexAssignedError(f, t, "Index assigned.") mock_change_index.side_effect = mock_change_index_side_effect with self.assertRaises( FileSet.FileCollisionError, msg= "The FileSet fails to recognize when a file is colliding due to the movement operation." ): test_set.move_range((1, 2), 3) assertion_calls = [(mock_change_index.assert_any_call, [2, 4]), (mock_change_index.assert_any_call, [1, 3])] mock_assert_many_msg( assertion_calls, "The FileSet does not actually try to move the range.") mock_assert_msg( mock_change_index.assert_any_call, [4, 2], "The FileSet does not properly undo its operations after discovering a file collision." )
def test_move_front_to_middle(self): """The FileSet should be able to move a range of files from front to middle.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), []) mock_add_file_set.return_value = 2 test_set.move_files((0, 1), (3, 4)) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (-1, 0), range(0, 1 + 1), strip_gaps(False), preserve_gaps(False) ], "The FileSet fails to temporarily remove the correct files when moving from the front to the middle." ) mock_assert_msg( mock_move_range.assert_any_call, [(2, 3), 0], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(2, range(0, 1 + 1)) mock_assert_msg( mock_move_range.assert_called_with, [(4, 8), 4], "The FileSet doesn't correctly attempt to close possibly resulting gap-space." )
def test_move_single_file_range(self): """The FileSet should be able to move a range that contains only a single file, i.e. follows the scheme: (n, n).""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), []) mock_add_file_set.return_value = 1 test_set.move_files((4, 4), (6, 7)) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (-1, 0), range(4, 4 + 1), strip_gaps(False), preserve_gaps(False) ], "The FileSet fails to temporarily remove the correct files when moving a single-file range." ) mock_assert_msg( mock_move_range.assert_any_call, [(5, 6), 4], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(6, range(0, 0 + 1)) mock_assert_msg( mock_move_range.assert_called_with, [(7, 8), 7], "The FileSet doesn't correctly attempt to close possibly resulting gap-space." )
def test_add_no_files(self): """The method should do nothing if no files to be added are given.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) files_to_add = [] mock_check_spot.return_value = (1, 2) test_set.add_files(files_to_add, (1, 2)) mock_assert_msg( mock_move_range.assert_not_called, [], "The method performs an operation even though the list of files to be added is empty." ) mock_assert_msg( mock_rename.assert_not_called, [], "The method performs an operation even though the list of files to be added is empty." ) mock_assert_msg( mock_add_logically.assert_not_called, [], "The method performs an operation even though the list of files to be added is empty." )
def test_add_multiple_files(self): """The method should be able to add multiple files at once.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) files_to_add = [ 'new_file1.add1', 'new_file2.add2', 'another_file.add3' ] mock_isfile.return_value = True mock_check_spot.return_value = (1, 2) test_set.add_files(files_to_add, (1, 2)) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 5], "The method fails to make space for the files to be added.") assertion_calls = [ (mock_rename.assert_any_call, ['new_file1.add1', 'test (2).add1']), (mock_rename.assert_any_call, ['new_file2.add2', 'test (3).add2']), (mock_rename.assert_any_call, ['another_file.add3', 'test (4).add3']) ] mock_assert_many_msg(assertion_calls, "The method fails to physically add the files.") assertion_calls = [(mock_add_logically.assert_any_call, [2, 'add1']), (mock_add_logically.assert_any_call, [3, 'add2']), (mock_add_logically.assert_any_call, [4, 'add3'])] mock_assert_many_msg(assertion_calls, "The method fails to logically add the files.")
def test_invalid_spot(self): """The method should recognize and raise an error when the given spot is invalid.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) files_to_add = [ 'new_file1.add1', 'new_file2.add2', 'another_file.add3' ] mock_isfile.return_value = True mock_check_spot.side_effect = ValueError() with self.assertRaises( ValueError, msg="The method fails to recognize an invalid spot."): test_set.add_files(files_to_add, ('a', 'b')) mock_assert_msg( mock_move_range.assert_not_called, [], "The method performs an operation even though an error was raised." ) mock_assert_msg( mock_rename.assert_not_called, [], "The method performs an operation even though an error was raised." ) mock_assert_msg( mock_add_logically.assert_not_called, [], "The method performs an operation even though an error was raised." )
def test_non_existent_files_ignore_mode(self): """When ignore_unfound_files=True, the method should ignore non-existent files and add the rest flawlessly.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) files_to_add = ['new_file1.add1', 'not_existent', 'another_file.add3'] mock_isfile.side_effect = lambda f: not (f == 'not_existent') mock_check_spot.return_value = (1, 2) try: test_set.add_files(files_to_add, (1, 2), ignore_unfound_files=True) except FileSet.FileNotFoundError: self.fail( "The method raises a FileNotFoundError even though ignore_unfound_files was set to True." ) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 4], "The method fails to make space for the files to be added.") assertion_calls = [(mock_rename.assert_any_call, ['new_file1.add1', 'test (2).add1']), (mock_rename.assert_any_call, ['another_file.add3', 'test (3).add3'])] mock_assert_many_msg(assertion_calls, "The method fails to physically add the files.") assertion_calls = [(mock_add_logically.assert_any_call, [2, 'add1']), (mock_add_logically.assert_any_call, [3, 'add3'])] mock_assert_many_msg(assertion_calls, "The method fails to logically add the files.")
def test_non_existent_files_normal_mode(self): """By default, the method should raise an error when one of the files does not exist.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) files_to_add = ['new_file1.add1', 'not_existent', 'another_file.add3'] mock_isfile.side_effect = lambda f: not (f == 'not_existent') mock_check_spot.return_value = (1, 2) with self.assertRaises( FileSet.FileNotFoundError, msg="The method fails to recognize invalid/not existent files." ): test_set.add_files(files_to_add, (1, 2), ignore_unfound_files=False) mock_assert_msg( mock_move_range.assert_not_called, [], "The method performs an operation even though an error was raised." ) mock_assert_msg( mock_rename.assert_not_called, [], "The method performs an operation even though an error was raised." ) mock_assert_msg( mock_add_logically.assert_not_called, [], "The method performs an operation even though an error was raised." )
def setUpClass(cls): cls.default_remove_set = FileSet(CLI.DEFAULT_REMOVE_PATTERN, []) cls.test_set = FileSet(('test (', ')'), [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg', 'test (9).jpg' ])
def test_move_range_with_gaps_middle_to_front_preserve_gaps(self): """The FileSet should be able to move a range with gaps from middle to front when preserving the gaps.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), []) mock_add_file_set.return_value = 3 test_set.move_files((2, 4), (-1, 0), preserve_gaps=True) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (-1, 0), range(2, 4 + 1), strip_gaps(False), preserve_gaps(True) ], "The FileSet fails to temporarily remove the correct files when moving a range with gaps to the front, preserving the gaps." ) mock_assert_msg( mock_move_range.assert_any_call, [(0, 1), 3], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(0, range(0, 2 + 1)) mock_assert_msg( mock_move_range.assert_called_with, [(5, 8), 5], "The FileSet doesn't correctly close resulting gap-space.")
def test_gap_in_foreign_add_files_preserve_gaps(self): """The FileSet should be able to preserve gaps if the corresponding keyword is set to True.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) try: amount_added = test_set.add_file_set(add_set, (1, 2), range(0, 2+1), preserve_gaps = True) except FileSet.IndexUnassignedError: self.fail("The FileSet raises an exception when encountering a gap even though preserve_gaps was set to True.") mock_assert_msg(mock_move_range.assert_called_once_with, [(2, 3), 5], "The FileSet fails to make space for the files and gaps when the latter are preserved.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (4).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files and gaps from the foreign FileSet when the gaps are preserved.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [4, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 3, "The FileSet fails to return the correct number of indexes (files and gaps) that were actually added when the files to be added contain gaps.")
def test_remove_into_given_empty_file_set(self): """The FileSet should manage to add a removed file into a given empty file set.""" removed_file_set = FileSet(('CustomRemoved (', ')'), []) test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg)', 'test (3).jpg' ] test_set = FileSet(self.pattern, test_files) test_set.remove_file(1, removed_file_set) mock_assert_msg( mock_add_file.assert_called_once_with, ['test (1).jpg', 0], msg= "The FileSet fails to properly remove the file into another FileSet." ) mock_assert_msg( mock_move_range.assert_called_once_with, [(2, 3), 1], msg= "The FileSet fails to close the resulting gap from the remove operation when removing into another FileSet." ) self.assertEqual( removed_file_set.pattern, ('CustomRemoved (', ')'), "The default pattern of the removed_file_set is incorrect.")
def test_gap_in_foreign_add_all_indexes(self): """The FileSet should automatically ignore gaps in the foreign file set and add all files if add_indexes is set to 'ALL' (default).""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) try: amount_added = test_set.add_file_set(add_set, (1, 2)) except FileSet.IndexUnassignedError as e: raise AssertionError(e.args, "The FileSet fails to add a FileSet with gaps, even though indexes='ALL' has been used.") mock_assert_msg(mock_move_range.assert_called_once_with, [(2, 3), 4], msg="The FileSet fails to make the correct space for the two files with a gap.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (3).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files with a gap into the FileSet.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when the files to be added contain gaps.")
def test_range_wrong_order(self): """The FileSet should be able to deal with a valid range that's given from higher to lower.""" test_files = [ 'test (0).jpg', 'test (1).jpg', 'test (2).jpg', 'test (3).jpg', 'test (4).jpg', 'test (5).jpg', 'test (6).jpg', 'test (7).jpg', 'test (8).jpg' ] test_set = FileSet(self.pattern, test_files) mock_files_detected.return_value = FileSet(('tmp', ''), []) mock_add_file_set.return_value = 3 test_set.move_files((4, 2), (6, 7)) mock_assert_msg( mock_add_file_set.assert_any_call, [ test_set, (-1, 0), range(2, 4 + 1), strip_gaps(False), preserve_gaps(False) ], "The FileSet fails to temporarily remove the correct files if the range is in the wrong order." ) mock_assert_msg( mock_move_range.assert_any_call, [(5, 6), 2], "The FileSet fails to make space to readd the removed files into their destined position." ) self.check_readd_call(4, range(0, 2 + 1)) mock_assert_msg( mock_move_range.assert_called_with, [(7, 8), 7], "The FileSet doesn't correctly attempt to close possibly resulting gap-space." )
def test_add_into_empty_set(self): """The FileSet should be able to add files even if it is empty.""" test_files = [] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (-1, 0)) mock_assert_msg(mock_move_range.assert_not_called, [], "The FileSet unnecessarily tries to make space when adding into an empty set.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (0).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (1).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (2).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files into a an empty FileSet.") assertion_calls = [ (mock_add_logically.assert_any_call, [0, 'add']), (mock_add_logically.assert_any_call, [1, 'add']), (mock_add_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 3, "The FileSet fails to return the number of indexes that were actually added when adding into an empty set.") self.assertEqual(test_set.max_index, 2, "The FileSet fails to update the max_index when adding into an empty file set.")
def test_fitting_gap(self): """The FileSet should be able to add files into a gap, filling it up completely without having to make extra space.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (4).jpg', 'test (5).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (1, 2)) mock_assert_msg(mock_move_range.assert_not_called, [], "The FileSet unnecessarily tries to make space when adding the files into a perfectly fitting gap.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (3).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files into a perfectly fitting gap.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 2, "The FileSet fails to return the number of indexes that were actually added when adding into a perfectly fitting gap.")
def test_too_small_gap(self): """The FileSet should be able to fill a small gap and automatically make the necessary extra-space if the amount of files requires it.""" test_files = ['test (0).jpg', 'test (1).jpg', 'test (4).jpg', 'test (5).jpg'] test_set = FileSet(self.pattern, test_files) add_files = ['add (0).add', 'add (1).add', 'add (2).add'] add_set = FileSet(('add (', ')'), add_files) amount_added = test_set.add_file_set(add_set, (1, 2)) mock_assert_msg(mock_move_range.assert_called_once_with, [(4, 5), 5], "The FileSet fails to make space for the added files when the gap is too small.") assertion_calls = [ (mock_rename.assert_any_call, ['add (0).add', 'test (2).add']), (mock_rename.assert_any_call, ['add (1).add', 'test (3).add']), (mock_rename.assert_any_call, ['add (2).add', 'test (4).add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to physically add the files into a gap that had to be widened.") assertion_calls = [ (mock_add_logically.assert_any_call, [2, 'add']), (mock_add_logically.assert_any_call, [3, 'add']), (mock_add_logically.assert_any_call, [4, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically add the files.") assertion_calls = [ (mock_remove_logically.assert_any_call, [0, 'add']), (mock_remove_logically.assert_any_call, [1, 'add']), (mock_remove_logically.assert_any_call, [2, 'add']) ] mock_assert_many_msg(assertion_calls, "The FileSet fails to logically remove the files from the foreign file_set's file list.") self.assertEqual(amount_added, 3, "The FileSet fails to return the number of indexes that were actually added when adding into a too-small gap.")
def main(): path = Util.getCommandLineArgument(1) code_writer = CodeWriter(path.replace('.vm', '') + '.asm') if os.path.isdir(path): files = FileSet(path, 'vm') while files.hasMoreFiles(): filename = files.nextFile() Main.parse(filename, code_writer) elif os.path.isfile(path): Main.parse(path, code_writer) code_writer.Close()
def main(): print("******************************************") print("*** FileSet Report ***") print("******************************************") print() fileORdir = Util.getCommandLineArgument(1) level = Util.getCommandLineArgument(2) files = FileSet(fileORdir, "hack") files.report() print() print("******************************************") print("*** Processing Report ***") print("******************************************") print() while files.hasMoreFiles(): inputFileSpec = files.nextFile() print("Processing: %s" % inputFileSpec) outputFileSpec = os.path.splitext(inputFileSpec)[0]+".dis" inputFile = open(inputFileSpec, "rU") outputFile = open(outputFileSpec, "w") parser = Parser(inputFile) while parser.hasMoreInstructions(): parser.advance() if (parser.instructionType() == "A_TYPE"): value = parser.value() inst = Code.a_type(value) if (parser.instructionType() == "C_TYPE"): dest = parser.dest() comp = parser.comp() jump = parser.jump() destMnemonic = Code.destMnemonic(dest) compMnemonic = Code.compMnemonic(comp) jumpMnemonic = Code.jumpMnemonic(jump) inst = Code.c_type(destMnemonic, compMnemonic, jumpMnemonic) if (parser.instructionType() == "INVALID"): inst = Code.invalid_type() inst += Util.repeatedChar(" ", 20-len(inst)) inst += "// %05i:" % parser.address() inst += " [%s]" % parser.hexInstruction() inst += " %s\n" % parser.parsedInstruction() outputFile.write(inst) outputFile.close() inputFile.close() print() print("Processing of file(s) complete.")
def main(): path = Util.getCommandLineArgument(1) if os.path.isdir(path): files = FileSet(path, 'jack') while files.hasMoreFiles(): filename = files.nextFile() Main.compile_jack(filename) elif os.path.isfile(path): Main.compile_jack(path) else: print '{} is not a file or dir'.format(path)
def test2(): test_fileset = os.path.join(os.getcwd(), 'fileset2') fs = FileSet(test_fileset) fs.next_run() test_data = range(0,10) fs.new_context('rotate') for i in range(0, 20): fs.next_iter() filename = fs.new_dataset('my_list') print filename fp = open(filename, 'w') for x in test_data: fp.write("%d\n" % x) test_data = test_data[1:] + test_data[0:1] fp.close() fs.commit()
def test1(): test_fileset = os.path.join(os.getcwd(), 'fileset1') fs = FileSet(test_fileset) fs.next_run() fs.new_context('compute') fs.next_iter() fs.new_dataset('alpha') fs.new_dataset('beta') fs.next_iter() fs.new_dataset('alpha') fs.new_dataset('beta') fs.commit()