|
19 | 19 | from tests.conftest import get_config_data, inside_dir
|
20 | 20 |
|
21 | 21 |
|
| 22 | +class TestConfiguredFile: |
| 23 | + """Tests of the ConfiguredFile class.""" |
| 24 | + |
| 25 | + class TestGetFileContents: |
| 26 | + """Tests of the get_file_content method.""" |
| 27 | + |
| 28 | + def test_returns_contents_of_existing_file(self, tmp_path: Path) -> None: |
| 29 | + """The get_file_content method returns the contents of an existing file.""" |
| 30 | + # Arrange |
| 31 | + filepath = tmp_path / "file.md" |
| 32 | + expected_data = "My current version is 1.2.3" |
| 33 | + filepath.write_text(expected_data, encoding="utf-8") |
| 34 | + overrides = { |
| 35 | + "current_version": "1.2.3", |
| 36 | + "files": [{"filename": str(filepath)}], |
| 37 | + } |
| 38 | + conf, version_config, current_version = get_config_data(overrides) |
| 39 | + assert len(conf.files_to_modify) == 1 |
| 40 | + assert conf.files_to_modify[0].filename == str(filepath) |
| 41 | + |
| 42 | + # Act |
| 43 | + configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config) |
| 44 | + |
| 45 | + # Assert |
| 46 | + assert configured_file.get_file_contents() == expected_data |
| 47 | + |
| 48 | + def test_raises_exception_if_file_not_found(self, tmp_path: Path) -> None: |
| 49 | + """The get_file_content method raises an exception if the file does not exist.""" |
| 50 | + filepath = tmp_path / "file.md" |
| 51 | + overrides = { |
| 52 | + "current_version": "1.2.3", |
| 53 | + "files": [{"filename": str(filepath)}], |
| 54 | + } |
| 55 | + conf, version_config, current_version = get_config_data(overrides) |
| 56 | + assert len(conf.files_to_modify) == 1 |
| 57 | + assert conf.files_to_modify[0].filename == str(filepath) |
| 58 | + configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config) |
| 59 | + |
| 60 | + # Act |
| 61 | + with pytest.raises(FileNotFoundError): |
| 62 | + configured_file.get_file_contents() |
| 63 | + |
| 64 | + class TestMakeFileChange: |
| 65 | + """Tests of the make_file_change function.""" |
| 66 | + |
| 67 | + def test_raises_exception_if_file_not_found(self, tmp_path: Path) -> None: |
| 68 | + """The make_file_change method raises an exception if the file does not exist.""" |
| 69 | + # Assemble |
| 70 | + filepath = tmp_path / "file.md" |
| 71 | + overrides = { |
| 72 | + "current_version": "1.2.3", |
| 73 | + "files": [{"filename": str(filepath)}], |
| 74 | + } |
| 75 | + conf, version_config, current_version = get_config_data(overrides) |
| 76 | + configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config) |
| 77 | + new_version = current_version.bump("patch") |
| 78 | + ctx = get_context(conf) |
| 79 | + |
| 80 | + # Act |
| 81 | + with pytest.raises(FileNotFoundError): |
| 82 | + configured_file.make_file_change(current_version, new_version, ctx, dry_run=True) |
| 83 | + |
| 84 | + def test_logs_missing_file_when_ignore_missing_file_set(self, tmp_path: Path, caplog) -> None: |
| 85 | + """The make_file_change method logs missing file when ignore_missing_file set to True.""" |
| 86 | + # Assemble |
| 87 | + filepath = tmp_path / "file.md" |
| 88 | + overrides = { |
| 89 | + "current_version": "1.2.3", |
| 90 | + "files": [{"filename": str(filepath), "ignore_missing_file": True}], |
| 91 | + } |
| 92 | + conf, version_config, current_version = get_config_data(overrides) |
| 93 | + configured_file = files.ConfiguredFile(conf.files_to_modify[0], version_config) |
| 94 | + new_version = current_version.bump("patch") |
| 95 | + ctx = get_context(conf) |
| 96 | + |
| 97 | + # Act |
| 98 | + configured_file.make_file_change(current_version, new_version, ctx, dry_run=True) |
| 99 | + |
| 100 | + # Assert |
| 101 | + logs = caplog.messages |
| 102 | + assert logs[0] == " Reading configuration" |
| 103 | + assert logs[1] == " Configuration file not found: missing." |
| 104 | + assert logs[2].endswith("file.md: replace `{current_version}` with `{new_version}`") |
| 105 | + assert logs[3] == " File not found, but ignoring" |
| 106 | + |
| 107 | + def test_dedents_properly_when_file_does_not_contain_pattern(self, fixtures_path: Path, caplog) -> None: |
| 108 | + """The make_file_change method dedents the logging context when it does not contain a pattern.""" |
| 109 | + # Assemble |
| 110 | + globs = fixtures_path / "glob" / "**/*.txt" |
| 111 | + overrides = { |
| 112 | + "current_version": "1.2.3", |
| 113 | + "files": [{"glob": str(globs), "ignore_missing_file": True, "ignore_missing_version": True}], |
| 114 | + } |
| 115 | + conf, version_config, current_version = get_config_data(overrides) |
| 116 | + configured_file1 = files.ConfiguredFile(conf.files_to_modify[0], version_config) |
| 117 | + configured_file2 = files.ConfiguredFile(conf.files_to_modify[1], version_config) |
| 118 | + configured_file3 = files.ConfiguredFile(conf.files_to_modify[2], version_config) |
| 119 | + new_version = current_version.bump("patch") |
| 120 | + ctx = get_context(conf) |
| 121 | + |
| 122 | + # Act |
| 123 | + configured_file1.make_file_change(current_version, new_version, ctx, dry_run=True) |
| 124 | + configured_file2.make_file_change(current_version, new_version, ctx, dry_run=True) |
| 125 | + configured_file3.make_file_change(current_version, new_version, ctx, dry_run=True) |
| 126 | + |
| 127 | + # Assert |
| 128 | + logs = caplog.messages |
| 129 | + assert logs[0] == " Reading configuration" |
| 130 | + assert logs[1] == " Configuration file not found: missing." |
| 131 | + assert logs[2] == ( |
| 132 | + f" \n File {configured_file1.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`" |
| 133 | + ) |
| 134 | + assert logs[3] == ( |
| 135 | + f" \n File {configured_file2.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`" |
| 136 | + ) |
| 137 | + assert logs[4] == ( |
| 138 | + f" \n File {configured_file3.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`" |
| 139 | + ) |
| 140 | + |
| 141 | + |
22 | 142 | def test_single_file_processed_twice(tmp_path: Path):
|
23 | 143 | """
|
24 | 144 | Verify that a single file "file2" can be processed twice.
|
|
0 commit comments