diff --git a/mock/py/mock.py b/mock/py/mock.py index 1e576ece5..5c3c31d9a 100755 --- a/mock/py/mock.py +++ b/mock/py/mock.py @@ -998,7 +998,9 @@ def run_command(options, args, config_opts, commands, buildroot, state): dest = args[-1] sources = [] for arg in args[:-1]: - matches = glob.glob(buildroot.make_chroot_path(arg.replace('~', buildroot.homedir))) + with util.env_var_override("HOME", buildroot.homedir): + arg = os.path.expanduser(arg) + matches = glob.glob(buildroot.make_chroot_path(arg)) if not matches: log.critical("%s not found", arg) return 50 diff --git a/mock/py/mockbuild/util.py b/mock/py/mockbuild/util.py index 9dce10ae6..f435c79ae 100644 --- a/mock/py/mockbuild/util.py +++ b/mock/py/mockbuild/util.py @@ -1079,3 +1079,22 @@ def nullcontext(): Python 3.6+ compatible because of EL 8 """ yield None + +@contextlib.contextmanager +def env_var_override(name, value): + """ + Temporary set environment variable NAME to VALUE, revert to the previous + state. + """ + + oldval = None + if name in os.environ: + oldval = os.environ[name] + + os.environ[name] = value + yield + + if oldval is None: + del os.environ[name] + else: + os.environ[name] = oldval diff --git a/releng/release-notes-next/expanduser-with-copyout.bugfix b/releng/release-notes-next/expanduser-with-copyout.bugfix new file mode 100644 index 000000000..f91edc4a1 --- /dev/null +++ b/releng/release-notes-next/expanduser-with-copyout.bugfix @@ -0,0 +1,4 @@ +Previous versions of Mock mistakenly expanded every `~` occurrence +(tilde character) in the specified source path with `--copyout`. So +files `~/foo~bar.txt` were searched on path `/builddir/foo/builddirbar.txt` +instead of just `/builddir/foo~bar.txt`. Fixes [rhbz#2239035][].