# -*- coding: utf-8 -*-
'''
Simple RPC
Copyright (c) 2013, Joaquin G. Duo
'''
import os
from simplerpc.base.SimpleRpcLogicBase import SimpleRpcLogicBase
from simplerpc.expose_api.javascript.JsTranslateUtil import JsTranslateUtil
from simplerpc.common.FileManager import FileManager
import subprocess
import sys

class NodeJsRunner(SimpleRpcLogicBase):
  def __post_init__(self):
    self.js_util = JsTranslateUtil(self.context)
    self.file_manager = FileManager(self.context)

  def setNodePath(self):
    NODE_PATH = 'NODE_PATH'
    path = self.js_util._getProjectJsPath()
    if NODE_PATH not in os.environ:
      os.environ[NODE_PATH] = path
    else:
      os.environ[NODE_PATH] += ':' + path

  def __printJasmineErrors(self, file_path, ret_val, out, err):
    if ret_val:
      path = self.file_manager.formatFilePath(file_path)
      self.log.w('Error running jasmine test return value %s:\n%s' %
                 (ret_val, path))
      self.log.w('Stdout:\n%s' % out)
      self.log.w('Stderr:\%s' % err)
    return ret_val

  def runJasmineTest(self, file_path):
    argv = [self.context.jasmine_node_command, file_path, '--matchall',
            '--captureExceptions', '--noColor']
    return self.__printJasmineErrors(file_path, *self.runNodeJs(argv, pipe=True))


  def runNodeJs(self, argv=None, pipe=False):
    self.setNodePath()
    #node_command = 'node'
    if not argv:
      argv = sys.argv[1:]
    argv = [self.context.node_command] + argv
    if pipe:
      p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      return p.wait(), p.stdout.read(), p.stderr.read()
    else:
      p = subprocess.Popen(argv)
      return p.wait()

def smokeTestModule():
  from simplerpc.context.SimpleRpcContext import SimpleRpcContext
  context = SimpleRpcContext('run node.js')
  NodeJsRunner(context)#.runNodeJs()

if __name__ == "__main__":
  smokeTestModule()

@@ -1245,222 +1245,144 @@
 elf.
+__
 run
-NodeJ
+Proces
 s(argv,
- pipe=True))%0A%0A%0A  def runNodeJs(self, argv=None, pipe=False):%0A    self.setNodePath()%0A    #node_command = 'node'%0A    if not argv:%0A      argv = sys.argv%5B1:%5D%0A    argv = %5Bself.context.node_command%5D + argv
+%0A                                                                   pipe=True))%0A%0A  def __runProcess(self, argv, pipe):
 %0A   
@@ -1594,16 +1594,243 @@
 wait()%0A%0A
+  def runNodeJs(self, argv=None, pipe=False):%0A    self.setNodePath()%0A    #node_command = 'node'%0A    if not argv:%0A      argv = sys.argv%5B1:%5D%0A    argv = %5Bself.context.node_command%5D + argv%0A    return self.__runProcess(argv, pipe)%0A%0A
 def smok

Fix jasmine command bug