def test_method_delete_duplicates(self): entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='mark', path=DIR1) entries.add_entry(mark='mark', path=DIR2) entries.add_entry(mark='other', path=DIR2) entries.delete_duplicates(marks=True, paths=False) self.assertEqual(entries.entries[0].path, DIR2) self.assertEqual(entries.entries[1].path, DIR2) entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='mark1', path=DIR1) entries.add_entry(mark='mark2', path=DIR1) entries.add_entry(mark='mark2', path=DIR2) entries.delete_duplicates(marks=False, paths=True) self.assertEqual(entries.entries[0].mark, 'mark2') self.assertEqual(entries.entries[1].mark, 'mark2') entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='mark1', path=DIR1) entries.add_entry(mark='mark1', path=DIR1) entries.add_entry(mark='mark2', path=DIR1) entries.add_entry(mark='mark1', path=DIR2) entries.delete_duplicates(marks=True, paths=True) self.assertEqual(len(entries.entries), 2) self.assertEqual(entries.entries[0].mark, 'mark2') self.assertEqual(entries.entries[0].path, DIR1) self.assertEqual(entries.entries[1].mark, 'mark1') self.assertEqual(entries.entries[1].path, DIR2)
def test_combinations(self): entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir2', path=DIR2) entries.add_entry(mark='dir3', path=DIR3) self.assertEqual(entries.get_entries(mark='dir3')[0].path, DIR3) # Delete one entry entries.delete_entries(mark='dir2') self.assertEqual(entries.get_entries(mark='dir3')[0].path, DIR3) # Sort entries.sort(reverse=True) self.assertEqual(entries.get_entries(mark='dir3')[0].path, DIR3) # Update one entry entries.update_entries(old_mark='dir1', new_path=DIR2) self.assertEqual(entries.get_entries(mark='dir3')[0].path, DIR3) entries.write() content = entries.get_raw() self.assertEqual( content, 'export DIR_dir3="{0}/dir3"\nexport DIR_dir1="{0}/dir2"\n'.format( TEST_PATH))
def test_method_write(self): old_path = os.path.join('test', 'files', 'sdirs') entries = ShellmarkEntries(path=old_path) new_path = tmp_file() entries.write(new_path=new_path) new_path_content = open(new_path, 'r').read() self.assertTrue(new_path_content)
def test_method_sort(self): sdirs = tmp_file() entries = ShellmarkEntries(path=sdirs) entries.add_entry(mark='dir3', path=DIR1) entries.add_entry(mark='dir2', path=DIR2) entries.add_entry(mark='dir1', path=DIR3) self.assertEqual(entries.entries[0].mark, 'dir3') self.assertEqual(entries.entries[1].mark, 'dir2') self.assertEqual(entries.entries[2].mark, 'dir1') self.assertEqual(entries.entries[0].path, DIR1) self.assertEqual(entries.entries[1].path, DIR2) self.assertEqual(entries.entries[2].path, DIR3) entries.sort() self.assertEqual(entries.entries[0].mark, 'dir1') self.assertEqual(entries.entries[1].mark, 'dir2') self.assertEqual(entries.entries[2].mark, 'dir3') entries.sort(reverse=True) self.assertEqual(entries.entries[0].mark, 'dir3') self.assertEqual(entries.entries[1].mark, 'dir2') self.assertEqual(entries.entries[2].mark, 'dir1') entries.sort(attribute_name='path') self.assertEqual(entries.entries[0].path, DIR1) self.assertEqual(entries.entries[1].path, DIR2) self.assertEqual(entries.entries[2].path, DIR3) entries.sort(attribute_name='path', reverse=True) self.assertEqual(entries.entries[0].path, DIR3) self.assertEqual(entries.entries[1].path, DIR2) self.assertEqual(entries.entries[2].path, DIR1)
def test_delete_on_empty_sdirs_by_path(self): mock_objects = mock_main({ 'sdirs': tmp_file(), 'path': '/dir1', 'state': 'absent' }) mock_objects['module'].exit_json.assert_called_with(changed=False)
def test_method_delete_entries_duplicates(self): entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir1', path=DIR1) self.assertEqual(len(entries.entries), 3) entries.delete_entries(mark='dir1') self.assertEqual(len(entries.entries), 0)
def test_method_update_entries_duplicates(self): entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir1', path=DIR1) entries.update_entries(old_mark='dir1', new_mark='new1') self.assertEqual(entries._index['marks']['new1'], [0, 1, 2]) self.assertEqual(entries._index['paths'][DIR1], [0, 1, 2])
def test_method_add_entry_duplicates(self): entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir1', path=DIR1) self.assertEqual(len(entries.entries), 3) self.assertEqual(entries._index['marks']['dir1'], [0, 1, 2]) self.assertEqual(entries._index['paths'][DIR1], [0, 1, 2])
def test_method_add_entry_exception(self): entries = ShellmarkEntries(path=tmp_file()) with self.assertRaises(MarkInvalidError): entries.add_entry(mark='dör1', path=DIR1) with self.assertRaises(MarkInvalidError): entries.add_entry(mark='d i r 1', path=DIR1) with self.assertRaises(MarkInvalidError): entries.add_entry(mark='dir 1', path=DIR1)
def mock_add(self, mark, path, sdirs=False): if not sdirs: sdirs = tmp_file() return mock_main(params={ 'mark': mark, 'path': path, 'sdirs': sdirs }, check_mode=False)
def test_nothing_to_do(self): entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='dir1', path=DIR1) entries.add_entry(mark='dir2', path=DIR2) entries.add_entry(mark='dir3', path=DIR3) entries.write() mock_objects = mock_main({'cleanup': True, 'sdirs': entries.path}) entries = ShellmarkEntries(path=mock_objects['module'].params['sdirs']) self.assertEqual(len(mock_objects['entries'].entries), 3) self.assertEqual(mock_objects['entries'].entries[0].path, DIR1) mock_objects['module'].exit_json.assert_called_with(changed=False)
def test_method_add_entry_avoid_duplicate_paths(self): entries = ShellmarkEntries(path=tmp_file()) self.assertEqual(entries.add_entry(mark='dir1', path=DIR1), 0) self.assertEqual( entries.add_entry(mark='dir1', path=DIR1, avoid_duplicate_paths=True), False) self.assertEqual(len(entries.entries), 1) self.assertEqual( entries.add_entry(mark='dir1', path=DIR1, avoid_duplicate_paths=True, delete_old_entries=True), 0) self.assertEqual(len(entries.entries), 1)
def test_method__update_index(self): entries = ShellmarkEntries(path=tmp_file()) entries.add_entry(mark='dir1', path=DIR1) self.assertEqual(entries._index['marks']['dir1'], [0]) self.assertEqual(entries._index['paths'][DIR1], [0]) # Delete the index store. delattr(entries, '_index') with self.assertRaises(AttributeError): getattr(entries, '_index') # Regenerate index. entries._update_index() self.assertEqual(entries._index['marks']['dir1'], [0]) self.assertEqual(entries._index['paths'][DIR1], [0])
def test_method__store_index_number(self): sdirs = tmp_file() entries = ShellmarkEntries(path=sdirs) entries._store_index_number('mark', 'downloads', 7) self.assertEqual(entries._index['marks']['downloads'], [7]) # The same again entries._store_index_number('mark', 'downloads', 7) self.assertEqual(entries._index['marks']['downloads'], [7]) # Add another index number. entries._store_index_number('mark', 'downloads', 9) self.assertEqual(entries._index['marks']['downloads'], [7, 9]) # Add a lower index number. entries._store_index_number('mark', 'downloads', 5) self.assertEqual(entries._index['marks']['downloads'], [5, 7, 9]) with self.assertRaises(ValueError) as cm: entries._store_index_number('lol', 'downloads', 1) self.assertEqual(str(cm.exception), 'attribute_name “lol” unkown.')
def setUp(self): self.sdirs = tmp_file() self.mock_add('tmp1', DIR1) self.mock_add('tmp2', DIR2) self.mock_add('tmp3', DIR3)