def test_list_methods(temp_file): """Test various list methods""" from osxmetadata import OSXMetaData from osxmetadata.constants import kMDItemKeywords attribute = kMDItemKeywords # updatekeywords meta = OSXMetaData(temp_file) keywordset = ["Test", "Green", "Foo"] meta.keywords = keywordset assert meta.keywords == keywordset assert meta.get_attribute(attribute) == keywordset idx = meta.keywords.index("Green") assert idx == 1 count = meta.keywords.count("Test") assert count == 1 count = meta.keywords.count("Bar") assert count == 0 meta.keywords.sort() assert meta.keywords == ["Foo", "Green", "Test"] assert meta.get_attribute(attribute) == ["Foo", "Green", "Test"] meta.keywords.sort(reverse=True) assert meta.keywords == ["Test", "Green", "Foo"] assert meta.get_attribute(attribute) == ["Test", "Green", "Foo"] # sort by key meta.keywords.sort(key=lambda tag: len(tag)) assert meta.keywords == ["Foo", "Test", "Green"] assert meta.get_attribute(attribute) == ["Foo", "Test", "Green"] meta.keywords.reverse() assert meta.keywords == ["Green", "Test", "Foo"] assert meta.get_attribute(attribute) == ["Green", "Test", "Foo"] tag_expected = "Test" tag_got = meta.keywords.pop(1) assert tag_got == tag_expected assert meta.keywords == ["Green", "Foo"] assert meta.get_attribute(attribute) == ["Green", "Foo"] # delete by assigning None meta.keywords = None assert meta.keywords == []
def test_cli_verbose_short_opts(temp_file): from osxmetadata import OSXMetaData, Tag from osxmetadata.__main__ import cli TESTDIR = None source_file = NamedTemporaryFile(dir=TESTDIR) source_filename = source_file.name meta_source = OSXMetaData(source_filename) meta_source.tags = [Tag("bar")] meta_source.keywords = ["foo"] meta_source.findercomment = "Bar" meta_source.comment = "Foo" runner = CliRunner() result = runner.invoke( cli, [ "-X", "-s", "keywords", "test", "-l", "-g", "keywords", "-c", "keywords", "-r", "keywords", "test", "-u", "keywords", "foo", "-m", "keywords", "tags", "-V", "-B", "-f", source_filename, temp_file, ], ) assert result.exit_code == 0 output = result.output assert "Processing file" in output assert "No metadata to wipe from" in output assert "Copying attributes from" in output assert "Copying com.apple.metadata:_kMDItemUserTags" in output assert "Copying com.apple.metadata:kMDItemComment" in output assert "Copying com.apple.metadata:kMDItemKeywords" in output assert "Copying com.apple.metadata:kMDItemFinderComment" in output assert "Clearing keywords" in output assert "Setting keywords=test" in output assert "Updating keywords=foo" in output assert "Removing keywords" in output assert "Mirroring keywords tags" in output assert "Backing up attribute data" in output source_file.close()
def test_cli_copy_from(temp_file): # test copy from source file from osxmetadata import OSXMetaData, Tag from osxmetadata.__main__ import cli TESTDIR = None source_file = NamedTemporaryFile(dir=TESTDIR) source_filename = source_file.name meta_source = OSXMetaData(source_filename) meta_source.tags = [Tag("bar")] meta_source.keywords = ["foo"] meta_source.findercomment = "Bar" meta_source.comment = "Foo" runner = CliRunner() result = runner.invoke(cli, ["--copyfrom", source_filename, temp_file]) assert result.exit_code == 0 meta = OSXMetaData(temp_file) assert meta.tags == [Tag("bar")] assert meta.keywords == ["foo"] assert meta.findercomment == "Bar" assert meta.comment == "Foo" source_file.close()