Skip to content

test(cli): add immutability and staging tests for intrinsic upload (#1812) - #1814

Open
txltedxgod wants to merge 1 commit into
generative-computing:mainfrom
txltedxgod:fix/1812-intrinsic-upload-immutable
Open

txltedxgod wants to merge 1 commit into
generative-computing:mainfrom
txltedxgod:fix/1812-intrinsic-upload-immutable

Conversation

@txltedxgod

@txltedxgod txltedxgod commented Nov 12, 2026 •

Copy link
Copy Markdown

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.py for 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 of weight_path (all file names and SHA-256 contents unchanged), README.md excluded from staged files, and expected path_in_repo.
  • test_upload_intrinsic_overrides_io_yaml_in_staging_only: io.yaml override occurs only in staging, local weight_path snapshot remains untouched, and INTRINSIC_README.md is uploaded as root README.md via upload_file (path_or_fileobj and path_in_repo).
  • test_upload_intrinsic_without_readme: upload succeeds without README.md or INTRINSIC_README.md while keeping weight_path unchanged.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

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 -v
  • python -m ruff format --check test/cli/test_intrinsic_uploader.py
  • python -m ruff check test/cli/test_intrinsic_uploader.py
  • python .github/scripts/check_license_headers.py --check test/cli/test_intrinsic_uploader.py

Attribution

  • AI coding assistants used

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.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

@txltedxgod
txltedxgod requested a review from a team as a code owner November 12, 2026 09:31
@github-actions github-actions Bot added the bug Something isn't working label Nov 12, 2026

@rafvasq rafvasq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/cli/test_intrinsic_uploader.py Resolved
@rafvasq
rafvasq requested a review from planetf1 November 12, 2026 17:47
@txltedxgod

Copy link
Copy Markdown
Author

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 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cli/alora/intrinsic_uploader.py Resolved
return _capture_upload_folder


def test_upload_intrinsic_preserves_local_adapter_directory(tmp_path, monkeypatch):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/cli/test_intrinsic_uploader.py Outdated

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 uploader

That replaces my earlier teardown-fixture suggestion.

@planetf1

planetf1 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

@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 io.yaml being overridden in staging only, and cover the no-README.md case rafvasq asked for. I checked all three against current main: they fail before the fix and pass after it.

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 test/cli/test_intrinsic_uploader.py, to become a tests-only PR under your name.

Let me know if you need any help this this

@txltedxgod
txltedxgod force-pushed the fix/1812-intrinsic-upload-immutable branch from d46fba1 to 719e4d3 Compare November 23, 2026 22:35
@txltedxgod txltedxgod changed the title fix(cli): keep local adapter directory immutable during intrinsic upload (#1812) test(cli): add immutability and staging tests for intrinsic upload (#1812) Nov 23, 2026
@txltedxgod

Copy link
Copy Markdown
Author

@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!

@txltedxgod
txltedxgod force-pushed the fix/1812-intrinsic-upload-immutable branch from 719e4d3 to c94dbbe Compare November 23, 2026 22:37

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/cli/test_intrinsic_uploader.py Outdated

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 uploader

That replaces my earlier teardown-fixture suggestion.

return _capture_upload_folder


def test_upload_intrinsic_preserves_local_adapter_directory(tmp_path, monkeypatch):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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"
)

@txltedxgod

Copy link
Copy Markdown
Author

@planetf1 Addressed all review feedback:

  • Replaced the custom sys.modules/fake-module loader in _load_uploader_with_mock_hub with plain cli.alora.intrinsic_uploader import + pytest.importorskip("huggingface_hub") so test_upload_modules_import_without_hf_folder stays order-independent.
  • Added _snapshot(weight_dir) (SHA-256 content hashes of all files) before and after upload_intrinsic() in all three tests to assert complete directory immutability.
  • Added assert upload_file.call_args.kwargs["path_or_fileobj"] == str(weight_dir / "INTRINSIC_README.md") in test_upload_intrinsic_overrides_io_yaml_in_staging_only.
  • Updated the PR description to reference Related to #1812 and reflect the tests-only scope.

Also opened #1870 for the Windows os.path.join backslash issue in path_in_repo (which reproduces locally on Windows in test_alora_upload.py::test_upload_intrinsic_token_lookup).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: m alora upload --intrinsic modifies the local adapter directory

3 participants