store relative link, add tests

This commit is contained in:
Wolf Vollprecht 2021-12-07 20:59:02 +01:00
parent 8df8a0ccdb
commit 34c2383465
2 changed files with 45 additions and 9 deletions

View File

@ -602,14 +602,17 @@ namespace mamba
auto all_files = read_lines(trash_txt);
for (auto& f : all_files)
{
LOG_INFO << "Trash: removing " << f;
if (!fs::exists(f) || fs::remove(f, ec))
fs::path full_path = prefix / f;
LOG_INFO << "Trash: removing " << full_path;
if (!fs::exists(full_path) || fs::remove(full_path, ec))
{
deleted_files += 1;
}
else
{
LOG_INFO << "Trash: could not remove " << full_path;
remainig_trash += 1;
// save relative path
remaining_files.push_back(f);
}
}
@ -619,12 +622,12 @@ namespace mamba
}
else
{
auto trash_out_file = open_ofstream(trash_txt, std::ios::binary | std::ios::trunc);
auto trash_out_file
= open_ofstream(trash_txt, std::ios::out | std::ios::binary | std::ios::trunc);
for (auto& rf : remaining_files)
{
trash_out_file << rf.string() << "\n";
}
trash_out_file.close();
}
}
@ -686,7 +689,8 @@ namespace mamba
while (ec)
{
LOG_ERROR << "Caught a filesystem error: " << ec.message();
LOG_INFO << "Caught a filesystem error for '" << path.string()
<< "':" << ec.message() << " (File in use?)";
fs::path trash_file = path;
std::size_t fcounter = 0;
@ -713,7 +717,9 @@ namespace mamba
std::ios::app | std::ios::binary);
// TODO add some unicode tests here?
trash_index << trash_file.string() << "\n";
trash_index
<< fs::relative(trash_file, Context::instance().target_prefix).string()
<< "\n";
return 1;
}

View File

@ -5,6 +5,7 @@ import random
import shutil
import string
import subprocess
import time
from pathlib import Path
import pytest
@ -128,13 +129,42 @@ class TestRemove:
res = remove("python", "-v", "-p", self.prefix, no_dry_run=True)
if platform.system() == "Windows":
print(pyexe.exists())
pyexe_trash = Path(str(pyexe) + ".mamba_trash")
print(pyexe_trash)
print(pyexe_trash.exists())
assert pyexe.exists() == False
assert pyexe_trash.exists()
trash_file = Path(self.prefix) / "conda-meta" / "mamba_trash.txt"
assert trash_file.exists()
all_trash_files = list(Path(self.prefix).rglob("*.mamba_trash"))
with open(trash_file, "r") as fi:
lines = [x.strip() for x in fi.readlines()]
assert all([l.endswith(".mamba_trash") for l in lines])
assert len(all_trash_files) == len(lines)
linesp = [Path(self.prefix) / l for l in lines]
for atf in all_trash_files:
assert atf in linesp
# No change if file still in use
install("cpp-filesystem", "-n", self.env_name, "--json", no_dry_run=True)
assert trash_file.exists()
assert pyexe_trash.exists()
with open(trash_file, "r") as fi:
lines = [x.strip() for x in fi.readlines()]
assert all([l.endswith(".mamba_trash") for l in lines])
assert len(all_trash_files) == len(lines)
linesp = [Path(self.prefix) / l for l in lines]
for atf in all_trash_files:
assert atf in linesp
subprocess.Popen("TASKKILL /F /PID {pid} /T".format(pid=pyproc.pid))
# check that another env mod clears lingering trash files
time.sleep(0.5)
install("xsimd", "-n", self.env_name, "--json", no_dry_run=True)
assert trash_file.exists() == False
assert pyexe_trash.exists() == False
else:
assert pyexe.exists() == False
pyproc.kill()