Fork of daniellemaywood.uk/gleam — Wasm codegen work
2.4 kB
73 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2026 The Gleam contributors
3
4use camino::Utf8PathBuf;
5use gleam_cli::{Command, ExportTarget, fs};
6use std::process;
7
8fn escript_compile(case: &str) -> Result<Utf8PathBuf, gleam_core::Error> {
9 let working_directory = Utf8PathBuf::from(&format!("./cases/{case}"));
10 let escript_path = working_directory.join(case);
11 fs::delete_file(&escript_path)
12 .and(fs::delete_file(&escript_path.with_extension("cmd")))
13 .expect("must be able to reset test directory");
14
15 Command::Export(ExportTarget::Escript)
16 .run(working_directory.clone())
17 .map(|_| escript_path)
18}
19
20fn assert_escript_compile(case: &str) -> Utf8PathBuf {
21 let escript_path = escript_compile(case).expect("should compile successfully");
22 assert!(
23 escript_path.exists() && escript_path.is_file(),
24 "escript should have been created"
25 );
26 escript_path
27}
28
29#[test]
30fn escript_success() {
31 let escript = assert_escript_compile("escript_ok");
32 let status = process::Command::new("escript")
33 .arg(&escript)
34 .status()
35 .expect("executable escript");
36 assert!(status.success(), "escript should run OK");
37
38 let cmd = escript.with_extension("cmd");
39 if cfg!(windows) {
40 assert!(cmd.exists(), "*.cmd should exist on Windows");
41 let code = std::fs::read_to_string(cmd).expect("read cmd file");
42 let expected = "@echo off\r\nescript.exe \"%~dpn0\" %*\r\n";
43 assert_eq!(code, expected, "cmd wrapper should run the escript");
44 } else {
45 assert!(!cmd.exists(), "{cmd} should only exist on Windows");
46 }
47}
48
49#[test]
50fn escript_success_with_dependency() {
51 let escript = assert_escript_compile("escript_with_dependency");
52 let status = process::Command::new("escript")
53 .arg(escript)
54 .status()
55 .expect("executable escript");
56 assert!(status.success(), "escript should run OK");
57}
58
59#[test]
60fn escript_without_main_function() {
61 let error = escript_compile("escript_without_main_function")
62 .expect_err("escripts require a main function")
63 .pretty_string();
64 insta::assert_snapshot!(error);
65}
66
67#[test]
68fn escript_with_wrong_arity_main_function() {
69 let error = escript_compile("escript_with_wrong_arity_main_function")
70 .expect_err("escripts require a main function")
71 .pretty_string();
72 insta::assert_snapshot!(error);
73}