bats-file icon indicating copy to clipboard operation
bats-file copied to clipboard

`temp_del` waits for user's input when trying to remove write-protected file

Open meleu opened this issue 8 months ago • 0 comments

Issue

I faced a scenario where my test was stuck, and after a Ctrl+C I noticed it was happening because the rm command in temp_del was waiting for user's input.

Specifically this line:

https://github.com/bats-core/bats-file/blob/c0e3a260d2901d5045e164e7737238a19db2d526/src/temp.bash#L174

I'm wondering if we should use rm -rf here (which probably requires extra care).

Using --interactive=never would be The Perfect™ solution, but I'm afraid it's not portable (GNU coreutils only).

Expected behavior

Given the fact that temp_del is supposed to be used programatically (never in an interactive session), it should never wait for user's input (it should either succeed or fail).

Steps to reproduce

Start a dummy project:

mkdir -p deleteme/test
cd deleteme
git init

git submodule add \
  https://github.com/bats-core/bats-file.git \
  test/test_helper/bats-file

git submodule add \
  https://github.com/bats-core/bats-support.git \
  test/test_helper/bats-support

Create a sample.bats like this:

setup() {
  load 'test/test_helper/bats-support/load'
  load 'test/test_helper/bats-file/load'

  TEST_TEMP_DIR="$(temp_make)"
}

teardown() {
  temp_del "${TEST_TEMP_DIR}"
}

@test "temp_del waits for user input" {
  cd "${TEST_TEMP_DIR}"
  mkdir my_dir
  chmod a=r my_dir
}

Run the test and see it getting stuck.

$ bats sample.bats
sample.bats
   temp_del waits for user input # 👈 Hit Ctrl+C
 ✗ temp_del waits for user input
   (from function `temp_del' in file test/test_helper/bats-file/src/temp.bash, line 1,
    from function `teardown' in test file sample.bats, line 11)
     `temp_del "${TEST_TEMP_DIR}"' failed

   -- ERROR: temp_del --
   rm: remove write-protected directory '/tmp/sample.bats-1-YgMpb7/my_dir'?
   --


   Received SIGINT, aborting ...


1 test, 1 failure

meleu avatar Mar 23 '25 15:03 meleu