amber-lang / amber

💎 Amber the programming language compiled to bash
https://amber-lang.com
GNU General Public License v3.0
3.8k stars 80 forks source link

[Feature]Execute Amber test in a folder in /tmp #329

Open Mte90 opened 1 month ago

Mte90 commented 1 month ago

In this way they are sandboxed (I execute them on my machine I dont want to have issues).

Following @CymDeveloppement proposal we can do something like:

import * from "std"
main {
    let tmpdir = unsafe $mktemp -d /tmp/amber-XXXX$
    unsafe $cd {tmpdir}$
    unsafe $touch test.txt$
    if file_exist("./test.txt") {
        echo "Hello, Amber!"
    } else {
        echo "Not Found"
    }
    unsafe $rm -fr {tmpdir}$
}

An idea can be on Rust side create that folder, execute the script inside that and remove it when finished. So we don't need to modify any test.

CymDeveloppement commented 1 month ago

using trap command is important, folder should be deleted also if script fail

CymDeveloppement commented 1 month ago
pub fun run_in_tmp(): Null {
    if(not shell_isset("CURRENT_TMP_DIR")) {
        $cd \$(mktemp -d /tmp/XXXXXX)$ failed {
            error("Failed to create temp folder")
            exit(1)
        }
        let tmp_dir = unsafe $pwd$
        unsafe shell_constant_set("CURRENT_TMP_DIR", tmp_dir)
        unsafe $trap "rm -Rf \$CURRENT_TMP_DIR" EXIT$    
    } else {
        unsafe $cd \$CURRENT_TMP_DIR$
    }
}