Skip to content

Commit

Permalink
Fix inconsistency with relative paths
Browse files Browse the repository at this point in the history
`picard_module_path` can be a file path or a dir path.
When it's a frozen package, that's always a directory, and when running from source it is always a file path.

Ensure it is always a directory (that is the parent of the file path if it happens)
  • Loading branch information
zas committed Apr 12, 2024
1 parent 607f923 commit b282a59
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
5 changes: 4 additions & 1 deletion picard/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
else:
picard_module_path = Path(PathFinder().find_spec('picard').origin).resolve()

if not picard_module_path.is_dir():
picard_module_path = picard_module_path.parent

_MAX_TAIL_LEN = 10**6


Expand Down Expand Up @@ -170,7 +173,7 @@ def name_filter(record):
# to avoid the exception handling.
if path.is_absolute():
try:
path = path.resolve().relative_to(picard_module_path.parent)
path = path.resolve().relative_to(picard_module_path)
except ValueError:
pass
parts = list(path.parts)
Expand Down
16 changes: 8 additions & 8 deletions test/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ class NameFilterTestRel(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='/path1/path2/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/module/file')
self.assertEqual(record.name, 'module/file')

def test_2(self):
record = FakeRecord(name=None, pathname='/path1/path2/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/module')
self.assertEqual(record.name, 'module')

def test_3(self):
record = FakeRecord(name=None, pathname='/path1/path2/module/subpath/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/module/subpath/file')
self.assertEqual(record.name, 'module/subpath/file')

def test_4(self):
record = FakeRecord(name=None, pathname='')
Expand All @@ -159,7 +159,7 @@ def test_4(self):
def test_5(self):
record = FakeRecord(name=None, pathname='/path1/path2/__init__/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/__init__/module')
self.assertEqual(record.name, '__init__/module')


@unittest.skipIf(IS_WIN, "Posix test")
Expand Down Expand Up @@ -204,17 +204,17 @@ class NameFilterTestRelWin(PicardTestCase):
def test_1(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/module/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/module/file')
self.assertEqual(record.name, 'module/file')

def test_2(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/module')
self.assertEqual(record.name, 'module')

def test_3(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/module/subpath/file.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/module/subpath/file')
self.assertEqual(record.name, 'module/subpath/file')

def test_4(self):
record = FakeRecord(name=None, pathname='')
Expand All @@ -224,7 +224,7 @@ def test_4(self):
def test_5(self):
record = FakeRecord(name=None, pathname='C:/path1/path2/__init__/module/__init__.py')
self.assertTrue(name_filter(record))
self.assertEqual(record.name, 'path2/__init__/module')
self.assertEqual(record.name, '__init__/module')


@unittest.skipUnless(IS_WIN, "Windows test")
Expand Down

0 comments on commit b282a59

Please sign in to comment.