|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Tests for the non-destructive output handling of ``convert_v3_to_v2.py``.""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from pathlib import Path |
| 21 | +import sys |
| 22 | +import types |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | + |
| 27 | +pytest.importorskip("pyarrow") |
| 28 | + |
| 29 | + |
| 30 | +def _install_lerobot_stubs() -> list[str]: |
| 31 | + """Install just enough fake ``lerobot`` modules to import the conversion script. |
| 32 | +
|
| 33 | + The conversion script lives in its own subproject with a real ``lerobot`` |
| 34 | + dependency; the destination-resolution logic under test does not need it. |
| 35 | + """ |
| 36 | + |
| 37 | + stub_attrs: dict[str, dict[str, object]] = { |
| 38 | + "lerobot": {}, |
| 39 | + "lerobot.datasets": {}, |
| 40 | + "lerobot.datasets.utils": { |
| 41 | + "DEFAULT_CHUNK_SIZE": 1000, |
| 42 | + "DEFAULT_DATA_PATH": "data/chunk-{chunk_index:03d}/file-{file_index:03d}.parquet", |
| 43 | + "DEFAULT_VIDEO_PATH": ( |
| 44 | + "videos/{video_key}/chunk-{chunk_index:03d}/file-{file_index:03d}.mp4" |
| 45 | + ), |
| 46 | + "EPISODES_DIR": "meta/episodes", |
| 47 | + "LEGACY_EPISODES_PATH": "meta/episodes.jsonl", |
| 48 | + "LEGACY_EPISODES_STATS_PATH": "meta/episodes_stats.jsonl", |
| 49 | + "LEGACY_TASKS_PATH": "meta/tasks.jsonl", |
| 50 | + "load_info": lambda *args, **kwargs: None, |
| 51 | + "load_tasks": lambda *args, **kwargs: None, |
| 52 | + "serialize_dict": lambda *args, **kwargs: None, |
| 53 | + "unflatten_dict": lambda *args, **kwargs: None, |
| 54 | + "write_info": lambda *args, **kwargs: None, |
| 55 | + }, |
| 56 | + "lerobot.utils": {}, |
| 57 | + "lerobot.utils.constants": {"HF_LEROBOT_HOME": Path("unused")}, |
| 58 | + "lerobot.utils.utils": {"init_logging": lambda *args, **kwargs: None}, |
| 59 | + } |
| 60 | + |
| 61 | + installed: list[str] = [] |
| 62 | + for name, attrs in stub_attrs.items(): |
| 63 | + if name in sys.modules: |
| 64 | + continue |
| 65 | + module = types.ModuleType(name) |
| 66 | + for key, value in attrs.items(): |
| 67 | + setattr(module, key, value) |
| 68 | + sys.modules[name] = module |
| 69 | + installed.append(name) |
| 70 | + return installed |
| 71 | + |
| 72 | + |
| 73 | +_stub_names = _install_lerobot_stubs() |
| 74 | +try: |
| 75 | + from scripts.lerobot_conversion.convert_v3_to_v2 import resolve_output_roots |
| 76 | +finally: |
| 77 | + for _name in _stub_names: |
| 78 | + sys.modules.pop(_name, None) |
| 79 | + |
| 80 | + |
| 81 | +def test_default_output_is_sibling_with_v21_suffix(tmp_path: Path) -> None: |
| 82 | + root = tmp_path / "dataset" |
| 83 | + root.mkdir() |
| 84 | + |
| 85 | + new_root, backup_root = resolve_output_roots(root, None, in_place=False) |
| 86 | + |
| 87 | + assert new_root == tmp_path / "dataset_v2.1" |
| 88 | + assert backup_root is None |
| 89 | + |
| 90 | + |
| 91 | +def test_refuses_output_equal_to_source(tmp_path: Path) -> None: |
| 92 | + root = tmp_path / "dataset" |
| 93 | + root.mkdir() |
| 94 | + |
| 95 | + with pytest.raises(ValueError, match="overwrite the source"): |
| 96 | + resolve_output_roots(root, root, in_place=False) |
| 97 | + |
| 98 | + |
| 99 | +def test_refuses_output_inside_source(tmp_path: Path) -> None: |
| 100 | + root = tmp_path / "dataset" |
| 101 | + root.mkdir() |
| 102 | + |
| 103 | + with pytest.raises(ValueError, match="overwrite the source"): |
| 104 | + resolve_output_roots(root, root / "v2", in_place=False) |
| 105 | + |
| 106 | + |
| 107 | +def test_refuses_existing_output_directory(tmp_path: Path) -> None: |
| 108 | + root = tmp_path / "dataset" |
| 109 | + root.mkdir() |
| 110 | + (tmp_path / "dataset_v2.1").mkdir() |
| 111 | + |
| 112 | + with pytest.raises(FileExistsError, match="Output directory already exists"): |
| 113 | + resolve_output_roots(root, None, in_place=False) |
| 114 | + |
| 115 | + |
| 116 | +def test_in_place_returns_backup_root(tmp_path: Path) -> None: |
| 117 | + root = tmp_path / "dataset" |
| 118 | + root.mkdir() |
| 119 | + |
| 120 | + new_root, backup_root = resolve_output_roots(root, None, in_place=True) |
| 121 | + |
| 122 | + assert new_root == tmp_path / "dataset_v2.1" |
| 123 | + assert backup_root == tmp_path / "dataset_backup_v3.0" |
| 124 | + |
| 125 | + |
| 126 | +def test_in_place_refuses_existing_backup(tmp_path: Path) -> None: |
| 127 | + root = tmp_path / "dataset" |
| 128 | + root.mkdir() |
| 129 | + (tmp_path / "dataset_backup_v3.0").mkdir() |
| 130 | + |
| 131 | + with pytest.raises(FileExistsError, match="Backup directory already exists"): |
| 132 | + resolve_output_roots(root, None, in_place=True) |
| 133 | + |
| 134 | + |
| 135 | +def test_output_and_in_place_are_mutually_exclusive(tmp_path: Path) -> None: |
| 136 | + root = tmp_path / "dataset" |
| 137 | + |
| 138 | + with pytest.raises(ValueError, match="mutually exclusive"): |
| 139 | + resolve_output_roots(root, tmp_path / "out", in_place=True) |
0 commit comments