urlsup 2.4.0

CLI to validate URLs in files
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use std::collections::HashSet;
use std::path::{Path, PathBuf};

use crate::core::error::{Result, UrlsUpError};

pub fn expand_paths(
    input_paths: Vec<&Path>,
    recursive: bool,
    file_types: Option<&HashSet<String>>,
) -> Result<Vec<PathBuf>> {
    let mut result_paths = Vec::new();

    for path in input_paths {
        if path.is_file() {
            // Check file extension if filtering is enabled
            if let Some(extensions) = file_types {
                if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                    if extensions.contains(ext) {
                        result_paths.push(path.to_path_buf());
                    }
                } else if extensions.contains("") {
                    // Include files without extensions if "" is in the set
                    result_paths.push(path.to_path_buf());
                }
            } else {
                result_paths.push(path.to_path_buf());
            }
        } else if path.is_dir() && recursive {
            let mut builder = ignore::WalkBuilder::new(path);
            builder.hidden(false); // Include hidden files

            for entry in builder.build() {
                let entry = entry?;
                let entry_path = entry.path();

                if entry_path.is_file() {
                    // Check file extension if filtering is enabled
                    if let Some(extensions) = file_types {
                        if let Some(ext) = entry_path.extension().and_then(|e| e.to_str()) {
                            if extensions.contains(ext) {
                                result_paths.push(entry_path.to_path_buf());
                            }
                        } else if extensions.contains("") {
                            // Include files without extensions if "" is in the set
                            result_paths.push(entry_path.to_path_buf());
                        }
                    } else {
                        result_paths.push(entry_path.to_path_buf());
                    }
                }
            }
        } else if path.is_dir() && !recursive {
            return Err(UrlsUpError::PathExpansion(format!(
                "'{}' is a directory. Use --recursive to process directories.",
                path.display()
            )));
        }
    }

    Ok(result_paths)
}

#[cfg(test)]
mod tests {
    #![allow(non_snake_case)]

    use super::*;
    use std::fs;
    use tempfile::TempDir;

    type TestResult = std::result::Result<(), Box<dyn std::error::Error>>;

    fn create_test_structure() -> std::result::Result<TempDir, Box<dyn std::error::Error>> {
        let temp_dir = tempfile::tempdir()?;
        let base = temp_dir.path();

        // Create directory structure
        fs::create_dir_all(base.join("subdir/nested"))?;
        fs::create_dir_all(base.join("other"))?;

        // Create files with different extensions
        fs::write(base.join("README.md"), "# Test\nhttps://example.com")?;
        fs::write(base.join("file.txt"), "Some text with https://test.com")?;
        fs::write(
            base.join("script.sh"),
            "#!/bin/bash\necho https://shell.com",
        )?;
        fs::write(base.join("config.json"), r#"{"url": "https://json.com"}"#)?;
        fs::write(base.join("no_extension"), "https://noext.com")?;

        // Create nested files
        fs::write(
            base.join("subdir/nested/deep.md"),
            "Deep file https://deep.com",
        )?;
        fs::write(
            base.join("other/another.txt"),
            "Another https://another.com",
        )?;

        // Create .gitignore
        fs::write(base.join(".gitignore"), "*.log\ntmp/\n")?;

        // Create ignored files
        fs::write(base.join("debug.log"), "Should be ignored")?;
        fs::create_dir_all(base.join("tmp"))?;
        fs::write(base.join("tmp/temp.md"), "Should be ignored")?;

        Ok(temp_dir)
    }

    #[test]
    fn test_expand_paths__single_file() -> TestResult {
        let temp_dir = create_test_structure()?;
        let readme_path = temp_dir.path().join("README.md");

        let result = expand_paths(vec![&readme_path], false, None)?;

        assert_eq!(result.len(), 1);
        assert_eq!(result[0], readme_path);
        Ok(())
    }

    #[test]
    fn test_expand_paths__file_with_extension_filter() -> TestResult {
        let temp_dir = create_test_structure()?;
        let readme_path = temp_dir.path().join("README.md");
        let txt_path = temp_dir.path().join("file.txt");

        let mut extensions = HashSet::new();
        extensions.insert("md".to_string());

        // Should include .md file
        let result = expand_paths(vec![&readme_path], false, Some(&extensions))?;
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], readme_path);

        // Should exclude .txt file
        let result = expand_paths(vec![&txt_path], false, Some(&extensions))?;
        assert_eq!(result.len(), 0);

        Ok(())
    }

    #[test]
    fn test_expand_paths__directory_without_recursive_fails() -> TestResult {
        let temp_dir = create_test_structure()?;

        let result = expand_paths(vec![temp_dir.path()], false, None);

        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("is a directory. Use --recursive")
        );
        Ok(())
    }

    #[test]
    fn test_expand_paths__recursive_all_files() -> TestResult {
        let temp_dir = create_test_structure()?;

        let result = expand_paths(vec![temp_dir.path()], true, None)?;

        // Should find all files in the directory structure
        // The exact count depends on gitignore behavior, but should find our main files
        assert!(result.len() >= 7); // At least the main files

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        // Check that we find the main files we created
        assert!(file_names.contains(&"README.md".to_string()));
        assert!(file_names.contains(&"file.txt".to_string()));
        assert!(file_names.contains(&"deep.md".to_string()));
        assert!(file_names.contains(&"another.txt".to_string()));

        Ok(())
    }

    #[test]
    fn test_expand_paths__recursive_with_file_type_filter() -> TestResult {
        let temp_dir = create_test_structure()?;

        let mut extensions = HashSet::new();
        extensions.insert("md".to_string());

        let result = expand_paths(vec![temp_dir.path()], true, Some(&extensions))?;

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        // Should only find markdown files
        assert!(file_names.contains(&"README.md".to_string()));
        assert!(file_names.contains(&"deep.md".to_string()));
        assert!(!file_names.contains(&"file.txt".to_string()));
        assert!(!file_names.contains(&"script.sh".to_string()));

        // All found files should have .md extension
        for path in &result {
            if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                assert_eq!(ext, "md");
            }
        }

        Ok(())
    }

    #[test]
    fn test_expand_paths__multiple_extensions() -> TestResult {
        let temp_dir = create_test_structure()?;

        let mut extensions = HashSet::new();
        extensions.insert("md".to_string());
        extensions.insert("txt".to_string());

        let result = expand_paths(vec![temp_dir.path()], true, Some(&extensions))?;

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        // Should find .md and .txt files
        assert!(file_names.contains(&"README.md".to_string()));
        assert!(file_names.contains(&"file.txt".to_string()));
        assert!(file_names.contains(&"deep.md".to_string()));
        assert!(file_names.contains(&"another.txt".to_string()));
        assert!(!file_names.contains(&"script.sh".to_string()));
        assert!(!file_names.contains(&"config.json".to_string()));

        // All found files should have .md or .txt extension
        for path in &result {
            if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                assert!(ext == "md" || ext == "txt");
            }
        }

        Ok(())
    }

    #[test]
    fn test_expand_paths__files_without_extension() -> TestResult {
        let temp_dir = create_test_structure()?;

        let mut extensions = HashSet::new();
        extensions.insert("".to_string()); // Empty string means files without extension

        let result = expand_paths(vec![temp_dir.path()], true, Some(&extensions))?;

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        // Should find files without extensions
        assert!(file_names.contains(&"no_extension".to_string()));

        // All found files should have no extension
        for path in &result {
            assert!(path.extension().is_none());
        }

        Ok(())
    }

    #[test]
    fn test_expand_paths__mixed_files_and_directories() -> TestResult {
        let temp_dir = create_test_structure()?;
        let readme_path = temp_dir.path().join("README.md");
        let subdir_path = temp_dir.path().join("subdir");

        let mut extensions = HashSet::new();
        extensions.insert("md".to_string());

        let result = expand_paths(vec![&readme_path, &subdir_path], true, Some(&extensions))?;

        // Should find README.md directly and deep.md from subdir recursively
        assert_eq!(result.len(), 2);

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        assert!(file_names.contains(&"README.md".to_string()));
        assert!(file_names.contains(&"deep.md".to_string()));

        Ok(())
    }

    #[test]
    fn test_expand_paths__nonexistent_file() -> TestResult {
        let result = expand_paths(
            vec![Path::new("/definitely/nonexistent/path/file.md")],
            false,
            None,
        )?;
        // Non-existent files are simply not included in the result
        assert!(result.is_empty());
        Ok(())
    }

    #[test]
    fn test_expand_paths__permission_denied() -> TestResult {
        // This test simulates permission issues on paths that may not be accessible
        // On most systems, this will pass but provides coverage for error handling
        let result = expand_paths(vec![Path::new("/proc/1/mem")], false, None);
        // The result may succeed or fail depending on system, but shouldn't panic
        let _ = result;
        Ok(())
    }

    #[test]
    fn test_expand_paths__empty_input() -> TestResult {
        let result = expand_paths(vec![], false, None)?;
        assert!(result.is_empty());
        Ok(())
    }

    #[test]
    fn test_expand_paths__directory_non_recursive_error() -> TestResult {
        let temp_dir = tempfile::tempdir()?;
        let dir_path = temp_dir.path();

        let result = expand_paths(vec![dir_path], false, None);
        assert!(result.is_err());

        if let Err(UrlsUpError::PathExpansion(msg)) = result {
            assert!(msg.contains("is a directory"));
            assert!(msg.contains("Use --recursive"));
        } else {
            panic!("Expected PathExpansion error");
        }

        Ok(())
    }

    #[test]
    fn test_expand_paths__file_extension_filtering() -> TestResult {
        let temp_dir = create_test_structure()?;
        let base = temp_dir.path();

        // Test with specific extension filter
        let mut extensions = HashSet::new();
        extensions.insert("txt".to_string());

        let result = expand_paths(
            vec![
                base.join("file.txt").as_path(),
                base.join("README.md").as_path(),
            ],
            false,
            Some(&extensions),
        )?;

        // Should only include the .txt file
        assert_eq!(result.len(), 1);
        assert!(
            result[0]
                .file_name()
                .unwrap()
                .to_string_lossy()
                .contains("file.txt")
        );

        Ok(())
    }

    #[test]
    fn test_expand_paths__file_without_extension() -> TestResult {
        let temp_dir = create_test_structure()?;
        let base = temp_dir.path();

        // Test with empty string in extensions (matches files without extension)
        let mut extensions = HashSet::new();
        extensions.insert("".to_string());

        let result = expand_paths(
            vec![base.join("no_extension").as_path()],
            false,
            Some(&extensions),
        )?;

        assert_eq!(result.len(), 1);
        assert!(
            result[0]
                .file_name()
                .unwrap()
                .to_string_lossy()
                .contains("no_extension")
        );

        Ok(())
    }

    #[test]
    fn test_expand_paths__file_extension_case_sensitive() -> TestResult {
        let temp_dir = tempfile::tempdir()?;
        let base = temp_dir.path();

        // Create files with different case extensions
        fs::write(base.join("file.MD"), "# Test\nhttps://example.com")?;
        fs::write(base.join("file.md"), "# Test\nhttps://example.com")?;

        let mut extensions = HashSet::new();
        extensions.insert("md".to_string()); // lowercase only

        let result = expand_paths(
            vec![
                base.join("file.MD").as_path(),
                base.join("file.md").as_path(),
            ],
            false,
            Some(&extensions),
        )?;

        // Should only match the lowercase .md file (case sensitive)
        assert_eq!(result.len(), 1);
        assert!(
            result[0]
                .file_name()
                .unwrap()
                .to_string_lossy()
                .contains("file.md")
        );

        Ok(())
    }

    #[test]
    fn test_expand_paths__multiple_extensions_selective() -> TestResult {
        let temp_dir = create_test_structure()?;
        let base = temp_dir.path();

        let mut extensions = HashSet::new();
        extensions.insert("md".to_string());
        extensions.insert("txt".to_string());
        extensions.insert("json".to_string());

        let result = expand_paths(
            vec![
                base.join("README.md").as_path(),
                base.join("file.txt").as_path(),
                base.join("config.json").as_path(),
                base.join("script.sh").as_path(),
            ],
            false,
            Some(&extensions),
        )?;

        // Should include md, txt, and json files but not sh
        assert_eq!(result.len(), 3);

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        assert!(file_names.contains(&"README.md".to_string()));
        assert!(file_names.contains(&"file.txt".to_string()));
        assert!(file_names.contains(&"config.json".to_string()));
        assert!(!file_names.contains(&"script.sh".to_string()));

        Ok(())
    }

    #[test]
    fn test_expand_paths__recursive_with_extension_filter() -> TestResult {
        let temp_dir = create_test_structure()?;
        let base = temp_dir.path();

        let mut extensions = HashSet::new();
        extensions.insert("txt".to_string());

        let result = expand_paths(vec![base], true, Some(&extensions))?;

        // Should find file.txt and other/another.txt
        assert_eq!(result.len(), 2);

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        assert!(file_names.contains(&"file.txt".to_string()));
        assert!(file_names.contains(&"another.txt".to_string()));

        Ok(())
    }

    #[test]
    fn test_expand_paths__recursive_no_filter() -> TestResult {
        let temp_dir = create_test_structure()?;
        let base = temp_dir.path();

        let result = expand_paths(vec![base], true, None)?;

        // Should find all files including nested ones
        assert!(result.len() >= 6); // At least the files we created

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        assert!(file_names.contains(&"README.md".to_string()));
        assert!(file_names.contains(&"file.txt".to_string()));
        assert!(file_names.contains(&"deep.md".to_string()));
        assert!(file_names.contains(&"another.txt".to_string()));

        Ok(())
    }

    #[test]
    fn test_expand_paths__mixed_files_and_directories_comprehensive() -> TestResult {
        let temp_dir = create_test_structure()?;
        let base = temp_dir.path();

        let result = expand_paths(
            vec![
                base.join("README.md").as_path(),
                base.join("subdir").as_path(),
            ],
            true,
            None,
        )?;

        // Should include README.md directly and files from subdir recursively
        assert!(result.len() >= 2);

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        assert!(file_names.contains(&"README.md".to_string()));
        assert!(file_names.contains(&"deep.md".to_string()));

        Ok(())
    }

    #[test]
    fn test_expand_paths__ignore_gitignore_files() -> TestResult {
        let temp_dir = tempfile::tempdir()?;
        let base = temp_dir.path();

        // Create a .gitignore file
        fs::write(base.join(".gitignore"), "ignored.txt\n*.tmp")?;

        // Create files that should be ignored and not ignored
        fs::write(base.join("ignored.txt"), "should be ignored")?;
        fs::write(base.join("test.tmp"), "should be ignored tmp")?;
        fs::write(base.join("normal.txt"), "should be included")?;

        let result = expand_paths(vec![base], true, None)?;

        let file_names: Vec<String> = result
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();

        // Should include normal.txt but might or might not include ignored files
        // depending on gitignore handling (this tests the ignore functionality)
        assert!(file_names.contains(&"normal.txt".to_string()));

        Ok(())
    }

    #[test]
    fn test_expand_paths__symlinks() -> TestResult {
        let temp_dir = tempfile::tempdir()?;
        let base = temp_dir.path();

        // Create a regular file
        fs::write(base.join("target.txt"), "target file")?;

        // Try to create a symlink (may fail on some systems)
        let symlink_path = base.join("link.txt");
        let target_path = base.join("target.txt");

        #[cfg(unix)]
        {
            if std::os::unix::fs::symlink(&target_path, &symlink_path).is_ok() {
                let result = expand_paths(vec![&symlink_path], false, None)?;

                // Should handle symlinks properly
                assert!(result.len() <= 1); // May be 0 or 1 depending on symlink handling
            }
        }

        // Always test the target file works
        let result = expand_paths(vec![&target_path], false, None)?;
        assert_eq!(result.len(), 1);

        Ok(())
    }
}