Skip to content

Commit 0edf5db

Browse files
committed
fix: address PR review feedback
Signed-off-by: Zendy <50132805+zendy199x@users.noreply.github.com>
1 parent c7f67bb commit 0edf5db

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

qlty-check/src/utils.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
use rand::{distributions::Alphanumeric, Rng};
1+
use rand::rngs::OsRng;
22

33
pub fn generate_random_id(length: usize) -> String {
4-
rand::thread_rng()
5-
.sample_iter(&Alphanumeric)
6-
.take(length)
7-
.map(char::from)
8-
.collect()
4+
// Use a URL/filename-safe alphabet to avoid control chars, path separators, etc.
5+
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789-_";
6+
7+
let mut buf = vec![0u8; length];
8+
getrandom(&mut buf)?;
9+
10+
let mut id = String::with_capacity(length);
11+
for byte in buf {
12+
let idx = (byte as usize) % ALPHABET.len();
13+
id.push(ALPHABET[idx] as char);
14+
}
15+
16+
id
917
}

tests/test_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from qlty_check.src.utils import generate_random_id
2+
3+
def test_generate_random_id_happy_path():
4+
"""Test that generate_random_id produces a string of the correct length"""
5+
result = generate_random_id(10)
6+
assert isinstance(result, str)
7+
assert len(result) == 10
8+
9+
def test_generate_random_id_edge_cases():
10+
"""Test generate_random_id with edge case lengths"""
11+
# Test with length 0
12+
result = generate_random_id(0)
13+
assert isinstance(result, str)
14+
assert len(result) == 0
15+
16+
# Test with larger length
17+
result = generate_random_id(100)
18+
assert isinstance(result, str)
19+
assert len(result) == 100
20+
21+
def test_generate_random_id_different_outputs():
22+
"""Test that generate_random_id produces different outputs on subsequent calls"""
23+
result1 = generate_random_id(10)
24+
result2 = generate_random_id(10)
25+
assert result1 != result2
26+
27+
# Ensure both are valid strings of same length
28+
assert isinstance(result1, str)
29+
assert isinstance(result2, str)
30+
assert len(result1) == 10
31+
assert len(result2) == 10

0 commit comments

Comments
 (0)