yt-dlp 2.7.2

🎬️ A Rust library (with auto dependencies installation) for Youtube downloading
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
use std::error::Error as StdError;
use std::path::PathBuf;

use yt_dlp::error::{ArchiveError, Error};
use yt_dlp::model::format::FormatType;

// ============================== Error Display ==============================

#[test]
fn error_io_display() {
    let err = Error::io(
        "read file",
        std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
    );
    let display = format!("{}", err);
    assert!(display.contains("IO error during read file"));
}

#[test]
fn error_io_with_path_display() {
    let err = Error::io_with_path(
        "read file",
        PathBuf::from("/tmp/test.mp4"),
        std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
    );
    let display = format!("{}", err);
    assert!(display.contains("IO error during read file"));
}

#[test]
fn error_timeout_display() {
    let err = Error::Timeout {
        operation: "downloading".to_string(),
        duration: std::time::Duration::from_secs(30),
    };
    let display = format!("{}", err);
    assert!(display.contains("Timeout"));
    assert!(display.contains("downloading"));
}

#[test]
fn error_command_failed_display() {
    let err = Error::CommandFailed {
        command: "yt-dlp".to_string(),
        exit_code: 1,
        stderr: "error output".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("yt-dlp"));
    assert!(display.contains("1"));
}

#[test]
fn error_video_fetch_display() {
    let err = Error::VideoFetch {
        url: "https://youtube.com/watch?v=abc".to_string(),
        reason: "geo-blocked".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("youtube.com"));
    assert!(display.contains("geo-blocked"));
}

#[test]
fn error_video_missing_field_display() {
    let err = Error::VideoMissingField {
        video_id: "abc123".to_string(),
        field: "title".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("abc123"));
    assert!(display.contains("title"));
}

#[test]
fn error_format_not_available_display() {
    let err = Error::FormatNotAvailable {
        video_id: "abc123".to_string(),
        format_type: FormatType::Audio,
        available_formats: vec!["mp4".to_string(), "webm".to_string()],
    };
    let display = format!("{}", err);
    assert!(display.contains("abc123"));
}

#[test]
fn error_format_no_url_display() {
    let err = Error::FormatNoUrl {
        video_id: "abc".to_string(),
        format_id: "251".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("251"));
    assert!(display.contains("no download URL"));
}

#[test]
fn error_format_incompatible_display() {
    let err = Error::FormatIncompatible {
        format_id: "251".to_string(),
        reason: "audio-only".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("251"));
    assert!(display.contains("audio-only"));
}

#[test]
fn error_no_thumbnail_display() {
    let err = Error::NoThumbnail {
        video_id: "abc".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("abc"));
    assert!(display.contains("thumbnail"));
}

#[test]
fn error_subtitle_not_available_display() {
    let err = Error::SubtitleNotAvailable {
        video_id: "abc".to_string(),
        language: "fr".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("abc"));
    assert!(display.contains("fr"));
}

#[test]
fn error_url_expired_display() {
    let err = Error::UrlExpired;
    assert_eq!(format!("{}", err), "URL expired");
}

#[test]
fn error_path_validation_display() {
    let err = Error::PathValidation {
        path: PathBuf::from("../../../etc/passwd"),
        reason: "path traversal detected".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("path traversal"));
}

#[test]
fn error_url_validation_display() {
    let err = Error::UrlValidation {
        url: "javascript:alert(1)".to_string(),
        reason: "unsafe scheme".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("javascript"));
    assert!(display.contains("unsafe scheme"));
}

#[test]
fn error_download_failed_display() {
    let err = Error::DownloadFailed {
        download_id: 42,
        reason: "connection reset".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("42"));
    assert!(display.contains("connection reset"));
}

#[test]
fn error_download_cancelled_display() {
    let err = Error::DownloadCancelled { download_id: 7 };
    let display = format!("{}", err);
    assert!(display.contains("7"));
    assert!(display.contains("cancelled"));
}

#[test]
fn error_invalid_partial_range_display() {
    let err = Error::InvalidPartialRange {
        reason: "start exceeds end".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("start exceeds end"));
}

#[test]
fn error_metadata_display() {
    let err = Error::Metadata {
        operation: "write".to_string(),
        path: PathBuf::from("/tmp/file.mp4"),
        reason: "unsupported format".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("write"));
    assert!(display.contains("unsupported format"));
}

#[test]
fn error_cache_miss_display() {
    let err = Error::CacheMiss {
        key: "video:abc123".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("video:abc123"));
}

#[test]
fn error_cache_expired_display() {
    let err = Error::CacheExpired {
        key: "video:abc123".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("expired"));
}

#[test]
fn error_checksum_mismatch_display() {
    let err = Error::ChecksumMismatch {
        path: PathBuf::from("/tmp/file.mp4"),
        expected: "abc123".to_string(),
        actual: "def456".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("abc123"));
    assert!(display.contains("def456"));
}

#[test]
fn error_invalid_header_display() {
    let err = Error::InvalidHeader {
        header: "Content-Type".to_string(),
        reason: "invalid value".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("Content-Type"));
}

#[test]
fn error_unexpected_status_display() {
    let err = Error::UnexpectedStatus {
        status: 403,
        url: "https://example.com".to_string(),
    };
    let display = format!("{}", err);
    assert!(display.contains("403"));
}

#[test]
fn error_unknown_display() {
    let err = Error::Unknown("something went wrong".to_string());
    let display = format!("{}", err);
    assert!(display.contains("something went wrong"));
}

// ============================== Error Debug ==============================

#[test]
fn error_debug() {
    let err = Error::UrlExpired;
    let debug = format!("{:?}", err);
    assert!(debug.contains("UrlExpired"));
}

// ============================== Error constructors ==============================

#[test]
fn error_io_constructor() {
    let err = Error::io("test op", std::io::Error::other("test"));
    match err {
        Error::IO { operation, path, .. } => {
            assert_eq!(operation, "test op");
            assert!(path.is_none());
        }
        _ => panic!("Expected IO variant"),
    }
}

#[test]
fn error_io_with_path_constructor() {
    let err = Error::io_with_path("test op", "/tmp/test.txt", std::io::Error::other("test"));
    match err {
        Error::IO { operation, path, .. } => {
            assert_eq!(operation, "test op");
            assert_eq!(path, Some(PathBuf::from("/tmp/test.txt")));
        }
        _ => panic!("Expected IO variant"),
    }
}

// ============================== From impls ==============================

#[test]
fn error_from_io() {
    let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
    let err: Error = io_err.into();
    matches!(err, Error::IO { .. });
}

#[test]
fn error_from_serde_json() {
    let json_err = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
    let err: Error = json_err.into();
    matches!(err, Error::Json { .. });
}

// ============================== NoBinaryRelease ==============================

#[test]
fn error_no_binary_release_display() {
    let err = Error::NoBinaryRelease {
        binary: "yt-dlp".to_string(),
        platform: yt_dlp::utils::platform::Platform::Mac,
        architecture: yt_dlp::utils::platform::Architecture::X64,
    };
    let display = format!("{}", err);
    assert!(display.contains("yt-dlp"));
}

#[test]
fn error_binary_not_found_display() {
    let err = Error::BinaryNotFound {
        binary: "ffmpeg".to_string(),
        path: PathBuf::from("/usr/local/bin/ffmpeg"),
    };
    let display = format!("{}", err);
    assert!(display.contains("ffmpeg"));
    assert!(display.contains("not found"));
}

// ============================== Error constructor: http ==============================

#[test]
fn error_http_constructor() {
    // Build a reqwest error by trying to parse an invalid URL
    let client = reqwest::Client::new();
    let req_err = client.get("http://[::ffff::127.0.0.1]").build().unwrap_err();
    let err = Error::http("https://example.com/video", "downloading video", req_err);
    match err {
        Error::Http {
            url, context, source, ..
        } => {
            assert_eq!(url, "https://example.com/video");
            assert_eq!(context, "downloading video");
            assert!(!source.to_string().is_empty());
        }
        _ => panic!("Expected Http variant"),
    }
}

// ============================== Error constructor: json ==============================

#[test]
fn error_json_constructor() {
    let json_err = serde_json::from_str::<serde_json::Value>("not json").unwrap_err();
    let err = Error::json("parsing video metadata", json_err);
    match err {
        Error::Json { context, source, .. } => {
            assert_eq!(context, "parsing video metadata");
            assert!(!source.to_string().is_empty());
        }
        _ => panic!("Expected Json variant"),
    }
}

// ============================== Error constructor: video_fetch ==============================

#[test]
fn error_video_fetch_constructor() {
    let err = Error::video_fetch("https://youtube.com/watch?v=abc", "geo-restricted");
    match err {
        Error::VideoFetch { url, reason } => {
            assert_eq!(url, "https://youtube.com/watch?v=abc");
            assert_eq!(reason, "geo-restricted");
        }
        _ => panic!("Expected VideoFetch variant"),
    }
}

// ============================== Error constructor: download_failed ==============================

#[test]
fn error_download_failed_constructor() {
    let err = Error::download_failed(42, "connection reset");
    match err {
        Error::DownloadFailed {
            download_id, reason, ..
        } => {
            assert_eq!(download_id, 42);
            assert_eq!(reason, "connection reset");
        }
        _ => panic!("Expected DownloadFailed variant"),
    }
}

// ============================== Error constructor: cache_miss ==============================

#[test]
fn error_cache_miss_constructor() {
    let err = Error::cache_miss("video:abc123");
    match err {
        Error::CacheMiss { key } => {
            assert_eq!(key, "video:abc123");
        }
        _ => panic!("Expected CacheMiss variant"),
    }
}

// ============================== Error constructor: cache_expired ==============================

#[test]
fn error_cache_expired_constructor() {
    let err = Error::cache_expired("video:abc123");
    match err {
        Error::CacheExpired { key } => {
            assert_eq!(key, "video:abc123");
        }
        _ => panic!("Expected CacheExpired variant"),
    }
}

// ============================== Error constructor: path_validation ==============================

#[test]
fn error_path_validation_constructor() {
    let err = Error::path_validation("/etc/passwd", "absolute path not allowed");
    match err {
        Error::PathValidation { path, reason } => {
            assert_eq!(path, PathBuf::from("/etc/passwd"));
            assert_eq!(reason, "absolute path not allowed");
        }
        _ => panic!("Expected PathValidation variant"),
    }
}

// ============================== Error constructor: url_validation ==============================

#[test]
fn error_url_validation_constructor() {
    let err = Error::url_validation("ftp://evil.com", "unsafe scheme");
    match err {
        Error::UrlValidation { url, reason } => {
            assert_eq!(url, "ftp://evil.com");
            assert_eq!(reason, "unsafe scheme");
        }
        _ => panic!("Expected UrlValidation variant"),
    }
}

// ============================== Error constructor: invalid_partial_range ==============================

#[test]
fn error_invalid_partial_range_constructor() {
    let err = Error::invalid_partial_range("start > end");
    match err {
        Error::InvalidPartialRange { reason } => {
            assert_eq!(reason, "start > end");
        }
        _ => panic!("Expected InvalidPartialRange variant"),
    }
}

// ============================== Error constructor: metadata ==============================

#[test]
fn error_metadata_constructor() {
    let err = Error::metadata("write tags", "/tmp/file.mp3", "unsupported format");
    match err {
        Error::Metadata {
            operation,
            path,
            reason,
        } => {
            assert_eq!(operation, "write tags");
            assert_eq!(path, PathBuf::from("/tmp/file.mp3"));
            assert_eq!(reason, "unsupported format");
        }
        _ => panic!("Expected Metadata variant"),
    }
}

// ============================== source() chaining ==============================

#[test]
fn error_io_source_chaining() {
    let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
    let err = Error::io("opening file", io_err);
    let source = err.source();
    assert!(source.is_some());
    assert!(source.unwrap().to_string().contains("access denied"));
}

#[test]
fn error_json_source_chaining() {
    let json_err = serde_json::from_str::<serde_json::Value>("{invalid}").unwrap_err();
    let err = Error::json("parsing response", json_err);
    let source = err.source();
    assert!(source.is_some());
}

#[test]
fn error_from_io_source_chaining() {
    let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
    let err: Error = io_err.into();
    assert!(err.source().is_some());
}

// ============================== ArchiveError ==============================

#[test]
fn archive_error_invalid_format_display() {
    let err = ArchiveError::InvalidFormat;
    assert_eq!(format!("{}", err), "Invalid archive format");
}

#[test]
fn archive_error_corrupted_display() {
    let err = ArchiveError::Corrupted;
    assert_eq!(format!("{}", err), "Corrupted archive");
}

#[test]
fn archive_error_debug() {
    let err = ArchiveError::InvalidFormat;
    let debug = format!("{:?}", err);
    assert!(debug.contains("InvalidFormat"));
}

#[test]
fn error_archive_display() {
    let err = Error::Archive {
        file: "yt-dlp.zip".to_string(),
        source: ArchiveError::Corrupted,
    };
    let display = format!("{}", err);
    assert!(display.contains("yt-dlp.zip"));
    assert!(display.contains("Corrupted"));
}

#[test]
fn error_archive_source_chaining() {
    let err = Error::Archive {
        file: "test.zip".to_string(),
        source: ArchiveError::InvalidFormat,
    };
    let source = err.source();
    assert!(source.is_some());
    assert_eq!(source.unwrap().to_string(), "Invalid archive format");
}

// ============================== From<media_seek::Error> ==============================

#[test]
fn error_from_zip_error() {
    let err: Error = zip::result::ZipError::FileNotFound.into();
    match err {
        Error::Archive { file, source } => {
            assert_eq!(file, "unknown");
            matches!(source, ArchiveError::Zip(_));
        }
        _ => panic!("Expected Archive variant"),
    }
}