Skip to content

Commit

Permalink
Merge pull request b-ryan#194 from b-ryan/master
Browse files Browse the repository at this point in the history
Fix for following symbolic links, plus start testing things
  • Loading branch information
amtrivedi91 committed Aug 30, 2016
2 parents f4f2588 + a353379 commit bf97c23
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nose
mock
2 changes: 1 addition & 1 deletion install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
31 changes: 18 additions & 13 deletions powerline-shell.py.template → powerline_shell_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
53 changes: 53 additions & 0 deletions test/cwd_test.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit bf97c23

Please sign in to comment.