File size: 2,471 Bytes
f95cbd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
from pathlib import Path
from types import SimpleNamespace
import pytest
from celestis_rl.release import build_manifest,verify_manifest,publish_release


def tree(tmp):
    (tmp/"README.md").write_text("reference release")
    (tmp/"src").mkdir();(tmp/"src/a.py").write_text("print('example')")
    return build_manifest(tmp)


def test_manifest_and_dry_run_no_network(tmp_path):
    m=tree(tmp_path);assert len(verify_manifest(tmp_path)["files"])==2
    r=publish_release(tmp_path,"example/research")
    assert not r["uploaded"] and r["operation"]=="dry-run"


def test_manifest_detects_changes_ignores_extras_and_rejects_unsafe_paths(tmp_path):
    tree(tmp_path);(tmp_path/"README.md").write_text("changed")
    with pytest.raises(ValueError,match="mismatch"):verify_manifest(tmp_path)
    # Local extras are ignored because publication stages only manifest files.
    tree_extra=tmp_path/"extra_case";tree_extra.mkdir();tree(tree_extra)
    (tree_extra/"local-note.jpg").write_bytes(b"not part of release")
    assert verify_manifest(tree_extra)["version"]=="2.0.0"
    dry=publish_release(tree_extra,"example/research")
    assert dry["ignored_local_files"]==["local-note.jpg"]
    tree2=tmp_path/"other";tree2.mkdir();tree(tree2)
    path=tree2/"release/MANIFEST.json";m=json.loads(path.read_text());m["files"]["../escape"]={}
    path.write_text(json.dumps(m))
    with pytest.raises(ValueError,match="unsafe"):verify_manifest(tree2)


def test_token_and_secret_scan(tmp_path):
    (tmp_path/"leak.txt").write_text("hf_"+"a"*32)
    with pytest.raises(ValueError,match="token"):build_manifest(tmp_path)


def test_mock_publication_stages_only_manifest_files(tmp_path):
    tree(tmp_path);calls=[]
    class Fake:
        def create_repo(self,**kw):calls.append(kw)
        def upload_folder(self,**kw):
            verify_manifest(kw["folder_path"]);calls.append(kw)
            return SimpleNamespace(commit_url="mock-commit")
    r=publish_release(tmp_path,"example/research",publish=True,public=True,api=Fake())
    assert r["uploaded"] and r["commit_url"]=="mock-commit"
    assert calls[0]["private"] is False and calls[0]["exist_ok"] is False
    assert calls[1]["repo_type"]=="dataset"


def test_publish_requires_auth_and_repo_id(tmp_path):
    tree(tmp_path)
    with pytest.raises(ValueError,match="HF_TOKEN"):publish_release(tmp_path,"x/y",publish=True)
    with pytest.raises(ValueError,match="namespace"):publish_release(tmp_path,"../x")