<commit_before>from __future__ import with_statement
import unittest
import tempfile
import inspect
import shutil
import sys
import os
import re
from os.path import abspath, dirname, exists, isabs, join, normpath

from robot.errors import DataError
from robot.utils.importer import Importer
from robot.utils.asserts import (assert_equals, assert_true, assert_raises,
                                 assert_raises_with_msg)


CURDIR = dirname(abspath(__file__))
LIBDIR = normpath(join(CURDIR, '..', '..', 'atest', 'testresources', 'testlibs'))
TEMPDIR = tempfile.gettempdir()
TESTDIR = join(TEMPDIR, 'robot-importer-testing')
WINDOWS_PATH_IN_ERROR = re.compile(r"'\w:\\")


def assert_prefix(error, expected):
    message = unicode(error)
    count = 3 if WINDOWS_PATH_IN_ERROR.search(message) else 2
    prefix = ':'.join(message.split(':')[:count]) + ':'
    assert_equals(prefix, expected)

def create_temp_file(name, attr=42, extra_content=''):
    if not exists(TESTDIR):
        os.mkdir(TESTDIR)
    path = join(TESTDIR, name)
    with open(path, 'w') as file:
        file.write('attr = %r\n' % attr)
        file.write('def func():\n  return attr\n')
        file.write(extra_content)
    return path

class LoggerStub(object):

    def __init__(self, remove_extension=False):
        self.messages = []
        self.remove_extension = remove_extension

    def info(self, msg):
        if self.remove_extension:
            for ext in '$py.class', '.pyc', '.py':
                msg = msg.replace(ext, '')
        self.messages.append(msg)

    def assert_message(self, msg, index=0):
        assert_equals(self.messages[index], msg)


class TestImportByPath(unittest.TestCase):

    def setUp(self):
        self.tearDown()

    def tearDown(self):
        if exists(TESTDIR):
            shutil.rmtree(TESTDIR)

    def test_python_file(self):
        path = create_temp_file('test.py')
        self._import_and_verify(path)

    def test_python_directory(self):
        create_temp_file('__init__.py')
        self._import_and_verify(TESTDIR + os.sep)

    def test_import_different_file_and_directory_with_same_name(self):
        removed = "Removed module 'test' from sys.modules to import fresh module."
        imported = "Imported module 'test' from '%s'."
        path1 = create_temp_file('test.py', attr=1)
        self._import_and_verify(path1, attr=1)
        self.logger.assert_message(imported % path1, index=-1)
        path2 = join(TESTDIR, 'test')
        os.mkdir(path2)
        create_temp_file(join(path2, '__init__.py'), attr=2)
        self._import_and_verify(path2, attr=2, directory=path2)
        self.logger.assert_message(removed)
        self.logger.assert_message(imported % path2, index=1)
        path3 = create_temp_file(join(path2, 'test.py'), attr=3)
        self._import_and_verify(path3, attr=3, directory=path2)
        self.logger.assert_message(removed)
        self.logger.assert_message(imported % path3, index=1)

    def test_import_class_from_file(self):
        path = create_temp_file('test.py', extra_content='class test:\n def m(s): return 1')
        klass = Importer().import_class_or_module_by_path(path)
        assert_true(inspect.isclass(klass))
        assert_equals(klass.__name__, 'test')
        assert_equals(klass().m(), 1)

    def test_invalid_python_file(self):
        path = create_temp_file('test.py', extra_content='invalid content')
        error = assert_raises(DataError, self._import_and_verify, path)
        assert_prefix(error, "Importing '%s' failed: SyntaxError:" % path)

    def test_logging_when_importing_module(self):
        path = join(LIBDIR, 'classes.py')
        self._import(path, name='lib')
        self.logger.assert_message("Imported lib module 'classes' from '%s'." % path)

    def test_logging_when_importing_python_class(self):
        path = join(LIBDIR, 'ExampleLibrary.py')<commit_msg>test fix<commit_after>from __future__ import with_statement
import unittest
import tempfile
import inspect
import shutil
import sys
import os
import re
from os.path import abspath, dirname, exists, isabs, join, normpath

from robot.errors import DataError
from robot.utils.importer import Importer
from robot.utils.asserts import (assert_equals, assert_true, assert_raises,
                                 assert_raises_with_msg)


CURDIR = dirname(abspath(__file__))
LIBDIR = normpath(join(CURDIR, '..', '..', 'atest', 'testresources', 'testlibs'))
TEMPDIR = tempfile.gettempdir()
TESTDIR = join(TEMPDIR, 'robot-importer-testing')
WINDOWS_PATH_IN_ERROR = re.compile(r"'\w:\\")


def assert_prefix(error, expected):
    message = unicode(error)
    count = 3 if WINDOWS_PATH_IN_ERROR.search(message) else 2
    prefix = ':'.join(message.split(':')[:count]) + ':'
    assert_equals(prefix, expected)

def create_temp_file(name, attr=42, extra_content=''):
    if not exists(TESTDIR):
        os.mkdir(TESTDIR)
    path = join(TESTDIR, name)
    with open(path, 'w') as file:
        file.write('attr = %r\n' % attr)
        file.write('def func():\n  return attr\n')
        file.write(extra_content)
    return path

class LoggerStub(object):

    def __init__(self, remove_extension=False):
        self.messages = []
        self.remove_extension = remove_extension

    def info(self, msg):
        if self.remove_extension:
            for ext in '$py.class', '.pyc', '.py':
                msg = msg.replace(ext, '')
        self.messages.append(msg)

    def assert_message(self, msg, index=-1):
        assert_equals(self.messages[index], msg)


class TestImportByPath(unittest.TestCase):

    def setUp(self):
        self.tearDown()

    def tearDown(self):
        if exists(TESTDIR):
            shutil.rmtree(TESTDIR)

    def test_python_file(self):
        path = create_temp_file('test.py')
        self._import_and_verify(path)

    def test_python_directory(self):
        create_temp_file('__init__.py')
        self._import_and_verify(TESTDIR + os.sep)

    def test_import_different_file_and_directory_with_same_name(self):
        removed = "Removed module 'test' from sys.modules to import fresh module."
        imported = "Imported module 'test' from '%s'."
        path1 = create_temp_file('test.py', attr=1)
        self._import_and_verify(path1, attr=1)
        self.logger.assert_message(imported % path1)
        path2 = join(TESTDIR, 'test')
        os.mkdir(path2)
        create_temp_file(join(path2, '__init__.py'), attr=2)
        self._import_and_verify(path2, attr=2, directory=path2)
        self.logger.assert_message(removed, index=0)
        self.logger.assert_message(imported % path2, index=1)
        path3 = create_temp_file(join(path2, 'test.py'), attr=3)
        self._import_and_verify(path3, attr=3, directory=path2)
        self.logger.assert_message(removed, index=0)
        self.logger.assert_message(imported % path3, index=1)

    def test_import_class_from_file(self):
        path = create_temp_file('test.py', extra_content='class test:\n def m(s): return 1')
        klass = Importer().import_class_or_module_by_path(path)
        assert_true(inspect.isclass(klass))
        assert_equals(klass.__name__, 'test')
        assert_equals(klass().m(), 1)

    def test_invalid_python_file(self):
        path = create_temp_file('test.py', extra_content='invalid content')
        error = assert_raises(DataError, self._import_and_verify, path)
        assert_prefix(error, "Importing '%s' failed: SyntaxError:" % path)

    def test_logging_when_importing_module(self):
        path = join(LIBDIR, 'classes.py')
        self._import(path, name='lib')
        self.logger.assert_message("Imported lib module 'classes' from '%s'." % path)

    def test_logging_when_importing_python_class(self):
        path = join(LIBDIR, 'ExampleLibrary.py')
{'fraction_non_alphanumeric': 0.12118100128369705, 'fraction_numerical': 0.007830551989730424, 'mean_word_length': 3.4393162393162395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6254891', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2479, 'n_words': 497}