Skip to content

Commit 257284f

Browse files
committed
test untested env functions
1 parent 6a9e02e commit 257284f

File tree

5 files changed

+125
-5
lines changed

5 files changed

+125
-5
lines changed

ci/expect_scripts/env.exp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/expect
2+
3+
# uncomment line below for debugging
4+
# exp_internal 1
5+
6+
set timeout 7
7+
8+
source ./ci/expect_scripts/shared-code.exp
9+
10+
spawn $env(TESTS_DIR)env
11+
12+
expect "Testing Env module functions..." {
13+
expect -re "Testing Env.cwd!:" {
14+
# Match cwd path that has ArbitraryBytes with non-empty list of integers
15+
# Use curly braces for the regex pattern to avoid Tcl interpretation of [ and ]
16+
expect -re {cwd: \(@InternalPath \(ArbitraryBytes \[\d+(, \d+)*\]\)\)} {
17+
18+
expect -re "Testing Env.exe_path!:" {
19+
# Match exe_path with any valid format
20+
expect -re {exe_path: \(@InternalPath \(.*\[\d+(, \d+)*\].*\)\)} {
21+
22+
expect -re "Testing Env.platform!:" {
23+
# Match platform info with any arch and OS
24+
# Literal braces in regex need to be escaped: \{ and \}
25+
expect -re {Current platform:\{arch: \w+, os: \w+\}} {
26+
27+
expect -re "Testing Env.dict!:" {
28+
# Match environment variables count as non-zero number
29+
expect -re {Environment variables count: (\d+)} {
30+
set env_count $expect_out(1,string)
31+
if {$env_count == 0} {
32+
puts stderr "\nExpect script failed: environment variable count is zero."
33+
exit 1
34+
}
35+
36+
# Match sample environment variables with non-empty strings
37+
# Literal brackets [], parentheses (), and quotes "" need escaping or careful handling
38+
expect -re {Sample environment variables:\[\("\w+", ".*"\)(, \("\w+", ".*"\))*\]} {
39+
40+
expect -re "Testing Env.set_cwd!:" {
41+
# Match changed directory path with non-empty list of integers
42+
expect -re {Changed current directory to: \(@InternalPath \(ArbitraryBytes \[\d+(, \d+)*\]\)\)} {
43+
44+
expect "All tests executed!" {
45+
expect eof {
46+
check_exit_and_segfault
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}
61+
62+
puts stderr "\nExpect script failed: output was different from expected value."
63+
exit 1

flake.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
inputs = {
55

6-
roc.url = "github:roc-lang/roc";
6+
# TODO change this back once that commit is merged
7+
roc.url = "github:roc-lang/roc/2e3a32d218f5404f4451cfe9997eaa95e75486cb";
78

89
nixpkgs.follows = "roc/nixpkgs";
910

tests/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sqlite
22
utc
3-
url
3+
url
4+
env

tests/env.roc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
app [main!] { pf: platform "../platform/main.roc" }
2+
3+
import pf.Stdout
4+
import pf.Env
5+
import pf.Path
6+
import pf.Arg exposing [Arg]
7+
8+
main! : List Arg => Result {} _
9+
main! = |_args|
10+
Stdout.line!("Testing Env module functions...")?
11+
12+
Stdout.line!("\nTesting Env.cwd!:")?
13+
cwd = Env.cwd!({})?
14+
Stdout.line!("cwd: ${Inspect.to_str(cwd)}")?
15+
16+
Stdout.line!("\nTesting Env.exe_path!:")?
17+
exe_path = Env.exe_path!({})?
18+
Stdout.line!("exe_path: ${Inspect.to_str(exe_path)}")?
19+
20+
# Test Env.platform!
21+
Stdout.line!("\nTesting Env.platform!:")?
22+
platform = Env.platform!({})
23+
Stdout.line!("Current platform:${Inspect.to_str(platform)}")?
24+
25+
# Test Env.dict!
26+
Stdout.line!("\nTesting Env.dict!:")?
27+
env_vars = Env.dict!({})
28+
var_count = Dict.len(env_vars)
29+
Stdout.line!("Environment variables count: ${Num.to_str(var_count)}")?
30+
31+
some_env_vars = Dict.to_list(env_vars) |> List.take_first(3)
32+
Stdout.line!("Sample environment variables:${Inspect.to_str(some_env_vars)}")?
33+
34+
# Test Env.set_cwd!
35+
Stdout.line!("\nTesting Env.set_cwd!:")?
36+
37+
# First get the current directory to restore it later
38+
original_dir = Env.cwd!({})?
39+
ls_list = Path.list_dir!(original_dir)?
40+
41+
dir_list =
42+
ls_list
43+
|> List.keep_if_try!(|path| Path.is_dir!(path))?
44+
45+
first_dir =
46+
List.first(dir_list)?
47+
48+
Env.set_cwd!(first_dir)?
49+
new_cwd = Env.cwd!({})?
50+
Stdout.line!("Changed current directory to: ${Inspect.to_str(new_cwd)}")?
51+
52+
Stdout.line!("\nAll tests executed!")?
53+
54+
Ok({})

0 commit comments

Comments
 (0)