test_modified.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """Tests for distutils._modified."""
  2. import os
  3. import types
  4. from distutils._modified import newer, newer_group, newer_pairwise, newer_pairwise_group
  5. from distutils.errors import DistutilsFileError
  6. from distutils.tests import support
  7. import pytest
  8. class TestDepUtil(support.TempdirManager):
  9. def test_newer(self):
  10. tmpdir = self.mkdtemp()
  11. new_file = os.path.join(tmpdir, 'new')
  12. old_file = os.path.abspath(__file__)
  13. # Raise DistutilsFileError if 'new_file' does not exist.
  14. with pytest.raises(DistutilsFileError):
  15. newer(new_file, old_file)
  16. # Return true if 'new_file' exists and is more recently modified than
  17. # 'old_file', or if 'new_file' exists and 'old_file' doesn't.
  18. self.write_file(new_file)
  19. assert newer(new_file, 'I_dont_exist')
  20. assert newer(new_file, old_file)
  21. # Return false if both exist and 'old_file' is the same age or younger
  22. # than 'new_file'.
  23. assert not newer(old_file, new_file)
  24. def _setup_1234(self):
  25. tmpdir = self.mkdtemp()
  26. sources = os.path.join(tmpdir, 'sources')
  27. targets = os.path.join(tmpdir, 'targets')
  28. os.mkdir(sources)
  29. os.mkdir(targets)
  30. one = os.path.join(sources, 'one')
  31. two = os.path.join(sources, 'two')
  32. three = os.path.abspath(__file__) # I am the old file
  33. four = os.path.join(targets, 'four')
  34. self.write_file(one)
  35. self.write_file(two)
  36. self.write_file(four)
  37. return one, two, three, four
  38. def test_newer_pairwise(self):
  39. one, two, three, four = self._setup_1234()
  40. assert newer_pairwise([one, two], [three, four]) == ([one], [three])
  41. def test_newer_pairwise_mismatch(self):
  42. one, two, three, four = self._setup_1234()
  43. with pytest.raises(ValueError):
  44. newer_pairwise([one], [three, four])
  45. with pytest.raises(ValueError):
  46. newer_pairwise([one, two], [three])
  47. def test_newer_pairwise_empty(self):
  48. assert newer_pairwise([], []) == ([], [])
  49. def test_newer_pairwise_fresh(self):
  50. one, two, three, four = self._setup_1234()
  51. assert newer_pairwise([one, three], [two, four]) == ([], [])
  52. def test_newer_group(self):
  53. tmpdir = self.mkdtemp()
  54. sources = os.path.join(tmpdir, 'sources')
  55. os.mkdir(sources)
  56. one = os.path.join(sources, 'one')
  57. two = os.path.join(sources, 'two')
  58. three = os.path.join(sources, 'three')
  59. old_file = os.path.abspath(__file__)
  60. # return true if 'old_file' is out-of-date with respect to any file
  61. # listed in 'sources'.
  62. self.write_file(one)
  63. self.write_file(two)
  64. self.write_file(three)
  65. assert newer_group([one, two, three], old_file)
  66. assert not newer_group([one, two, old_file], three)
  67. # missing handling
  68. os.remove(one)
  69. with pytest.raises(OSError):
  70. newer_group([one, two, old_file], three)
  71. assert not newer_group([one, two, old_file], three, missing='ignore')
  72. assert newer_group([one, two, old_file], three, missing='newer')
  73. @pytest.fixture
  74. def groups_target(tmp_path):
  75. """
  76. Set up some older sources, a target, and newer sources.
  77. Returns a simple namespace with these values.
  78. """
  79. filenames = ['older.c', 'older.h', 'target.o', 'newer.c', 'newer.h']
  80. paths = [tmp_path / name for name in filenames]
  81. for mtime, path in enumerate(paths):
  82. path.write_text('', encoding='utf-8')
  83. # make sure modification times are sequential
  84. os.utime(path, (mtime, mtime))
  85. return types.SimpleNamespace(older=paths[:2], target=paths[2], newer=paths[3:])
  86. def test_newer_pairwise_group(groups_target):
  87. older = newer_pairwise_group([groups_target.older], [groups_target.target])
  88. newer = newer_pairwise_group([groups_target.newer], [groups_target.target])
  89. assert older == ([], [])
  90. assert newer == ([groups_target.newer], [groups_target.target])
  91. def test_newer_group_no_sources_no_target(tmp_path):
  92. """
  93. Consider no sources and no target "newer".
  94. """
  95. assert newer_group([], str(tmp_path / 'does-not-exist'))