Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[Feature] Handle possible failure of mktemp in stdlib tests

Open karpfediem opened this issue 1 year ago • 5 comments

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.

karpfediem avatar Jul 25 '24 22:07 karpfediem

➜  ~ mktemp -d /retghfgndsrfdvxXXX
mktemp: failed to create directory via template ‘/retghfgndsrfdvxXXX’: Permission denied
➜  ~ echo $?
1

b1ek avatar Jul 26 '24 02:07 b1ek

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

b1ek avatar Jul 26 '24 02:07 b1ek

yep that sounds easy enough haha, well I would but currently on vacation and git on phone is a bit difficult 😛

karpfediem avatar Jul 26 '24 04:07 karpfediem

git on phone

lmao dont do that. either do it properly on a workstation or not do it at all

b1ek avatar Jul 26 '24 05:07 b1ek

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

Mte90 avatar Jul 26 '24 09:07 Mte90

I am working on writing a stdlib function wraps mktemp #718 which maybe covers this case.

lens0021 avatar Jun 21 '25 22:06 lens0021