Skip to content

Commit

Permalink
fix(tools): idf_tools.py tests should keep the original constraint file
Browse files Browse the repository at this point in the history
  • Loading branch information
dobairoland committed Aug 7, 2023
1 parent f5d9986 commit 88b40b4
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions tools/test_idf_tools/test_idf_tools_python_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,37 @@ def test_default_arguments(self): # type: () -> None


class TestCheckPythonDependencies(BasePythonInstall):

"""
The constraint file name is available as the constraint_file attribute. The content of the file is changed by these
tests. The backup_constraint_file is a temporary file with the content of the original constraint file. This is
kept in order to restore the original content of the constraint file. Keeping the original constraint file is
important for consequent tests which should not download a new one especially when the test was run with a custom
constraint file different from the one on dl.espressif.com.
"""
constraint_file: str
backup_constraint_file: str

@classmethod
def setUpClass(cls): # type: () -> None
cls.constraint_file = idf_tools.get_constraints(idf_tools.get_idf_version(), online=False)
with tempfile.NamedTemporaryFile() as f:
cls.backup_constraint_file = f.name
shutil.copyfile(cls.constraint_file, cls.backup_constraint_file)

@classmethod
def tearDownClass(cls): # type: () -> None
try:
os.remove(cls.backup_constraint_file)
except OSError:
pass

def setUp(self): # type: () -> None
if os.path.isdir(PYTHON_DIR):
shutil.rmtree(PYTHON_DIR)

def tearDown(self): # type: () -> None
shutil.copyfile(self.backup_constraint_file, self.constraint_file)
if os.path.isdir(PYTHON_DIR):
shutil.rmtree(PYTHON_DIR)

Expand All @@ -256,14 +282,10 @@ def test_check_python_dependencies(self): # type: () -> None
# are also present in the freeze list.
con_list = [r.replace('==', '>') for r in freeze_output.splitlines() if r.split('==')[0] in req_list]

con_fn = idf_tools.get_constraints(idf_tools.get_idf_version(), online=False)
# delete modified constraints file after this test is finished
self.addCleanup(os.remove, con_fn)

# Write the created constraints list into existing constraints file.
# It will not be overwritten by subsequent idf_tools.py run, because
# there is timestamp check.
with open(con_fn, 'w') as fd:
with open(self.constraint_file, 'w') as fd:
fd.write(os.linesep.join(con_list))

# Test that check_python_dependencies reports that requirements are not satisfied for
Expand All @@ -283,12 +305,8 @@ def test_check_required_packages_only(self): # type: () -> None
foo_pkg = self.dump_foopackage()
self.run_in_venv(['-m', 'pip', 'install', foo_pkg])

con_fn = idf_tools.get_constraints(idf_tools.get_idf_version(), online=False)
# delete modified constraints file after this test is finished
self.addCleanup(os.remove, con_fn)

# append foopackage constraint to the existing constraints file
with open(con_fn, 'a') as fd:
with open(self.constraint_file, 'a') as fd:
fd.write('foopackage>0.99')

# check-python-dependencies should not complain about dummy_package
Expand All @@ -305,20 +323,16 @@ def test_dev_version(self): # type: () -> None
foo_pkg = self.dump_foopackage_dev()
self.run_in_venv(['-m', 'pip', 'install', foo_pkg])

con_fn = idf_tools.get_constraints(idf_tools.get_idf_version(), online=False)
# delete modified constraints file after this test is finished
self.addCleanup(os.remove, con_fn)

# append foopackage constraint to the existing constraints file
with open(con_fn, 'r+') as fd:
with open(self.constraint_file, 'r+') as fd:
con_lines = fd.readlines()
fd.write('foopackage~=0.98')

output = self.run_idf_tools(['check-python-dependencies'])
self.assertIn(REQ_SATISFIED, output)

# append foopackage dev version constraint to the existing constraints file
with open(con_fn, 'r+') as fd:
with open(self.constraint_file, 'r+') as fd:
fd.writelines(con_lines + ['foopackage==0.99.dev0'])

output = self.run_idf_tools(['check-python-dependencies'])
Expand Down

0 comments on commit 88b40b4

Please sign in to comment.