debian-workspace 0.181.1

Workspace for manipulating Debian packages
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
605
606
607
608
609
610
//! `Workspace`: an abstraction over the on-disk or in-editor state of a
//! Debian source package.
//!
//! Fixers historically reached into the working tree directly via
//! `std::fs`. That ties them to a particular host (the lintian-brush CLI,
//! which writes the tree to disk before invoking fixers). The
//! `Workspace` trait abstracts that access so the same fixer code can
//! also run inside an editor host (debian-lsp), where the source of truth for
//! a file is the open buffer rather than the path on disk.
//!
//! Two implementations are intended:
//!
//! * [`FsWorkspace`] — pure-`std` shim that operates on a base
//!   directory on disk. Used by the lintian-brush CLI; preserves the
//!   existing semantics where the harness writes the tree to disk, the
//!   fixer mutates files there, and the harness diffs the result.
//! * `LspWorkspace` (lives in debian-lsp) — wraps a salsa-backed
//!   in-memory workspace. Mutations are accumulated as a single
//!   `WorkspaceEdit` rather than being written back to disk.
//!
//! The trait is deliberately `breezyshim`-free so that hosts that don't want
//! a Python runtime (notably debian-lsp) can depend on it without pulling in
//! PyO3.

use std::path::{Path, PathBuf};

use debian_changelog::ChangeLog;
use debian_control::lossless::Control;
use debian_copyright::lossless::Copyright;
use debian_watch::parse::ParsedWatchFile;
use dep3::lossless::PatchHeader;
use makefile_lossless::Makefile;
use patchkit::edit::Patch;
use patchkit::quilt::Series;
use toml_edit::DocumentMut;

use crate::{Error, Version};

/// An editor handle for a single file in a [`Workspace`].
///
/// The parsed value is reachable via `Deref`/`DerefMut`; mutate it as you
/// would the bare type. Changes are persisted by calling
/// [`commit`](Self::commit). Dropping an editor without committing discards
/// the changes (and emits a warning) — explicit commit is required so that
/// serialisation failures can be reported.
///
/// `T` is the parsed representation (e.g.
/// [`debian_control::lossless::Control`]).
pub trait Editor<T>: std::ops::Deref<Target = T> + std::ops::DerefMut<Target = T> {
    /// Persist any modifications to the underlying workspace.
    ///
    /// For a tree-backed workspace this writes the file back to disk; for an
    /// editor-backed workspace it records a `TextEdit` against the buffer.
    /// Calling `commit` more than once is a no-op.
    fn commit(self: Box<Self>) -> Result<(), Error>;
}

/// Access to a Debian source package, as seen by a fixer.
///
/// Each typed accessor returns an editor for a well-known file. Callers can
/// also reach less-common files via [`read_file`](Self::read_file) /
/// [`write_file`](Self::write_file).
pub trait Workspace {
    /// The source package name, as read from `debian/changelog`.
    ///
    /// Returns `None` when the changelog is missing or unreadable. Hosts
    /// that legitimately don't have a changelog (e.g. an LSP that lost
    /// access to it) should return `None` rather than fabricating a name.
    fn package(&self) -> Option<&str>;

    /// The current version of the package, as read from `debian/changelog`.
    ///
    /// Returns `None` when the changelog is missing or unreadable.
    fn current_version(&self) -> Option<&Version>;

    /// Read `debian/control` and return a parsed value.
    ///
    /// Returns `Err(Error::NotFound)` if the file is missing —
    /// detectors typically want that exact response.
    ///
    /// Parsing is relaxed: syntax errors are tolerated and the resulting
    /// AST may have missing or partially-recovered nodes. Detectors that
    /// need to reject malformed input should validate the structure they
    /// care about (e.g. that the source paragraph or a particular field
    /// exists) rather than expecting `Err`.
    ///
    /// Implementations may cache the parse; the returned value is owned
    /// (`Control` is cheap to clone — its rowan green nodes are shared
    /// internally).
    fn parsed_control(&self) -> Result<Control, Error>;

    /// Read `debian/changelog` and return a parsed value.
    ///
    /// Returns `Err(Error::NotFound)` if the file is missing. Parsing is
    /// relaxed; see [`parsed_control`](Self::parsed_control) for details
    /// on what that means.
    fn parsed_changelog(&self) -> Result<ChangeLog, Error>;

    /// Read `debian/copyright` and return a parsed value.
    ///
    /// Returns `Err(Error::NotFound)` if the file is missing, and
    /// `Err(Error::Parse)` only when the file isn't a machine-readable
    /// DEP-5 document at all (i.e. doesn't start with `Format:`).
    /// Parsing is otherwise relaxed; see
    /// [`parsed_control`](Self::parsed_control) for details on what that
    /// means.
    fn parsed_copyright(&self) -> Result<Copyright, Error>;

    /// Read `debian/upstream/metadata` and return its parsed YAML.
    ///
    /// Returns `Err(Error::NotFound)` if the file is missing or
    /// unparseable.
    fn parsed_upstream_metadata(&self) -> Result<yaml_edit::YamlFile, Error>;

    /// Read `debian/watch` and return a parsed value.
    ///
    /// Returns `Err(Error::NotFound)` if the file is missing.
    fn parsed_watch(&self) -> Result<ParsedWatchFile, Error>;

    /// Read `debian/rules` and return the parsed Makefile.
    ///
    /// Returns `Err(Error::NotFound)` if the file is missing. Uses
    /// `Makefile::read_relaxed`, mirroring the behaviour every fixer
    /// currently expects from `debian/rules` parsing.
    fn parsed_rules(&self) -> Result<Makefile, Error>;

    /// Read and parse `debian/patches/series`, the quilt patch series.
    ///
    /// Returns `Ok(None)` if the file does not exist (the package ships
    /// no quilt patches). Returns `Err` only if the file exists but
    /// cannot be read as a series.
    fn parsed_patches_series(&self) -> Result<Option<Series>, Error> {
        let rel = Path::new("debian/patches/series");
        match self.read_file(rel)? {
            None => Ok(None),
            Some(bytes) => {
                let series = Series::read(&bytes[..]).map_err(Error::Io)?;
                Ok(Some(series))
            }
        }
    }

    /// Read a quilt patch file and return its parsed DEP-3 header
    /// together with the parsed diff.
    ///
    /// `rel` is the patch's path relative to the package root (e.g.
    /// `debian/patches/fix-foo.patch`), as obtained by joining
    /// `debian/patches` with a name from [`parsed_patches_series`].
    ///
    /// Returns `Ok(None)` when the file does not exist. On success the
    /// tuple's first element is the patch's DEP-3 header, or `None` when
    /// the patch carries no header (a bare diff) or its header does not
    /// parse — the header is optional metadata. The second element is
    /// the lossless parse of the diff body; that parser is
    /// error-recovering, so a [`Patch`] is produced even for a malformed
    /// diff.
    ///
    /// Returns `Err(Error::Parse)` if the file exists but is not valid
    /// UTF-8.
    ///
    /// [`parsed_patches_series`]: Self::parsed_patches_series
    fn parsed_patch(&self, rel: &Path) -> Result<Option<(Option<PatchHeader>, Patch)>, Error> {
        let Some(bytes) = self.read_file(rel)? else {
            return Ok(None);
        };
        let content = std::str::from_utf8(&bytes)
            .map_err(|e| Error::Parse(format!("{} is not valid UTF-8: {}", rel.display(), e)))?;
        let header_end = dep3::lossless::header_end(content);
        let header_text = &content[..header_end];
        let header = if header_text.trim().is_empty() {
            None
        } else {
            header_text.parse::<PatchHeader>().ok()
        };
        let patch = patchkit::edit::parse(&content[header_end..]).tree();
        Ok(Some((header, patch)))
    }

    /// Read the trimmed contents of `debian/source/format`.
    ///
    /// Returns `Ok(None)` if the file is missing. The default format
    /// (`1.0`) is *not* substituted — callers see exactly what is on
    /// disk so they can distinguish "no file" from "explicit 1.0".
    fn source_format(&self) -> Result<Option<String>, Error>;

    /// Open `debian/control` for editing.
    ///
    /// Takes `&self` so that fixers can hold an editor and still call
    /// other workspace methods (`read_file`, …). Implementations
    /// that need to record edits on the workspace itself should use interior
    /// mutability.
    ///
    /// Detectors don't need this — they emit `Action`s for the appliers to
    /// run. Use [`parsed_control`](Self::parsed_control) instead.
    fn control(&self) -> Result<Box<dyn Editor<Control> + '_>, Error>;

    /// Open `debian/changelog` for editing. See [`control`](Self::control).
    fn changelog(&self) -> Result<Box<dyn Editor<ChangeLog> + '_>, Error>;

    /// Read `debian/debcargo.toml` and return a parsed TOML document.
    ///
    /// Returns `Ok(None)` if the file does not exist (package is not a
    /// debcargo-managed crate). Returns `Err` if the file exists but cannot
    /// be parsed.
    fn parsed_debcargo(&self) -> Result<Option<DocumentMut>, Error> {
        let rel = Path::new("debian/debcargo.toml");
        match self.read_file(rel)? {
            None => Ok(None),
            Some(bytes) => {
                let text = String::from_utf8(bytes.into_owned()).map_err(|e| {
                    Error::Parse(format!("debcargo.toml is not valid UTF-8: {}", e))
                })?;
                let doc: DocumentMut = text
                    .parse()
                    .map_err(|e| Error::Parse(format!("Failed to parse debcargo.toml: {}", e)))?;
                Ok(Some(doc))
            }
        }
    }

    /// Open `debian/debcargo.toml` for editing.
    ///
    /// Returns `Ok(None)` if the file does not exist.
    /// Returns `Err` if the file exists but cannot be parsed.
    fn debcargo(&self) -> Result<Option<Box<dyn Editor<DocumentMut> + '_>>, Error>;

    /// Read raw bytes of an arbitrary file relative to the package root.
    ///
    /// Returns `Ok(None)` if the file does not exist.
    ///
    /// The returned `Cow` is borrowed when the host has the bytes
    /// already in memory (an LSP host with the file open in an editor
    /// buffer) and owned when they had to be fetched (a disk read).
    /// Detectors that need owned bytes can call `.into_owned()`.
    fn read_file(&self, rel: &Path) -> Result<Option<std::borrow::Cow<'_, [u8]>>, Error>;

    /// Write raw bytes to an arbitrary file relative to the package root.
    ///
    /// Creates the file if it does not exist.
    fn write_file(&self, rel: &Path, content: &[u8]) -> Result<(), Error>;

    /// List the entries of a directory relative to the package root.
    ///
    /// Returns the file (and subdirectory) names within `rel`, without any
    /// path prefix. Returns `Ok(None)` if the directory does not exist.
    ///
    /// The order of returned entries is unspecified — a non-`Tree` host
    /// (an LSP) may not have a meaningful directory ordering.
    fn list_dir(&self, rel: &Path) -> Result<Option<Vec<String>>, Error>;

    /// Recursively walk `rel`, returning the relative paths of every
    /// regular file beneath it (paths are relative to the package root,
    /// not to `rel`).
    ///
    /// Symbolic links and other non-regular entries are skipped. Returns
    /// `Ok(None)` if `rel` does not exist.
    ///
    /// The order of returned paths is unspecified. Hosts that can't
    /// meaningfully walk a tree (e.g. an LSP that only knows about open
    /// buffers) may return only the files they currently track.
    fn walk_dir(&self, rel: &Path) -> Result<Option<Vec<PathBuf>>, Error> {
        // Default impl: depth-first walk via list_dir + read_file.
        // Hosts that have a faster path can override.
        let Some(top_entries) = self.list_dir(rel)? else {
            return Ok(None);
        };
        let mut out = Vec::new();
        let mut stack: Vec<(PathBuf, Vec<String>)> = vec![(rel.to_path_buf(), top_entries)];
        while let Some((dir, entries)) = stack.pop() {
            for name in entries {
                let child = dir.join(&name);
                match self.list_dir(&child)? {
                    Some(sub) => stack.push((child, sub)),
                    None => out.push(child),
                }
            }
        }
        Ok(Some(out))
    }

    /// Read the Unix file mode of `rel`, or `None` if the file is missing.
    ///
    /// Hosts that don't track a meaningful mode (e.g. an LSP serving an
    /// in-memory buffer) may return `Ok(None)` even when the file exists.
    /// Detectors that key off mode (e.g. checking that `debian/rules` is
    /// executable) treat that the same as "not present" and skip.
    fn file_mode(&self, rel: &Path) -> Result<Option<u32>, Error>;

    /// On-disk root for hosts that have one.
    ///
    /// Returns `Some` for the lintian-brush CLI ([`FsWorkspace`])
    /// where the package has been materialised to disk. Returns `None`
    /// for in-memory hosts (an LSP serving open buffers); detectors that
    /// genuinely need to walk the source tree (e.g. an upstream-metadata
    /// guesser, a license scanner) should treat `None` as "skip — we
    /// can't help here".
    ///
    /// Prefer the typed accessors ([`read_file`](Self::read_file),
    /// [`list_dir`](Self::list_dir), …) wherever possible. Reach for
    /// this only when an external library insists on a `&Path` for the
    /// whole tree.
    fn base_path(&self) -> Option<&Path> {
        None
    }
}

/// Read the debhelper compat level from a workspace.
///
/// Looks at `debian/compat` first, then falls back to the `X-DH-Compat`
/// field or a `debhelper-compat` build dependency in `debian/control`.
/// Returns `Ok(None)` when neither source is present or parseable.
pub fn compat_level(ws: &dyn Workspace) -> Result<Option<u8>, Error> {
    if let Some(bytes) = ws.read_file(Path::new("debian/compat"))? {
        if let Ok(text) = std::str::from_utf8(&bytes) {
            let trimmed = text
                .split_once('#')
                .map_or(text, |(before, _)| before)
                .trim();
            if let Ok(level) = trimmed.parse::<u8>() {
                return Ok(Some(level));
            }
        }
    }

    let control = match ws.parsed_control() {
        Ok(c) => c,
        Err(Error::NotFound) => return Ok(None),
        Err(e) => return Err(e),
    };
    let Some(source) = control.source() else {
        return Ok(None);
    };

    if let Some(dh_compat) = source.as_deb822().get("X-DH-Compat") {
        let trimmed = dh_compat
            .split_once('#')
            .map_or(dh_compat.as_str(), |(before, _)| before)
            .trim();
        if let Ok(level) = trimmed.parse::<u8>() {
            return Ok(Some(level));
        }
    }

    let Some(build_depends) = source.build_depends() else {
        return Ok(None);
    };
    let Some(rel) = build_depends
        .entries()
        .flat_map(|entry| entry.relations().collect::<Vec<_>>())
        .find(|r| r.try_name().as_deref() == Some("debhelper-compat"))
    else {
        return Ok(None);
    };
    Ok(rel
        .version()
        .and_then(|(_op, v)| v.to_string().parse::<u8>().ok()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fs_workspace::FsWorkspace;
    use std::collections::BTreeMap;
    use tempfile::TempDir;

    /// A minimal in-memory `Workspace` used to exercise the trait's *default*
    /// method bodies. `FsWorkspace` overrides `walk_dir`, so it can't drive the
    /// default walk; this mock deliberately does not.
    #[derive(Default)]
    struct MockWorkspace {
        // Maps a relative path to its raw bytes. Directories are implied by
        // the path components of the files they contain.
        files: BTreeMap<PathBuf, Vec<u8>>,
    }

    impl MockWorkspace {
        fn with_file(mut self, rel: &str, content: &[u8]) -> Self {
            self.files.insert(PathBuf::from(rel), content.to_vec());
            self
        }

        /// The set of directory paths implied by the stored files.
        fn dirs(&self) -> std::collections::BTreeSet<PathBuf> {
            let mut dirs = std::collections::BTreeSet::new();
            for f in self.files.keys() {
                let mut cur = f.parent();
                while let Some(p) = cur {
                    if p.as_os_str().is_empty() {
                        break;
                    }
                    dirs.insert(p.to_path_buf());
                    cur = p.parent();
                }
            }
            dirs
        }
    }

    impl Workspace for MockWorkspace {
        fn package(&self) -> Option<&str> {
            None
        }
        fn current_version(&self) -> Option<&Version> {
            None
        }
        fn parsed_control(&self) -> Result<Control, Error> {
            Err(Error::NotFound)
        }
        fn parsed_changelog(&self) -> Result<ChangeLog, Error> {
            Err(Error::NotFound)
        }
        fn parsed_copyright(&self) -> Result<Copyright, Error> {
            Err(Error::NotFound)
        }
        fn parsed_upstream_metadata(&self) -> Result<yaml_edit::YamlFile, Error> {
            Err(Error::NotFound)
        }
        fn parsed_watch(&self) -> Result<ParsedWatchFile, Error> {
            Err(Error::NotFound)
        }
        fn parsed_rules(&self) -> Result<Makefile, Error> {
            Err(Error::NotFound)
        }
        fn source_format(&self) -> Result<Option<String>, Error> {
            Ok(None)
        }
        fn control(&self) -> Result<Box<dyn Editor<Control> + '_>, Error> {
            Err(Error::NotFound)
        }
        fn changelog(&self) -> Result<Box<dyn Editor<ChangeLog> + '_>, Error> {
            Err(Error::NotFound)
        }
        fn debcargo(&self) -> Result<Option<Box<dyn Editor<DocumentMut> + '_>>, Error> {
            Ok(None)
        }
        fn read_file(&self, rel: &Path) -> Result<Option<std::borrow::Cow<'_, [u8]>>, Error> {
            Ok(self
                .files
                .get(rel)
                .map(|v| std::borrow::Cow::Borrowed(v.as_slice())))
        }
        fn write_file(&self, _rel: &Path, _content: &[u8]) -> Result<(), Error> {
            unimplemented!("not needed by tests")
        }
        fn list_dir(&self, rel: &Path) -> Result<Option<Vec<String>>, Error> {
            let dirs = self.dirs();
            // The directory must exist (be implied by some file).
            if !dirs.contains(rel) {
                return Ok(None);
            }
            let mut names = std::collections::BTreeSet::new();
            // Direct child files.
            for f in self.files.keys() {
                if f.parent() == Some(rel) {
                    names.insert(f.file_name().unwrap().to_string_lossy().into_owned());
                }
            }
            // Direct child directories.
            for d in &dirs {
                if d.parent() == Some(rel) {
                    names.insert(d.file_name().unwrap().to_string_lossy().into_owned());
                }
            }
            Ok(Some(names.into_iter().collect()))
        }
        fn file_mode(&self, _rel: &Path) -> Result<Option<u32>, Error> {
            Ok(None)
        }
    }

    fn make_pkg_with_control(dir: &Path, control: &str) {
        let debian = dir.join("debian");
        std::fs::create_dir_all(&debian).unwrap();
        std::fs::write(debian.join("control"), control).unwrap();
    }

    fn fs_workspace(dir: &Path) -> FsWorkspace {
        FsWorkspace::new(dir, None, None)
    }

    #[test]
    fn default_walk_dir_recurses_nested_dirs() {
        let ws = MockWorkspace::default()
            .with_file("src/top.txt", b"a")
            .with_file("src/sub/deep.txt", b"b")
            .with_file("src/sub/other.txt", b"c");
        let mut files = ws.walk_dir(Path::new("src")).unwrap().unwrap();
        files.sort();
        assert_eq!(
            files,
            vec![
                PathBuf::from("src/sub/deep.txt"),
                PathBuf::from("src/sub/other.txt"),
                PathBuf::from("src/top.txt"),
            ]
        );
    }

    #[test]
    fn default_walk_dir_missing_returns_none() {
        let ws = MockWorkspace::default().with_file("src/top.txt", b"a");
        assert_eq!(ws.walk_dir(Path::new("nonexistent")).unwrap(), None);
    }

    #[test]
    fn default_walk_dir_empty_dir_returns_empty_vec() {
        // An existing-but-empty directory must yield Some(empty), distinct from
        // both Ok(None) (missing) and a non-empty result.
        let ws = EmptyDirWorkspace;
        assert_eq!(
            ws.walk_dir(Path::new("empty")).unwrap(),
            Some(Vec::<PathBuf>::new())
        );
    }

    /// Workspace whose `empty` directory exists but has no entries.
    struct EmptyDirWorkspace;
    impl Workspace for EmptyDirWorkspace {
        fn package(&self) -> Option<&str> {
            None
        }
        fn current_version(&self) -> Option<&Version> {
            None
        }
        fn parsed_control(&self) -> Result<Control, Error> {
            Err(Error::NotFound)
        }
        fn parsed_changelog(&self) -> Result<ChangeLog, Error> {
            Err(Error::NotFound)
        }
        fn parsed_copyright(&self) -> Result<Copyright, Error> {
            Err(Error::NotFound)
        }
        fn parsed_upstream_metadata(&self) -> Result<yaml_edit::YamlFile, Error> {
            Err(Error::NotFound)
        }
        fn parsed_watch(&self) -> Result<ParsedWatchFile, Error> {
            Err(Error::NotFound)
        }
        fn parsed_rules(&self) -> Result<Makefile, Error> {
            Err(Error::NotFound)
        }
        fn source_format(&self) -> Result<Option<String>, Error> {
            Ok(None)
        }
        fn control(&self) -> Result<Box<dyn Editor<Control> + '_>, Error> {
            Err(Error::NotFound)
        }
        fn changelog(&self) -> Result<Box<dyn Editor<ChangeLog> + '_>, Error> {
            Err(Error::NotFound)
        }
        fn debcargo(&self) -> Result<Option<Box<dyn Editor<DocumentMut> + '_>>, Error> {
            Ok(None)
        }
        fn read_file(&self, _rel: &Path) -> Result<Option<std::borrow::Cow<'_, [u8]>>, Error> {
            Ok(None)
        }
        fn write_file(&self, _rel: &Path, _content: &[u8]) -> Result<(), Error> {
            unimplemented!()
        }
        fn list_dir(&self, rel: &Path) -> Result<Option<Vec<String>>, Error> {
            if rel == Path::new("empty") {
                Ok(Some(vec![]))
            } else {
                Ok(None)
            }
        }
        fn file_mode(&self, _rel: &Path) -> Result<Option<u32>, Error> {
            Ok(None)
        }
    }

    #[test]
    fn compat_level_from_compat_file() {
        let tmp = TempDir::new().unwrap();
        std::fs::create_dir_all(tmp.path().join("debian")).unwrap();
        std::fs::write(tmp.path().join("debian/compat"), "10\n").unwrap();
        assert_eq!(compat_level(&fs_workspace(tmp.path())).unwrap(), Some(10));
    }

    #[test]
    fn compat_level_from_build_depends() {
        let tmp = TempDir::new().unwrap();
        make_pkg_with_control(
            tmp.path(),
            "Source: foo\nBuild-Depends: debhelper-compat (= 13)\n\nPackage: foo\nArchitecture: any\nDescription: x\n y\n",
        );
        assert_eq!(compat_level(&fs_workspace(tmp.path())).unwrap(), Some(13));
    }

    #[test]
    fn compat_level_build_depends_without_debhelper_compat() {
        let tmp = TempDir::new().unwrap();
        make_pkg_with_control(
            tmp.path(),
            "Source: foo\nBuild-Depends: debhelper (>= 12)\n\nPackage: foo\nArchitecture: any\nDescription: x\n y\n",
        );
        assert_eq!(compat_level(&fs_workspace(tmp.path())).unwrap(), None);
    }

    #[test]
    fn compat_level_none_when_no_sources() {
        let tmp = TempDir::new().unwrap();
        make_pkg_with_control(
            tmp.path(),
            "Source: foo\n\nPackage: foo\nArchitecture: any\nDescription: x\n y\n",
        );
        assert_eq!(compat_level(&fs_workspace(tmp.path())).unwrap(), None);
    }
}