!******************************************************************************!
! This test verifies the command status code set by NeXtMidas.
!
! @bug      NXM-3780 NXM builds need to set a status code
! @since    NeXtMidas 4.1.2
! @author:  Zachary Matthews
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  if interAct then
    fname writeAuxDir AREA=^aux.write OPT=AUX
    set sourceTxtFile "nxm.sys.test.nmopts.nxm.cmdstatus.test.CmdStatusError.txt"
    set destTxtFile   "^{writeAuxDir}nmopts/nxm/cmdstatus/test/CmdStatusError.txt"
    set destJavaFile  "^{writeAuxDir}nmopts/nxm/cmdstatus/test/CmdStatusError.java"

    if ^{env.NMROOT} EQS "classpath:" then
      info "SKIPPING ^reg.main.name since running from a jar file"
    else
      file copy/m/f sourceTxtFile destTxtFile

      if env.osname eqss "Linux" then
        set checkCmd "echo $?"
      else
        set checkCmd "echo %nm_cmd_errorlevel%"
      endif

      set instructions "This test builds a test option tree and verifies the command status set by NeXtMidas.\n\n&
                        1. Open a new OS shell terminal.\n&
                        2. Navigate to $NMROOT.\n&
                        3. nmstart\n&
                        4. nm option cmdstatus ^{writeAuxDir}nmopts\n&
                        5. nm path add cmdstatus\n&
                        6. nm make all cmdstatus\n&
                        7. ^{checkCmd}\n&
                        => The command status should be 0.\n&
                        8. Click on 'Swap Files'.\n&
                        9. Repeat steps 6 and 7.\n&
                        => The command status should be 1.\n\n&
                        If all tests passed, please click PASS to pass this test.\n\n&
                        _________________________________________________________\n&
                        Note: \n&
                        ^{ENV.NMROOT}nxm/sys/test/nmopts/nxm/cmdstatus is automatically &
                        copied to ^{writeAuxDir}nmopts/nxm/cmdstatus"

      pipe on
        panel/msgid=MAIN/setup/controls=gc
        gcontrol label macro "Test MACRO"
        gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

        gcontrol text  idtext "Instructions" instructions 24 21 /title

        gcontrol button swapfiles "Swap Files" "Swap Files"
      pipe off

      ! Delete option tree directory in write aux
      new java.io.File("^{writeAuxDir}nmopts") testDir
      invoke testPath testDir.toPath()
      invoke ,, nxm.sys.lib.FileUtil.deleteDir(testPath)
    endif
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "SWAPFILES" then
    if destJavaFile NFEXISTS then
      file copy destTxtFile destJavaFile
    else
      erase destJavaFile
    endif
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return
