Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyLothar committed Dec 5, 2014
1 parent 585ca5a commit 2f2713f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
SHCMD ~~上海崇明岛~~
====================

Note: Work in Progress.

This lib aims to provide a Human friendly interface for subprocess.

If you need piped subprocesses, give [envoy](https://github.com/kennethreitz/envoy) a try.


[![Build Status][travis-image]][travis-url]
[![Coverage Status][coverage-image]][coverage-url]
[![Requirements Status][req-status-image]][req-status-url]


### Usage
```python
import shcmd

with shcmd.cd("/tmp"):
# get result directly
assert shcmd.run("pwd") == "/tmp"
# get streamed result packed in a generator
streamed = shcmd.run("ls", stream=True)
for filename in streamed.iter_lines():
print(filename)
# get full stdout/stderr
print(streamed.stdout)
print(streamed.stderr)
```


[travis-url]: https://travis-ci.org/SkyLothar/shcmd
[travis-image]: https://travis-ci.org/SkyLothar/shcmd.svg?branch=master
[coverage-image]: https://coveralls.io/repos/SkyLothar/shcmd/badge.png
[coverage-url]: https://coveralls.io/r/SkyLothar/shcmd
[req-status-url]: https://requires.io/github/SkyLothar/shcmd/requirements/?branch=master
[req-status-image]: https://requires.io/github/SkyLothar/shcmd/requirements.svg?branch=master
Empty file added shcmd/__init__.py
Empty file.
22 changes: 13 additions & 9 deletions shcmd/cmd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf8 -*-

import contextlib
import os
import shlex
import subprocess

from . import compat

Expand All @@ -24,14 +24,24 @@ def split_args(cmd_args):
return args_list


@contextlib.contextmanager
def cd(cd_path):
oricwd = os.getcwd()
try:
os.chdir(cd_path)
yield
finally:
os.chdir(oricwd)


class CmdRequest(object):
def __init__(self, cmd, cwd=None):
self._raw = cmd
self._cmd = split_args(cmd)
self._cwd = cwd or os.getcwd()
self._cwd = os.path.realpath(cwd or os.getcwd())

def __str__(self):
return "<CmdRequest {0}@{1}>".format(self._raw, self.cwd)
return "<CmdRequest ({0})@{1}>".format(self._raw, self.cwd)

@property
def raw(self):
Expand All @@ -44,9 +54,3 @@ def cmd(self):
@property
def cwd(self):
return self._cwd

def run(self):
pass

def streaming(self):
pass
39 changes: 33 additions & 6 deletions tests/test-cmd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- coding: utf8 -*-

import nose
import os
import tempfile

import nose

import shcmd.cmd

Expand All @@ -11,20 +13,45 @@ def test_asc_split_args():

# str
result = shcmd.cmd.split_args("/bin/bash echo 上海崇明岛")
nose.tools.eq_(result, correct)
nose.tools.eq_(result, correct, "str test failed")

# unicode
result = shcmd.cmd.split_args(u"/bin/bash echo 上海崇明岛")
nose.tools.eq_(result, correct)
nose.tools.eq_(result, correct, "unicode test failed")

# bytes
result = shcmd.cmd.split_args(u"/bin/bash echo 上海崇明岛".encode("utf8"))
nose.tools.eq_(result, correct)
nose.tools.eq_(result, correct, "bytes test failed")

# list
result = shcmd.cmd.split_args(["/bin/bash", "echo", "上海崇明岛"])
nose.tools.eq_(result, correct)
nose.tools.eq_(result, correct, "list test failed")

# tuple
result = shcmd.cmd.split_args(("/bin/bash", "echo", "上海崇明岛"))
nose.tools.eq_(result, correct)
nose.tools.eq_(result, correct, "tuple test failed")


def test_cd():
tmpdir = os.path.realpath(tempfile.gettempdir())
oridir = os.getcwd()
nose.tools.ok_(oridir != tmpdir, "tmp dir == curr dir")
with shcmd.cmd.cd(tmpdir):
nose.tools.eq_(
os.path.realpath(os.getcwd()),
tmpdir,
"not cd to dir"
)
nose.tools.eq_(os.getcwd(), oridir, "not cd back")


def test_request():
cwd = os.path.realpath("tmp")
cmd_req = shcmd.cmd.CmdRequest("/bin/bash eval 'ls'", "tmp")
nose.tools.eq_(cmd_req.cmd, ["/bin/bash", "eval", "ls"])
nose.tools.eq_(cmd_req.raw, "/bin/bash eval 'ls'")
nose.tools.eq_(cmd_req.cwd, cwd)
nose.tools.eq_(
str(cmd_req),
"<CmdRequest (/bin/bash eval 'ls')@{0}>".format(cwd)
)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ commands = python setup.py nosetests \
commands = flake8 {toxinidir}

[flake8]
exclude = .tox,.git,*.egg,build,.ropeproject
exclude = .tox,.git,*.egg,build,.ropeproject,compat.py
ignore = H102,H304,H803

0 comments on commit 2f2713f

Please sign in to comment.