1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

draft for path and path_list (#513)

* draft for path_list

* updaate doc

* add input check

* fix types

* fix schema: add missing properties

* add _process

* fix _process docs

* refactor: extract path.py parser

* swap order of names alphabetically

* documentation and comments

* path parser: add early return for nodata

* path and path-list parser: add test and fixtures

* typo in file name

* add early return for nodata

* add test and fixtures

* typo in file name

* rename fixtures

* rename fixtures

* refactor to pathlib.Path

* failing on windows - use PurePosixPath

* changed the way to strip dot from suffix

* add POSIX to path

* test commit to see results on windows is failing

* test commit to see results on windows is failing

* add windows path detection

* somehow Path not like the newline from input line

* add test with more items

* remove debug print

* wrap test loops into into subTest

* remove print statements

* add path and path-list to CHANGELOG

---------

Co-authored-by: Kelly Brazil <kellyjonbrazil@gmail.com>
This commit is contained in:
Muescha
2024-01-31 05:04:55 +01:00
committed by Kelly Brazil
parent 2cc212bfab
commit c332c4febf
28 changed files with 1041 additions and 1 deletions

View File

@ -12,7 +12,8 @@ jc changelog
identifying which input matches to which output when using `--slurp` or when converting multiple
files via `/proc` file magic syntax.
- Add `kv-dup` parser for Key/Value files with duplicate keys
- TODO: Add `path-list` string parser to parse path list strings found in env variables
- Add `path` string parser to parse posix path
- Add `path-list` string parser to parse path list strings found in env variables
- Enhance `nsd-control` parser to support more zone information
- Enhance `ping` and `ping-s` parsers to support the `-I` command option
- Enhance `proc-net-tcp` parser to add opposite endian support for architectures

View File

@ -101,6 +101,8 @@ parsers: List[str] = [
'os-prober',
'os-release',
'passwd',
'path',
'path-list',
'pci-ids',
'pgpass',
'pidstat',

118
jc/parsers/path.py Normal file
View File

@ -0,0 +1,118 @@
"""jc - JSON Convert POSIX path string parser
Parse a POSIX path.
Usage (cli):
$ echo "/Users/admin/.docker/bin" | jc --path
Usage (module):
import jc
result = jc.parse('path', path_string)
Schema:
{
"path": string or null,
"parent": string or null,
"filename": string or null,
"stem": string or null,
"extension": string or null,
"path_list": [ array or null
string
],
}
Examples:
$ echo "/abc/def/gh.txt" | jc --path -p
{
"path": "/abc/def/gh.txt",
"parent": "/abc/def",
"filename": "gh.txt",
"stem": "gh",
"extension": "txt",
"path_list": [
"/",
"abc",
"def",
"gh.txt"
]
}
"""
from pathlib import PurePosixPath, PureWindowsPath
from typing import Dict
import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.0'
description = 'path string parser'
author = 'Michael Nietzold'
author_email = 'https://github.com/muescha'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
tags = ['standard', 'string', 'slurpable']
__version__ = info.version
def _process(proc_data: Dict) -> Dict:
"""
Final processing to conform to the schema.
Parameters:
proc_data: (Dictionary) raw structured data to process
Returns:
Dictionary. Structured to conform to the schema.
"""
# no changes
return proc_data
def parse(data, raw=False, quiet=False):
"""
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) unprocessed output if True
quiet: (boolean) suppress warning messages if True
Returns:
Dictionary representing a Key/Value pair document.
"""
jc.utils.compatibility(__name__, info.compatible, quiet)
jc.utils.input_type_check(data)
if not jc.utils.has_data(data):
return {}
data = data.rstrip("\n")
if "\\" in data:
path = PureWindowsPath(data)
else:
path = PurePosixPath(data)
raw_output = {
'path': str(path),
'parent': str(path.parent),
'filename': path.name,
'stem': path.stem,
'extension': path.suffix[1:],
'path_list': list(path.parts)
}
return raw_output if raw else _process(raw_output)

127
jc/parsers/path_list.py Normal file
View File

@ -0,0 +1,127 @@
"""jc - JSON Convert POSIX path list string parser
Parse a colon-separated POSIX path list, commonly found in environment variables.
Usage (cli):
$ echo "/Users/admin/.docker/bin:/Users/admin/.asdf/shims" | jc --path-list
Usage (module):
import jc
result = jc.parse('path-list', path_string)
Schema:
[
{
"path": string or null,
"parent": string or null,
"filename": string or null,
"stem": string or null,
"extension": string or null,
"path_list": [ array or null
string
],
}
]
Examples:
$ echo "/abc/def/gh.txt:/xyz/uvw/ab.app" | jc --path-list -p
[
{
"path": "/abc/def/gh.txt",
"parent": "/abc/def",
"filename": "gh.txt",
"stem": "gh",
"extension": "txt",
"path_list": [
"/",
"abc",
"def",
"gh.txt"
]
},
{
"path": "/xyz/uvw/ab.app",
"parent": "/xyz/uvw",
"filename": "ab.app",
"stem": "ab",
"extension": "app",
"path_list": [
"/",
"xyz",
"uvw",
"ab.app"
]
}
]
"""
import jc.parsers.path as path
from typing import Dict, List
import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.0'
description = 'path list string parser'
author = 'Michael Nietzold'
author_email = 'https://github.com/muescha'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
tags = ['standard', 'string', 'slurpable']
__version__ = info.version
def _process(proc_data: List[Dict]) -> List[Dict]:
"""
Final processing to conform to the schema.
Parameters:
proc_data: (List of Dictionaries) raw structured data to process
Returns:
List of Dictionaries. Structured to conform to the schema.
"""
# no changes
return proc_data
def parse(data, raw=False, quiet=False):
"""
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) unprocessed output if True
quiet: (boolean) suppress warning messages if True
Returns:
List of Dictionaries representing a Key/Value pair document.
"""
jc.utils.compatibility(__name__, info.compatible, quiet)
jc.utils.input_type_check(data)
# This parser is an alias of path.py
path.info = info # type: ignore
delimiter = ":" if "\\" not in data else ";"
raw_output = [
path.parse(line, raw=raw, quiet=quiet)
for line in data.split(delimiter)
if jc.utils.has_data(data)
]
return raw_output if raw else _process(raw_output)

17
tests/fixtures/generic/path--long.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
"path": "/Library/Application Support/Script Editor/Templates/Cocoa-AppleScript Applet.app/Contents/Info.plist",
"parent": "/Library/Application Support/Script Editor/Templates/Cocoa-AppleScript Applet.app/Contents",
"filename": "Info.plist",
"stem": "Info",
"extension": "plist",
"path_list": [
"/",
"Library",
"Application Support",
"Script Editor",
"Templates",
"Cocoa-AppleScript Applet.app",
"Contents",
"Info.plist"
]
}

1
tests/fixtures/generic/path--long.out vendored Normal file
View File

@ -0,0 +1 @@
/Library/Application Support/Script Editor/Templates/Cocoa-AppleScript Applet.app/Contents/Info.plist

13
tests/fixtures/generic/path--one.json vendored Normal file
View File

@ -0,0 +1,13 @@
{
"path": "/abc/def/gh.txt",
"parent": "/abc/def",
"filename": "gh.txt",
"stem": "gh",
"extension": "txt",
"path_list": [
"/",
"abc",
"def",
"gh.txt"
]
}

1
tests/fixtures/generic/path--one.out vendored Normal file
View File

@ -0,0 +1 @@
/abc/def/gh.txt

View File

@ -0,0 +1,14 @@
{
"path": "C:\\Windows\\Program Files\\xfolder\\file.txt",
"parent": "C:\\Windows\\Program Files\\xfolder",
"filename": "file.txt",
"stem": "file",
"extension": "txt",
"path_list": [
"C:\\",
"Windows",
"Program Files",
"xfolder",
"file.txt"
]
}

View File

@ -0,0 +1 @@
C:\Windows\Program Files\xfolder\file.txt

View File

@ -0,0 +1,14 @@
{
"path": "/User/admin/Application Support/App.app",
"parent": "/User/admin/Application Support",
"filename": "App.app",
"stem": "App",
"extension": "app",
"path_list": [
"/",
"User",
"admin",
"Application Support",
"App.app"
]
}

View File

@ -0,0 +1 @@
/User/admin/Application Support/App.app

View File

@ -0,0 +1,273 @@
[
{
"extension": "",
"filename": "bin",
"parent": "/Users/macuser/.docker",
"path": "/Users/macuser/.docker/bin",
"path_list": [
"/",
"Users",
"macuser",
".docker",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "shims",
"parent": "/Users/macuser/.asdf",
"path": "/Users/macuser/.asdf/shims",
"path_list": [
"/",
"Users",
"macuser",
".asdf",
"shims"
],
"stem": "shims"
},
{
"extension": "",
"filename": "bin",
"parent": "/opt/homebrew/opt/asdf/libexec",
"path": "/opt/homebrew/opt/asdf/libexec/bin",
"path_list": [
"/",
"opt",
"homebrew",
"opt",
"asdf",
"libexec",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/opt/homebrew",
"path": "/opt/homebrew/bin",
"path_list": [
"/",
"opt",
"homebrew",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "sbin",
"parent": "/opt/homebrew",
"path": "/opt/homebrew/sbin",
"path_list": [
"/",
"opt",
"homebrew",
"sbin"
],
"stem": "sbin"
},
{
"extension": "",
"filename": "bin",
"parent": "/usr/local",
"path": "/usr/local/bin",
"path_list": [
"/",
"usr",
"local",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/System/Cryptexes/App/usr",
"path": "/System/Cryptexes/App/usr/bin",
"path_list": [
"/",
"System",
"Cryptexes",
"App",
"usr",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/usr",
"path": "/usr/bin",
"path_list": [
"/",
"usr",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/",
"path": "/bin",
"path_list": [
"/",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "sbin",
"parent": "/usr",
"path": "/usr/sbin",
"path_list": [
"/",
"usr",
"sbin"
],
"stem": "sbin"
},
{
"extension": "",
"filename": "sbin",
"parent": "/",
"path": "/sbin",
"path_list": [
"/",
"sbin"
],
"stem": "sbin"
},
{
"extension": "",
"filename": "bin",
"parent": "/Library/Apple/usr",
"path": "/Library/Apple/usr/bin",
"path_list": [
"/",
"Library",
"Apple",
"usr",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local",
"path": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin",
"path_list": [
"/",
"var",
"run",
"com.apple.security.cryptexd",
"codex.system",
"bootstrap",
"usr",
"local",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr",
"path": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin",
"path_list": [
"/",
"var",
"run",
"com.apple.security.cryptexd",
"codex.system",
"bootstrap",
"usr",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal",
"path": "/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin",
"path_list": [
"/",
"var",
"run",
"com.apple.security.cryptexd",
"codex.system",
"bootstrap",
"usr",
"appleinternal",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/Users/macuser/.fig",
"path": "/Users/macuser/.fig/bin",
"path_list": [
"/",
"Users",
"macuser",
".fig",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "bin",
"parent": "/Users/macuser/.local",
"path": "/Users/macuser/.local/bin",
"path_list": [
"/",
"Users",
"macuser",
".local",
"bin"
],
"stem": "bin"
},
{
"extension": "",
"filename": "scripts",
"parent": "/Users/macuser/Library/Application Support/JetBrains/Toolbox",
"path": "/Users/macuser/Library/Application Support/JetBrains/Toolbox/scripts",
"path_list": [
"/",
"Users",
"macuser",
"Library",
"Application Support",
"JetBrains",
"Toolbox",
"scripts"
],
"stem": "scripts"
},
{
"extension": "",
"filename": "bin",
"parent": "/opt/homebrew/opt/fzf",
"path": "/opt/homebrew/opt/fzf/bin",
"path_list": [
"/",
"opt",
"homebrew",
"opt",
"fzf",
"bin"
],
"stem": "bin"
}
]

View File

@ -0,0 +1 @@
/Users/macuser/.docker/bin:/Users/macuser/.asdf/shims:/opt/homebrew/opt/asdf/libexec/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/macuser/.fig/bin:/Users/macuser/.local/bin:/Users/macuser/Library/Application Support/JetBrains/Toolbox/scripts:/opt/homebrew/opt/fzf/bin

View File

@ -0,0 +1,15 @@
[
{
"path": "/abc/def/gh.txt",
"parent": "/abc/def",
"filename": "gh.txt",
"stem": "gh",
"extension": "txt",
"path_list": [
"/",
"abc",
"def",
"gh.txt"
]
}
]

View File

@ -0,0 +1 @@
/abc/def/gh.txt

View File

@ -0,0 +1,28 @@
[
{
"path": "/abc/def/gh.txt",
"parent": "/abc/def",
"filename": "gh.txt",
"stem": "gh",
"extension": "txt",
"path_list": [
"/",
"abc",
"def",
"gh.txt"
]
},
{
"path": "/xyz/uvw/ab.app",
"parent": "/xyz/uvw",
"filename": "ab.app",
"stem": "ab",
"extension": "app",
"path_list": [
"/",
"xyz",
"uvw",
"ab.app"
]
}
]

View File

@ -0,0 +1 @@
/abc/def/gh.txt:/xyz/uvw/ab.app

View File

@ -0,0 +1,26 @@
[
{
"path": "%PROGRAMFILES%\\Git\\bin",
"parent": "%PROGRAMFILES%\\Git",
"filename": "bin",
"stem": "bin",
"extension": "",
"path_list": [
"%PROGRAMFILES%",
"Git",
"bin"
]
},
{
"path": "c:\\my\\folder",
"parent": "c:\\my",
"filename": "folder",
"stem": "folder",
"extension": "",
"path_list": [
"c:\\",
"my",
"folder"
]
}
]

View File

@ -0,0 +1 @@
%PROGRAMFILES%\Git\bin;c:\my\folder

View File

@ -0,0 +1,202 @@
[
{
"path": "D:\\Program Files\\Autodesk\\Maya2008\\bin",
"parent": "D:\\Program Files\\Autodesk\\Maya2008",
"filename": "bin",
"stem": "bin",
"extension": "",
"path_list": [
"D:\\",
"Program Files",
"Autodesk",
"Maya2008",
"bin"
]
},
{
"path": "C:\\Ruby192\\bin",
"parent": "C:\\Ruby192",
"filename": "bin",
"stem": "bin",
"extension": "",
"path_list": [
"C:\\",
"Ruby192",
"bin"
]
},
{
"path": "C:\\WINDOWS\\system32",
"parent": "C:\\WINDOWS",
"filename": "system32",
"stem": "system32",
"extension": "",
"path_list": [
"C:\\",
"WINDOWS",
"system32"
]
},
{
"path": "C:\\WINDOWS",
"parent": "C:\\",
"filename": "WINDOWS",
"stem": "WINDOWS",
"extension": "",
"path_list": [
"C:\\",
"WINDOWS"
]
},
{
"path": "C:\\WINDOWS\\System32\\Wbem",
"parent": "C:\\WINDOWS\\System32",
"filename": "Wbem",
"stem": "Wbem",
"extension": "",
"path_list": [
"C:\\",
"WINDOWS",
"System32",
"Wbem"
]
},
{
"path": "C:\\PROGRA~1\\DISKEE~2\\DISKEE~1",
"parent": "C:\\PROGRA~1\\DISKEE~2",
"filename": "DISKEE~1",
"stem": "DISKEE~1",
"extension": "",
"path_list": [
"C:\\",
"PROGRA~1",
"DISKEE~2",
"DISKEE~1"
]
},
{
"path": "c:\\Program Files\\Microsoft SQL Server\\90\\Tools\\binn",
"parent": "c:\\Program Files\\Microsoft SQL Server\\90\\Tools",
"filename": "binn",
"stem": "binn",
"extension": "",
"path_list": [
"c:\\",
"Program Files",
"Microsoft SQL Server",
"90",
"Tools",
"binn"
]
},
{
"path": "C:\\Program Files\\QuickTime\\QTSystem",
"parent": "C:\\Program Files\\QuickTime",
"filename": "QTSystem",
"stem": "QTSystem",
"extension": "",
"path_list": [
"C:\\",
"Program Files",
"QuickTime",
"QTSystem"
]
},
{
"path": "D:\\Program Files\\TortoiseSVN\\bin",
"parent": "D:\\Program Files\\TortoiseSVN",
"filename": "bin",
"stem": "bin",
"extension": "",
"path_list": [
"D:\\",
"Program Files",
"TortoiseSVN",
"bin"
]
},
{
"path": "D:\\Program Files\\Bazaar",
"parent": "D:\\Program Files",
"filename": "Bazaar",
"stem": "Bazaar",
"extension": "",
"path_list": [
"D:\\",
"Program Files",
"Bazaar"
]
},
{
"path": "C:\\Program Files\\Android\\android-sdk\\tools",
"parent": "C:\\Program Files\\Android\\android-sdk",
"filename": "tools",
"stem": "tools",
"extension": "",
"path_list": [
"C:\\",
"Program Files",
"Android",
"android-sdk",
"tools"
]
},
{
"path": "D:\\Program Files\\Microsoft Visual Studio\\Common\\Tools\\WinNT",
"parent": "D:\\Program Files\\Microsoft Visual Studio\\Common\\Tools",
"filename": "WinNT",
"stem": "WinNT",
"extension": "",
"path_list": [
"D:\\",
"Program Files",
"Microsoft Visual Studio",
"Common",
"Tools",
"WinNT"
]
},
{
"path": "D:\\Program Files\\Microsoft Visual Studio\\Common\\MSDev98\\Bin",
"parent": "D:\\Program Files\\Microsoft Visual Studio\\Common\\MSDev98",
"filename": "Bin",
"stem": "Bin",
"extension": "",
"path_list": [
"D:\\",
"Program Files",
"Microsoft Visual Studio",
"Common",
"MSDev98",
"Bin"
]
},
{
"path": "D:\\Program Files\\Microsoft Visual Studio\\Common\\Tools",
"parent": "D:\\Program Files\\Microsoft Visual Studio\\Common",
"filename": "Tools",
"stem": "Tools",
"extension": "",
"path_list": [
"D:\\",
"Program Files",
"Microsoft Visual Studio",
"Common",
"Tools"
]
},
{
"path": "D:\\Program Files\\Microsoft Visual Studio\\VC98\\bin",
"parent": "D:\\Program Files\\Microsoft Visual Studio\\VC98",
"filename": "bin",
"stem": "bin",
"extension": "",
"path_list": [
"D:\\",
"Program Files",
"Microsoft Visual Studio",
"VC98",
"bin"
]
}
]

View File

@ -0,0 +1 @@
D:\Program Files\Autodesk\Maya2008\bin;C:\Ruby192\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\PROGRA~1\DISKEE~2\DISKEE~1\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\QuickTime\QTSystem\;D:\Program Files\TortoiseSVN\bin;D:\Program Files\Bazaar;C:\Program Files\Android\android-sdk\tools;D:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;D:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\Microsoft Visual Studio\VC98\bin

View File

@ -0,0 +1,27 @@
[
{
"path": "c:\\Program Files (x86)\\Git\\bin",
"parent": "c:\\Program Files (x86)\\Git",
"filename": "bin",
"stem": "bin",
"extension": "",
"path_list": [
"c:\\",
"Program Files (x86)",
"Git",
"bin"
]
},
{
"path": "c:\\my\\folder",
"parent": "c:\\my",
"filename": "folder",
"stem": "folder",
"extension": "",
"path_list": [
"c:\\",
"my",
"folder"
]
}
]

View File

@ -0,0 +1 @@
c:\Program Files (x86)\Git\bin;c:\my\folder

View File

@ -0,0 +1,29 @@
[
{
"path": "/User/admin/Application Support/App.app",
"parent": "/User/admin/Application Support",
"filename": "App.app",
"stem": "App",
"extension": "app",
"path_list": [
"/",
"User",
"admin",
"Application Support",
"App.app"
]
},
{
"path": "/User/admin/path",
"parent": "/User/admin",
"filename": "path",
"stem": "path",
"extension": "",
"path_list": [
"/",
"User",
"admin",
"path"
]
}
]

View File

@ -0,0 +1 @@
/User/admin/Application Support/App.app:/User/admin/path

60
tests/test_path.py Normal file
View File

@ -0,0 +1,60 @@
import os
import json
import unittest
from typing import Dict
from jc.parsers.path import parse
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FIXTURES_DIR = 'fixtures/generic/'
def open_file(name, ext):
return open(get_path(name, ext), 'r', encoding='utf-8')
def get_path(name, ext):
return os.path.join(THIS_DIR, name + '.' + ext)
class MyTests(unittest.TestCase):
f_in: Dict = {}
f_json: Dict = {}
fixtures = {
'path--one': 'fixtures/generic/path--one',
'path--long': 'fixtures/generic/path--long',
'path--windows': 'fixtures/generic/path--windows',
'path--with-spaces': 'fixtures/generic/path--with-spaces',
}
@classmethod
def setUpClass(cls):
for file, filepath in cls.fixtures.items():
with open_file(filepath, 'out') as in_file, \
open_file(filepath, 'json') as json_file:
cls.f_in[file] = in_file.read()
cls.f_json[file] = json.loads(json_file.read())
def test_path_nodata(self):
"""
Test 'path' with no data
"""
self.assertEqual(parse('', quiet=True), {})
def test_path(self):
"""
Test 'path' with various logs
"""
for file in self.fixtures:
with self.subTest("fixture: " + file):
self.assertEqual(
parse(self.f_in[file], quiet=True),
self.f_json[file],
"Should be equal for test files: {0}.*".format(file)
)
if __name__ == '__main__':
unittest.main()

63
tests/test_path_list.py Normal file
View File

@ -0,0 +1,63 @@
import os
import json
import unittest
from typing import Dict
from jc.parsers.path_list import parse
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
FIXTURES_DIR = 'fixtures/generic/'
def open_file(name, ext):
return open(get_path(name, ext), 'r', encoding='utf-8')
def get_path(name, ext):
return os.path.join(THIS_DIR, name + '.' + ext)
class MyTests(unittest.TestCase):
f_in: Dict = {}
f_json: Dict = {}
fixtures = {
'path_list--one': 'fixtures/generic/path_list--one',
'path_list--two': 'fixtures/generic/path_list--two',
'path_list--long': 'fixtures/generic/path_list--long',
'path_list--windows': 'fixtures/generic/path_list--windows',
'path_list--windows-long': 'fixtures/generic/path_list--windows-long',
'path_list--windows-environment': 'fixtures/generic/path_list--windows-environment',
'path_list--with-spaces': 'fixtures/generic/path_list--with-spaces',
}
@classmethod
def setUpClass(cls):
for file, filepath in cls.fixtures.items():
with open_file(filepath, 'out') as in_file, \
open_file(filepath, 'json') as json_file:
cls.f_in[file] = in_file.read()
cls.f_json[file] = json.loads(json_file.read())
def test_path_nodata(self):
"""
Test 'path' with no data
"""
self.assertEqual(parse('', quiet=True), [])
def test_path(self):
"""
Test 'path' with various logs
"""
for file in self.fixtures:
with self.subTest("fixture: " + file):
self.assertEqual(
parse(self.f_in[file], quiet=True),
self.f_json[file],
"Should be equal for test files: {0}.*".format(file)
)
if __name__ == '__main__':
unittest.main()