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_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_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(self): entries = ShellmarkEntries(path=os.path.join('test', 'xxx')) entries.add_entry(mark='dir1', path=DIR1) self.assertEqual(len(entries.entries), 1) self.assertEqual(entries.entries[0].mark, 'dir1') self.assertEqual(entries._index['marks']['dir1'], [0]) entries.add_entry(mark='dir2', path=DIR2) self.assertEqual(len(entries.entries), 2) self.assertEqual(entries.entries[1].mark, 'dir2') self.assertEqual(entries._index['marks']['dir2'], [1])
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_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 test_property_changed(self): entries = ShellmarkEntries(path=os.path.join('test', 'files', 'sdirs')) self.assertEqual(entries.changed, False) entries.add_entry(mark='dir1', path=DIR1) self.assertEqual(entries.changed, True)
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)