Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions conan/internal/graph/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,20 @@ def migrate_compatibility_files(cache_folder):
compatibility_file = os.path.join(compatible_folder, "compatibility.py")
cppstd_compat_file = os.path.join(compatible_folder, "cppstd_compat.py")

def _should_migrate_file(file_path):
def _is_migratable(file_path):
if not os.path.exists(file_path):
return True
content = load(file_path)
first_line = content.lstrip().split("\n", 1)[0]
return CONAN_GENERATED_COMMENT in first_line

if _should_migrate_file(compatibility_file) and _should_migrate_file(cppstd_compat_file):
if os.path.exists(compatibility_file) and load(compatibility_file) != _default_compat:
ConanOutput().success("Migration: Successfully updated compatibility.py")
save(compatibility_file, _default_compat)
if _is_migratable(compatibility_file) and _is_migratable(cppstd_compat_file):
compatibility_exists = os.path.exists(compatibility_file)
needs_update = not compatibility_exists or load(compatibility_file) != _default_compat
if needs_update:
save(compatibility_file, _default_compat)
if compatibility_exists:
ConanOutput().success("Migration: Successfully updated compatibility.py")
if os.path.exists(cppstd_compat_file):
os.remove(cppstd_compat_file)

Expand Down
37 changes: 30 additions & 7 deletions test/integration/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
[("profile.py", "msvc", "EME_ESE_VC"),
("compatibility/compatibility.py", "conanfile", "conian_file")])
def test_migration_profile_checker_plugin(plugin_path, string_replace, new_string):
t = TestClient()
t = TestClient(light=True)
# Any command that checks the package cache generates the DB
t.run("list")
assert os.path.exists(os.path.join(t.cache_folder, "p", "cache.sqlite3"))
Expand All @@ -41,7 +41,7 @@ def test_migration_profile_checker_plugin(plugin_path, string_replace, new_strin
assert new_string not in contents

# New client, everything new
t2 = TestClient()
t2 = TestClient(light=True)
# This generates the new plugin file
t2.run("list")

Expand All @@ -68,7 +68,7 @@ def test_migration_profile_checker_plugin(plugin_path, string_replace, new_strin


def test_back_migrations():
t = TestClient()
t = TestClient(light=True)

# add 3 migrations
for number in (1, 2, 3):
Expand Down Expand Up @@ -97,7 +97,7 @@ def migrate(cache_folder):


def test_back_default_compatibility_migration():
t = TestClient()
t = TestClient(light=True)
t.run("-v") # Fire the backward migration
migration_file = os.path.join(t.cache_folder, "migrations", "2.4_1-migrate.py")
assert os.path.exists(migration_file)
Expand All @@ -113,7 +113,7 @@ def test_back_default_compatibility_migration():

class TestMigrationCppstdCompat:
def test_migration(self):
t = TestClient()
t = TestClient(light=True)
t.run("-v")
cppstd_compat_path = "extensions/plugins/compatibility/cppstd_compat.py"
compatibility_path = "extensions/plugins/compatibility/compatibility.py"
Expand All @@ -125,7 +125,7 @@ def test_migration(self):
assert not os.path.exists(os.path.join(t.cache_folder, cppstd_compat_path))

def test_cppstd_modified(self):
t = TestClient()
t = TestClient(light=True)
t.run("-v")
cppstd_compat_path = "extensions/plugins/compatibility/cppstd_compat.py"
compatibility_path = "extensions/plugins/compatibility/compatibility.py"
Expand All @@ -139,7 +139,7 @@ def test_cppstd_modified(self):
assert "def cppstd_compat(conanfile)" not in t.load_home(compatibility_path)

def test_compatibility_modified(self):
t = TestClient()
t = TestClient(light=True)
t.run("-v")
cppstd_compat_path = "extensions/plugins/compatibility/cppstd_compat.py"
compatibility_path = "extensions/plugins/compatibility/compatibility.py"
Expand All @@ -150,3 +150,26 @@ def test_compatibility_modified(self):
assert t.load_home(compatibility_path) == "Modified file"
# not Removed because compatibility was modified
assert "POTATO" in t.load_home(cppstd_compat_path)

def test_compatibility_modified_by_conan(self):
t = TestClient(light=True)
t.run("-v")
compatibility_path = "extensions/plugins/compatibility/compatibility.py"
old = t.load_home(compatibility_path)
# In old versions of Conan, assume POTATO was present in the file
new = old + "\n# POTATO"
t.save_home({"version.txt": "2.11",
compatibility_path: new})
# But now with the newer version, Conan removed it
# so the triggered migration should remove it
t.run("-v")
assert "Successfully updated compatibility.py" in t.out
assert "# POTATO" not in t.load_home(compatibility_path)

def test_compatibility_no_rewrite(self):
t = TestClient(light=True)
t.run("-v")
# compatibility.py exists and is not modified
t.save_home({"version.txt": "2.11"})
t.run("-v")
assert "Successfully updated compatibility.py" not in t.out
Loading