import unittest
import os, getpass, socket, sys, tempfile, time
from inspect import *

from multiprocessing import Process, Pipe

from jadsh.shell import Shell

def createShell(childStdin, stdout):
	stdin = os.fdopen(childStdin, "r")
	shell = Shell(stdin = stdin, stdout = stdout)
	stdin.close()

class BaseShellTest(unittest.TestCase):
	def setUp(self):
		childStdin, parentStdout = os.pipe()

		# Significantly easier to read a file, than with a pipe
		self.stdin = self.tempFile("r+")

		# Communicate with the shell via a pipe
		self.stdout = os.fdopen(parentStdout, "w")

		self.shell = Process(target=createShell, args=(childStdin, self.stdin, ))
		self.shell.start()

	def tearDown(self):
		# Safely exit the shell
		self.runCommand("exit")
		# Wait for shell to exit before continuing
		self.shell.join()
		self.stdin.close()
		self.stdout.close()
		# Delete temp file
		os.unlink(self.stdin.name)

	def tempFile(self, permissions):
		(file_descriptor, file_name) = tempfile.mkstemp()
		os.close(file_descriptor)
		return open(file_name, permissions)

	def runCommand(self, command):
		self.stdout.write(str(command) + "\n")
		self.stdout.flush()
		return self.getOutput()

	def getOutput(self, pause = 0.1):
		# Slightly hacky way to make sure the shell is finished with its job before grabbing output
		time.sleep(pause)
		self.stdin.seek(0)
		return self.stdin.readlines()

	def getcwd(self):
		home = os.path.expanduser("~")
		pwd = os.getcwd()
		pwd = pwd.replace(home, "~")
		return pwd

class StartupTest(BaseShellTest):
	def test_startup(self):
		"""
		On startup, the shell should display a welcome message and the prompt
		"""
		username = getpass.getuser()
		hostname = socket.gethostname()

		expected_value = "Welcome to jadsh, Just Another Dumb SHell\n"
		expected_value += "Type help for instructions on how to use jadsh\n"
		expected_value += username + "@" + hostname + ":" + self.getcwd() + ":$ \n"

		welcome = ''.join(self.getOutput())

		self.assertEqual(welcome, expected_value, "Welcome message does not appear")

if __name__ == '__main__':
	unittest.main()
@@ -61,31 +61,8 @@
 ime%0A
-from inspect import *%0A%0A
 from
@@ -96,14 +96,8 @@
 cess
-, Pipe
 %0A%0Afr
@@ -129,423 +129,402 @@
 ll%0A%0A
-def createShell(childStdin, stdout):%0A%09stdin = os.fdopen(childStdin, %22r%22)%0A%09shell = Shell(stdin = stdin, stdout = stdout)%0A%09stdin.close()%0A%0Aclass BaseShellTest(unittest.TestCase):%0A%09def setUp(self):%0A%09%09childStdin, parentStdout = os.pipe()%0A%0A%09%09# Significantly easier to read a file, than with a pipe%0A%09%09self.stdin = self.tempFile(%22r+%22)%0A%0A%09%09# Communicate with the shell via a pipe%0A%09%09self.stdout = os.fdopen(parentStdout, %22w%22)%0A
+class BaseShellTest(unittest.TestCase):%0A%09def setUp(self):%0A%09%09# One way communication to child process%0A%09%09childStdin, parentStdout = os.pipe()%0A%0A%09%09# Open file descriptor for manipulation%0A%09%09self.stdout = os.fdopen(parentStdout, %22w%22)%0A%0A%09%09# One way communication from the child process%0A%09%09self.stdin = self.tempFile(%22r+%22)%0A%0A%09%09# Create new shell process (passing in stdin and stdout for the child process)
 %0A%09%09s
@@ -550,16 +550,21 @@
 (target=
+self.
 createSh
@@ -619,24 +619,170 @@
 ll.start()%0A%0A
+%09def createShell(self, childStdin, stdout):%0A%09%09stdin = os.fdopen(childStdin, %22r%22)%0A%09%09shell = Shell(stdin = stdin, stdout = stdout)%0A%09%09stdin.close()%0A%0A
 %09def tearDow
@@ -907,16 +907,43 @@
 .join()%0A
+%09%09# Close file descriptors%0A
 %09%09self.s
@@ -1062,16 +1062,73 @@
 sions):%0A
+%09%09# Generate new temporary file and return a file object%0A
 %09%09(file_
@@ -1270,16 +1270,46 @@
 mmand):%0A
+%09%09# Pass command to the shell%0A
 %09%09self.s

Comment on tests