mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2026-04-03 17:44:07 +02:00
Fix dir parser stripping D: drive letter from parent directory path
lstrip(" Directory of ") strips any character in the set
{' ','D','i','r','e','c','t','o','y','f'}, which incorrectly removes
the 'D' from D:\ paths. Use fixed-length prefix removal with [len():]
instead.
Fixes #687
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -183,8 +183,8 @@ def parse(data, raw=False, quiet=False):
|
||||
if jc.utils.has_data(data):
|
||||
|
||||
for line in data.splitlines():
|
||||
if line.startswith(" Directory of"):
|
||||
parent_dir = line.lstrip(" Directory of ")
|
||||
if line.startswith(" Directory of "):
|
||||
parent_dir = line[len(" Directory of "):]
|
||||
continue
|
||||
# skip lines that don't start with a date
|
||||
if not re.match(r'^\d{2}/\d{2}/\d{4}', line):
|
||||
|
||||
@@ -95,6 +95,25 @@ class MyTests(unittest.TestCase):
|
||||
self.assertEqual(jc.parsers.dir.parse(self.windows_10_dir_S, quiet=True),
|
||||
self.windows_10_dir_S_json)
|
||||
|
||||
def test_dir_drive_letter_d(self):
|
||||
"""
|
||||
Test that the D: drive letter is not stripped from the parent path.
|
||||
Regression test: lstrip(" Directory of ") strips any char in the set
|
||||
{' ','D','i','r','e','c','t','o','y','f'}, which incorrectly removes
|
||||
the 'D' from 'D:\\'.
|
||||
"""
|
||||
data = (
|
||||
' Volume in drive D has no label.\r\n'
|
||||
' Volume Serial Number is 1234-5678\r\n'
|
||||
'\r\n'
|
||||
' Directory of D:\\Users\\testuser\r\n'
|
||||
'\r\n'
|
||||
'03/24/2021 03:15 PM <DIR> .\r\n'
|
||||
'03/24/2021 03:15 PM <DIR> ..\r\n'
|
||||
)
|
||||
result = jc.parsers.dir.parse(data, quiet=True)
|
||||
self.assertEqual(result[0]['parent'], 'D:\\Users\\testuser')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user