From 02faa7b97e80f24e7f4753c9348d76fede3d80d5 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Thu, 5 Sep 2024 14:53:29 +0200 Subject: reindex: ensure database is empty before reindex (#46199) fixes two tests that did not clear the in-memory bits of a database before calling reindex. --- lib/spack/spack/test/cmd/reindex.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/test/cmd/reindex.py b/lib/spack/spack/test/cmd/reindex.py index 1aceeab197..6e48a3c21c 100644 --- a/lib/spack/spack/test/cmd/reindex.py +++ b/lib/spack/spack/test/cmd/reindex.py @@ -2,9 +2,10 @@ # Spack Project Developers. See the top-level COPYRIGHT file for details. # # SPDX-License-Identifier: (Apache-2.0 OR MIT) -import os +import shutil import spack.store +from spack.database import Database from spack.main import SpackCommand install = SpackCommand("install") @@ -23,20 +24,31 @@ def test_reindex_basic(mock_packages, mock_archive, mock_fetch, install_mockery) assert spack.store.STORE.db.query() == all_installed -def test_reindex_db_deleted(mock_packages, mock_archive, mock_fetch, install_mockery): +def _clear_db(tmp_path): + empty_db = Database(str(tmp_path)) + with empty_db.write_transaction(): + pass + shutil.rmtree(spack.store.STORE.db.database_directory) + shutil.copytree(empty_db.database_directory, spack.store.STORE.db.database_directory) + # force a re-read of the database + assert len(spack.store.STORE.db.query()) == 0 + + +def test_reindex_db_deleted(mock_packages, mock_archive, mock_fetch, install_mockery, tmp_path): install("libelf@0.8.13") install("libelf@0.8.12") all_installed = spack.store.STORE.db.query() - os.remove(spack.store.STORE.db._index_path) + _clear_db(tmp_path) + reindex() assert spack.store.STORE.db.query() == all_installed def test_reindex_with_deprecated_packages( - mock_packages, mock_archive, mock_fetch, install_mockery + mock_packages, mock_archive, mock_fetch, install_mockery, tmp_path ): install("libelf@0.8.13") install("libelf@0.8.12") @@ -46,7 +58,8 @@ def test_reindex_with_deprecated_packages( all_installed = spack.store.STORE.db.query(installed=any) non_deprecated = spack.store.STORE.db.query(installed=True) - os.remove(spack.store.STORE.db._index_path) + _clear_db(tmp_path) + reindex() assert spack.store.STORE.db.query(installed=any) == all_installed -- cgit v1.2.3-70-g09d2