def testWithCd (self): ayrton.main ('''import os.path with cd ("bin"): print (os.path.split (pwd ())[-1])''') # close stdout as per the description of setUpMockStdout() os.close (1) self.assertEqual (self.r.read (), b'bin\n')
def testBg (self): ayrton.main ('''a= find ("/usr", _bg=True, _out=None); echo ("yes!"); echo (a.exit_code ())''') # close stdout as per the description of setUpMockStdout() os.close (1) self.assertEqual (self.r.read (), b'yes!\n0\n')
def testStdEqNone (self): # do the test ayrton.main ('echo ("foo", _out=None)') # close stdout as per the description of setUpMockStdout() os.close (1) # the output is empty, as it went to /dev/null self.assertEqual (self.r.read (), b'')
def testEnvironment(self): # NOTE: we convert envvars to str when we export() them ayrton.main( """sh (-c='echo environments works: $FOO', _env=dict (FOO='yes'))""" ) tearDownMockStdOut(self) self.assertEqual(self.mock_stdout.read(), 'environments works: yes\n') self.mock_stdout.close()
def testStdEqCapture (self): # do the test ayrton.main ('''f= echo ("foo", _out=Capture); print ("echo: %s" % f)''') # the output is empty, as it went to /dev/null # BUG: check why there's a second \n # ANS: because echo adds the first one and print adds the second one self.assertEqual (self.a.buffer.getvalue (), b'echo: foo\n\n')
def testGt (self): fn= tempfile.mkstemp ()[1] ayrton.main ('echo ("yes") > "%s"' % fn) contents= open (fn).read () # read() does not return bytes! self.assertEqual (contents, 'yes\n') os.unlink (fn)
def testUnknown (self): try: ayrton.main ('''foo''') except CommandNotFound: raise except NameError: pass self.assertRaises (NameError, ayrton.main, '''fff()''') self.assertRaises (CommandNotFound, ayrton.main, '''fff()''')
def testLt (self): fd, fn= tempfile.mkstemp () os.write (fd, b'42\n') os.close (fd) ayrton.main ('cat () < "%s"' % fn) self.assertEqual (self.a.buffer.getvalue (), b'42\n') os.unlink (fn)
def testUnknown(self): try: ayrton.main("""foo""") except CommandNotFound: raise except NameError: pass self.assertRaises(NameError, ayrton.main, """fff()""") self.assertRaises(CommandNotFound, ayrton.main, """fff()""")
def testUnset (self): ayrton.main ('''export (TEST_ENV=42) print (TEST_ENV) unset ("TEST_ENV") try: TEST_ENV except NameError: print ("yes")''') self.assertEqual (self.a.buffer.getvalue (), b'42\nyes\n')
def Remote (self): """This test only succeeds if you you have password/passphrase-less access to localhost""" ayrton.main ('''with remote ('localhost', allow_agent=False) as s: print (SSH_CLIENT) print (s[1].readlines ())''') expected1= b'''[b'127.0.0.1 ''' expected2= b''' 22\\n']\n''' self.assertEqual (self.a.buffer.getvalue ()[:len (expected1)], expected1) self.assertEqual (self.a.buffer.getvalue ()[-len (expected2):], expected2)
def testStdEqCapture (self): # do the test ayrton.main ('''f= echo ("foo", _out=Capture); print ("echo: %s" % f)''') # close stdout as per the description of setUpMockStdout() os.close (1) # the output is empty, as it went to /dev/null # BUG: check why there's a second \n # ANS: because echo adds the first one and print adds the second one self.assertEqual (self.r.read (), b'echo: foo\n\n')
def testLt (self): fd, fn= tempfile.mkstemp () os.write (fd, b'42\n') os.close (fd) ayrton.main ('cat () < "%s"' % fn) # close stdout as per the description of setUpMockStdout() os.close (1) self.assertEqual (self.r.read (), b'42\n') os.unlink (fn)
def testUnset (self): ayrton.main ('''export (TEST_ENV=42) print (TEST_ENV) unset ("TEST_ENV") try: TEST_ENV except NameError: print ("yes")''') # close stdout as per the description of setUpMockStdout() os.close (1) self.assertEqual (self.r.read (), b'42\nyes\n')
def testLtGt (self): fd, fn1= tempfile.mkstemp () os.write (fd, b'42\n') os.close (fd) fn2= tempfile.mkstemp ()[1] ayrton.main ('cat () < "%s" > "%s"' % (fn1, fn2)) contents= open (fn2).read () # read() does not return bytes! self.assertEqual (contents, '42\n') os.unlink (fn1) os.unlink (fn2)
def testIterable (self): self.maxDiff= None ayrton.main ('''lines_read= 0 for line in echo ('yes'): if line=='yes': print ('yes!') else: print (repr (line)) lines_read+= 1 print (lines_read)''') tearDownMockStdOut (self) self.assertEqual (self.mock_stdout.read (), 'yes!\n1\n')
def testIterable(self): ans = ayrton.main('''lines_read= 0 for line in echo ('yes'): if line=='yes': lines_read+= 1 exit (lines_read)''') self.assertEqual(ans, 1)
def testIterable (self): ans= ayrton.main ('''lines_read= 0 for line in echo ('yes'): if line=='yes': lines_read+= 1 exit (lines_read)''') self.assertEqual (ans, 1)
def __testRemote (self): """This test only succeeds if you you have password/passphrase-less access to localhost""" output= ayrton.main ('''with remote ('127.0.0.1', allow_agent=False) as s: print (USER) value= s[1].readlines () # close the fd's, otherwise the test does not finish because the paramiko.Client() is waiting # this means even more that the current remote() API sucks s[0].close () s[1].close () s[2].close () return value''') self.assertEqual ( output, [ ('%s\n' % os.environ['USER']) ] )
def __testRemoteReturn (self): """This test only succeeds if you you have password/passphrase-less access to localhost""" output= ayrton.main ('''with remote ('127.0.0.1', allow_agent=False, _debug=True) as s: return 42 # close the fd's, otherwise the test does not finish because the paramiko.Client() is waiting # this means even more that the current remote() API sucks s[0].close () s[1].close () #s[2].close () try: return foo except Exception as e: return e''') self.assertEqual (output, '''42\n''')
def __testRemoteEnv (self): """This test only succeeds if you you have password/passphrase-less access to localhost""" output= ayrton.main ('''with remote ('127.0.0.1', allow_agent=False) as s: print (SSH_CLIENT) value= s[1].readlines () # close the fd's, otherwise the test does not finish because the paramiko.Client() is waiting # this means even more that the current remote() API sucks s[0].close () s[1].close () s[2].close () return value''') expected1= '''127.0.0.1 ''' expected2= ''' 22\n''' self.assertEqual (output[0][:len (expected1)], expected1) self.assertEqual (output[0][-len (expected2):], expected2)
def testOrderedOptions (self): ayrton.main ("""echo (-l=True, --more=55, --ordered_options='yes!')""") tearDownMockStdOut (self) self.assertEqual (self.mock_stdout.read (), '-l --more 55 --ordered_options yes!\n') self.mock_stdout.close ()
def __testSimpleReturn (self): self.assertEqual (ayrton.main ('''return 50'''), 50)
def testSimpleFor (self): ayrton.main ('''for a in (1, 2): pass''')
def testTupleAssign (self): ayrton.main ('''(a, b)= (1, 2)''')
def testOrderedOptions(self): ayrton.main("""echo (-l=True, --more=55, --ordered_options='yes!')""") tearDownMockStdOut(self) self.assertEqual(self.mock_stdout.read(), '-l --more 55 --ordered_options yes!\n') self.mock_stdout.close()
def testDefFun1 (self): ayrton.main ('''def foo (): true= 42 true ()''')
def testAssign (self): ayrton.main ('''a= lambda x: x a (1)''')
def testFromImportAs (self): ayrton.main ('''from random import seed as foo foo ()''')
def testFromImport (self): ayrton.main ('''from random import seed; seed ()''')
def testSimpleCase (self): ayrton.main ('true ()')
def testEnvironment (self): # NOTE: we convert envvars to str when we export() them ayrton.main ("""sh (-c='echo environments works: $FOO', _env=dict (FOO='yes'))""") tearDownMockStdOut (self) self.assertEqual (self.mock_stdout.read (), 'environments works: yes\n') self.mock_stdout.close ()
def testDefFunFails1 (self): ayrton.main ('''option ('errexit') def foo (): false= lambda: True false ()''')