Skip to content

Commit

Permalink
Only try to move v1 schema files that exist. (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasr committed Nov 6, 2022
1 parent 14205d3 commit 0a829e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
6 changes: 3 additions & 3 deletions signac/contrib/migration/v1_to_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ def _migrate_v1_to_v2(root_directory):
),
}
for src, dst in files_to_move.items():
os.replace(
os.sep.join((root_directory, src)), os.sep.join((root_directory, dst))
)
src = os.sep.join((root_directory, src))
if os.path.isfile(src):
os.replace(src, os.sep.join((root_directory, dst)))
39 changes: 23 additions & 16 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,7 @@ def test_no_migration(self):
assert len(migrations) == 0


def _initialize_v1_project(dirname, with_workspace=True):
def _initialize_v1_project(dirname, with_workspace=True, with_other_files=True):
# Create v1 config file.
cfg_fn = os.path.join(dirname, "signac.rc")
workspace_dir = "workspace_dir"
Expand All @@ -2345,30 +2345,34 @@ def _initialize_v1_project(dirname, with_workspace=True):
if with_workspace:
os.makedirs(os.path.join(dirname, "workspace"))

# Create a shell history file.
history_fn = os.path.join(dirname, ".signac_shell_history")
with open(history_fn, "w") as f:
f.write("print(project)")
if with_other_files:
# Create a shell history file.
history_fn = os.path.join(dirname, ".signac_shell_history")
with open(history_fn, "w") as f:
f.write("print(project)")

# Create a statepoint cache. Note that this cache does not
# correspond to actual statepoints since we don't currently have
# any in this project, but that's fine for migration testing.
sp_cache = os.path.join(dirname, ".signac_sp_cache.json.gz")
sp = {"a": 1}
with gzip.open(sp_cache, "wb") as f:
f.write(json.dumps({calc_id(sp): sp}).encode())
# Create a statepoint cache. Note that this cache does not
# correspond to actual statepoints since we don't currently have
# any in this project, but that's fine for migration testing.
sp_cache = os.path.join(dirname, ".signac_sp_cache.json.gz")
sp = {"a": 1}
with gzip.open(sp_cache, "wb") as f:
f.write(json.dumps({calc_id(sp): sp}).encode())

return cfg_fn


class TestSchemaMigration:
@pytest.mark.parametrize("implicit_version", [True, False])
@pytest.mark.parametrize("workspace_exists", [True, False])
def test_project_schema_version_migration(self, implicit_version, workspace_exists):
@pytest.mark.parametrize("with_other_files", [True, False])
def test_project_schema_version_migration(
self, implicit_version, workspace_exists, with_other_files
):
from signac.contrib.migration import apply_migrations

with TemporaryDirectory() as dirname:
cfg_fn = _initialize_v1_project(dirname, workspace_exists)
cfg_fn = _initialize_v1_project(dirname, workspace_exists, with_other_files)

# If no schema version is present in the config it is equivalent to
# version 0, so we test both explicit and implicit versions.
Expand Down Expand Up @@ -2397,8 +2401,11 @@ def test_project_schema_version_migration(self, implicit_version, workspace_exis
assert "0 to 1" in err.getvalue()
assert "1 to 2" in err.getvalue()
assert os.path.isfile(project.fn(PROJECT_CONFIG_FN))
assert os.path.isfile(project.fn(os.sep.join((".signac", "shell_history"))))
assert os.path.isfile(project.fn(Project.FN_CACHE))
if with_other_files:
assert os.path.isfile(
project.fn(os.sep.join((".signac", "shell_history")))
)
assert os.path.isfile(project.fn(Project.FN_CACHE))

def test_project_init_old_schema(self):
with TemporaryDirectory() as dirname:
Expand Down

0 comments on commit 0a829e0

Please sign in to comment.