Skip to content

Commit

Permalink
examples: lang: Test that each of the mcl examples compiles
Browse files Browse the repository at this point in the history
We let these rot, so fixup the issues and test them!
  • Loading branch information
purpleidea committed Jul 31, 2024
1 parent 6de7d8b commit d0ed004
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/lang/class-include.mcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class foo {
$wow = "this is wow" # gets captured

class bar($a, $b) { # a parameterized class
print "bar-"+ $a {
print ["bar-"+ $a,] {
msg => fmt.printf("inside bar: %s", $b + "; " + $wow),

Meta:autogroup => false,
Expand Down
7 changes: 5 additions & 2 deletions examples/lang/cron3.mcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
$home = getenv("HOME")
import "fmt"
import "sys"

$home = sys.getenv("HOME")

cron "purpleidea-oneshot" {
session => true,
Expand All @@ -11,4 +14,4 @@ svc "purpleidea-oneshot" {
}

# TODO: do we need a state => $const.res.file.state.exists specified here?
file printf("%s/.config/systemd/user/purpleidea-oneshot.service", $home) {}
file [fmt.printf("%s/.config/systemd/user/purpleidea-oneshot.service", $home),] {}
7 changes: 5 additions & 2 deletions examples/lang/cron4.mcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
$home = getenv("HOME")
import "fmt"
import "sys"

$home = sys.getenv("HOME")

cron "purpleidea-oneshot" {
state => "absent",
Expand All @@ -12,6 +15,6 @@ svc "purpleidea-oneshot" {
session => true,
}

file printf("%s/.config/systemd/user/purpleidea-oneshot.service", $home) {
file [fmt.printf("%s/.config/systemd/user/purpleidea-oneshot.service", $home),] {
state => $const.res.file.state.absent,
}
2 changes: 1 addition & 1 deletion examples/lang/env-bad.mcl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "fmt"
import "sys"

$x = sys.getenv("TEST", "321")
$x = sys.defaultenv("TEST", "321")

print "print1" {
msg => fmt.printf("TEST is: %s", $x),
Expand Down
6 changes: 3 additions & 3 deletions examples/lang/nspawn0.mcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pkg "debootstrap" {

$dir = $codename + "-" + "nspawn" # dir name
$cmd = fmt.printf("debootstrap --include=systemd-container %s %s %s", $codename, $dir, $baserepo)
exec "debootstrap-" + $codename {
exec ["debootstrap-" + $codename,] {
cwd => $rootpath,

shell => "/bin/bash",
Expand All @@ -24,8 +24,8 @@ exec "debootstrap-" + $codename {
Depend => Pkg["debootstrap"],
}

nspawn $dir {
nspawn [$dir,] {
state => "running",

Depend => Exec["debootstrap-" + $codename],
Depend => Exec[["debootstrap-" + $codename,]],
}
10 changes: 5 additions & 5 deletions examples/lang/polymorphic-lambda.mcl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import "fmt"

$add = func($x) {
func add($x) { # if this was a lambda, type unification would fail! (not allowed)
$x + $x
}

$num = 2
$out1 = $add($num) # 4
$out1 = add($num) # 4

print fmt.printf("%d + %d is %d", $num, $num, $out1) { # simple math
print [fmt.printf("%d + %d is %d", $num, $num, $out1),] { # simple math
Meta:autogroup => false,
}

$val = "hello"
$out2 = $add($val) # hellohello
$out2 = add($val) # hellohello

print fmt.printf("%s + %s is %s", $val, $val, $out2) { # simple concat
print [fmt.printf("%s + %s is %s", $val, $val, $out2),] { # simple concat
Meta:autogroup => false,
}
2 changes: 1 addition & 1 deletion examples/lang/sendrecv1.mcl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exec "exec0" {
kv "kv0" {
key => $ns,
#value => "two",
mapped = true,
#mapped => true,
}

Exec["exec0"].output -> Kv["kv0"].value
Expand Down
6 changes: 3 additions & 3 deletions examples/lang/states0.mcl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if $state == "one" or $state == "default" {
kv "${ns}" {
key => $ns,
value => "two",
mapped = true,
#mapped => true,
}
Exec["timer"] -> Kv["${ns}"]
}
Expand All @@ -34,7 +34,7 @@ if $state == "two" {
kv "${ns}" {
key => $ns,
value => "three",
mapped = true,
#mapped => true,
}
Exec["timer"] -> Kv["${ns}"]
}
Expand All @@ -51,7 +51,7 @@ if $state == "three" {
kv "${ns}" {
key => $ns,
value => "one",
mapped = true,
#mapped => true,
}
Exec["timer"] -> Kv["${ns}"]
}
11 changes: 10 additions & 1 deletion test/test-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ cd "${ROOT}"

failures=''

# TODO: test examples/lang/ directory to see if the .mcl files compile correctly
# Test examples/lang/ directory to see if the .mcl files compile correctly.

find_mcl_examples() {
git ls-files | grep '\.mcl$' | grep '^examples/lang/' | grep -v 'modules/'
}

for file in $(find_mcl_examples); do
#echo "mcl: $file"
run-test ./mgmt run --tmp-prefix lang --only-unify "$file" &> /dev/null || fail_test "could not compile: $file"
done

buildout='test-examples.out'
# make symlink to outside of package
Expand Down

0 comments on commit d0ed004

Please sign in to comment.