Skip to content

Rewrite logic with system paths and imports in python #1578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 1, 2023

Conversation

tamarinvs19
Copy link
Collaborator

@tamarinvs19 tamarinvs19 commented Dec 23, 2022

Description

  1. Now all paths which we use in python code have row-string format. It helps when path contains special symbols with \.
  2. Also we collecting in sys.paths all path for imported modules:
    • for import <module>
    • for from <module> import <smth>

Fixes #1562

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Refactoring (typos and non-functional changes)

How Has This Been Tested?

Manual Scenario

Test for import modules

  1. Create python project with the next files
# /mod1/a.py

def sum(x, y):
    return x + y
# /mod1/b.py

import a

def double_sum(x, y):
    return a.sum(x, y)
  1. Generate tests for double_sum in b.py
  2. Expected result: generated test
import sys
sys.path.append(r'..')
sys.path.append(r'..\mod1')
import builtins
import mod1
import unittest
import mod1.b


class TestTopLevelFunctions(unittest.TestCase):
    # region Test suites for executable mod1.b.double_sum
    
    # region
    
    def test_double_sum(self):
        actual = mod1.b.double_sum(True, False)
        
        self.assertEqual(1, actual)
    # endregion
    
    # endregion

Test for import modules with another import type from <modult> import <smth>

  1. Create python project with the next files
# /mod1/submod1/a.py

def sum(x, y):
    return x + y
# /mod1/b.py
from submod1 import a

def double_sum(x, y):
    return a.sum(x, y)
  1. Generate tests for double_sum in b.py
  2. Expected result: generated test
import sys
sys.path.append(r'..')
sys.path.append(r'..\mod1')
import builtins
import mod1
import unittest
import mod1.b


class TestTopLevelFunctions(unittest.TestCase):
    # region Test suites for executable mod1.b.double_sum
    
    # region
    
    def test_double_sum(self):
        actual = mod1.b.double_sum(83, False)
        
        self.assertEqual(83, actual)
    # endregion
    
    # endregion

Checklist (remove irrelevant options):

  • The change followed the style guidelines of the UTBot project
  • Self-review of the code is passed
  • The change contains enough commentaries, particularly in hard-to-understand areas
  • No new warnings
  • New tests have been added
  • All tests pass locally with my changes

@tamarinvs19 tamarinvs19 added the lang-python Issue is related to python support label Dec 23, 2022
@alisevych alisevych added ctg-refactoring Issue related to refactoring process ctg-bug-fix PR is fixing a bug labels Jan 24, 2023
@tamarinvs19 tamarinvs19 merged commit d2edb45 into main Feb 1, 2023
@tamarinvs19 tamarinvs19 deleted the tamarinvs19/fix_module_sys_paths branch February 1, 2023 07:44
@alisevych alisevych added this to the 2023.03 Release milestone Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ctg-bug-fix PR is fixing a bug ctg-refactoring Issue related to refactoring process lang-python Issue is related to python support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot generate tests when user code calls function from another module
3 participants