sapling/tests/test-fb-hgext-patchrmdir.py
Jun Wu 9dc21f8d0b codemod: import from the edenscm package
Summary:
D13853115 adds `edenscm/` to `sys.path` and code still uses `import mercurial`.
That has nasty problems if both `import mercurial` and
`import edenscm.mercurial` are used, because Python would think `mercurial.foo`
and `edenscm.mercurial.foo` are different modules so code like
`try: ... except mercurial.error.Foo: ...`, or `isinstance(x, mercurial.foo.Bar)`
would fail to handle the `edenscm.mercurial` version. There are also some
module-level states (ex. `extensions._extensions`) that would cause trouble if
they have multiple versions in a single process.

Change imports to use the `edenscm` so ideally the `mercurial` is no longer
imported at all. Add checks in extensions.py to catch unexpected extensions
importing modules from the old (wrong) locations when running tests.

Reviewed By: phillco

Differential Revision: D13868981

fbshipit-source-id: f4e2513766957fd81d85407994f7521a08e4de48
2019-01-29 17:25:32 -08:00

67 lines
1.3 KiB
Python

from __future__ import absolute_import, print_function
import os
import sys
from edenscm.hgext import patchrmdir
from edenscm.mercurial import util
if not sys.platform.startswith("linux"):
sys.stderr.write("skipped: linux required\n")
sys.exit(80)
patchrmdir.uisetup(None)
testtmp = os.environ["TESTTMP"]
d1 = os.path.join(testtmp, "d1")
d2 = os.path.join(testtmp, "d1", "d2")
os.mkdir(d1)
os.mkdir(d2)
def tryfunc(func):
try:
func()
except Exception as ex:
# normalize the error message across platforms
if "Non-empty directory" not in str(ex):
ex = "*"
else:
ex = "Non-empty directory"
print(" error: %s" % ex)
else:
print(" success")
print("rmdir d1 - should fail with ENOTEMPTY")
tryfunc(lambda: os.rmdir(d1))
print("rmdir d1/d2 - should succeed")
tryfunc(lambda: os.rmdir(d2))
open(d2, "w").close()
print("rmdir d1 - should fail with ENOTEMPTY")
tryfunc(lambda: os.rmdir(d1))
os.unlink(d2)
print("rmdir d1 - should succeed")
tryfunc(lambda: os.rmdir(d1))
print("rmdir d1 - should fail with ENOENT")
tryfunc(lambda: os.rmdir(d1))
os.mkdir(d1)
os.mkdir(d2)
print("removedirs d2 (and d1) - should succeed")
tryfunc(lambda: util.removedirs(d2))
print("removedirs d1 - should fail with ENOENT")
tryfunc(lambda: util.removedirs(d1))