Skip to content

Sym two #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f4c0ed6
some changes to file reading
etiennerichart Jun 25, 2020
347707d
Some work on symbolic link files
etiennerichart Jun 26, 2020
3f13a28
Cleaned up symlink fix ideas
etiennerichart Jun 26, 2020
47ea5ee
More clean up
etiennerichart Jun 26, 2020
6eff3b2
Make it easier to test different methods
etiennerichart Jun 26, 2020
8e08f3a
Found an even better way. useNameSubname = 4
etiennerichart Jun 26, 2020
2b96e8d
"Used os.path.realpath to fix issue"
etiennerichart Jun 29, 2020
44f76f7
Removed code that I was using for testing
etiennerichart Jun 29, 2020
513f37d
Made variable to store paths for comparison.
etiennerichart Jun 30, 2020
01a6a4c
further finalize changes to importcompletion.py
etiennerichart Jul 3, 2020
2edc977
Working on test but am having issues with the try
etiennerichart Jul 6, 2020
62bed48
replaced import test file with them file directory
etiennerichart Jul 7, 2020
45ada74
fixed importtest.py
etiennerichart Jul 8, 2020
9fafb2e
moved and renamed importtest to test folder
etiennerichart Jul 8, 2020
52cef9f
cleaned up test file
etiennerichart Jul 9, 2020
6983428
improved test
etiennerichart Jul 9, 2020
5928198
made it so test skips in python 2
etiennerichart Jul 9, 2020
dbc1d4f
made skipping work
etiennerichart Jul 9, 2020
5f15d7d
removed chdir and made better test skip
etiennerichart Jul 13, 2020
8bd85e9
fix symbolic links for the test
etiennerichart Jul 16, 2020
0a29731
used black auto format
etiennerichart Jul 16, 2020
cec627a
improved test to work on other computers
etiennerichart Jul 16, 2020
629bf1e
made testing strickter
etiennerichart Jul 16, 2020
11369fc
further testing
etiennerichart Jul 16, 2020
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
12 changes: 9 additions & 3 deletions bpython/importcompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@

# The cached list of all known modules
modules = set()
# List of stored paths to compare against so that real paths are not repeated
# handles symlinks not mount points
paths = set()
fully_loaded = False


Expand Down Expand Up @@ -190,9 +193,12 @@ def find_modules(path):
continue
else:
if is_package:
for subname in find_modules(pathname):
if subname != "__init__":
yield "%s.%s" % (name, subname)
path_real = os.path.realpath(pathname)
if path_real not in paths:
paths.add(path_real)
for subname in find_modules(pathname):
if subname != "__init__":
yield "%s.%s" % (name, subname)
yield name


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the convention being followed here is two lines between top-level statements. You should pip install black and run black on this file; that will autoformat it, dealing with these details. Or better you can set up VSCode to run black automatically.

Expand Down
92 changes: 92 additions & 0 deletions bpython/test/test_import_not_cyclical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from bpython.test import unittest
from bpython.importcompletion import find_modules
import os, sys, tempfile


@unittest.skipIf(sys.version_info[0] <= 2, "Test doesn't work in python 2.")
class TestAvoidSymbolicLinks(unittest.TestCase):
def setUp(self):
with tempfile.TemporaryDirectory() as import_test_folder:
os.mkdir(os.path.join(import_test_folder, "Level0"))
os.mkdir(os.path.join(import_test_folder, "Right"))
os.mkdir(os.path.join(import_test_folder, "Left"))

current_path = os.path.join(import_test_folder, "Level0")
with open(
os.path.join(current_path, "__init__.py"), "x"
) as init_file:
pass

current_path = os.path.join(current_path, "Level1")
os.mkdir(current_path)
with open(
os.path.join(current_path, "__init__.py"), "x"
) as init_file:
pass

current_path = os.path.join(current_path, "Level2")
os.mkdir(current_path)
with open(
os.path.join(current_path, "__init__.py"), "x"
) as init_file:
pass

os.symlink(
os.path.join(import_test_folder, "Level0/Level1"),
os.path.join(current_path, "Level3"),
True,
)

current_path = os.path.join(import_test_folder, "Right")
with open(
os.path.join(current_path, "__init__.py"), "x"
) as init_file:
pass

os.symlink(
os.path.join(import_test_folder, "Left"),
os.path.join(current_path, "toLeft"),
True,
)

current_path = os.path.join(import_test_folder, "Left")
with open(
os.path.join(current_path, "__init__.py"), "x"
) as init_file:
pass

os.symlink(
os.path.join(import_test_folder, "Right"),
os.path.join(current_path, "toRight"),
True,
)

self.foo = list(find_modules(os.path.abspath(import_test_folder)))
self.filepaths = [
"Left.toRight.toLeft",
"Left.toRight",
"Left",
"Level0.Level1.Level2.Level3",
"Level0.Level1.Level2",
"Level0.Level1",
"Level0",
"Right",
"Right.toLeft",
"Right.toLeft.toRight",
]

def test_simple_symbolic_link_loop(self):
for thing in self.foo:
self.assertTrue(thing in self.filepaths)
if thing == "Left.toRight.toLeft":
self.filepaths.remove("Right.toLeft")
self.filepaths.remove("Right.toLeft.toRight")
if thing == "Right.toLeft.toRight":
self.filepaths.remove("Left.toRight.toLeft")
self.filepaths.remove("Left.toRight")
self.filepaths.remove(thing)
self.assertFalse(self.filepaths)


if __name__ == "__main__":
unittest.main()