Skip to content

Commit

Permalink
fixes nice_path() for when you do something weird like delete the wor…
Browse files Browse the repository at this point in the history
…king dir
  • Loading branch information
kumar303 committed Feb 16, 2011
1 parent 9a09868 commit 8f2e35e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 8 additions & 2 deletions nosenicedots/plugin.py
Expand Up @@ -172,8 +172,14 @@ def get_context(test):

def nice_path(path):
path = os.path.abspath(path)
if path.startswith(os.getcwd()):
path = path.replace(os.getcwd(), '')[1:] # shorten and remove slash
try:
wd = os.getcwd()
except OSError:
# I guess you did something stupid like delete the current directory.
pass
else:
if path.startswith(wd):
path = path.replace(wd, '')[1:] # shorten and remove slash
if path.endswith('.pyc'):
path = path[0:-1]
return path
24 changes: 23 additions & 1 deletion nosenicedots/tests/test_units.py
@@ -1,5 +1,8 @@

import os
import sys
import shutil
import tempfile
import unittest

import nose.case
Expand All @@ -8,7 +11,7 @@
from nose.tools import eq_
from nose.pyversion import unbound_method

from nosenicedots import nice_test_address
from nosenicedots import nice_test_address, nice_path

class TestNiceAddresses(unittest.TestCase):

Expand Down Expand Up @@ -97,3 +100,22 @@ def test_catastropic_failure(self):
case = Failure(exc[0], exc[1], exc[2], address=None)
eq_(nice_test_address(case),
'??')


class TestNicePath(unittest.TestCase):

def test_missing_working_dir(self):
tmp = tempfile.mkdtemp()
pwd = os.getcwd()
try:
os.chdir(tmp)
shutil.rmtree(tmp)
eq_(nice_path(__file__), __file__)
finally:
os.chdir(pwd)

def test_rel_path(self):
eq_(nice_path(os.path.dirname(__file__)), 'nosenicedots/tests')

def test_pyc_is_stripped(self):
eq_(nice_path('some_file.pyc'), 'some_file.py')

0 comments on commit 8f2e35e

Please sign in to comment.