diff --git a/README.md b/README.md index d7b6e9a6..620de282 100644 --- a/README.md +++ b/README.md @@ -109,10 +109,10 @@ prompt immediately. ### Contributing new types of segments The `segments` directory contains python scripts which are injected as is into -a single file `powerline-shell.py.template`. Each segment script defines a -function that inserts one or more segments into the prompt. If you want to add a -new segment, simply create a new file in the segments directory and add its name -to the `config.py` file at the appropriate location. +a single file `powerline_shell_base.py`. Each segment script defines a function +that inserts one or more segments into the prompt. If you want to add a new +segment, simply create a new file in the segments directory and add its name to +the `config.py` file at the appropriate location. Make sure that your script does not introduce new globals which might conflict with other scripts. Your script should fail silently and run quickly in any diff --git a/dev_requirements.txt b/dev_requirements.txt new file mode 100644 index 00000000..a6786964 --- /dev/null +++ b/dev_requirements.txt @@ -0,0 +1,2 @@ +nose +mock diff --git a/install.py b/install.py index 98db7617..2264093c 100755 --- a/install.py +++ b/install.py @@ -10,7 +10,7 @@ shutil.copyfile('config.py.dist', 'config.py') import config -TEMPLATE_FILE = 'powerline-shell.py.template' +TEMPLATE_FILE = 'powerline_shell_base.py' OUTPUT_FILE = 'powerline-shell.py' SEGMENTS_DIR = 'segments' THEMES_DIR = 'themes' diff --git a/powerline-shell.py.template b/powerline_shell_base.py similarity index 85% rename from powerline-shell.py.template rename to powerline_shell_base.py index 42149141..89442d14 100755 --- a/powerline-shell.py.template +++ b/powerline_shell_base.py @@ -88,21 +88,26 @@ def get_valid_cwd(): We return the original cwd because the shell still considers that to be the working directory, so returning our guess will confuse people """ + # Prefer the PWD environment variable. Python's os.getcwd function follows + # symbolic links, which is undesirable. But if PWD is not set then fall + # back to this func try: - cwd = os.getcwd() + cwd = os.getenv('PWD') or os.getcwd() except: - cwd = os.getenv('PWD') # This is where the OS thinks we are - parts = cwd.split(os.sep) - up = cwd - while parts and not os.path.exists(up): - parts.pop() - up = os.sep.join(parts) - try: - os.chdir(up) - except: - warn("Your current directory is invalid.") - sys.exit(1) - warn("Your current directory is invalid. Lowest valid directory: " + up) + warn("Your current directory is invalid. If you open a ticket at " + + "https://github.com/milkbikis/powerline-shell/issues/new " + + "we would love to help fix the issue.") + sys.stdout.write("> ") + sys.exit(1) + + parts = cwd.split(os.sep) + up = cwd + while parts and not os.path.exists(up): + parts.pop() + up = os.sep.join(parts) + if cwd != up: + warn("Your current directory is invalid. Lowest valid directory: " + + up) return cwd diff --git a/test/cwd_test.py b/test/cwd_test.py new file mode 100644 index 00000000..111863e8 --- /dev/null +++ b/test/cwd_test.py @@ -0,0 +1,53 @@ +import unittest +import mock +import os +import tempfile +import shutil +import powerline_shell_base as p + + +class CwdTest(unittest.TestCase): + + def setUp(self): + self.dirname = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.dirname) + + @mock.patch('os.getenv') + @mock.patch('powerline_shell_base.warn') + def test_normal(self, warn, getenv): + getenv.return_value = self.dirname + self.assertEqual(p.get_valid_cwd(), self.dirname) + self.assertEqual(warn.call_count, 0) + + @mock.patch('os.getenv') + @mock.patch('powerline_shell_base.warn') + def test_nonexistent_warns(self, warn, getenv): + subdir = os.path.join(self.dirname, 'subdir') + getenv.return_value = subdir + self.assertEqual(p.get_valid_cwd(), subdir) + self.assertEqual(warn.call_count, 1) + + @mock.patch('os.getenv') + @mock.patch('powerline_shell_base.warn') + def test_falls_back_to_getcwd(self, warn, getenv): + getenv.return_value = None + os.chdir(self.dirname) + self.assertEqual(p.get_valid_cwd(), self.dirname) + self.assertEqual(warn.call_count, 0) + + @mock.patch('os.getenv') + @mock.patch('powerline_shell_base.warn') + def test_nonexistent_getcwd_warns(self, warn, getenv): + subdir = os.path.join(self.dirname, 'subdir') + getenv.return_value = None + + os.mkdir(subdir) + os.chdir(subdir) + os.rmdir(subdir) + + with self.assertRaises(SystemExit) as e: + p.get_valid_cwd() + + self.assertEqual(warn.call_count, 1)