1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2026-04-03 17:44:07 +02:00

Merge pull request #688 from queelius/fix/dir-lstrip-drive-letter

Fix dir parser: lstrip strips D: drive letter from path
This commit is contained in:
Kelly Brazil
2026-03-30 11:49:51 -07:00
committed by GitHub
2 changed files with 21 additions and 2 deletions

View File

@@ -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):

View File

@@ -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()