From 8e20f216f16f0a754b2b1061ea43151574ecc9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Mon, 7 Mar 2016 15:09:07 +0100 Subject: [PATCH] [paths] Document and test translate_path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The documentation was inaccurate. Signed-off-by: Lubomír Sedlář --- doc/configuration.rst | 7 +++---- pungi/paths.py | 8 ++++++-- tests/test_paths.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 tests/test_paths.py diff --git a/doc/configuration.rst b/doc/configuration.rst index 00e92473..57ccf2dd 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -905,13 +905,12 @@ Translate Paths Settings ======================== **translate_paths** - (*list*) -- list of paths to translate; format: [(path,translated_path)] + (*list*) -- list of paths to translate; format: ``[(path, translated_path)]`` .. note:: This feature becomes useful when you need to transform compose location - into e.g. a http repo which is can be passed to koji image-build. - Translation needs to be invoked by a function call in pungi. - os.path.normpath() is applied on both path and translated_path + into e.g. a HTTP repo which is can be passed to ``koji image-build``. + The ``path`` part is normalized via ``os.path.normpath()``. Example config diff --git a/pungi/paths.py b/pungi/paths.py index d47c5b4b..186bcb30 100644 --- a/pungi/paths.py +++ b/pungi/paths.py @@ -25,6 +25,7 @@ import os from pungi.util import makedirs + def translate_path(compose, path): """ @param compose - required for access to config @@ -36,11 +37,14 @@ def translate_path(compose, path): for prefix, newvalue in mapping: prefix = os.path.normpath(prefix) if normpath.startswith(prefix): - # don't call os.path.normpath on result since that would break http:// -> http:/ and so on - return normpath.replace(prefix, newvalue, 1) # replace only 1 occurance + # We can't call os.path.normpath on result since it is not actually + # a path - http:// would get changed to http:/ and so on. + # Only the first occurance should be replaced. + return normpath.replace(prefix, newvalue, 1) return normpath + class Paths(object): def __init__(self, compose): paths_module_name = compose.conf.get("paths_module", None) diff --git a/tests/test_paths.py b/tests/test_paths.py new file mode 100644 index 00000000..9f7894a1 --- /dev/null +++ b/tests/test_paths.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +import mock +import unittest +import os +import sys + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) + +from pungi import paths + + +class TranslatePathTestCase(unittest.TestCase): + def test_does_nothing_without_config(self): + compose = mock.Mock(conf={}) + ret = paths.translate_path(compose, '/mnt/koji/compose/rawhide/XYZ') + self.assertEqual(ret, '/mnt/koji/compose/rawhide/XYZ') + + def test_translates_prefix(self): + compose = mock.Mock(conf={ + 'translate_paths': [('/mnt/koji', 'http://example.com')] + }) + ret = paths.translate_path(compose, '/mnt/koji/compose/rawhide/XYZ') + self.assertEqual(ret, 'http://example.com/compose/rawhide/XYZ') + + def test_does_not_translate_not_matching(self): + compose = mock.Mock(conf={ + 'translate_paths': [('/mnt/koji', 'http://example.com')] + }) + ret = paths.translate_path(compose, '/mnt/fedora_koji/compose/rawhide/XYZ') + self.assertEqual(ret, '/mnt/fedora_koji/compose/rawhide/XYZ') + + +if __name__ == "__main__": + unittest.main()