|
| 1 | +""" Test bool attributes """ |
| 2 | + |
| 3 | +import platform |
| 4 | +from tempfile import NamedTemporaryFile, TemporaryDirectory |
| 5 | + |
| 6 | +import pytest |
| 7 | +from osxmetadata.attributes import ATTRIBUTES |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(params=["file", "dir"]) |
| 11 | +def temp_file(request): |
| 12 | + |
| 13 | + # TESTDIR for temporary files usually defaults to "/tmp", |
| 14 | + # which may not have XATTR support (e.g. tmpfs); |
| 15 | + # manual override here. |
| 16 | + TESTDIR = None |
| 17 | + if request.param == "file": |
| 18 | + tempfile = NamedTemporaryFile(dir=TESTDIR) |
| 19 | + tempfilename = tempfile.name |
| 20 | + yield tempfilename |
| 21 | + tempfile.close() |
| 22 | + else: |
| 23 | + tempdir = TemporaryDirectory(dir=TESTDIR) |
| 24 | + tempdirname = tempdir.name |
| 25 | + yield tempdirname |
| 26 | + tempdir.cleanup() |
| 27 | + |
| 28 | + |
| 29 | +test_data = [ |
| 30 | + (attr) |
| 31 | + for attr in sorted(list(ATTRIBUTES.keys())) |
| 32 | + if ATTRIBUTES[attr].class_ == bool |
| 33 | +] |
| 34 | +ids = [ |
| 35 | + attr for attr in sorted(list(ATTRIBUTES.keys())) if ATTRIBUTES[attr].class_ == bool |
| 36 | +] |
| 37 | + |
| 38 | +test_data2 = [ |
| 39 | + (attribute_name) |
| 40 | + for attribute_name in { |
| 41 | + ATTRIBUTES[attr].name |
| 42 | + for attr in sorted(ATTRIBUTES) |
| 43 | + if ATTRIBUTES[attr].class_ == bool |
| 44 | + } |
| 45 | +] |
| 46 | +ids2 = [ |
| 47 | + attribute_name |
| 48 | + for attribute_name in { |
| 49 | + ATTRIBUTES[attr].name |
| 50 | + for attr in sorted(ATTRIBUTES) |
| 51 | + if ATTRIBUTES[attr].class_ == bool |
| 52 | + } |
| 53 | +] |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize("attribute", test_data, ids=ids) |
| 57 | +def test_str_attribute(temp_file, attribute): |
| 58 | + """test set_attribute, get_attribute, etc""" |
| 59 | + from osxmetadata import OSXMetaData |
| 60 | + from osxmetadata.constants import _FINDER_COMMENT_NAMES |
| 61 | + |
| 62 | + meta = OSXMetaData(temp_file) |
| 63 | + expected = True |
| 64 | + meta.set_attribute(attribute, expected) |
| 65 | + got = meta.get_attribute(attribute) |
| 66 | + assert expected == got |
| 67 | + |
| 68 | + meta.set_attribute(attribute, not expected) |
| 69 | + assert not meta.get_attribute(attribute) |
| 70 | + |
| 71 | + meta.clear_attribute(attribute) |
| 72 | + assert meta.get_attribute(attribute) is None |
| 73 | + |
| 74 | + # test setting empty string |
| 75 | + # setting finder comment to empty string clears it |
| 76 | + # but other fields get set to empty string |
| 77 | + # this mirrors the way finder comments work in mdls |
| 78 | + meta.set_attribute(attribute, "") |
| 79 | + assert not meta.get_attribute(attribute) |
| 80 | + |
| 81 | + with pytest.raises(AttributeError): |
| 82 | + meta.update_attribute(attribute, "foo") |
| 83 | + |
| 84 | + with pytest.raises(TypeError): |
| 85 | + meta.discard_attribute(attribute, "foo") |
| 86 | + |
| 87 | + with pytest.raises(TypeError): |
| 88 | + meta.remove_attribute(attribute, "foo") |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("attribute", test_data2, ids=ids2) |
| 92 | +def test_str_attribute_2(temp_file, attribute): |
| 93 | + """test getting and setting attribute by name""" |
| 94 | + from osxmetadata import OSXMetaData |
| 95 | + |
| 96 | + meta = OSXMetaData(temp_file) |
| 97 | + setattr(meta, attribute, True) |
| 98 | + test_attr = getattr(meta, attribute) |
| 99 | + assert test_attr |
| 100 | + assert meta.get_attribute(attribute) |
| 101 | + |
| 102 | + |
| 103 | +def test_stationary(temp_file): |
| 104 | + """test string functions on one of the bool attributes""" |
| 105 | + from osxmetadata import OSXMetaData |
| 106 | + |
| 107 | + meta = OSXMetaData(temp_file) |
| 108 | + meta.stationary = True |
| 109 | + assert meta.stationary |
| 110 | + assert meta.get_attribute("stationary") |
| 111 | + |
| 112 | + meta.stationary = not meta.stationary |
| 113 | + assert not meta.stationary |
| 114 | + assert not meta.get_attribute("stationary") |
| 115 | + |
| 116 | + meta.stationary = None |
| 117 | + assert meta.stationary is None |
| 118 | + assert meta.get_attribute("stationary") is None |
| 119 | + |
| 120 | + meta.stationary = True |
| 121 | + assert meta.stationary |
| 122 | + assert meta.get_attribute("stationary") |
0 commit comments