Skip to content

Commit

Permalink
Merge pull request b-ryan#212 from b-ryan/feature/test-framework
Browse files Browse the repository at this point in the history
Test framework
  • Loading branch information
amtrivedi91 committed Aug 30, 2016
2 parents a41315c + f8b67c2 commit 1af2df6
Show file tree
Hide file tree
Showing 26 changed files with 195 additions and 94 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ scenario.
Make sure you introduce new default colors in `themes/default.py` for every new
segment you create. Test your segment with this theme first.

You should add tests for your segment as best you are able. Unit and
integration tests are both welcome. Run your tests with the `nosetests` command
after install the requirements in `dev_requirements.txt`.

### Themes

The `themes` directory stores themes for your prompt, which are basically color
Expand Down
5 changes: 5 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies:
pre:
- sudo pip install -r dev_requirements.txt
- git config --global user.email "[email protected]"
- git config --global user.name "Tester McGee"
5 changes: 3 additions & 2 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nose
mock
nose>=1.3.7
mock>=1.3.0
sh>=1.11
4 changes: 4 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def load_source(srcfile):
for segment in config.SEGMENTS:
source += load_source(os.path.join(SEGMENTS_DIR, segment + '.py'))

# assumes each segment file will have a function called
# add_segment__[segment] that accepts the powerline object
source += 'add_{}_segment(powerline)\n'.format(segment)

source += 'sys.stdout.write(powerline.draw())\n'

try:
Expand Down
Empty file added segments/__init__.py
Empty file.
8 changes: 3 additions & 5 deletions segments/cwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def requires_special_home_display(name):
return (name == '~' and Color.HOME_SPECIAL_DISPLAY)


def maybe_shorten_name(name):
def maybe_shorten_name(powerline, name):
"""If the user has asked for each directory name to be shortened, will
return the name up to their specified length. Otherwise returns the full
name."""
Expand All @@ -45,7 +45,7 @@ def get_fg_bg(name):
return (Color.PATH_FG, Color.PATH_BG,)


def add_cwd_segment():
def add_cwd_segment(powerline):
cwd = powerline.cwd or os.getenv('PWD')
if not py3:
cwd = cwd.decode("utf-8")
Expand Down Expand Up @@ -86,7 +86,5 @@ def add_cwd_segment():
separator = None
separator_fg = None

powerline.append(' %s ' % maybe_shorten_name(name), fg, bg,
powerline.append(' %s ' % maybe_shorten_name(powerline, name), fg, bg,
separator, separator_fg)

add_cwd_segment()
4 changes: 1 addition & 3 deletions segments/exit_code.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
def add_exit_code_segment():
def add_exit_code_segment(powerline):
if powerline.args.prev_error == 0:
return
fg = Color.CMD_FAILED_FG
bg = Color.CMD_FAILED_BG
powerline.append(' %s ' % str(powerline.args.prev_error), fg, bg)

add_exit_code_segment()
24 changes: 17 additions & 7 deletions segments/fossil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_fossil_status():

return has_modified_files, has_untracked_files, has_missing_files

def add_fossil_segment():
def _add_fossil_segment(powerline):
subprocess.Popen(['fossil'], stdout=subprocess.PIPE).communicate()[0]
branch = ''.join([i.replace('*','').strip() for i in os.popen("fossil branch 2> /dev/null").read().strip().split("\n") if i.startswith('*')])
if len(branch) == 0:
Expand All @@ -32,9 +32,19 @@ def add_fossil_segment():
branch += (' ' + extra if extra != '' else '')
powerline.append(' %s ' % branch, fg, bg)

try:
add_fossil_segment()
except OSError:
pass
except subprocess.CalledProcessError:
pass
def add_fossil_segment(powerline):
"""Wraps _add_fossil_segment in exception handling."""

# FIXME This function was added when introducing a testing framework,
# during which the 'powerline' object was passed into the
# `add_[segment]_segment` functions instead of being a global variable. At
# that time it was unclear whether the below exceptions could actually be
# thrown. It would be preferable to find out whether they ever will. If so,
# write a comment explaining when. Otherwise remove.

try:
_add_fossil_segment(powerline)
except OSError:
pass
except subprocess.CalledProcessError:
pass
46 changes: 27 additions & 19 deletions segments/git.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import subprocess
import os

GIT_SYMBOLS = {
'detached': u'\u2693',
Expand All @@ -11,17 +12,24 @@
'conflicted': u'\u273C'
}

git_subprocess_env = {
# LANG is specified to ensure git always uses a language we are expecting.
# Otherwise we may be unable to parse the output.
"LANG": "C",
def get_PATH():
"""Normally gets the PATH from the OS. This function exists to enable
easily mocking the PATH in tests.
"""
return os.getenv("PATH")

# https://github.com/milkbikis/powerline-shell/pull/126
"HOME": os.getenv("HOME"),
def git_subprocess_env():
return {
# LANG is specified to ensure git always uses a language we are expecting.
# Otherwise we may be unable to parse the output.
"LANG": "C",

# https://github.com/milkbikis/powerline-shell/pull/153
"PATH": os.getenv("PATH"),
}
# https://github.com/milkbikis/powerline-shell/pull/126
"HOME": os.getenv("HOME"),

# https://github.com/milkbikis/powerline-shell/pull/153
"PATH": get_PATH(),
}


def parse_git_branch_info(status):
Expand All @@ -32,7 +40,7 @@ def parse_git_branch_info(status):
def _get_git_detached_branch():
p = subprocess.Popen(['git', 'describe', '--tags', '--always'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=git_subprocess_env)
env=git_subprocess_env())
detached_ref = p.communicate()[0].decode("utf-8").rstrip('\n')
if p.returncode == 0:
branch = u'{} {}'.format(GIT_SYMBOLS['detached'], detached_ref)
Expand Down Expand Up @@ -62,10 +70,15 @@ def _n_or_empty(_dict, _key):
return _dict[_key] if int(_dict[_key]) > 1 else u''


def add_git_segment():
p = subprocess.Popen(['git', 'status', '--porcelain', '-b'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=git_subprocess_env)
def add_git_segment(powerline):
try:
p = subprocess.Popen(['git', 'status', '--porcelain', '-b'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=git_subprocess_env())
except OSError:
# Popen will throw an OSError if git is not found
return

pdata = p.communicate()
if p.returncode != 0:
return
Expand Down Expand Up @@ -101,8 +114,3 @@ def _add(_dict, _key, fg, bg):
_add(stats, 'notstaged', Color.GIT_NOTSTAGED_FG, Color.GIT_NOTSTAGED_BG)
_add(stats, 'untracked', Color.GIT_UNTRACKED_FG, Color.GIT_UNTRACKED_BG)
_add(stats, 'conflicted', Color.GIT_CONFLICTED_FG, Color.GIT_CONFLICTED_BG)

try:
add_git_segment()
except (OSError, subprocess.CalledProcessError):
pass
4 changes: 1 addition & 3 deletions segments/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_hg_status():
has_modified_files = True
return has_modified_files, has_untracked_files, has_missing_files

def add_hg_segment():
def add_hg_segment(powerline):
branch = os.popen('hg branch 2> /dev/null').read().rstrip()
if len(branch) == 0:
return False
Expand All @@ -37,5 +37,3 @@ def add_hg_segment():
extra += '!'
branch += (' ' + extra if extra != '' else '')
return powerline.append(' %s ' % branch, fg, bg)

add_hg_segment()
5 changes: 1 addition & 4 deletions segments/hostname.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def add_hostname_segment():
def add_hostname_segment(powerline):
if powerline.args.colorize_hostname:
from lib.color_compliment import stringToHashToColorAndOpposite
from lib.colortrans import rgb2short
Expand All @@ -19,6 +19,3 @@ def add_hostname_segment():
host_prompt = ' %s ' % socket.gethostname().split('.')[0]

powerline.append(host_prompt, Color.HOSTNAME_FG, Color.HOSTNAME_BG)


add_hostname_segment()
4 changes: 1 addition & 3 deletions segments/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
import subprocess

def add_jobs_segment():
def add_jobs_segment(powerline):
pppid_proc = subprocess.Popen(['ps', '-p', str(os.getppid()), '-oppid='],
stdout=subprocess.PIPE)
pppid = pppid_proc.communicate()[0].decode("utf-8").strip()
Expand All @@ -15,5 +15,3 @@ def add_jobs_segment():

if num_jobs > 0:
powerline.append(' %d ' % num_jobs, Color.JOBS_FG, Color.JOBS_BG)

add_jobs_segment()
4 changes: 1 addition & 3 deletions segments/node_version.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import subprocess


def add_node_version_segment():
def add_node_version_segment(powerline):
try:
p1 = subprocess.Popen(["node", "--version"], stdout=subprocess.PIPE)
version = p1.communicate()[0].decode("utf-8").rstrip()
version = "node " + version
powerline.append(version, 15, 18)
except OSError:
return

add_node_version_segment()
4 changes: 1 addition & 3 deletions segments/php_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess


def add_php_version_segment():
def add_php_version_segment(powerline):
try:
output = subprocess.check_output(['php', '-r', 'echo PHP_VERSION;'], stderr=subprocess.STDOUT)
if '-' in output:
Expand All @@ -12,5 +12,3 @@ def add_php_version_segment():
powerline.append(version, 15, 4)
except OSError:
return

add_php_version_segment()
4 changes: 1 addition & 3 deletions segments/read_only.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os

def add_read_only_segment():
def add_read_only_segment(powerline):
cwd = powerline.cwd or os.getenv('PWD')

if not os.access(cwd, os.W_OK):
powerline.append(' %s ' % powerline.lock, Color.READONLY_FG, Color.READONLY_BG)

add_read_only_segment()
4 changes: 1 addition & 3 deletions segments/root.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def add_root_indicator_segment():
def add_root_segment(powerline):
root_indicators = {
'bash': ' \\$ ',
'zsh': ' %# ',
Expand All @@ -10,5 +10,3 @@ def add_root_indicator_segment():
fg = Color.CMD_FAILED_FG
bg = Color.CMD_FAILED_BG
powerline.append(root_indicators[powerline.args.shell], fg, bg)

add_root_indicator_segment()
4 changes: 1 addition & 3 deletions segments/ruby_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess


def add_ruby_version_segment():
def add_ruby_version_segment(powerline):
try:
p1 = subprocess.Popen(["ruby", "-v"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["sed", "s/ (.*//"], stdin=p1.stdout, stdout=subprocess.PIPE)
Expand All @@ -13,5 +13,3 @@ def add_ruby_version_segment():
powerline.append(version, 15, 1)
except OSError:
return

add_ruby_version_segment()
4 changes: 1 addition & 3 deletions segments/set_term_title.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def add_term_title_segment():
def add_set_term_title_segment(powerline):
term = os.getenv('TERM')
if not (('xterm' in term) or ('rxvt' in term)):
return
Expand All @@ -13,5 +13,3 @@ def add_term_title_segment():

powerline.append(set_title, None, None, '')


add_term_title_segment()
4 changes: 1 addition & 3 deletions segments/ssh.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

def add_ssh_segment():
def add_ssh_segment(powerline):

if os.getenv('SSH_CLIENT'):
powerline.append(' %s ' % powerline.network, Color.SSH_FG, Color.SSH_BG)

add_ssh_segment()
26 changes: 19 additions & 7 deletions segments/svn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import subprocess

def add_svn_segment():

def _add_svn_segment(powerline):
is_svn = subprocess.Popen(['svn', 'status'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
is_svn_output = is_svn.communicate()[1].decode("utf-8").strip()
Expand All @@ -17,9 +18,20 @@ def add_svn_segment():
changes = output.strip()
powerline.append(' %s ' % changes, Color.SVN_CHANGES_FG, Color.SVN_CHANGES_BG)

try:
add_svn_segment()
except OSError:
pass
except subprocess.CalledProcessError:
pass

def add_svn_segment(powerline):
"""Wraps _add_svn_segment in exception handling."""

# FIXME This function was added when introducing a testing framework,
# during which the 'powerline' object was passed into the
# `add_[segment]_segment` functions instead of being a global variable. At
# that time it was unclear whether the below exceptions could actually be
# thrown. It would be preferable to find out whether they ever will. If so,
# write a comment explaining when. Otherwise remove.

try:
_add_svn_segment(powerline)
except OSError:
pass
except subprocess.CalledProcessError:
pass
4 changes: 1 addition & 3 deletions segments/time.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def add_time_segment():
def add_time_segment(powerline):
if powerline.args.shell == 'bash':
time = ' \\t '
elif powerline.args.shell == 'zsh':
Expand All @@ -8,5 +8,3 @@ def add_time_segment():
time = ' %s ' % time.strftime('%H:%M:%S')

powerline.append(time, Color.HOSTNAME_FG, Color.HOSTNAME_BG)

add_time_segment()
12 changes: 1 addition & 11 deletions segments/uptime.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import subprocess
import re

# uptime output samples
# 1h: 00:00:00 up 1:00, 2 users, load average: 0,00, 0,00, 0,00
# 10+h: 00:00:00 up 10:00, 2 users, load average: 0,00, 0,00, 0,00
# 1+d: 00:00:00 up 1 days, 1:00, 2 users, load average: 0,00, 0,00, 0,00
# 9+d: 00:00:00 up 12 days, 1:00, 2 users, load average: 0,00, 0,00, 0,00
# -1h 00:00:00 up 120 days, 49 min, 2 users, load average: 0,00, 0,00, 0,00
# mac: 00:00:00 up 23 3 day(s), 10:00, 2 users, load average: 0,00, 0,00, 0,00

def add_uptime_segment():
def add_uptime_segment(powerline):
try:
output = subprocess.check_output(['uptime'], stderr=subprocess.STDOUT)
raw_uptime = re.search('(?<=up).+(?=,\s+\d+\s+user)', output).group(0)
Expand All @@ -22,5 +14,3 @@ def add_uptime_segment():
powerline.append(uptime, Color.CWD_FG, Color.PATH_BG)
except OSError:
return

add_uptime_segment()
Loading

0 comments on commit 1af2df6

Please sign in to comment.