From d0ed004b24ca76d65077133ab4b4bbd6d18faf76 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 31 Jul 2024 16:01:51 -0400 Subject: [PATCH] examples: lang: Test that each of the mcl examples compiles We let these rot, so fixup the issues and test them! --- examples/lang/class-include.mcl | 2 +- examples/lang/cron3.mcl | 7 +++++-- examples/lang/cron4.mcl | 7 +++++-- examples/lang/env-bad.mcl | 2 +- examples/lang/nspawn0.mcl | 6 +++--- examples/lang/polymorphic-lambda.mcl | 10 +++++----- examples/lang/sendrecv1.mcl | 2 +- examples/lang/states0.mcl | 6 +++--- test/test-examples.sh | 11 ++++++++++- 9 files changed, 34 insertions(+), 19 deletions(-) diff --git a/examples/lang/class-include.mcl b/examples/lang/class-include.mcl index f69a42350..05289ac3e 100644 --- a/examples/lang/class-include.mcl +++ b/examples/lang/class-include.mcl @@ -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, diff --git a/examples/lang/cron3.mcl b/examples/lang/cron3.mcl index c9b4db8b1..4c8305bba 100644 --- a/examples/lang/cron3.mcl +++ b/examples/lang/cron3.mcl @@ -1,4 +1,7 @@ -$home = getenv("HOME") +import "fmt" +import "sys" + +$home = sys.getenv("HOME") cron "purpleidea-oneshot" { session => true, @@ -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),] {} diff --git a/examples/lang/cron4.mcl b/examples/lang/cron4.mcl index aa2951b85..694348413 100644 --- a/examples/lang/cron4.mcl +++ b/examples/lang/cron4.mcl @@ -1,4 +1,7 @@ -$home = getenv("HOME") +import "fmt" +import "sys" + +$home = sys.getenv("HOME") cron "purpleidea-oneshot" { state => "absent", @@ -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, } diff --git a/examples/lang/env-bad.mcl b/examples/lang/env-bad.mcl index e4a2b2885..08e7303be 100644 --- a/examples/lang/env-bad.mcl +++ b/examples/lang/env-bad.mcl @@ -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), diff --git a/examples/lang/nspawn0.mcl b/examples/lang/nspawn0.mcl index f022f5f87..fb096c1ec 100644 --- a/examples/lang/nspawn0.mcl +++ b/examples/lang/nspawn0.mcl @@ -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", @@ -24,8 +24,8 @@ exec "debootstrap-" + $codename { Depend => Pkg["debootstrap"], } -nspawn $dir { +nspawn [$dir,] { state => "running", - Depend => Exec["debootstrap-" + $codename], + Depend => Exec[["debootstrap-" + $codename,]], } diff --git a/examples/lang/polymorphic-lambda.mcl b/examples/lang/polymorphic-lambda.mcl index 796f0f245..05580b208 100644 --- a/examples/lang/polymorphic-lambda.mcl +++ b/examples/lang/polymorphic-lambda.mcl @@ -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, } diff --git a/examples/lang/sendrecv1.mcl b/examples/lang/sendrecv1.mcl index 9dee1b4c9..695b8d5a7 100644 --- a/examples/lang/sendrecv1.mcl +++ b/examples/lang/sendrecv1.mcl @@ -13,7 +13,7 @@ exec "exec0" { kv "kv0" { key => $ns, #value => "two", - mapped = true, + #mapped => true, } Exec["exec0"].output -> Kv["kv0"].value diff --git a/examples/lang/states0.mcl b/examples/lang/states0.mcl index 35dddd6d3..df56807b5 100644 --- a/examples/lang/states0.mcl +++ b/examples/lang/states0.mcl @@ -17,7 +17,7 @@ if $state == "one" or $state == "default" { kv "${ns}" { key => $ns, value => "two", - mapped = true, + #mapped => true, } Exec["timer"] -> Kv["${ns}"] } @@ -34,7 +34,7 @@ if $state == "two" { kv "${ns}" { key => $ns, value => "three", - mapped = true, + #mapped => true, } Exec["timer"] -> Kv["${ns}"] } @@ -51,7 +51,7 @@ if $state == "three" { kv "${ns}" { key => $ns, value => "one", - mapped = true, + #mapped => true, } Exec["timer"] -> Kv["${ns}"] } diff --git a/test/test-examples.sh b/test/test-examples.sh index 5f18b6571..36d03b605 100755 --- a/test/test-examples.sh +++ b/test/test-examples.sh @@ -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