Skip to content

Commit

Permalink
use findmnt CLI cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jan 2, 2024
1 parent 279555a commit 431fa60
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,24 +1149,44 @@ def df(path):
@unittest.skipIf(not LINUX, "LINUX only")
class TestSystemDiskPartitions(PsutilTestCase):

def test_against_mount(self):
def parse_mount(out):
# def test_against_mount(self):
# def parse_mount(out):
# ls = []
# for line in out.splitlines():
# fields = line.split()
# device = fields[0]
# mountpoint = fields[2]
# fstype = fields[4]
# opts = fields[5][1:-1]
# # Happens when we have a USB stick. This is not
# # reported by /proc/pid/mountinfo.
# opts = opts.replace("uhelper=udisks2", "")
# opts = opts.rstrip(",")
# ls.append((device, mountpoint, fstype, opts))
# return ls

# out = sh("mount")
# sys_mounts = parse_mount(out)
# psutil_mounts = [x[:4] for x in psutil.disk_partitions(all=True)]
# self.assertEqual(len(sys_mounts), len(psutil_mounts))
# for idx in range(len(sys_mounts)):
# with self.subTest(line=sys_mounts[idx]):
# self.assertEqual(sys_mounts[idx], psutil_mounts[idx])

def test_against_findmnt(self):
# Originally we test psutil against "mount" output, but "man mount"
# declares listing as deprecated, and to use findmnt instead.

def parse_findmnt(out):
ls = []
for line in out.splitlines():
fields = line.split()
device = fields[0]
mountpoint = fields[2]
fstype = fields[4]
opts = fields[5][1:-1]
# Happens when we have a USB stick. This is not
# reported by /proc/pid/mountinfo.
opts = opts.replace("uhelper=udisks2", "")
opts = opts.rstrip(",")
out = sh(["findmnt", "--all", "--list"])
for line in out.splitlines()[1:]:
mountpoint, device, fstype, opts = line.split()
ls.append((device, mountpoint, fstype, opts))
return ls

out = sh("mount")
sys_mounts = parse_mount(out)
sys_mounts = parse_findmnt(out)
psutil_mounts = [x[:4] for x in psutil.disk_partitions(all=True)]
self.assertEqual(len(sys_mounts), len(psutil_mounts))
for idx in range(len(sys_mounts)):
Expand Down

0 comments on commit 431fa60

Please sign in to comment.