def test_basename(self) -> None: fmt = PackageFormatter() self.assertEqual( fmt.format('{basename}', Package(name='foo', basename='bar')), 'bar') self.assertEqual(fmt.format('{basename}', Package(name='foo')), 'foo')
def test_filter_basename(self) -> None: fmt = PackageFormatter() self.assertEqual( fmt.format('{srcname|basename}', spawn_package(srcname='foo/bar')), 'bar') self.assertEqual( fmt.format('{srcname|dirname}', spawn_package(srcname='foo/bar')), 'foo')
def test_filter_libfirstletter(self) -> None: fmt = PackageFormatter() self.assertEqual( fmt.format('{name|libfirstletter}', Package(name='FOO', version='1.0')), 'f') self.assertEqual( fmt.format('{name|libfirstletter}', Package(name='LIBFOO', version='1.0')), 'libf')
def test_filter_stripdmo(self) -> None: fmt = PackageFormatter() self.assertEqual( fmt.format('{name|stripdmo}', spawn_package(name='mplayer')), 'mplayer') self.assertEqual( fmt.format('{name|stripdmo}', spawn_package(name='mplayer-dmo')), 'mplayer')
def test_incdec(self) -> None: fmt = PackageFormatter() self.assertEqual( fmt.format('{num|inc}', spawn_package(name='foo', extrafields={'num': 'NaN'})), '1') self.assertEqual( fmt.format('{num|inc}', spawn_package(name='foo', extrafields={'num': 1})), '2') self.assertEqual( fmt.format('{num|dec}', spawn_package(name='foo', extrafields={'num': 1})), '0')
def test_simple(self) -> None: pkg = Package(name='foo', version='1.0', origversion='1.0_1', category='devel', subrepo='main', extrafields={'foo': 'bar'}) fmt = PackageFormatter() self.assertEqual(fmt.format('Just A String', pkg), 'Just A String') self.assertEqual( fmt.format( '{name} {version} {origversion} {category} {subrepo} {foo}', pkg), 'foo 1.0 1.0_1 devel main bar')
def test_escaping(self) -> None: self.assertEqual( PackageFormatter().format( 'package name is {name}', Package(name='foo && $(bar) `date` "<>" \'<>\'')), 'package name is foo && $(bar) `date` "<>" \'<>\'') self.assertEqual( PackageFormatter(escape_mode='url').format( 'http://example.com/{name}', Package(name='foo && $(bar) `date` "<>" \'<>\'')), 'http://example.com/foo%20%26%26%20%24%28bar%29%20%60date%60%20%22%3C%3E%22%20%27%3C%3E%27' ) self.assertEqual( PackageFormatter(escape_mode='shell').format( 'echo {name}', Package(name='foo && $(bar) `date` "<>" \'<>\'')), """echo 'foo && $(bar) `date` "<>" '"'"'<>'"'"''""" # What we'd actually prefer is something more like: # 'echo foo\ \&\& \$\(bar\)\ \`date\`\ \"\<\>\"\ \'\<\>\'' )
def test_filter_lowercase(self) -> None: fmt = PackageFormatter() self.assertEqual( fmt.format('{name|lowercase}', Package(name='FOO', version='1.0')), 'foo')
def pkg_format(value, pkg): return PackageFormatter().format(value, pkg)
def test_empty_origversion(self): fmt = PackageFormatter() self.assertEqual( fmt.format('{origversion}', Package(name='foo', version='1.0')), '1.0')
def pkg_format(value: str, pkg: Package, escape_mode: Optional[str] = None) -> str: return PackageFormatter(escape_mode=escape_mode).format( value, pkg, escape_mode=escape_mode)