From 0edf5db75d271488daca492feea48e80672070e3 Mon Sep 17 00:00:00 2001 From: Zendy <50132805+zendy199x@users.noreply.github.com> Date: Sat, 28 Mar 2026 23:00:00 +0700 Subject: [PATCH] fix: address PR review feedback Signed-off-by: Zendy <50132805+zendy199x@users.noreply.github.com> --- qlty-check/src/utils.rs | 20 ++++++++++++++------ tests/test_utils.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 tests/test_utils.py diff --git a/qlty-check/src/utils.rs b/qlty-check/src/utils.rs index 0621678d3..dc192108f 100644 --- a/qlty-check/src/utils.rs +++ b/qlty-check/src/utils.rs @@ -1,9 +1,17 @@ -use rand::{distributions::Alphanumeric, Rng}; +use rand::rngs::OsRng; pub fn generate_random_id(length: usize) -> String { - rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(length) - .map(char::from) - .collect() + // Use a URL/filename-safe alphabet to avoid control chars, path separators, etc. + const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789-_"; + + let mut buf = vec![0u8; length]; + getrandom(&mut buf)?; + + let mut id = String::with_capacity(length); + for byte in buf { + let idx = (byte as usize) % ALPHABET.len(); + id.push(ALPHABET[idx] as char); + } + + id } diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 000000000..4fd4cc9a2 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,31 @@ +from qlty_check.src.utils import generate_random_id + +def test_generate_random_id_happy_path(): + """Test that generate_random_id produces a string of the correct length""" + result = generate_random_id(10) + assert isinstance(result, str) + assert len(result) == 10 + +def test_generate_random_id_edge_cases(): + """Test generate_random_id with edge case lengths""" + # Test with length 0 + result = generate_random_id(0) + assert isinstance(result, str) + assert len(result) == 0 + + # Test with larger length + result = generate_random_id(100) + assert isinstance(result, str) + assert len(result) == 100 + +def test_generate_random_id_different_outputs(): + """Test that generate_random_id produces different outputs on subsequent calls""" + result1 = generate_random_id(10) + result2 = generate_random_id(10) + assert result1 != result2 + + # Ensure both are valid strings of same length + assert isinstance(result1, str) + assert isinstance(result2, str) + assert len(result1) == 10 + assert len(result2) == 10 \ No newline at end of file