##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:       modules.00-init/%M%
#   Revision:       %I%
#   First Edition:  2017/05/06
#   Last Mod.:      %U%, %G%
#
#   Authors:        Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:    Testuite testsequence
#   Command:
#   Sub-Command:
#
#   Comment:    %C{
#           Check basic set of module commands on all shells
#       }C%
#
##############################################################################


set bad_cmd "$error_msgs: Invalid command 'foo'"

#
# The tests
#

if {$verbose > 0} {
   send_user "\tChecking basic set of module commands\n"
}

foreach shell $shell_list {
   testall_cmd_re "$shell" "module --version" {} "Modules Release \\S+ (\\S+)" 0
   testall_cmd "$shell" "module foo" "" "$bad_cmd\n$type_help" 1
   testall_cmd "$shell" "module use $contrib_modpath\; module purge\; module load null\; module list -t" "" "$cur_loaded\nnull" 0
   testall_cmd "$shell" "module use $contrib_modpath\; module purge\; module load null\; module unload null\; module list -t" "" "$no_loaded" 0
   testall_cmd "$shell" "module is-loaded unk" "" "" 1
   testall_cmd "$shell" "module use $contrib_modpath\; module load null\; module is-loaded null" "" "" 0
   testall_cmd "$shell" "module info-loaded unk" "" "" 0
   testall_cmd "$shell" "module use $contrib_modpath\; module load null\; module info-loaded null" "null" "" 0
}

# ensure tools used in complex shell alias definition are available
if {[find_bin grep] ne "" && [find_bin tr] ne "" && [find_bin awk] ne ""} {
   foreach shell $shell_list {
      if {$shell eq {csh} || $shell eq {tcsh}} {
         # alias evaluation leads to a syntax error if wa_277 is enabled
         testall_cmd_re "$shell" "module load $testsuite_modpath/alias/3.0\; testsuite; ts2" [expr {$install_wa277 eq {y} ? {} : "Release\nhttp://an.example.web\\?&param=one"}] ".*" [expr {$install_wa277 eq {y} ? 1 : 0}]
      } else {
         testall_cmd_re "$shell" "module load $testsuite_modpath/alias/3.0\; testsuite; ts2" "Release\nhttp://an.example.web\\?&param=one" ".*" 0
      }
   }
} elseif {$verbose > 0} {
   send_user "\tSkipping shell alias definition tests\n"
}

foreach shell $othlang_list {
   testall_cmd_re "$shell" "--version" {} "Modules Release \\S+ (\\S+)" 0
   testall_cmd_re "$shell" "foo" {} "$bad_cmd\n$type_help.*" 1
   testall_cmd "$shell" "use,$contrib_modpath:purge:load,null:list,-t" "" "$cur_loaded\nnull" 0
   testall_cmd "$shell" "use,$contrib_modpath:purge:load,null:unload,null:list,-t" "" "$no_loaded" 0
   switch -- $shell {
      cmake - r {
         testall_cmd_re "$shell" "is-loaded,unk" "" ".*" 1
      }
      default {
         testall_cmd_re "$shell" "is-loaded,unk" "" "" 1
      }
   }
   testall_cmd "$shell" "use,$contrib_modpath:load,null:is-loaded,null" "" "" 0
   switch -- $shell {
      r {
         testall_cmd "$shell" "info-loaded,unk" "\[1\] \"\"" {} 0
      }
      python - ruby {
         testall_cmd "$shell" "info-loaded,unk" "\n" "" 0
      }
      default {
         testall_cmd "$shell" "info-loaded,unk" "" "" 0
      }
   }

   switch -- $shell {
      {cmake} {
         set anstxt "-- null"
      }
      {r} {
         set anstxt "\[1\] \"null\""
      }
      default {
         set anstxt "null"
      }
   }
   testall_cmd "$shell" "use,$contrib_modpath:load,null:info-loaded,null" $anstxt "" 0
}


# test unload of previously loaded modulefile
if { $verbose > 0 } {
    send_user "\tSetup LOADEDMODULES = 'null'\n"
    send_user "\tSetup _LMFILES_ = '$contrib_modpath/null'\n"
}
set env(LOADEDMODULES) "null"
set env(_LMFILES_) "$contrib_modpath/null"

foreach shell $shell_list {
   testall_cmd "$shell" "module use $contrib_modpath\; module unload null\; module list -t" "" "$no_loaded" 0
}

foreach shell $othlang_list {
   testall_cmd "$shell" "use,$contrib_modpath:unload,null:list,-t" "" "$no_loaded" 0
}

# ensure advanced_version_spec is efficient on all shells
set loadunloadout "Loading advvers6/1.3\n  Loading requirement: advvers7/1.0\nUnloading advvers7/1.0\n  Unloading dependent: advvers6/1.3"
if {![is_config_locked implicit_default]} {
   foreach shell $shell_list {
      testall_cmd $shell "module use $testsuite_modpath.2\; module config implicit_default 1\; module config verbosity normal\; module config advanced_version_spec 1\; module load --auto advvers6@1.3\; module unload --auto advvers7@1.0" {} $loadunloadout 0
   }
   foreach shell $othlang_list {
      testall_cmd $shell "use,$testsuite_modpath.2:config,implicit_default,1:config,verbosity,normal:config,advanced_version_spec,1:load,--auto,advvers6@1.3:unload,--auto,advvers7@1.0" {} $loadunloadout 0
   }
} elseif {$verbose > 0} {
   send_user "\tSkipping advanced_version_spec tests as implicit_default config is locked\n"
}

# test modulepath and module names containing space
if {!$is_symlink_supported} {
    send_user "\tskipping tests over '$testsuite_modpath wspace' modulepath as symbolic links are not supported on filesystem\n"
} else {

set loadunloadout "Loading 'space yc/4'\nUnloading 'space yc/4'"
foreach shell $shell_list {
   testall_cmd $shell "module use \"$testsuite_modpath wspace\"\; module load -v \"space yc/4\"\; module unload -v \"space yc/4\"" {} $loadunloadout 0
}
foreach shell $othlang_list {
   testall_cmd $shell "use,$testsuite_modpath wspace:load,-v,space yc/4:unload,-v,space yc/4" {} $loadunloadout 0
}

}

#
#  Clean up variables used in this test case
#

reset_test_env

# vim:set tabstop=3 shiftwidth=3 expandtab autoindent:
