• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/38_system

  1 The `system()` function executes the given shell command or raw command
  2 vector, optionally terminating the spawned process after the specified
  3 timeout.
  4 
  5 Throws an exception if a timeout is specified but not a valid positive
  6 integer value.
  7 
  8 Throws an exception if the command argument is neither an array nor a
  9 string value.
 10 
 11 Throws an exception if an empty command vector is given.
 12 
 13 Returns the exit code of the invoked process.
 14 
 15 -- Testcase --
 16 {%
 17         // When passing the command as string, `/bin/sh -c` is invoked with
 18         // the given command string as second argument
 19         system('x=1; echo $((x + x))');
 20 
 21         // When passing the command as array, the first value is taken as
 22         // executable to invoke and any further item as argument to the
 23         // invoked program. Internally `execvp()` is used, which means that
 24         // the executable path may be relative in which case it is looked
 25         // up in the directories specified by `$PATH`. Any array items are
 26         // implicitly stringified.
 27         system([ '/bin/sh', TESTFILES_PATH + '/testscripts/hello.sh', true, 0x42, 123.456000, { some: "dict" } ]);
 28 
 29         // By specifying a timeout, maximum execution time is limited to
 30         // that many milliseconds. If the program does not finish before the
 31         // timeout occurs, it is forcibly terminated with SIGKILL.
 32         system([ '/bin/sh', TESTFILES_PATH + '/testscripts/sleep.sh' ], 100);
 33 
 34         // The return value of system() is the exit code of the invoked program.
 35         let rc = system([ '/bin/sh', TESTFILES_PATH + '/testscripts/exit.sh' ]);
 36 
 37         printf("Return value is %d\n", rc);
 38 %}
 39 -- End --
 40 
 41 -- File testscripts/hello.sh --
 42 #!/bin/sh
 43 
 44 echo "This is our test program running!"
 45 echo "My arguments are:"
 46 
 47 for arg in "$@"; do
 48         echo "<$arg>"
 49 done
 50 -- End --
 51 
 52 -- File testscripts/sleep.sh --
 53 #!/bin/sh
 54 
 55 echo "I'll sleep for 10s now..."
 56 sleep 10
 57 echo "I am done sleeping."
 58 -- End --
 59 
 60 -- File testscripts/exit.sh --
 61 #!/bin/sh
 62 
 63 echo "I'll exit with code 5 now."
 64 exit 5
 65 -- End --
 66 
 67 -- Expect stdout --
 68 2
 69 This is our test program running!
 70 My arguments are:
 71 <true>
 72 <66>
 73 <123.456>
 74 <{ "some": "dict" }>
 75 I'll sleep for 10s now...
 76 I'll exit with code 5 now.
 77 Return value is 5
 78 -- End --
 79 
 80 
 81 Passing an invalid command value throws an exception.
 82 
 83 -- Testcase --
 84 {%
 85         system(true);
 86 %}
 87 -- End --
 88 
 89 -- Expect stderr --
 90 Type error: Passed command is neither string nor array
 91 In line 2, byte 13:
 92 
 93  `    system(true);`
 94   Near here -----^
 95 
 96 
 97 -- End --
 98 
 99 -- Testcase --
100 {%
101         system([]);
102 %}
103 -- End --
104 
105 -- Expect stderr --
106 Type error: Passed command array is empty
107 In line 2, byte 11:
108 
109  `    system([]);`
110   Near here ---^
111 
112 
113 -- End --
114 
115 
116 Passing an invalid timeout throws an exception.
117 
118 -- Testcase --
119 {%
120         system("exit 0", "invalid")
121 %}
122 -- End --
123 
124 -- Expect stderr --
125 Type error: Invalid timeout specified
126 In line 2, byte 28:
127 
128  `    system("exit 0", "invalid")`
129   Near here --------------------^
130 
131 
132 -- End --
133 
134 -- Testcase --
135 {%
136         system("exit 0", -100)
137 %}
138 -- End --
139 
140 -- Expect stderr --
141 Type error: Invalid timeout specified
142 In line 2, byte 23:
143 
144  `    system("exit 0", -100)`
145   Near here ---------------^
146 
147 
148 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt