Skip to content

Commit 6c486a5

Browse files
picnixzAA-Turner
andauthored
Fix detecting file changes for the overwritten file warning (#12627)
Co-authored-by: Adam Turner <[email protected]>
1 parent 2bd973e commit 6c486a5

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bugs fixed
55
----------
66

77
* #12096: Warn when files are overwritten in the build directory.
8-
Patch by Adam Turner.
8+
Patch by Adam Turner and Bénédikt Tran.
99
* #12620: Ensure that old-style object description options are respected.
1010
Patch by Adam Turner.
1111
* #12601, #12625: Support callable objects in :py:class:`~typing.Annotated` type

sphinx/util/fileutil.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def copy_asset_file(source: str | os.PathLike[str], destination: str | os.PathLi
7575
msg = ('Copying the rendered template %s to %s will overwrite data, '
7676
'as a file already exists at the destination path '
7777
'and the content does not match.')
78+
# See https://github.com/sphinx-doc/sphinx/pull/12627#issuecomment-2241144330
79+
# for the rationale for logger.info().
7880
logger.info(msg, os.fsdecode(source), os.fsdecode(destination),
7981
type='misc', subtype='copy_overwrite')
8082

sphinx/util/osutil.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ def copyfile(
106106
msg = f'{os.fsdecode(source)} does not exist'
107107
raise FileNotFoundError(msg)
108108

109-
if not (dest_exists := path.exists(dest)) or not filecmp.cmp(source, dest):
109+
if (
110+
not (dest_exists := path.exists(dest)) or
111+
# comparison must be done using shallow=False since
112+
# two different files might have the same size
113+
not filecmp.cmp(source, dest, shallow=False)
114+
):
110115
if __overwrite_warning__ and dest_exists:
111116
# sphinx.util.logging imports sphinx.util.osutil,
112117
# so use a local import to avoid circular imports

tests/roots/test-util-copyasset_overwrite/myext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
def _copy_asset_overwrite_hook(app):
77
css = app.outdir / '_static' / 'custom-styles.css'
88
# html_static_path is copied by default
9-
assert css.read_text() == '/* html_static_path */\n'
9+
assert css.read_text() == '/* html_static_path */\n', 'invalid default text'
1010
# warning generated by here
1111
copy_asset(
1212
Path(__file__).parent.joinpath('myext_static', 'custom-styles.css'),
1313
app.outdir / '_static',
1414
)
1515
# This demonstrates the overwriting
16-
assert css.read_text() == '/* extension styles */\n'
16+
assert css.read_text() == '/* extension styles */\n', 'overwriting failed'
1717
return []
1818

1919

tests/test_util/test_util_fileutil.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,16 @@ def excluded(path):
106106
assert not (destdir / '_templates' / 'sidebar.html').exists()
107107

108108

109-
@pytest.mark.xfail(reason='Filesystem chicanery(?)')
110109
@pytest.mark.sphinx('html', testroot='util-copyasset_overwrite')
111110
def test_copy_asset_overwrite(app):
112111
app.build()
113-
warnings = strip_colors(app.warning.getvalue())
114112
src = app.srcdir / 'myext_static' / 'custom-styles.css'
115113
dst = app.outdir / '_static' / 'custom-styles.css'
116-
assert warnings == (
117-
f'WARNING: Copying the source path {src} to {dst} will overwrite data, '
114+
assert (
115+
f'Copying the source path {src} to {dst} will overwrite data, '
118116
'as a file already exists at the destination path '
119117
'and the content does not match.\n'
120-
)
118+
) in strip_colors(app.status.getvalue())
121119

122120

123121
def test_template_basename():

0 commit comments

Comments
 (0)