Example #1
0
    def test_02(self):
        """
        Test Case 02:
        Test :py:mod:`magrathea.conf` via the :py:func:`~magrathea.conf.get_conf` function interface (invalid key).

        Test is passed if returned value is None.
        """
        key = 'TEST_TEST_TEST'
        result = get_conf(key)
        self.assertIsNone(result)
Example #2
0
    def test_01(self):
        """
        Test Case 01:
        Test :py:mod:`magrathea.conf` via the :py:func:`~magrathea.conf.get_conf` function interface (valid key).

        Test is passed if returned value corresponds to expected value.
        """
        key = 'DEFAULT_CHARSET'
        result = get_conf(key)
        self.assertEqual(result, 'utf-8')
Example #3
0
def comp_open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
    """
    Compatibility wrapper for :py:func:`open`. This function makes more advanced
    options from Python 3 available for Python 2. Similar to the Python 3 implementation
    of :py:func:`open`, this function may act as :py:keyword:`with` statement context manager.

    Other than the original :py:func:`open` function in Python 2, this function does not
    return a legacy file object (``<type 'file'>``) when used on Python 2. Instead, as in
    Python 3, it returns an :py:mod:`io` wrapper object, depending on what kind of file has
    been opened (binary or text). For text files, this will most likely be something like
    ``<type '_io.TextIOWrapper'>``.

    .. note::

       In case no encoding is specified, the default encoding as defined by
       :py:data:`magrathea.conf.default.DEFAULT_CHARSET` will be used.

    :param file: file is either a string or bytes object giving the pathname or
                 an integer file descriptor of the file to be wrapped
    :param mode: specifies the mode in which the file is opened
    :param buffering: optional integer used to set the buffering policy
    :param encoding: name of the encoding used to decode or encode the file
    :param errors: optional string that specifies how encoding and decoding errors are to be handled
    :param newline: controls how universal newlines mode works
    :param closefd: if False and a file descriptor rather than a filename was given, the underlying
                    file descriptor will be kept open when the file is closed
    :param opener: custom opener
    :returns: a :py:term:`file object`
    """
    if not encoding:
        encoding = get_conf('DEFAULT_CHARSET')
    if sys.version_info < (3, 0, 0):
        fp = io_open(
            file,
            mode=mode,
            buffering=buffering,
            encoding=encoding,
            errors=errors,
            newline=newline,
            closefd=closefd
        )
    else:
        fp = open(
            file,
            mode=mode,
            buffering=buffering,
            encoding=encoding,
            errors=errors,
            newline=newline,
            closefd=closefd,
            opener=opener
        )
    return fp
Example #4
0
    def test_05(self):
        """
        Test Case 05:
        Open a file using :py:func:`magrathea.utils.compat.comp_open`.

        Test is passed if file content can be read and equals the given input.
        """
        test_string = "String with Ünicøde characters"
        fp, name = tempfile.mkstemp()
        os.write(fp, to_bytes(test_string))
        os.close(fp)
        fd = comp_open(name, mode='r', encoding=get_conf('DEFAULT_CHARSET'))
        result_string = fd.read()
        fd.close()
        os.unlink(name)
        self.assertEqual(test_string, result_string)
Example #5
0
    def test_06(self):
        """
        Test Case 06:
        Use :py:func:`magrathea.utils.compat.comp_open` as :py:keyword:`with` statement context manager.

        Test is passed if file content can be read, equals the given input and the file pointer is closed.
        """
        test_string = "String with Ünicøde characters"
        fp, name = tempfile.mkstemp()
        os.write(fp, to_bytes(test_string))
        os.close(fp)
        with comp_open(name, mode='r', encoding=get_conf('DEFAULT_CHARSET')) as fd:
            result_string = fd.read()
        os.unlink(name)
        self.assertTrue(fd.closed)
        self.assertEqual(test_string, result_string)
Example #6
0
    def test_07(self):
        """
        Test Case 07:
        Deploy a template into a temporary directory.

        Test is passed if deployment doesn't raise exception and all expected files exist.
        """
        def _filter(item):
            _file_map = {
                'makefile': 'Makefile',
                'cmakelists.txt': 'CMakeLists.txt',
                'readme': 'README'
            }
            if item in _file_map:
                return _file_map[item]
            return item

        td = tempfile.mkdtemp()
        obj = Template(template='planet', path=td)
        obj.deploy()
        cp = CompConfigParser(allow_no_value=True)
        cf = ApplicationConf.get_instance()
        with comp_open(os.path.join(get_conf('DEFAULT_TEMPLATE_PATH'), 'planet', '__init__.ini'), mode='r') as fp:
            content = fp.read()
            content = content.format(**cf)
        cp.read_string(content)
        flag = True
        if cp.has_section('dirs'):
            for directory in cp.options('dirs'):
                flag = flag and os.path.exists(os.path.join(obj.path, directory))
        if cp.has_section('files'):
            for file in cp.options('files'):
                flag = flag and os.path.exists(os.path.join(obj.path, _filter(file)))
        if cp.has_section('binaries'):
            for binary in cp.options('binaries'):
                flag = flag and os.path.exists(os.path.join(obj.path, _filter(binary)))
        shutil.rmtree(td)
        self.assertTrue(flag)