!******************************************************************************!
!  Purpose: Test the OS intrinsic
!   Author: J. Czechowski IV
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  call setDatDir
  invoke skipBecauseHeadless nxm.sys.lib.Shell.isHeadless()

  call osBasic
  call captureOutput

  if skipBecauseHeadless then
    info "^reg.main.name: skipping tests because HEADLESS"
  elseif (baseline and (ENV.OSTYPE EQS "UNIX")) then
    ! This tests Bug 1323
    info "This will print out 'pipe on' on the next line."
    echo "pipe on"
    pipe on
      noop ^{datDir}.apenny.prm _mypipe
      noop _mypipe ram:temp
    pipe off
  endif
endmacro

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
!  test basic option capability
!   create a test file, delete it, verify it no longer exists
!******************************************************************************!
procedure osBasic
  if ENV.OSTYPE EQS "UNIX" then
    fname LABEL=fullFileName AREA=^{AUX.WRITE} NAME="test_os_command" EXT=tmp OPT=AUX
    os touch ^fullFileName
    assert/text="OS: File creation command" "^fullFileName" fexists

    os rm ^fullFileName
    assert/text="OS: File removal command" "^fullFileName" nfexists
  else
    info "No ^reg.main.name tests for OS type = ^env.ostype"
  endif
return

!******************************************************************************!
! test capturing output of OS command for BUG 1357
procedure captureOutput
  set expStr "Test case for BUG 1357 to capture output of OS command"

  if ENV.OSNAME EQSS "Windows" then
    os/stdout=myres cmd /c echo ^expStr
  else
    os/stdout=myres echo ^expStr
  endif
  ! doing EQSS compare since echo adds a newline to the output
  assert/text="^reg.main.name: OS/STDOUT=<res> captures stdout of OS command" myres EQSS expStr

  if baseline isTrue and skipBecauseHeadless isFalse then
    import "JAVAHOME" javahome
    if javahome ndexists then import "JAVA_HOME" javahome
    file name java_cmd "(^{javahome},bin)" java

    !When running in a jar, we need to use the classpath that got us running - grab the java.class.path system property.
    if ^{env.NMROOT} EQS "classpath:" then
      invoke temppath java.lang.System.getProperty("java.class.path")
      os/stderr=errOutput ^java_cmd -cp ^temppath nxm.sys.test.echo2err ^expStr
    else
      !if not running from a jar, use the env.nmroot as the classpath
      os/stderr=errOutput ^java_cmd -cp ^env.nmroot nxm.sys.test.echo2err ^expStr
    endif
    ! doing EQSS compare since echo adds a newline to the output
    assert/text="^reg.main.name: OS/STDERR=<res> captures stderr of OS command" errOutput EQSS expStr
  endif
return
