Debian Codegraph @

test/util/test_hidden.py

raw
1 """Tests for the 'hidden' utility."""
2
3 import ctypes
4 import errno
5 import subprocess
6 import sys
7 import tempfile
8 import unittest
9
10 from beets.util import hidden
11
12
13 class HiddenFileTest(unittest.TestCase):
14 def setUp(self):
15 pass
16
17 def test_osx_hidden(self):
18 if not sys.platform == "darwin":
19 self.skipTest("sys.platform is not darwin")
20 return
21
22 with tempfile.NamedTemporaryFile(delete=False) as f:
23 try:
24 command = ["chflags", "hidden", f.name]
25 subprocess.Popen(command).wait()
26 except OSError as e:
27 if e.errno == errno.ENOENT:
28 self.skipTest("unable to find chflags")
29 else:
30 raise e
31
32 assert hidden.is_hidden(f.name)
33
34 def test_windows_hidden(self):
35 if not sys.platform == "win32":
36 self.skipTest("sys.platform is not windows")
37 return
38
39 # FILE_ATTRIBUTE_HIDDEN = 2 (0x2) from GetFileAttributes documentation.
40 hidden_mask = 2
41
42 with tempfile.NamedTemporaryFile() as f:
43 # Hide the file using
44 success = ctypes.windll.kernel32.SetFileAttributesW(
45 f.name, hidden_mask
46 )
47
48 if not success:
49 self.skipTest("unable to set file attributes")
50
51 assert hidden.is_hidden(f.name)
52
53 def test_other_hidden(self):
54 if sys.platform == "darwin" or sys.platform == "win32":
55 self.skipTest("sys.platform is known")
56 return
57
58 with tempfile.NamedTemporaryFile(prefix=".tmp") as f:
59 fn = f.name
60 assert hidden.is_hidden(fn)