[Feature] Handle possible failure of mktemp in stdlib tests
Currently in amber stdlib tests we use the following approach:
https://github.com/amber-lang/amber/blob/master/src/tests/stdlib/file_append.ab#L3 https://github.com/amber-lang/amber/blob/master/src/tests/stdlib/get_env_var.ab#L4
let tmpdir = unsafe $mktemp -d /tmp/amber-XXXX$
# some file ops within temp dir ...
unsafe $rm -fr {tmpdir}$
An example issue could arise where mktemp fails, e.g. due to insufficient permission. In that case tmpdir is an empty variable and rm -rf gets called without arguments.
let v = unsafe $mktemp -d /doesnt/exist-XXXX$
echo v
Gives no output.
Calling rm -rf without any argument seems harmless, it apparently doesn't do anything.
Even if that case is fine, it might be somewhat dangerous to leave around a possibly empty rm -rf in case of future code changes.
I propose we adjust the tests to handle failure cases of mktemp and unify the approach even further.
I believe people already talked a bit about this and possibly a new stdlib method that wraps commands safely in a temp dir.
➜ ~ mktemp -d /retghfgndsrfdvxXXX
mktemp: failed to create directory via template ‘/retghfgndsrfdvxXXX’: Permission denied
➜ ~ echo $?
1
this should be easy enough to fix, just add a failed block that will exit and fail the test.
you can draft a PR if you want to be listed as one of the cool kids who contributed to amber
yep that sounds easy enough haha, well I would but currently on vacation and git on phone is a bit difficult 😛
git on phone
lmao dont do that. either do it properly on a workstation or not do it at all
Talking with @CymDeveloppement in a PR we were discussing that can be helpful if cargo can automatically create a folder in /tmp/ where the tests will be executed with the right current working directory.
In this way everything is sandboxed and we don't need that code in the tests and they are more simple.
Ref: https://github.com/amber-lang/amber/issues/445
I am working on writing a stdlib function wraps mktemp #718 which maybe covers this case.