Skip to content

Commit

Permalink
Add tests for write_dependency_update
Browse files Browse the repository at this point in the history
  • Loading branch information
albertyw committed Feb 5, 2024
1 parent c40df1a commit 9c4852d
Showing 1 changed file with 68 additions and 21 deletions.
89 changes: 68 additions & 21 deletions req_update/tests/test_python.py
Expand Up @@ -184,82 +184,129 @@ def test_edit_requirements_not_found(self) -> None:

class TestWriteDependencyUpdate(unittest.TestCase):
def setUp(self) -> None:
self.tempfile = tempfile.NamedTemporaryFile()
self.tempfile_requirements = tempfile.NamedTemporaryFile()
self.original_reqfiles = python.REQUIREMENTS_FILES
python.REQUIREMENTS_FILES = [self.tempfile.name]
python.REQUIREMENTS_FILES = [self.tempfile_requirements.name]
self.tempfile_pyproject = tempfile.NamedTemporaryFile()
self.original_pyprojectfiles = python.PYPROJECT_FILES
python.PYPROJECT_FILES = [self.tempfile_pyproject.name]
u = util.Util()
self.python = python.Python(u)
self.python.util.dry_run = False

def tearDown(self) -> None:
self.tempfile.close()
self.tempfile_requirements.close()
self.tempfile_pyproject.close()
python.REQUIREMENTS_FILES = self.original_reqfiles
python.PYPROJECT_FILES = self.original_pyprojectfiles

def test_write_dependency_update_no_comment(self) -> None:
with open(self.tempfile.name, 'w') as handle:
with open(self.tempfile_requirements.name, 'w') as handle:
handle.write('abcd==0.0.1\nvarsnap==1.0.0')
with open(self.tempfile_pyproject.name, 'w') as handle:
handle.write(' "abcd==0.0.1",\n "varsnap==1.0.0"')
updated = self.python.write_dependency_update('varsnap', '1.2.3')
self.assertTrue(updated)
with open(self.tempfile.name, 'r') as handle:
with open(self.tempfile_requirements.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip('\n'), 'abcd==0.0.1')
self.assertEqual(lines[1].strip('\n'), 'varsnap==1.2.3')
self.assertIn(self.tempfile.name, self.python.updated_files)
with open(self.tempfile_pyproject.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip("\n"), ' "abcd==0.0.1",')
self.assertEqual(lines[1].strip("\n"), ' "varsnap==1.2.3",')
self.assertIn(self.tempfile_requirements.name, self.python.updated_files)
self.assertIn(self.tempfile_pyproject.name, self.python.updated_files)

def test_write_dependency_update(self) -> None:
with open(self.tempfile.name, 'w') as handle:
with open(self.tempfile_requirements.name, 'w') as handle:
handle.write('abcd==0.0.1\nvarsnap==1.0.0 # qwer')
with open(self.tempfile_pyproject.name, 'w') as handle:
handle.write(' "abcd==0.0.1",\n "varsnap==1.0.0", # qwer')
updated = self.python.write_dependency_update('varsnap', '1.2.3')
self.assertTrue(updated)
with open(self.tempfile.name, 'r') as handle:
with open(self.tempfile_requirements.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip('\n'), 'abcd==0.0.1')
self.assertEqual(lines[1].strip('\n'), 'varsnap==1.2.3 # qwer')
self.assertIn(self.tempfile.name, self.python.updated_files)
with open(self.tempfile_pyproject.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip("\n"), ' "abcd==0.0.1",')
self.assertEqual(lines[1].strip("\n"), ' "varsnap==1.2.3", # qwer')
self.assertIn(self.tempfile_requirements.name, self.python.updated_files)
self.assertIn(self.tempfile_pyproject.name, self.python.updated_files)

def test_write_dependency_update_aligned(self) -> None:
with open(self.tempfile.name, 'w') as handle:
with open(self.tempfile_requirements.name, 'w') as handle:
handle.write('abcd==0.0.1\nvarsnap==1.0 # qwer')
with open(self.tempfile_pyproject.name, 'w') as handle:
handle.write(' "abcd==0.0.1",\n "varsnap==1.0", # qwer')
updated = self.python.write_dependency_update('varsnap', '1.2.3')
self.assertTrue(updated)
with open(self.tempfile.name, 'r') as handle:
with open(self.tempfile_requirements.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip('\n'), 'abcd==0.0.1')
self.assertEqual(lines[1].strip('\n'), 'varsnap==1.2.3 # qwer')
self.assertIn(self.tempfile.name, self.python.updated_files)
with open(self.tempfile_pyproject.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip("\n"), ' "abcd==0.0.1",')
self.assertEqual(lines[1].strip("\n"), ' "varsnap==1.2.3", # qwer')
self.assertIn(self.tempfile_requirements.name, self.python.updated_files)
self.assertIn(self.tempfile_pyproject.name, self.python.updated_files)

def test_write_dependency_update_no_op(self) -> None:
with open(self.tempfile.name, 'w') as handle:
with open(self.tempfile_requirements.name, 'w') as handle:
handle.write('abcd==0.0.1\nvarsnap==1.0.0 # qwer')
with open(self.tempfile_pyproject.name, 'w') as handle:
handle.write(' "abcd==0.0.1",\n "varsnap==1.0.0", # qwer')
updated = self.python.write_dependency_update('varsnap', '1.0.0')
self.assertFalse(updated)
with open(self.tempfile.name, 'r') as handle:
with open(self.tempfile_requirements.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip('\n'), 'abcd==0.0.1')
self.assertEqual(lines[1].strip('\n'), 'varsnap==1.0.0 # qwer')
self.assertNotIn(self.tempfile.name, self.python.updated_files)
with open(self.tempfile_pyproject.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip("\n"), ' "abcd==0.0.1",')
self.assertEqual(lines[1].strip("\n"), ' "varsnap==1.0.0", # qwer')
self.assertNotIn(self.tempfile_requirements.name, self.python.updated_files)
self.assertNotIn(self.tempfile_pyproject.name, self.python.updated_files)

def test_write_dependency_update_post(self) -> None:
with open(self.tempfile.name, 'w') as handle:
with open(self.tempfile_requirements.name, 'w') as handle:
handle.write('abcd==0.0.1\nvarsnap==1.0.0post0 # qwer')
with open(self.tempfile_pyproject.name, 'w') as handle:
handle.write(' "abcd==0.0.1",\n "varsnap==1.0.0post0", # qwer')
updated = self.python.write_dependency_update('varsnap', '1.2.3')
self.assertTrue(updated)
with open(self.tempfile.name, 'r') as handle:
with open(self.tempfile_requirements.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip('\n'), 'abcd==0.0.1')
self.assertEqual(lines[1].strip('\n'), 'varsnap==1.2.3 # qwer')
self.assertIn(self.tempfile.name, self.python.updated_files)
with open(self.tempfile_pyproject.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip("\n"), ' "abcd==0.0.1",')
self.assertEqual(lines[1].strip("\n"), ' "varsnap==1.2.3", # qwer')
self.assertIn(self.tempfile_requirements.name, self.python.updated_files)
self.assertIn(self.tempfile_pyproject.name, self.python.updated_files)

def test_add_spacing(self) -> None:
with open(self.tempfile.name, 'w') as handle:
with open(self.tempfile_requirements.name, 'w') as handle:
handle.write('abcd==0.0.1\nvarsnap==1.0 # qwer')
with open(self.tempfile_pyproject.name, 'w') as handle:
handle.write(' "abcd==0.0.1",\n "varsnap==1.0", # qwer')
updated = self.python.write_dependency_update('varsnap', '1.2.3')
self.assertTrue(updated)
with open(self.tempfile.name, 'r') as handle:
with open(self.tempfile_requirements.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip('\n'), 'abcd==0.0.1')
self.assertEqual(lines[1].strip('\n'), 'varsnap==1.2.3 # qwer')
self.assertIn(self.tempfile.name, self.python.updated_files)
with open(self.tempfile_pyproject.name, 'r') as handle:
lines = handle.readlines()
self.assertEqual(lines[0].strip("\n"), ' "abcd==0.0.1",')
self.assertEqual(lines[1].strip("\n"), ' "varsnap==1.2.3", # qwer')
self.assertIn(self.tempfile_requirements.name, self.python.updated_files)
self.assertIn(self.tempfile_pyproject.name, self.python.updated_files)


class TestInstallUpdates(unittest.TestCase):
Expand Down

0 comments on commit 9c4852d

Please sign in to comment.