Skip to content

Commit

Permalink
fix packager
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpoelen committed Aug 12, 2022
1 parent e8b5c12 commit 2a0a4a2
Show file tree
Hide file tree
Showing 7 changed files with 555 additions and 177 deletions.
2 changes: 1 addition & 1 deletion packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
try:
run_packager(args)
except Exception as e:
from .wallix_packager.error import print_error
from wallix_packager.error import print_error
print_error(e)
sys.exit(1)
2 changes: 1 addition & 1 deletion pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
run_synchronizer(gitconfig, submodule_path, args)
except Exception as e:
from .wallix_packager.error import print_error
print_error(e, f'Setting {submodule_path} submodule failed: ')
print_error(f'Setting {submodule_path} submodule failed: {e}')
sys.exit(1)
59 changes: 53 additions & 6 deletions tests/test_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
# -*- coding: utf-8 -*-

import unittest
from wallix_packager.packager import replace_dict_all, read_and_update_config, update_config_variables
from wallix_packager.packager import (read_config,
replace_dict_all,
normalize_config,
extract_version_or_die,
update_config_variables,
ExtractedVersion,
PackagerError)

class TestPackager(unittest.TestCase):
def test_replace_dict_all(self):
Expand All @@ -16,27 +22,68 @@ def test_replace_dict_all(self):
'AA': 'plop',
}
),
'a AA aa plop xY a AA%D% aa A xY')
'a AA aa plop xY a AA aa A xY')

def test_read_and_update_config(self):
config = read_and_update_config(
'tests/data/config1', {'DIST_NAME':'debian', 'DIST_ID':'squeeze'})
def test_read_config(self):
with open('tests/data/config1') as f:
config = read_config(
f, {'DIST_NAME':'debian', 'DIST_ID':'squeeze'})
self.assertEqual(config, {
'DIST_ID': 'squeeze',
'DIST_NAME': 'debian',
'PKG_DISTRIBUTION': 'squeeze',
'X': '3',
'VAR1': 'v1',
'VAR2': 'v2',
'VAR3': 'v3'
})

def test_normalize_config(self):
config = {
'DIST_ID': 'squeeze',
}
normalize_config(config)
self.assertEqual(config, {
'DIST_ID': 'squeeze',
'PKG_DISTRIBUTION': 'squeeze',
})

config = {
'DIST_ID': 'ubuntu',
}
normalize_config(config)
self.assertEqual(config, {
'DIST_ID': 'ubuntu',
'PKG_DISTRIBUTION': 'unstable',
'TARGET_NAME': '+ubuntu',
})

config = {
'DIST_ID': 'squeeze',
'PKG_DISTRIBUTION': 'buble',
}
normalize_config(config)
self.assertEqual(config, {
'DIST_ID': 'squeeze',
'PKG_DISTRIBUTION': 'buble',
})

def test_update_config_variables(self):
config = {'V':'x'}
unparsed = update_config_variables(config, ['XYZ', 'X-Y', 'X=', 'ABC=123', 'V+=y'])
self.assertEqual(config, {'V': 'xy', 'X': '', 'ABC': '123'})
self.assertEqual(unparsed, ['XYZ', 'X-Y'])

def test_extract_version_or_die(self):
normalizer = lambda s: s
content = '#abc\n#abc\nversion=123a b\nxyz'
self.assertEqual(
extract_version_or_die('version=(\d+\w+)', content, normalizer),
ExtractedVersion('123a', (18, 22), content))

with self.assertRaises(PackagerError):
extract_version_or_die('^VERSION=(\d+\w+)', content, normalizer),
ExtractedVersion('123a', (18, 22), content)


if __name__ == '__main__':
unittest.main()
11 changes: 7 additions & 4 deletions wallix_packager/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

import sys
import traceback
from typing import Union


def print_error(exception: Exception, prefix: str = '', file=sys.stderr) -> None:
s = str(exception)
border = '=' * (len(s) + len(prefix) + 4)
print(f'\x1b[31m{border}\n= {prefix}{s} =\n{border}\x1b[0m', file=file)
def print_error(s: Union[str, Exception], file=sys.stderr) -> None:
parts = str(s).split('\n')
line_size = max(map(len, parts))
border = '=' * (line_size + 4)
s = ''.join(f'| {s}{" " * (line_size - len(s))} |\n' for s in parts)
print(f'\x1b[31m{border}\n{s}{border}\x1b[0m', file=file)
traceback.print_tb(sys.exc_info()[2], file=file)
Loading

0 comments on commit 2a0a4a2

Please sign in to comment.