amber-lang/amber

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

Mte90 opened this issue · 2 comments

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.

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

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$
    }
}