def test_redirect_binary(self): with sh.pipes(sh.cat(__file__)) as cmd: cmd > 'tmp' ls = str(sh.ls('-l tmp')) with sh.pipes(sh.cat(__file__)) as cmd: cmd >> 'tmp' self.assertFalse(ls == str(sh.ls('-l tmp'))) sh.rm('tmp')
def test_stdin(self): content = open(__file__).read().strip() if not isinstance(content, six.binary_type): bcontent = content.encode('utf-8') else: bcontent = content self.assertEqual(content, str(sh.stdin(bcontent) | sh.cat('-'))) self.assertEqual(content, str(sh.stdin(open(__file__, 'rb')) | sh.cat('-')))
def test_repr(self): self.assertEqual(repr(sh.stdin(six.b('')) | sh.cat('-')), repr(str('stdin | cat -'))) @sh.wraps def w(): pass self.assertEqual(repr(sh.cat('-') | w), repr(str('cat - | w()'))) self.assertEqual(str('<sh>'), repr(sh.sh))
def test_slices(self): pipe = sh.cat('tmp') | sh.grep('tmp') | sh.wc('-l') self.assertEqual(pipe[0:1]._binary, 'cat') self.assertEqual(pipe.__getitem__(slice(0, 1))._binary, 'cat') self.assertEqual(pipe.__getslice__(0, 1)._binary, 'cat') self.assertEqual(pipe[1:]._binary, 'wc') self.assertEqual(pipe.__getitem__(slice(1, 3))._binary, 'wc') self.assertEqual(pipe.__getslice__(1, 3)._binary, 'wc')
def test_redirect_python(self): @sh.wraps def grep(stdin): for line in stdin: if b'__' in line: yield line pipe = sh.cat(__file__) | grep with sh.pipes(pipe) as cmd: cmd > 'tmp' ls = str(sh.ls('-l tmp')) with sh.pipes(pipe) as cmd: cmd >> 'tmp' self.assertFalse(ls == str(sh.ls('-l tmp')), ls)
def test_stdin2(self): head = str( sh.stdin(open(self.__file__, 'rb') ) | sh.cat('-') | sh.head('-n1')) self.assertTrue(len(head) > 1, head) self.assertTrue(len(head) > 2, head)
def test_redirect_stdin(self): sh.stdin(b'blah') > 'tmp' self.assertEqual(str(sh.cat('tmp')), 'blah') sh.stdin(b'blah') >> 'tmp' self.assertEqual(str(sh.cat('tmp')), 'blahblah')