click-rs 1.0.0

A Rust port of Python's Click library for creating command-line interfaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Tests for the utilities module.

use click::utils::{
    expand_path, format_filename, get_app_dir, get_extension, get_os_args,
    get_os_args_skip_program, get_text_stderr, get_text_stdout, home_dir,
    join_with_conjunction, make_safe_filename, pluralize, safecall, strip_extension,
};
use std::env;
use std::io::Write;
use std::path::{Path, PathBuf};

// =============================================================================
// Stream Function Tests
// =============================================================================

#[test]
fn test_get_text_stdout() {
    let mut stdout = get_text_stdout();
    // Should be writable
    assert!(write!(stdout, "").is_ok());
}

#[test]
fn test_get_text_stderr() {
    let mut stderr = get_text_stderr();
    // Should be writable
    assert!(write!(stderr, "").is_ok());
}

// =============================================================================
// Path Formatting Tests
// =============================================================================

#[test]
fn test_format_filename_absolute() {
    let path = Path::new("/usr/local/bin/test");
    let formatted = format_filename(path);

    // Should use forward slashes
    assert!(!formatted.contains('\\'));
}

#[test]
fn test_format_filename_relative() {
    let path = Path::new("./relative/path");
    let formatted = format_filename(path);

    assert_eq!(formatted, "./relative/path");
}

#[test]
fn test_format_filename_home_shortening() {
    if let Some(home) = home_dir() {
        let path = home.join("documents").join("file.txt");
        let formatted = format_filename(&path);

        // Should start with ~ if path is under home
        assert!(
            formatted.starts_with("~/") || formatted.starts_with("~\\"),
            "Expected path to start with ~/, got: {}",
            formatted
        );
        assert!(formatted.contains("documents"));
    }
}

// =============================================================================
// App Directory Tests
// =============================================================================

#[test]
fn test_get_app_dir_non_roaming() {
    let app_dir = get_app_dir("testapp", false);

    // Should return a valid path
    assert!(!app_dir.as_os_str().is_empty());

    // Should contain the app name
    assert!(
        app_dir.to_string_lossy().contains("testapp")
            || app_dir.to_string_lossy().contains(".testapp"),
        "App dir should contain app name: {}",
        app_dir.display()
    );
}

#[test]
fn test_get_app_dir_roaming() {
    let app_dir = get_app_dir("testapp", true);

    // Should return a valid path
    assert!(!app_dir.as_os_str().is_empty());

    // Should contain the app name
    assert!(
        app_dir.to_string_lossy().contains("testapp"),
        "App dir should contain app name: {}",
        app_dir.display()
    );
}

#[test]
fn test_get_app_dir_different_apps() {
    let dir1 = get_app_dir("app1", false);
    let dir2 = get_app_dir("app2", false);

    // Different apps should have different directories
    assert_ne!(dir1, dir2);
}

// =============================================================================
// Path Expansion Tests
// =============================================================================

#[test]
fn test_expand_path_no_expansion() {
    let path = expand_path("/absolute/path");
    assert_eq!(path, PathBuf::from("/absolute/path"));

    let path = expand_path("relative/path");
    assert_eq!(path, PathBuf::from("relative/path"));
}

#[test]
fn test_expand_path_tilde_home() {
    if let Some(home) = home_dir() {
        let path = expand_path("~");
        assert_eq!(path, home);
    }
}

#[test]
fn test_expand_path_tilde_subdir() {
    if let Some(home) = home_dir() {
        let path = expand_path("~/documents");
        assert_eq!(path, home.join("documents"));
    }
}

#[test]
fn test_expand_path_env_var_dollar() {
    env::set_var("CLICK_TEST_EXPAND", "/test/path");

    let path = expand_path("$CLICK_TEST_EXPAND/subdir");
    assert_eq!(path, PathBuf::from("/test/path/subdir"));

    env::remove_var("CLICK_TEST_EXPAND");
}

#[test]
fn test_expand_path_env_var_braces() {
    env::set_var("CLICK_TEST_EXPAND2", "/another/path");

    let path = expand_path("${CLICK_TEST_EXPAND2}/subdir");
    assert_eq!(path, PathBuf::from("/another/path/subdir"));

    env::remove_var("CLICK_TEST_EXPAND2");
}

#[test]
fn test_expand_path_undefined_env_var() {
    env::remove_var("CLICK_UNDEFINED_VAR");

    let path = expand_path("$CLICK_UNDEFINED_VAR/subdir");
    // Undefined variable should be removed
    assert_eq!(path, PathBuf::from("/subdir"));
}

#[test]
fn test_expand_path_multiple_vars() {
    env::set_var("CLICK_VAR1", "/first");
    env::set_var("CLICK_VAR2", "second");

    let path = expand_path("$CLICK_VAR1/$CLICK_VAR2/file");
    assert_eq!(path, PathBuf::from("/first/second/file"));

    env::remove_var("CLICK_VAR1");
    env::remove_var("CLICK_VAR2");
}

// =============================================================================
// OS Args Tests
// =============================================================================

#[test]
fn test_get_os_args() {
    let args = get_os_args();

    // Should have at least the program name
    assert!(!args.is_empty());
}

#[test]
fn test_get_os_args_skip_program() {
    let args = get_os_args_skip_program();

    // This is the args without the program name
    // In tests, we typically don't have extra args
    let _ = args;
}

// =============================================================================
// Home Directory Tests
// =============================================================================

#[test]
fn test_home_dir() {
    // home_dir should return Some on most systems
    // Even if it doesn't, it shouldn't panic
    let _ = home_dir();
}

#[test]
fn test_home_dir_with_env() {
    // If HOME is set, we should get a result
    if env::var("HOME").is_ok() {
        let home = home_dir();
        assert!(home.is_some(), "home_dir() should return Some when HOME is set");
    }
}

// =============================================================================
// String Utility Tests
// =============================================================================

#[test]
fn test_pluralize_one() {
    assert_eq!(pluralize(1, "file", "files"), "file");
    assert_eq!(pluralize(1, "item", "items"), "item");
}

#[test]
fn test_pluralize_zero() {
    assert_eq!(pluralize(0, "file", "files"), "files");
    assert_eq!(pluralize(0, "item", "items"), "items");
}

#[test]
fn test_pluralize_many() {
    assert_eq!(pluralize(2, "file", "files"), "files");
    assert_eq!(pluralize(5, "item", "items"), "items");
    assert_eq!(pluralize(100, "person", "people"), "people");
}

#[test]
fn test_join_with_conjunction_empty() {
    let items: Vec<&str> = vec![];
    assert_eq!(join_with_conjunction(&items, ", ", " and "), "");
}

#[test]
fn test_join_with_conjunction_one() {
    let items = vec!["apple"];
    assert_eq!(join_with_conjunction(&items, ", ", " and "), "apple");
}

#[test]
fn test_join_with_conjunction_two() {
    let items = vec!["apple", "banana"];
    assert_eq!(join_with_conjunction(&items, ", ", " and "), "apple and banana");
}

#[test]
fn test_join_with_conjunction_three() {
    let items = vec!["apple", "banana", "cherry"];
    assert_eq!(join_with_conjunction(&items, ", ", " and "), "apple, banana and cherry");
}

#[test]
fn test_join_with_conjunction_or() {
    let items = vec!["red", "green", "blue"];
    assert_eq!(join_with_conjunction(&items, ", ", " or "), "red, green or blue");
}

#[test]
fn test_safecall_normal() {
    assert_eq!(safecall("normal text"), "normal text");
    assert_eq!(safecall("with spaces"), "with spaces");
    assert_eq!(safecall("with-dashes"), "with-dashes");
}

#[test]
fn test_safecall_newlines() {
    assert_eq!(safecall("line1\nline2"), "line1\nline2");
}

#[test]
fn test_safecall_tabs() {
    assert_eq!(safecall("col1\tcol2"), "col1\tcol2");
}

#[test]
fn test_safecall_control_chars() {
    // Bell character
    assert_eq!(safecall("before\x07after"), "before\\x07after");

    // Null character
    assert_eq!(safecall("before\x00after"), "before\\x00after");
}

// =============================================================================
// Filename Safety Tests
// =============================================================================

#[test]
fn test_make_safe_filename_normal() {
    assert_eq!(make_safe_filename("normal.txt"), "normal.txt");
    assert_eq!(make_safe_filename("file_name.txt"), "file_name.txt");
}

#[test]
fn test_make_safe_filename_spaces() {
    assert_eq!(make_safe_filename("file name.txt"), "file_name.txt");
    assert_eq!(make_safe_filename("multiple   spaces"), "multiple_spaces");
}

#[test]
fn test_make_safe_filename_unsafe_chars() {
    assert_eq!(make_safe_filename("a<b>c.txt"), "abc.txt");
    assert_eq!(make_safe_filename("a:b.txt"), "ab.txt");
    assert_eq!(make_safe_filename("a/b\\c.txt"), "abc.txt");
    assert_eq!(make_safe_filename("a|b.txt"), "ab.txt");
    assert_eq!(make_safe_filename("a?b.txt"), "ab.txt");
    assert_eq!(make_safe_filename("a*b.txt"), "ab.txt");
    assert_eq!(make_safe_filename("a\"b.txt"), "ab.txt");
}

#[test]
fn test_make_safe_filename_leading_trailing_spaces() {
    assert_eq!(make_safe_filename("  file.txt  "), "_file.txt");
}

#[test]
fn test_make_safe_filename_all_unsafe() {
    assert_eq!(make_safe_filename("<>:/\\|?*"), "");
}

// =============================================================================
// File Extension Tests
// =============================================================================

#[test]
fn test_get_extension_normal() {
    assert_eq!(get_extension(Path::new("file.txt")), Some("txt"));
    assert_eq!(get_extension(Path::new("file.tar.gz")), Some("gz"));
    assert_eq!(get_extension(Path::new("document.pdf")), Some("pdf"));
}

#[test]
fn test_get_extension_none() {
    assert_eq!(get_extension(Path::new("file")), None);
    assert_eq!(get_extension(Path::new(".hidden")), None);
}

#[test]
fn test_get_extension_path() {
    assert_eq!(get_extension(Path::new("/path/to/file.txt")), Some("txt"));
    assert_eq!(get_extension(Path::new("./relative/file.md")), Some("md"));
}

#[test]
fn test_strip_extension_normal() {
    assert_eq!(strip_extension(Path::new("file.txt")), PathBuf::from("file"));
    assert_eq!(strip_extension(Path::new("file.tar.gz")), PathBuf::from("file.tar"));
}

#[test]
fn test_strip_extension_no_extension() {
    assert_eq!(strip_extension(Path::new("file")), PathBuf::from("file"));
}

#[test]
fn test_strip_extension_path() {
    assert_eq!(
        strip_extension(Path::new("/path/to/file.txt")),
        PathBuf::from("/path/to/file")
    );
}

// =============================================================================
// Edge Case Tests
// =============================================================================

#[test]
fn test_expand_path_empty() {
    let path = expand_path("");
    assert_eq!(path, PathBuf::from(""));
}

#[test]
fn test_format_filename_empty() {
    let formatted = format_filename(Path::new(""));
    assert_eq!(formatted, "");
}

#[test]
fn test_make_safe_filename_empty() {
    assert_eq!(make_safe_filename(""), "");
}

#[test]
fn test_get_extension_empty() {
    assert_eq!(get_extension(Path::new("")), None);
}

#[test]
fn test_pluralize_edge_cases() {
    // Very large numbers
    assert_eq!(pluralize(usize::MAX, "item", "items"), "items");

    // With irregular plurals
    assert_eq!(pluralize(1, "mouse", "mice"), "mouse");
    assert_eq!(pluralize(2, "mouse", "mice"), "mice");
}

#[test]
fn test_join_with_conjunction_with_empty_items() {
    let items = vec!["", "a", ""];
    let result = join_with_conjunction(&items, ", ", " and ");
    // Empty strings are still included
    assert_eq!(result, ", a and ");
}

#[test]
fn test_expand_path_consecutive_vars() {
    env::set_var("CLICK_A", "a");
    env::set_var("CLICK_B", "b");

    let path = expand_path("$CLICK_A$CLICK_B");
    assert_eq!(path, PathBuf::from("ab"));

    env::remove_var("CLICK_A");
    env::remove_var("CLICK_B");
}

#[test]
fn test_make_safe_filename_unicode() {
    // Unicode characters should be preserved
    assert_eq!(make_safe_filename("file_日本語.txt"), "file_日本語.txt");
    assert_eq!(make_safe_filename("файл.txt"), "файл.txt");
}

#[test]
fn test_format_filename_preserves_unicode() {
    let path = Path::new("/path/to/日本語/file.txt");
    let formatted = format_filename(path);
    assert!(formatted.contains("日本語"));
}