Skip to content

Commit e616e60

Browse files
authored
Use 'toit' instead of 'toit.run'. (#87)
1 parent e82ed1d commit e616e60

File tree

7 files changed

+124
-100
lines changed

7 files changed

+124
-100
lines changed

tests/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
file(GLOB TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*_test.toit" "*_test_slow.toit")
66

77
# Add windows exe extension.
8-
set(TOIT_EXEC "toit.run${CMAKE_EXECUTABLE_SUFFIX}" CACHE FILEPATH "The executable used to run the tests")
9-
set(TPKG_EXEC "toit.pkg${CMAKE_EXECUTABLE_SUFFIX}" CACHE FILEPATH "The executable used to install the packages")
8+
set(TOIT_EXEC "toit${CMAKE_EXECUTABLE_SUFFIX}" CACHE FILEPATH "The executable used to run the tests")
109
set(TEST_TIMEOUT 40 CACHE STRING "The maximal amount of time each test is allowed to run")
1110
set(SLOW_TEST_TIMEOUT 200 CACHE STRING "The maximal amount of time each slow test is allowed to run")
1211

1312
add_custom_target(
1413
"install-pkgs"
15-
COMMAND "${TPKG_EXEC}" install
14+
COMMAND "${TOIT_EXEC}" pkg install
1615
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
1716
)
1817

@@ -48,7 +47,7 @@ foreach(file ${TESTS})
4847

4948
add_test(
5049
NAME "${test_name}"
51-
COMMAND "${TOIT_EXEC}" "tests/${file}" ${TOIT_EXEC} $<TARGET_FILE:crash>
50+
COMMAND "${TOIT_EXEC}" run "tests/${file}" -- ${TOIT_EXEC} $<TARGET_FILE:crash>
5251
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
5352
)
5453

tests/block_stdin_test.toit

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,48 @@ import host.pipe
77
import host.os
88
import host.file
99

10+
import .utils
11+
1012
main args:
1113
if args.size < 1:
1214
print "Usage: block_std_test.toit <toit_exe>"
1315
exit 1
1416

15-
if not file.is-file "tests/block_stdout_child.toit":
16-
print "Cannot find toit file 'block_stdout_child.toit' in tests directory"
17-
exit 1
18-
1917
if not file.is-file "tests/block_stdin_child.toit":
2018
print "Cannot find toit file 'block_stdin_child.toit' in tests directory"
2119
exit 1
2220

2321
toit-exe := args[0]
2422

25-
// Try to run the toit executable.
26-
print "Trying to run $toit-exe"
27-
exception := catch: pipe.backticks toit-exe "--version"
28-
if exception:
29-
print "Running the given toit executable '$toit-exe' failed: $exception"
30-
exit 1
31-
32-
print "Managed to run $toit-exe"
33-
34-
["close", "read"].do: | action |
35-
subprocess := pipe.fork
36-
--create-stdin
37-
--create-stdout
38-
toit-exe
39-
[toit-exe, "tests/block_stdin_child.toit", action]
40-
41-
print "Started subprocess"
42-
43-
subprocess-stdin := subprocess.stdin
44-
subprocess-stdout := subprocess.stdout
45-
pid := subprocess.pid
46-
47-
line := subprocess-stdout.in.read
48-
expect-equals "Message through stdout." line.to-string
49-
print "$line.to-string"
50-
if action == "read":
51-
subprocess-stdin.out.write "There is an art to flying, or rather a knack.\n"
52-
else:
53-
subprocess-stdin.close
54-
55-
exit-value := subprocess.wait
56-
expect-equals 0
57-
pipe.exit-code exit-value
58-
expect-equals null
59-
pipe.exit-signal exit-value
60-
print "OK exit"
23+
check-toit-exe toit-exe
24+
25+
with-compiled --toit-exe=toit-exe "tests/block_stdin_child.toit": | compiled-path/string |
26+
// We use a compiled executable, so that we don't see compilation warnings of the child program.
27+
28+
["close", "read"].do: | action |
29+
subprocess := pipe.fork
30+
--create-stdin
31+
--create-stdout
32+
compiled-path
33+
[compiled-path, action]
34+
35+
print "Started subprocess"
36+
37+
subprocess-stdin := subprocess.stdin
38+
subprocess-stdout := subprocess.stdout
39+
pid := subprocess.pid
40+
41+
line := subprocess-stdout.in.read
42+
expect-equals "Message through stdout." line.to-string
43+
print "$line.to-string"
44+
if action == "read":
45+
subprocess-stdin.out.write "There is an art to flying, or rather a knack.\n"
46+
else:
47+
subprocess-stdin.close
48+
49+
exit-value := subprocess.wait
50+
expect-equals 0
51+
pipe.exit-code exit-value
52+
expect-equals null
53+
pipe.exit-signal exit-value
54+
print "OK exit"

tests/block_stdout_test.toit

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import host.os
88
import host.file
99
import semver
1010

11+
import .utils
12+
13+
tt [block]: block.call
1114
main args:
1215
if args.size < 1:
1316
print "Usage: block_std_test.toit <toit_exe>"
@@ -17,51 +20,47 @@ main args:
1720
print "Cannot find toit file 'block_stdout_child.toit' in tests directory"
1821
exit 1
1922

20-
if not file.is-file "tests/block_stdin_child.toit":
21-
print "Cannot find toit file 'block_stdin_child.toit' in tests directory"
22-
exit 1
23-
2423
toit-exe := args[0]
2524

26-
// Try to run the toit executable.
27-
exception := catch: pipe.backticks toit-exe "--version"
28-
if exception:
29-
print "Running the given toit executable '$toit-exe' failed: $exception"
30-
exit 1
25+
// check-toit-exe toit-exe
26+
27+
with-compiled --toit-exe=toit-exe "tests/block_stdout_child.toit": | compiled-path/string |
28+
// We use an executable, so that we don't see compilation warnings of the child program.
3129
32-
["close", "write"].do: | action |
33-
subprocess := pipe.fork
34-
--create-stdout
35-
--create-stderr
36-
toit-exe
37-
[toit-exe, "tests/block_stdout_child.toit", action]
30+
["close", "write"].do: | action |
31+
print "Running $compiled-path $action"
32+
subprocess := pipe.fork
33+
--create-stdout
34+
--create-stderr
35+
compiled-path
36+
[compiled-path, action]
3837

39-
subprocess-stdout := subprocess.stdout
40-
subprocess-stderr := subprocess.stderr
38+
subprocess-stdout := subprocess.stdout
39+
subprocess-stderr := subprocess.stderr
4140

42-
// Get the stderr first even though the subproces is blocking on stdout.
43-
print "Waiting for read of child stderr."
44-
line := subprocess-stderr.in.read
45-
// If this gets the wrong message then the buffer on stdout is too big and
46-
// we need to increase the size of the loop in block_stdout_child.toit.
47-
if action == "close":
48-
if line != null: print "<$line.to-string>"
49-
expect-equals null line
50-
print "Close woke up the task."
51-
else:
52-
expect-equals "Message through stderr." line.to-string
53-
print "$line.to-string"
54-
task::
55-
while read := subprocess-stdout.in.read:
56-
null
57-
if action != "close":
58-
line = subprocess-stderr.in.read
59-
expect-equals "Done with stdout." line.to-string
60-
print "$line.to-string"
41+
// Get the stderr first even though the subproces is blocking on stdout.
42+
print "Waiting for read of child stderr."
43+
line := subprocess-stderr.in.read
44+
// If this gets the wrong message then the buffer on stdout is too big and
45+
// we need to increase the size of the loop in block_stdout_child.toit.
46+
if action == "close":
47+
if line != null: print "<$line.to-string>"
48+
expect-equals null line
49+
print "Close woke up the task."
50+
else:
51+
expect-equals "Message through stderr." line.to-string
52+
print "$line.to-string"
53+
task::
54+
while read := subprocess-stdout.in.read:
55+
null
56+
if action != "close":
57+
line = subprocess-stderr.in.read
58+
expect-equals "Done with stdout." line.to-string
59+
print "$line.to-string"
6160

62-
exit-value := subprocess.wait
63-
expect-equals 0
64-
pipe.exit-code exit-value
65-
expect-equals null
66-
pipe.exit-signal exit-value
67-
print "OK exit"
61+
exit-value := subprocess.wait
62+
expect-equals 0
63+
pipe.exit-code exit-value
64+
expect-equals null
65+
pipe.exit-signal exit-value
66+
print "OK exit"

tests/env_test.toit

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import host.os
88
import host.file
99
import system show platform PLATFORM-WINDOWS
1010

11+
import .utils
12+
1113
main args:
1214
if args.size < 1:
1315
print "Usage: env_test.toit <toit_exe>"
@@ -19,28 +21,24 @@ main args:
1921

2022
toit-exe := args[0]
2123

22-
// Try to run the toit executable.
23-
exception := catch: pipe.backticks toit-exe "--version"
24-
if exception:
25-
print "Running the given toit executable '$toit-exe' failed: $exception"
26-
exit 1
24+
check-toit-exe toit-exe
2725

28-
pipe.system "$toit-exe tests/echo.toit FOO=\$FOO"
29-
pipe.system --environment={"FOO": 123} "$toit-exe tests/echo.toit FOO=\$FOO"
26+
pipe.system "$toit-exe run -- tests/echo.toit FOO=\$FOO"
27+
pipe.system --environment={"FOO": 123} "$toit-exe run -- tests/echo.toit FOO=\$FOO"
3028

3129
shell := platform == PLATFORM-WINDOWS ? ["cmd", "/S", "/C"] : ["sh", "-c"]
3230

3331
expect-equals "BAR=1.5"
3432
(pipe.backticks --environment={"BAR": 1.5} toit-exe "tests/echo.toit" "BAR=\$BAR").trim
3533
expect-equals "BAR="
36-
(pipe.backticks shell + ["$toit-exe tests/echo.toit BAR=\$BAR"]).trim
34+
(pipe.backticks shell + ["$toit-exe run -- tests/echo.toit BAR=\$BAR"]).trim
3735

3836
fd := pipe.from --environment={"FISH": "HORSE"} toit-exe "tests/echo.toit" "\$FISH"
3937
expect-equals "HORSE" fd.in.read.to-string.trim
4038

4139
user := os.env.get "USER"
4240
if user:
4341
expect-equals "$user"
44-
(pipe.backticks shell + ["$toit-exe tests/echo.toit \$USER"]).trim
42+
(pipe.backticks shell + ["$toit-exe run -- tests/echo.toit \$USER"]).trim
4543
user-fd := pipe.from --environment={"USER": null} toit-exe "tests/echo.toit" "\$USER"
4644
expect-equals "" user-fd.in.read.to-string.trim

tests/env_unicode_test.toit

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ external-test args:
3535

3636
toit-exe := args[0]
3737

38-
pipe.system "$toit-exe tests/echo.toit FOO=\$FOO"
39-
pipe.system --environment={"FOO": 123} "$toit-exe tests/echo.toit FOO=\$FOO"
38+
pipe.system "$toit-exe run -- tests/echo.toit FOO=\$FOO"
39+
pipe.system --environment={"FOO": 123} "$toit-exe run -- tests/echo.toit FOO=\$FOO"
4040

4141
expect-equals "BAR=1.5"
4242
(pipe.backticks --environment={"BAR": 1.5} toit-exe "tests/echo.toit" "BAR=\$BAR").trim
4343

4444
expect-equals "string with \" in it"
45-
(pipe.backticks toit-exe "tests/echo.toit" "string with \" in it").trim
45+
(pipe.backticks toit-exe "run" "--" "tests/echo.toit" "string with \" in it").trim
4646

4747
expect-equals "string with \\\" in it"
48-
(pipe.backticks toit-exe "tests/echo.toit" "string with \\\" in it").trim
48+
(pipe.backticks toit-exe "run" "--" "tests/echo.toit" "string with \\\" in it").trim

tests/pipe_test_slow.toit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ low-level-test toit-exe:
4040
process := pipe.fork
4141
--create-stdout
4242
toit-exe
43-
["ignored-0-argument", "tests/echo.toit", "horse"]
43+
["ignored-0-argument", "run", "--", "tests/echo.toit", "horse"]
4444
stdout := process.stdout
4545
expect-equals
4646
"horse"

tests/utils.toit

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (C) 2025 Toit contributors
2+
// Use of this source code is governed by a Zero-Clause BSD license that can
3+
// be found in the tests/TESTS_LICENSE file.
4+
5+
import host.directory
6+
import host.pipe
7+
8+
check-toit-exe toit-exe/string:
9+
// Try to run the toit executable.
10+
print "Trying to run $toit-exe"
11+
exception := catch: pipe.backticks toit-exe "--version"
12+
if exception:
13+
print "Running the given toit executable '$toit-exe' failed: $exception"
14+
exit 1
15+
16+
print "Managed to run $toit-exe"
17+
18+
19+
with-tmp-dir prefix="/tmp/test" [block]:
20+
dir := directory.mkdtemp prefix
21+
try:
22+
block.call dir
23+
finally:
24+
directory.rmdir --recursive --force dir
25+
26+
with-compiled --toit-exe/string source/string [block]:
27+
with-tmp-dir: | dir/string |
28+
compiled-path := "$dir/out.exe"
29+
30+
exit-value := pipe.run-program [toit-exe, "compile", "-o", compiled-path, source]
31+
if exit-value != 0:
32+
throw "Failed to create exe: $exit-value"
33+
34+
block.call compiled-path

0 commit comments

Comments
 (0)