core/tsrc/fshell-basic-test.script
changeset 0 7f656887cf89
child 60 3ad902ef5222
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 #!fshell
       
     2 # fshell-basic-test.script
       
     3 # 
       
     4 # Copyright (c) 2010 Accenture. All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of the "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 # 
       
    10 # Initial Contributors:
       
    11 # Accenture - Initial contribution
       
    12 #
       
    13 
       
    14 # Syntax: fshell fshell-basic-test.script
       
    15 # Tests fundamental fshell primitives.
       
    16 # Start with comments with awkward things in > && < in; I suppose ' "
       
    17 
       
    18 var ARG_COUNT == 0 || error -6 "fshell-basic-test should not be called with any arguments"
       
    19 
       
    20 # Check the basics -  environment variables, export, var
       
    21 export TEST testing
       
    22 var TEST == testing || error
       
    23 
       
    24 export TEST
       
    25 var TEST not-defined || error
       
    26 
       
    27 # Test multistage pipelines
       
    28 # Pipeline operators are right-associative, ie "x || y && z" means "x || (y && z)" and *NOT* (x || y) && z
       
    29 export TEST 1 && var TEST == 2 && error
       
    30 export TEST 1 && var TEST == 2 || var TEST add 3
       
    31 var TEST == 4 || error
       
    32 export TEST 1 && var TEST == 2 || var TEST add 3 && var TEST == 4 || error # As above but all in one line
       
    33 var NONEXISTANT not-defined || var NONEXISTANT defined && error
       
    34 
       
    35 # Test that an env var can be expanded to a whole pipeline stage
       
    36 export TEST_CMD "export TEST_CMD_COMPLETED 1"
       
    37 $TEST_CMD
       
    38 var TEST_CMD_COMPLETED == 1 || error
       
    39 
       
    40 # Now we know that works, use it to give ourselves better error reporting
       
    41 # Apparently an env var can't expand to more than one pipeline stage, which is why we have to wrap this in fshell -e
       
    42 var KEEP_GOING not-defined && export Error 'fshell -e "echo ^"Test failed, env is:^" && env && error"'
       
    43 var KEEP_GOING defined && export Error 'error -39 "Test failure in $SCRIPT_NAME at line $SCRIPT_LINE"'
       
    44 
       
    45 # Check we've defined the things we're supposed to in a script
       
    46 var SCRIPT_PATH defined || $Error
       
    47 var SCRIPT_NAME == fshell-basic-test.script || $Error
       
    48 var SCRIPT_LINE == 48 || error -39 "SCRIPT_LINE isn't 48, it's $SCRIPT_LINE" # Remember to update this if you change the script above this point!
       
    49 var 0 == $SCRIPT_PATH$SCRIPT_NAME || $Error
       
    50 export DOLLAR_SCRIPT_LINE_VALUE $SCRIPT_LINE
       
    51 var DOLLAR_SCRIPT_LINE_VALUE == 50 || $Error
       
    52 
       
    53 # Test pipes, redirection
       
    54 
       
    55 echo -n "Testing pipes" | export -s TEST
       
    56 var TEST == "Testing pipes" || $Error
       
    57 
       
    58 # Check export -s can cope with complex stuff
       
    59 echo -n "" | export -s TEST
       
    60 var TEST == "" || $Error
       
    61 echo -n "a^r^nb" | export -s TEST
       
    62 var TEST == "a^r^nb" || $Error
       
    63 
       
    64 # Check echo's handling of newlines is correct
       
    65 echo --no-newline "Hello world!" | export -s TEST
       
    66 var TEST == "Hello world!" || $Error
       
    67 echo "Hello world!" | export -s TEST
       
    68 var TEST == "Hello world!^r^n" || $Error
       
    69 echo "Hello world!^r^n" | export -s TEST
       
    70 # If there's already a newline, echo shouldn't add another
       
    71 var TEST == "Hello world!^r^n" || $Error
       
    72 
       
    73 # Check that hashes, ampersands etc can be quoted ok
       
    74 export TEST "something ^x3e somewhere ^x26^x26 something else ^x23 with comment^x0d^x0a"
       
    75 echo "something > somewhere && something else # with comment^r^n" | export -s RESULT
       
    76 var RESULT == "$TEST" || $Error
       
    77 var TEST == "$RESULT" || $Error # Check it's a reflexive relationship
       
    78 echo something^ ^>^ somewhere^ ^&^&^ something^ else^ ^#^ with^ comment^r^n | export -s RESULT
       
    79 var RESULT == "$TEST" || $Error
       
    80 var TEST == "$RESULT" || $Error # Check it's a reflexive relationship
       
    81 export "#" "Let's define ^$# just for the fun of it"
       
    82 var "#" == "Let's define ^$# just for the fun of it" || $Error
       
    83 
       
    84 # Test hashes inside single quotes
       
    85 var '#' == 'Let^'s define $# just for the fun of it' || $Error # Balance ' for my struggling text editor syntax highlighter
       
    86 
       
    87 # Check that 2>&1 works as expected
       
    88 echo --stderr --no-newline Hello 2>&1 | export -s TEST
       
    89 var TEST == Hello || $Error
       
    90 
       
    91 echo "SHOULDN'T SEE THIS ON STDOUT (1)" > NUL
       
    92 echo "SHOULDN'T SEE THIS ON STDOUT (2)" > /dev/null
       
    93 
       
    94 echo --stderr "SHOULDN'T SEE THIS ON STDERR (1)" 2> NUL
       
    95 echo --stderr "SHOULDN'T SEE THIS ON STDERR (2)" 2> /dev/null
       
    96 
       
    97 exists /dev/null && $Error # Check we didn't actually create a file called /dev/null
       
    98 variant wins || exists NUL && $Error # Check we didn't actually create a file called NUL. Don't do this check on wins because apparently NUL gets passed through to the underlying Win32 file system, on which NUL is valid... it really shouldn't do that!
       
    99 
       
   100 # Check we can use &| to continue past an error
       
   101 echo -n "" &| export OK 1
       
   102 var OK defined || $Error
       
   103 export OK
       
   104 error -39 "Shouldn't ever see this!" 2>NUL &| export OK 1
       
   105 var OK defined || $Error
       
   106 export OK
       
   107 
       
   108 export FILE c:\fshell-basic-test.txt
       
   109 rm $FILE 2>/dev/null &| echo -n "" # I don't like using "&| echo" syntax to indicate don't care if it fails. Probably rm -f should be quiet like unix version
       
   110 echo -n "Testing file redirection" > c:\fshell-basic-test.txt
       
   111 
       
   112 # The redirect stdin operation doesn't get used much
       
   113 export -s FILECONTENTS < $FILE
       
   114 var FILECONTENTS == "Testing file redirection" || $Error
       
   115 
       
   116 # Check doing the same thing via a pipe-line also works
       
   117 cat -e utf-8 $FILE | export -s FILECONTENTS
       
   118 var FILECONTENTS == "Testing file redirection" || $Error
       
   119 
       
   120 rm $FILE # clean up
       
   121 
       
   122 # Test that source works, takes arguments ok, and inherits environment correctly
       
   123 source $SCRIPT_PATH\setenv.script SOMETHING 1234
       
   124 var SOMETHING == 1234 || $Error
       
   125 source $SCRIPT_PATH\addenv.script SOMETHING 4321
       
   126 var SOMETHING == 5555 || $Error
       
   127 
       
   128 source $SCRIPT_PATHsetenv.script SOMETHING # Check that the \ isn't required after the $SCRIPT_PATH
       
   129 var SOMETHING not-defined || $Error
       
   130 
       
   131 # Check that source hasn't nuked our vars
       
   132 var SCRIPT_NAME == fshell-basic-test.script || $Error
       
   133 var 0 == $SCRIPT_PATH$SCRIPT_NAME || $Error
       
   134 var ARG_COUNT defined || $Error
       
   135 var ARG_COUNT == 0 || $Error
       
   136 # And that none of the sources we just ran have polluted $1 etc with their arguments
       
   137 var 1 not-defined || $Error
       
   138 
       
   139 # Ambiguous variable expansion - appears to be done by longest prefix match
       
   140 export AA bb
       
   141 export AAAA bbbb
       
   142 export AAA bbb
       
   143 echo -n $AAAa | export -s RESULT
       
   144 var RESULT == bbba || $Error
       
   145 echo -n $AAAAa | export -s RESULT
       
   146 var RESULT == bbbba || $Error
       
   147 
       
   148 # Ambiguous *nested* variable expansion. Evil!
       
   149 export ANEST correct
       
   150 export INNER NEST
       
   151 echo -n $A$INNER | export -s RESULT 
       
   152 var RESULT == correct || $Error
       
   153 export A aaa
       
   154 #TODO with $A defined this no longer works - is this reasonable? I'm guessing the string is expanded left-to-right.
       
   155 #echo $A$INNER | export -s RESULT # I'd like this to expand to $ANEST then to "correct" but it expands to aaaNEST
       
   156 
       
   157 # Test that errors reported from scripts are reported with correct line numbers
       
   158 export EXPECTED_ERR "Error: Aborted ^"$SCRIPT_PATHprinterror.script^" at line 17 : KErrNotFound (-1)^r^n"
       
   159 
       
   160 fshell $SCRIPT_PATHprinterror.script 2>&1 | export -s PRINTERR
       
   161 var PRINTERR == "$EXPECTED_ERR" || $Error
       
   162 
       
   163 source $SCRIPT_PATHprinterror.script 2>&1 | export -s PRINTERR
       
   164 var PRINTERR == "$EXPECTED_ERR" || $Error