generate_badfile

Function generate_badfile 

Source
pub fn generate_badfile(length: usize) -> String
Expand description

Generates a random alphanumeric filename that does not exist in the current directory.

This function creates a random alphanumeric string of the specified length, checks whether a file with that name already exists in the current working directory, and returns it only if the name is not already used. This ensures that the generated filename can safely be used for temporary files or testing without clashing with existing files.

§Parameters

  • length: The length of the generated filename. Must be greater than 0.

§Returns

  • A String representing a randomly generated, non-existent filename.

§Examples

use regd_testing;

let x = regd_testing::rand::generate_badfile(12);
println!("Generated unique filename: {}", x);
assert!(std::fs::metadata(&x).is_err()); // Confirm file does not exist

§Panics

  • This function will panic if length == 0.

§Notes

  • The function uses a loop and may retry multiple times if name collisions occur, although with a reasonable length (e.g., ≥8), collisions are very unlikely.
  • The check is limited to the current working directory.