Repository navigation
test(cli): add immutability and staging tests for intrinsic upload (#1812) - #1814
txltedxgod wants to merge 1 commit into
Conversation
rafvasq
left a comment
There was a problem hiding this comment.
Thanks @txltedxgod, this looks good and seems to address the issue as described. I'll leave approval to @planetf1, but I think a third test that covers a weight_path without a README.md is worth adding as well. Also a nit below.
|
Thanks for the review @rafvasq! Removed the unused \pytest\ import and added \ est_upload_intrinsic_without_readme\ covering \weight_path\ without a \README.md. |
planetf1
left a comment
There was a problem hiding this comment.
The fix does what #1812 asks. Nothing writes to weight_path any more, so the local directory comes through intact even when the upload fails part-way (I checked by making upload_folder raise). A few smaller points inline. Only the README one changes behaviour.
One thing outside the diff, so it can't go inline: temp_dir is only set at line 88. If one of the asserts, the token check or create_repo fails first, the finally then raises UnboundLocalError and hides the real error. A missing weight_path reports cannot access local variable 'temp_dir' instead of the AssertionError the docstring documents. Moving tempfile.mkdtemp() above the try fixes it. This predates the PR, so happy for it to be a separate one.
| return _capture_upload_folder | ||
|
|
||
|
|
||
| def test_upload_intrinsic_preserves_local_adapter_directory(tmp_path, monkeypatch): |
There was a problem hiding this comment.
The immutability guarantee matters most when the upload fails, and all three tests take the success path. Could you add one where upload_folder raises, then assert weight_path is unchanged? Comparing a hash of every file before and after (needs import hashlib) would also let the existing tests check the whole directory. At the moment they check README.md and io.yaml:
def _snapshot(path):
return {
p.relative_to(path): hashlib.sha256(p.read_bytes()).hexdigest()
for p in path.rglob("*")
if p.is_file()
}One more case that's cheap to cover: passing weight_path/io.yaml as io_yaml used to fail with SameFileError and works now.
There was a problem hiding this comment.
Following up here too. The snapshot part still applies on the new head. The tests compare the adapter directory's file names, plus the README.md content (and io.yaml in test 2), so a change to any other file goes unnoticed. I tried appending to the source adapter_config.json after the copy, and truncating the source adapter_model.safetensors: every test in this file still passes. Taking before = _snapshot(weight_dir) once the files are written, and comparing _snapshot(weight_dir) == before at the end of each test, catches both (_snapshot is the helper above and needs import hashlib). Edge case, cheap to cover, and it's what the test names promise.
I'm dropping the other two asks. The uploader no longer writes to the adapter directory at all, so a failing-upload test adds nothing, and the SameFileError case went away with the same change.
|
|
||
| def _load_uploader_with_mock_hub(monkeypatch): | ||
| """Import `cli.alora.intrinsic_uploader` without requiring Hub 0.x/1.x parity.""" | ||
| sys.modules.pop("cli.alora.intrinsic_uploader", None) |
There was a problem hiding this comment.
Nothing undoes this pop after the test. The locked huggingface-hub has no HfFolder, so the reimported uploader binds to the fake hub module and stays in sys.modules for the rest of the session. Nothing else imports it today, so it's harmless for now. Swapping in monkeypatch.delitem only helps when the module was already imported, so a fixture that yields the uploader and then pops it on teardown is the safer fix.
There was a problem hiding this comment.
Following up on this one. "Nothing else imports it today" was true when I wrote it, but test_alora_upload.py (added by #1798) now imports the uploader too. If I put the HfFolder import back into the uploader and run this file first (pytest test/cli/test_intrinsic_uploader.py test/cli/test_alora_upload.py), all 11 tests pass, so test_upload_modules_import_without_hf_folder no longer catches that regression. In the default order it still fails as it should, so this only matters when someone names this file first. Low likelihood, and the fix deletes code.
Since #1798 the uploader only uses get_token, and the locked Hub has no HfFolder, so the helper always swaps in the fake module. A plain import against the real Hub works. Replacing lines 6-47 with this keeps the three tests passing and catches the HfFolder regression in either file order:
import os
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
pytest.importorskip("huggingface_hub")
def _load_uploader_with_mock_hub(monkeypatch):
from cli.alora import intrinsic_uploader as uploader
monkeypatch.setattr(uploader, "get_token", lambda: "test-token")
monkeypatch.setattr(
uploader,
"create_repo",
Mock(return_value=SimpleNamespace(url="https://example.invalid")),
)
return uploaderThat replaces my earlier teardown-fixture suggestion.
|
@txltedxgod Your PR came first and is correct; rafvasq and I both reviewed it . #1857 was opened a week later for the same issue, and I reviewed that one without noticing the overlap. Sorry about that - we should have noticed when assigning You did submit some stronger tests the ones in #1857: they compare file contents rather than names, cover a local I'm going tomerge #1857 for the code change as it's based on the current code in main so makes sense to go first, then could you rebase this onto main which should leave Let me know if you need any help this this |
d46fba1 to
719e4d3
Compare
|
@planetf1 Rebased onto main and dropped the code hunk as #1857 is merged. This PR is now strictly tests-only, adding the comprehensive test suite in \ est/cli/test_intrinsic_uploader.py\ (verifying local directory immutability, staging-only \io.yaml\ override, and upload without \README.md). All checks pass cleanly. Thanks! |
719e4d3 to
c94dbbe
Compare
planetf1
left a comment
There was a problem hiding this comment.
Overall this looks good, apart from the two follow-ups in the existing threads and one nit inline.
Could you also update the description for the tests-only version? It still says Fixes #1812 and describes the code change, which went in with #1857 (that PR closed #1812), so Related to #1812 fits better. The line saying the regressions failed on main before the fix, and the note about #1798 still being open, are out of date too.
One more thing, separate from this PR. The uploader builds path_in_repo with os.path.join (cli/alora/intrinsic_uploader.py line 118), and huggingface_hub doesn't normalise that path, so on Windows it would probably contain backslashes. Our main test workflow runs on Linux and none of the existing developers use Windows, so we wouldn't have seen it, and Windows fixes will need to come from people who use it. If that's you, would you open a separate issue listing the Windows-specific problems you run into, starting with paths, and be ready to contribute the fixes? There's no need to fix them in this PR.
|
|
||
| def _load_uploader_with_mock_hub(monkeypatch): | ||
| """Import `cli.alora.intrinsic_uploader` without requiring Hub 0.x/1.x parity.""" | ||
| sys.modules.pop("cli.alora.intrinsic_uploader", None) |
There was a problem hiding this comment.
Following up on this one. "Nothing else imports it today" was true when I wrote it, but test_alora_upload.py (added by #1798) now imports the uploader too. If I put the HfFolder import back into the uploader and run this file first (pytest test/cli/test_intrinsic_uploader.py test/cli/test_alora_upload.py), all 11 tests pass, so test_upload_modules_import_without_hf_folder no longer catches that regression. In the default order it still fails as it should, so this only matters when someone names this file first. Low likelihood, and the fix deletes code.
Since #1798 the uploader only uses get_token, and the locked Hub has no HfFolder, so the helper always swaps in the fake module. A plain import against the real Hub works. Replacing lines 6-47 with this keeps the three tests passing and catches the HfFolder regression in either file order:
import os
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
pytest.importorskip("huggingface_hub")
def _load_uploader_with_mock_hub(monkeypatch):
from cli.alora import intrinsic_uploader as uploader
monkeypatch.setattr(uploader, "get_token", lambda: "test-token")
monkeypatch.setattr(
uploader,
"create_repo",
Mock(return_value=SimpleNamespace(url="https://example.invalid")),
)
return uploaderThat replaces my earlier teardown-fixture suggestion.
| return _capture_upload_folder | ||
|
|
||
|
|
||
| def test_upload_intrinsic_preserves_local_adapter_directory(tmp_path, monkeypatch): |
There was a problem hiding this comment.
Following up here too. The snapshot part still applies on the new head. The tests compare the adapter directory's file names, plus the README.md content (and io.yaml in test 2), so a change to any other file goes unnoticed. I tried appending to the source adapter_config.json after the copy, and truncating the source adapter_model.safetensors: every test in this file still passes. Taking before = _snapshot(weight_dir) once the files are written, and comparing _snapshot(weight_dir) == before at the end of each test, catches both (_snapshot is the helper above and needs import hashlib). Edge case, cheap to cover, and it's what the test names promise.
I'm dropping the other two asks. The uploader no longer writes to the adapter directory at all, so a failing-upload test adds nothing, and the SameFileError case went away with the same change.
| assert "README.md" not in staged_files | ||
| assert staged_files["io.yaml"] == "fresh: true\n" | ||
| upload_file.assert_called_once() | ||
| assert upload_file.call_args.kwargs["path_in_repo"] == "README.md" |
There was a problem hiding this comment.
Nit: only path_in_repo is checked here, so uploading the local README.md instead of INTRINSIC_README.md still passes (I tried it). Checking the source path as well catches that:
| assert upload_file.call_args.kwargs["path_in_repo"] == "README.md" | |
| assert upload_file.call_args.kwargs["path_in_repo"] == "README.md" | |
| assert upload_file.call_args.kwargs["path_or_fileobj"] == str( | |
| weight_dir / "INTRINSIC_README.md" | |
| ) |
…enerative-computing#1812) Signed-off-by: txltedXgod <[email protected]>
c94dbbe to
91ee6ac
Compare
|
@planetf1 Addressed all review feedback:
Also opened #1870 for the Windows |
Pull Request
Thanks for sending a pull request! Please make sure you read the contributing guidelines.
Issue
Related to #1812
Description
Follow-up tests-only PR adding unit tests in
test/cli/test_intrinsic_uploader.pyfor the intrinsic adapter upload immutability fix merged in #1857 (which closed #1812).The tests verify:
test_upload_intrinsic_preserves_local_adapter_directory: full directory snapshot immutability ofweight_path(all file names and SHA-256 contents unchanged),README.mdexcluded from staged files, and expectedpath_in_repo.test_upload_intrinsic_overrides_io_yaml_in_staging_only:io.yamloverride occurs only in staging, localweight_pathsnapshot remains untouched, andINTRINSIC_README.mdis uploaded as rootREADME.mdviaupload_file(path_or_fileobjandpath_in_repo).test_upload_intrinsic_without_readme: upload succeeds withoutREADME.mdorINTRINSIC_README.mdwhile keepingweight_pathunchanged.Testing
Local checks (
Python 3.14.7, mocked Hugging Face Hub network calls):python -m pytest test/cli/test_intrinsic_uploader.py test/cli/test_alora_upload.py --no-cov -vpython -m ruff format --check test/cli/test_intrinsic_uploader.pypython -m ruff check test/cli/test_intrinsic_uploader.pypython .github/scripts/check_license_headers.py --check test/cli/test_intrinsic_uploader.pyAttribution
Assisted by Antigravity. All tests were verified locally.
Adding a new component, requirement, sampling strategy, or tool?
If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.