plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


abccf58 — Peter John 2026-11-10T11:09:15+05:30
CLI tests: scratch folders and served children are cleaned up on every path
compiler/cli/tests/cli/args_clock_test.rs CHANGED
@@ -47,8 +47,7 @@ fn args_after_dashdash_several_with_spaces() {
47
47
 
48
48
  #[test]
49
49
  fn args_built_exe() {
50
- let dir = std::env::temp_dir().join(format!("plum_args_exe_{}", std::process::id()));
50
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_args_exe_{}", std::process::id())));
51
- std::fs::create_dir_all(&dir).unwrap();
52
51
  let exe = dir.join("args_exe");
53
52
  let out = Command::new(plumBin())
54
53
  .current_dir(root())
@@ -64,8 +63,7 @@ fn args_built_exe() {
64
63
 
65
64
  #[test]
66
65
  fn now_micros_monotonic() {
67
- let dir = std::env::temp_dir().join(format!("plum_clock_{}", std::process::id()));
66
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_clock_{}", std::process::id())));
68
- std::fs::create_dir_all(&dir).unwrap();
69
67
  let file = dir.join("main.plum");
70
68
  let src = "import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\n\nfun main(io: Io) =\n t0 := io.nowMicros()\n t1 := io.nowMicros()\n io.printLn(\"{t1 >= t0} {t1 - t0 < 1000.0} {t0 > 1600000000000000.0}\")\n";
71
69
  std::fs::write(&file, src).unwrap();
compiler/cli/tests/cli/assert_once_test.rs CHANGED
@@ -15,8 +15,7 @@ fn plumBin() -> std::path::PathBuf {
15
15
  /// report the very values it compared.
16
16
  #[test]
17
17
  fn assert_operands_evaluated_once() {
18
- let dir = std::env::temp_dir().join(format!("plum_assert_once_{}", std::process::id()));
18
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_assert_once_{}", std::process::id())));
19
- std::fs::create_dir_all(&dir).unwrap();
20
19
  let src = dir.join("once.plum");
21
20
  std::fs::write(
22
21
  &src,
compiler/cli/tests/cli/common.rs CHANGED
@@ -44,6 +44,25 @@ impl Scratch {
44
44
  }
45
45
  }
46
46
 
47
+ impl std::ops::Deref for Scratch {
48
+ type Target = Path;
49
+ fn deref(&self) -> &Path {
50
+ &self.0
51
+ }
52
+ }
53
+
54
+ impl AsRef<Path> for Scratch {
55
+ fn as_ref(&self) -> &Path {
56
+ &self.0
57
+ }
58
+ }
59
+
60
+ impl AsRef<std::ffi::OsStr> for Scratch {
61
+ fn as_ref(&self) -> &std::ffi::OsStr {
62
+ self.0.as_os_str()
63
+ }
64
+ }
65
+
47
66
  impl Drop for Scratch {
48
67
  fn drop(&mut self) {
49
68
  let _ = std::fs::remove_dir_all(&self.0);
compiler/cli/tests/cli/compile_tests.rs CHANGED
@@ -15,9 +15,8 @@ fn plumBin() -> std::path::PathBuf {
15
15
  /// (so parallel test binaries don't collide) — these tests write their own
16
16
  /// minimal fixture files here rather than depending on anything checked into
17
17
  /// the repo, so they stay self-contained regardless of what `test/` holds.
18
- fn freshTempDir(name: &str) -> std::path::PathBuf {
18
+ fn freshTempDir(name: &str) -> crate::common::Scratch {
19
- let dir = std::env::temp_dir().join(format!("plum_{}_{}", name, std::process::id()));
19
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_{}_{}", name, std::process::id())));
20
- std::fs::create_dir_all(&dir).expect("failed to create temp dir");
21
20
  dir
22
21
  }
23
22
 
compiler/cli/tests/cli/crash_test.rs CHANGED
@@ -13,8 +13,7 @@ fn plumBin() -> std::path::PathBuf {
13
13
 
14
14
  #[test]
15
15
  fn crash_prints_message() {
16
- let dir = std::env::temp_dir().join(format!("plum_crash_{}", std::process::id()));
16
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_crash_{}", std::process::id())));
17
- std::fs::create_dir_all(&dir).unwrap();
18
17
  let src = dir.join("crash.plum");
19
18
  std::fs::write(&src, "import std/Str\n\nfun main() =\n crash \"boom\"\n").unwrap();
20
19
  let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
@@ -36,8 +35,7 @@ fn crash_prints_message() {
36
35
 
37
36
  #[test]
38
37
  fn int_literal_overflow_error_has_position() {
39
- let dir = std::env::temp_dir().join(format!("plum_litpos_{}", std::process::id()));
38
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_litpos_{}", std::process::id())));
40
- std::fs::create_dir_all(&dir).unwrap();
41
39
  let src = dir.join("lit.plum");
42
40
  std::fs::write(
43
41
  &src,
@@ -66,8 +64,7 @@ fn int_literal_overflow_error_has_position() {
66
64
 
67
65
  #[test]
68
66
  fn crash_in_a_loop_names_the_function_not_its_lifted_loop() {
69
- let dir = std::env::temp_dir().join(format!("plum_crash_loop_{}", std::process::id()));
67
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_crash_loop_{}", std::process::id())));
70
- std::fs::create_dir_all(&dir).unwrap();
71
68
  let src = dir.join("crash_loop.plum");
72
69
  std::fs::write(
73
70
  &src,
compiler/cli/tests/cli/explain_layout_test.rs CHANGED
@@ -14,8 +14,7 @@ fn plumBin() -> std::path::PathBuf {
14
14
  /// `--explain` prints each struct's layout as decided by `plum_core::layout`.
15
15
  #[test]
16
16
  fn explainPrintsStructLayouts() {
17
- let dir = std::env::temp_dir().join(format!("plum_explain_layout_{}", std::process::id()));
17
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_explain_layout_{}", std::process::id())));
18
- std::fs::create_dir_all(&dir).unwrap();
19
18
  let src = dir.join("layouts.plum");
20
19
  std::fs::write(
21
20
  &src,
compiler/cli/tests/cli/fusion_agree_test.rs CHANGED
@@ -79,8 +79,7 @@ fn closureCopiesInTailPositionAgree() {
79
79
  ("range_find_empty", "fun big(n: Int) -> Option[Int] = 0.until(n).find(|x| x > 1)\n\nfun main() -> Int = big(0).unwrapOr(-1)\n", "-1"),
80
80
  ];
81
81
  let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap();
82
- let dir = std::env::temp_dir().join(format!("plum_tail_copies_{}", std::process::id()));
82
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_tail_copies_{}", std::process::id())));
83
- std::fs::create_dir_all(&dir).unwrap();
84
83
  let mut failures = Vec::new();
85
84
  for (name, program, want) in cases {
86
85
  let src = dir.join(format!("{name}.plum"));
compiler/cli/tests/cli/game_entry_test.rs CHANGED
@@ -16,8 +16,7 @@ fn plumBin() -> std::path::PathBuf {
16
16
 
17
17
  #[test]
18
18
  fn aStructWithoutUpdateAndDrawIsNotAGame() {
19
- let dir = std::env::temp_dir().join(format!("plum_gameentry_{}", std::process::id()));
19
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_gameentry_{}", std::process::id())));
20
- std::fs::create_dir_all(&dir).unwrap();
21
20
  let src = dir.join("app.plum");
22
21
  std::fs::write(
23
22
  &src,
@@ -44,8 +43,7 @@ fn aGameThatCrashesInAFrameSaysSoOnThePage() {
44
43
  }
45
44
 
46
45
  fn compileSource(name: &str, text: &str) -> (bool, String) {
47
- let dir = std::env::temp_dir().join(format!("plum_gameentry_{}_{name}", std::process::id()));
46
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_gameentry_{}_{name}", std::process::id())));
48
- std::fs::create_dir_all(&dir).unwrap();
49
47
  let src = dir.join("app.plum");
50
48
  std::fs::write(&src, text).unwrap();
51
49
  let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap();
@@ -102,8 +100,7 @@ fn theOldUpdateAndDrawShapeNamesGameStart() {
102
100
  #[test]
103
101
  fn aTypeWithOnlyUpdateOrDrawIsAProgram() {
104
102
  let src = format!("{HEAD} fun draw(self, io: Io) =\n io.printLn(\"x\")\n\nfun main(io: Io) -> Counter =\n Counter(n: 0)\n");
105
- let dir = std::env::temp_dir().join(format!("plum_gameentry_{}_onlyDraw", std::process::id()));
103
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_gameentry_{}_onlyDraw", std::process::id())));
106
- std::fs::create_dir_all(&dir).unwrap();
107
104
  let path = dir.join("app.plum");
108
105
  std::fs::write(&path, &src).unwrap();
109
106
  let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap();
compiler/cli/tests/cli/game_web_test.rs CHANGED
@@ -20,8 +20,8 @@ fn root() -> std::path::PathBuf {
20
20
  }
21
21
 
22
22
  /// Stand-in asset files: the build copies and serves them without decoding.
23
- fn makeAssets(name: &str) -> (std::path::PathBuf, std::path::PathBuf) {
23
+ fn makeAssets(name: &str) -> (crate::common::Scratch, std::path::PathBuf) {
24
- let scratch = std::env::temp_dir().join(format!("plum_game_web_{}_{name}", std::process::id()));
24
+ let scratch = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_game_web_{}_{name}", std::process::id())));
25
25
  let assets = scratch.join("assets");
26
26
  std::fs::create_dir_all(assets.join("sub")).unwrap();
27
27
  std::fs::write(assets.join("a.png"), b"x").unwrap();
@@ -93,22 +93,8 @@ fn get(port: u16, path: &str) -> (String, String, Vec<u8>) {
93
93
  #[test]
94
94
  fn serveStartsAndAnswersWithTheRightContentTypes() {
95
95
  let (scratch, assets) = makeAssets("serve");
96
- let mut child = Command::new(plumBin())
97
- .current_dir(root())
98
- .args(["run", FIXTURE, "--no-open", "--port", "0", "--assets"])
96
+ let served = serve(std::path::Path::new(FIXTURE), &["--assets", assets.to_str().unwrap()]);
99
- .arg(&assets)
100
- .stdout(Stdio::piped())
101
- .stderr(Stdio::null())
102
- .spawn()
103
- .expect("failed to run plum");
104
- let mut line = String::new();
105
- BufReader::new(child.stdout.take().unwrap()).read_line(&mut line).unwrap();
106
- let port: u16 = line
97
+ let port = served.port;
107
- .split("127.0.0.1:")
108
- .nth(1)
109
- .and_then(|r| r.split('/').next())
110
- .and_then(|p| p.parse().ok())
111
- .unwrap_or_else(|| panic!("no url in `{line}`"));
112
98
 
113
99
  let (status, ctype, body) = get(port, "/");
114
100
  assert!(status.contains("200"), "{status}");
@@ -129,10 +115,8 @@ fn serveStartsAndAnswersWithTheRightContentTypes() {
129
115
  assert!(get(port, "/nope").0.contains("404"));
130
116
  assert!(get(port, "/assets/../a.png").0.contains("404"));
131
117
  assert!(get(port, "/assets/%2e%2e/a.png").0.contains("404"));
132
-
118
+ drop(served);
133
- child.kill().unwrap();
119
+ drop(scratch);
134
- child.wait().unwrap();
135
- let _ = std::fs::remove_dir_all(&scratch);
136
120
  }
137
121
 
138
122
  #[test]
@@ -160,8 +144,7 @@ fn removedGameCommandIsRejected() {
160
144
 
161
145
  #[test]
162
146
  fn oldInitEntryIsRejectedWithAPointer() {
163
- let scratch = std::env::temp_dir().join(format!("plum_game_web_{}_oldinit", std::process::id()));
147
+ let scratch = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_game_web_{}_oldinit", std::process::id())));
164
- std::fs::create_dir_all(&scratch).unwrap();
165
148
  let file = scratch.join("old.plum");
166
149
  std::fs::write(
167
150
  &file,
@@ -179,18 +162,8 @@ fn oldInitEntryIsRejectedWithAPointer() {
179
162
 
180
163
  #[test]
181
164
  fn mainGameServes() {
182
- let mut child = Command::new(plumBin())
165
+ let served = serve(std::path::Path::new(MAIN_GAME), &[]);
183
- .current_dir(root())
184
- .args(["run", MAIN_GAME, "--no-open", "--port", "0"])
185
- .stdout(Stdio::piped())
186
- .stderr(Stdio::null())
166
+ assert!(served.port > 0);
187
- .spawn()
188
- .expect("failed to run plum");
189
- let mut line = String::new();
190
- BufReader::new(child.stdout.take().unwrap()).read_line(&mut line).unwrap();
191
- child.kill().unwrap();
192
- child.wait().unwrap();
193
- assert!(line.starts_with("serving http://127.0.0.1:"), "{line}");
194
167
  }
195
168
 
196
169
  #[test]
@@ -209,21 +182,15 @@ fn gameOptionsAreRejectedForACliProgram() {
209
182
 
210
183
  // ---- hot reload (`plum run` watches) ----
211
184
 
212
- /// A running `plum run` that is killed by PID when dropped, on every path.
185
+ /// A running `plum run`, killed by PID when dropped, on every path.
213
186
  struct Served {
187
+ #[allow(dead_code)]
214
- child: std::process::Child,
188
+ child: crate::common::KillOnDrop,
215
189
  port: u16,
216
190
  }
217
191
 
218
- impl Drop for Served {
219
- fn drop(&mut self) {
220
- let _ = self.child.kill();
221
- let _ = self.child.wait();
222
- }
223
- }
224
-
225
192
  fn serve(game: &std::path::Path, extra: &[&str]) -> Served {
226
- let mut child = Command::new(plumBin())
193
+ let child = Command::new(plumBin())
227
194
  .current_dir(root())
228
195
  .arg("run")
229
196
  .arg(game)
@@ -233,8 +200,10 @@ fn serve(game: &std::path::Path, extra: &[&str]) -> Served {
233
200
  .stderr(Stdio::null())
234
201
  .spawn()
235
202
  .expect("failed to run plum");
203
+ let mut child = crate::common::KillOnDrop(child);
236
204
  let mut line = String::new();
237
- BufReader::new(child.stdout.take().unwrap()).read_line(&mut line).unwrap();
205
+ let _ = BufReader::new(child.0.stdout.take().unwrap()).read_line(&mut line);
206
+ assert!(line.starts_with("serving http://127.0.0.1:"), "{line}");
238
207
  let port = line
239
208
  .split("127.0.0.1:")
240
209
  .nth(1)
@@ -242,16 +211,12 @@ fn serve(game: &std::path::Path, extra: &[&str]) -> Served {
242
211
  .and_then(|p| p.parse().ok());
243
212
  match port {
244
213
  Some(port) => Served { child, port },
245
- None => {
246
- let _ = child.kill();
247
- let _ = child.wait();
248
- panic!("no url in `{line}`");
214
+ None => panic!("no url in `{line}`"),
249
- }
250
215
  }
251
216
  }
252
217
 
253
218
  /// A scratch folder holding a copy of the `main` game fixture, and its assets folder.
254
- fn tempGame(name: &str) -> (std::path::PathBuf, std::path::PathBuf, std::path::PathBuf) {
219
+ fn tempGame(name: &str) -> (crate::common::Scratch, std::path::PathBuf, std::path::PathBuf) {
255
220
  let (scratch, assets) = makeAssets(name);
256
221
  let game = scratch.join("game.plum");
257
222
  std::fs::copy(root().join(MAIN_GAME), &game).unwrap();
compiler/cli/tests/cli/host_stub_test.rs CHANGED
@@ -12,8 +12,7 @@ fn plumBin() -> std::path::PathBuf {
12
12
  }
13
13
 
14
14
  fn runPlumTest(name: &str, source: &str) -> (bool, String) {
15
- let dir = std::env::temp_dir().join(format!("plum_stub_{}_{}", name, std::process::id()));
15
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_stub_{}_{}", name, std::process::id())));
16
- std::fs::create_dir_all(&dir).unwrap();
17
16
  let src = dir.join("game.plum");
18
17
  std::fs::write(&src, source).unwrap();
19
18
  let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
compiler/cli/tests/cli/main_result_test.rs CHANGED
@@ -13,8 +13,7 @@ fn plumBin() -> std::path::PathBuf {
13
13
  }
14
14
 
15
15
  fn runSource(name: &str, body: &str) -> Output {
16
- let dir = std::env::temp_dir().join(format!("plum_result_{}_{name}", std::process::id()));
16
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_result_{}_{name}", std::process::id())));
17
- std::fs::create_dir_all(&dir).unwrap();
18
17
  let src = dir.join("app.plum");
19
18
  let text = format!(
20
19
  "module app\nimport std/Result\nimport std/Int\nimport std/Str\nimport std/Io\n\nenum AppError =\n | Bad\n | Worse(code: Int)\n\n{body}"
compiler/cli/tests/cli/no_fbip_test.rs CHANGED
@@ -30,8 +30,7 @@ fn run(src: &std::path::Path, flags: &[&str]) -> (String, String) {
30
30
  /// and the program prints the same either way.
31
31
  #[test]
32
32
  fn noFbipTurnsOffUniqueSpreads() {
33
- let dir = std::env::temp_dir().join(format!("plum_no_fbip_{}", std::process::id()));
33
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_no_fbip_{}", std::process::id())));
34
- std::fs::create_dir_all(&dir).unwrap();
35
34
  let src = dir.join("spread.plum");
36
35
  std::fs::write(&src, SRC).unwrap();
37
36
  let (outOn, errOn) = run(&src, &[]);
compiler/cli/tests/cli/old_forms_test.rs CHANGED
@@ -11,8 +11,7 @@ fn compileOutcome(name: &str, src: &str) -> (bool, String) {
11
11
  bin.pop();
12
12
  }
13
13
  bin.push("plum");
14
- let dir = std::env::temp_dir().join(format!("plum_oldform_{}_{}", name, std::process::id()));
14
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_oldform_{}_{}", name, std::process::id())));
15
- std::fs::create_dir_all(&dir).unwrap();
16
15
  let file = dir.join("main.plum");
17
16
  std::fs::write(&file, src).unwrap();
18
17
  let out = Command::new(bin)
compiler/cli/tests/cli/replay_test.rs CHANGED
@@ -21,9 +21,8 @@ fn root() -> PathBuf {
21
21
  Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap().to_path_buf()
22
22
  }
23
23
 
24
- fn scratch(name: &str) -> PathBuf {
24
+ fn scratch(name: &str) -> crate::common::Scratch {
25
- let dir = std::env::temp_dir().join(format!("plum_replay_{name}_{}", std::process::id()));
25
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_replay_{name}_{}", std::process::id())));
26
- std::fs::create_dir_all(&dir).unwrap();
27
26
  dir
28
27
  }
29
28
 
compiler/cli/tests/cli/test_scope_test.rs CHANGED
@@ -27,9 +27,8 @@ fn runPlum(args: &[&str]) -> (String, bool) {
27
27
  )
28
28
  }
29
29
 
30
- fn scratch(name: &str) -> std::path::PathBuf {
30
+ fn scratch(name: &str) -> crate::common::Scratch {
31
- let dir = std::env::temp_dir().join(format!("plum_scope_{}_{}", name, std::process::id()));
31
+ let dir = crate::common::Scratch::at(std::env::temp_dir().join(format!("plum_scope_{}_{}", name, std::process::id())));
32
- std::fs::create_dir_all(&dir).unwrap();
33
32
  dir
34
33
  }
35
34
 
@@ -55,10 +54,7 @@ fn failingImportedTestDoesNotFailImporter() {
55
54
  let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
56
55
  .ancestors().nth(2)
57
56
  .unwrap();
58
- let dir = root
59
- .join("target")
60
- .join(format!("scope_imp_{}", std::process::id()));
57
+ let dir = crate::common::Scratch::at(root.join("target").join(format!("scope_imp_{}", std::process::id())));
61
- std::fs::create_dir_all(&dir).unwrap();
62
58
  let rel = format!("target/scope_imp_{}/lib", std::process::id());
63
59
  std::fs::write(dir.join("lib.plum"), "module lib\n\nimport std/Int\n\nfun one() -> Int =\n 1\n\ntest \"bad\"\n assert 1 == 2\n").unwrap();
64
60
  let f = dir.join("main.plum");