mortise-core 1.0.1

Reads Laravel apps that use nwidart/laravel-modules. No interface.
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Reading one app folder into a snapshot.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH;

use serde::{Deserialize, Serialize};

use crate::boundary::links::{self, Cycle, Link, ModuleSource};
use crate::boundary::rules::{self, CoverageSettings, Rules, RulesSource};
use crate::findings::{self, DEFAULT_COVERAGE_LIMIT, Findings, Subject};
use crate::modules::clover::{self, Coverage};
use crate::modules::code::propose_codes;
use crate::modules::composer::{Lock, Version};
use crate::modules::git::{self, Git};
use crate::modules::kind::Kinds;
use crate::modules::layout::Layout;
use crate::modules::module::{Module, ModuleState, ReadCache, find_modules};
use crate::package::PackageData;

#[cfg(feature = "watch")]
pub(crate) mod live;

const LARAVEL: &str = "laravel/framework";
const PACKAGE: &str = "nwidart/laravel-modules";
const ACTUATOR: &str = "nwidart/laravel-actuator";

/// The coverage report an app's coverage figures come from.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CoverageReport {
    /// The clover file. Absolute.
    pub path: PathBuf,
    /// When the file was written, in seconds since 1970.
    pub written: Option<i64>,
}

/// Everything Mortise knows about an app from one read of its folder.
///
/// A snapshot is never changed. A later read gives a new one.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AppSnapshot {
    /// The app folder: the one that holds `artisan`. Absolute.
    pub root: PathBuf,
    /// The name of the app folder.
    pub name: String,
    /// The version of `laravel/framework`. Absent when the lock file does not list it.
    pub laravel: Option<Version>,
    /// The version of `nwidart/laravel-modules`.
    pub package: Version,
    /// The version of `nwidart/laravel-actuator`, when the app has it.
    pub actuator: Option<Version>,
    /// `APP_URL` from `.env`, to propose the base URL of an environment.
    pub app_url: Option<String>,
    /// `APP_ENV` from `.env`. It tells Mortise to ask before a command in production.
    /// `APP_URL` and `APP_ENV` are the only lines Mortise reads from that file.
    pub app_env: Option<String>,
    pub layout: Layout,
    /// The modules, sorted by name.
    pub modules: Vec<Module>,
    /// The coverage report, when one was found.
    pub coverage_report: Option<CoverageReport>,
    pub git: Git,
    /// Which module uses which, by the name of the module that uses and then of the
    /// one that is used.
    pub links: Vec<Link>,
    /// The groups of modules that are on and use each other.
    pub cycles: Vec<Cycle>,
    /// The boundary rules the links were checked against.
    pub rules: Rules,
    pub rules_source: RulesSource,
    /// Why the `coverage` object of `mortise.json` is ignored, when it cannot be read.
    /// The parser's message. Coverage then comes from the settings.
    pub coverage_ignored: Option<String>,
    /// What the rules and the checks on files found.
    pub findings: Findings,
    /// A module covered less than this many per cent is a warning.
    pub coverage_limit: u8,
    /// What the package said in the reads the user started, from [`ReadOptions::package`].
    /// The read fills in the tables of each module's ran migrations.
    pub package_data: PackageData,
}

impl AppSnapshot {
    /// How many modules are turned on.
    pub fn modules_on(&self) -> usize {
        self.modules
            .iter()
            .filter(|module| module.state == ModuleState::On)
            .count()
    }

    /// Files in all modules together.
    pub fn files(&self) -> u64 {
        self.modules.iter().map(|module| module.files).sum()
    }

    /// Lines of PHP in all modules together.
    pub fn lines(&self) -> u64 {
        self.modules.iter().map(|module| module.lines).sum()
    }

    /// Tests in all modules together.
    pub fn tests(&self) -> u32 {
        self.modules.iter().map(|module| module.tests).sum()
    }

    /// The coverage of all modules together. `None` when no module has a figure.
    pub fn coverage(&self) -> Option<Coverage> {
        let mut total: Option<Coverage> = None;
        for coverage in self.modules.iter().filter_map(|module| module.coverage) {
            total.get_or_insert_default().add(coverage);
        }
        total
    }

    /// The link from one module to another, if the first uses the second.
    pub fn link(&self, from: &str, to: &str) -> Option<&Link> {
        self.links
            .iter()
            .find(|link| link.from == from && link.to == to)
    }

    /// The module of the circle of `module` that `module` uses directly: the link a
    /// circle finding opens on. `None` when `module` is in no circle.
    pub fn circle_neighbour(&self, module: &str) -> Option<&str> {
        crate::boundary::links::circle_neighbour(&self.links, module)
    }

    /// The links of modules that `name` uses.
    pub fn uses<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a Link> {
        self.links.iter().filter(move |link| link.from == name)
    }

    /// The links of modules that use `name`.
    pub fn used_by<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a Link> {
        self.links.iter().filter(move |link| link.to == name)
    }

    /// The links that break a rule.
    pub fn problem_links(&self) -> impl Iterator<Item = &Link> {
        self.links
            .iter()
            .filter(|link| link.is_problem(&self.rules))
    }

    /// The modules that carry a circle `name` is in without carrying it, by name. Empty
    /// while "No circular use" is off, because then no circle is a finding.
    pub fn circles_counted_elsewhere<'a>(&'a self, name: &str) -> Vec<&'a str> {
        let mut carriers: Vec<&str> = Vec::new();
        if self.rules.no_circular_use {
            for cycle in &self.cycles {
                let member = cycle.modules.iter().any(|module| module == name);
                if member && cycle.carrier != name && !carriers.contains(&cycle.carrier.as_str()) {
                    carriers.push(&cycle.carrier);
                }
            }
        }
        carriers.sort_unstable();
        carriers
    }

    /// The module with this name.
    pub fn module(&self, name: &str) -> Option<&Module> {
        self.modules.iter().find(|module| module.name == name)
    }

    /// The code of every module, by module name. Store it, and pass it to the next read.
    pub fn codes(&self) -> BTreeMap<String, String> {
        self.modules
            .iter()
            .map(|module| (module.name.clone(), module.code.clone()))
            .collect()
    }

    /// The modules folder as the user writes it: relative to the app folder when it
    /// is inside it, with `/` between the parts.
    pub fn modules_dir_shown(&self) -> String {
        self.path_shown(&self.layout.modules_dir)
    }

    /// A path as the user writes it: relative to the app folder when it is inside it.
    pub fn path_shown(&self, path: &Path) -> String {
        shown(&self.root, path)
    }
}

/// `path` relative to `root` when it is inside it, else whole. Parts are joined with `/`
/// on every platform, so a line opens in an editor and in a CI log on Windows too.
/// `.` and `..` are resolved by name first, so `Modules/../../shared` is outside.
pub(crate) fn shown(root: &Path, path: &Path) -> String {
    use std::path::{Component, Prefix};
    let path = lexical(path);
    let rest = path.strip_prefix(lexical(root)).unwrap_or(&path);
    let mut start = String::new();
    let mut parts: Vec<String> = Vec::new();
    for component in rest.components() {
        match component {
            // `C:` or `\\server\share` on Windows. A verbatim `\\?\D:` or `\\?\UNC\server\share`
            // names the same folder as `D:` or `\\server\share`, and only those take `/`.
            Component::Prefix(prefix) => match prefix.kind() {
                Prefix::Disk(drive) | Prefix::VerbatimDisk(drive) => {
                    start = format!("{}:", char::from(drive));
                }
                Prefix::UNC(server, share) | Prefix::VerbatimUNC(server, share) => {
                    start = format!("//{}/{}", server.to_string_lossy(), share.to_string_lossy());
                }
                // Any other verbatim path or a device keeps its spelling: it needs `\`.
                Prefix::Verbatim(_) | Prefix::DeviceNS(_) => {
                    return path.to_string_lossy().into_owned();
                }
            },
            Component::RootDir => start.push('/'),
            other => parts.push(other.as_os_str().to_string_lossy().into_owned()),
        }
    }
    start + &parts.join("/")
}

/// `path` with `.` and `..` resolved by name, without asking the file system.
/// A `..` at the root stays at the root; a `..` at the start of a relative path stays.
pub(crate) fn lexical(path: &Path) -> PathBuf {
    use std::path::Component;
    let mut parts: Vec<Component<'_>> = Vec::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => match parts.last() {
                Some(Component::Normal(_)) => {
                    parts.pop();
                }
                Some(Component::RootDir | Component::Prefix(_)) => {}
                _ => parts.push(component),
            },
            other => parts.push(other),
        }
    }
    parts.iter().collect()
}

/// Why a folder could not be read as an app.
#[derive(Debug, thiserror::Error)]
pub enum ReadError {
    /// The folder has no `artisan` file and no `composer.json` that requires `laravel/framework`.
    #[error("{} is not a Laravel app", root.display())]
    NotLaravel { root: PathBuf },
    /// The folder has no `composer.lock`, so no version can be read.
    #[error("{} has no composer.lock", root.display())]
    NoLock { root: PathBuf },
    /// `composer.lock` has no entry for `nwidart/laravel-modules`.
    #[error("{} does not use nwidart/laravel-modules", root.display())]
    NoPackage { root: PathBuf },
    /// `composer.lock` is not valid JSON.
    #[error("{} is not valid JSON: {message}", path.display())]
    InvalidLock { path: PathBuf, message: String },
    /// The coverage report given for the read, such as with `mortise check --coverage`,
    /// cannot be read or is no clover report.
    #[error("cannot read the coverage report {}: {message}", path.display())]
    CoverageReport { path: PathBuf, message: String },
    #[error("cannot read {}: {source}", path.display())]
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
}

/// `APP_ENV` from the `.env` file of the app in `root`, read now. A confirmation
/// checks it again just before a destructive command starts.
pub fn read_app_env(root: &Path) -> Option<String> {
    read_env(root).1
}

/// `APP_URL` and `APP_ENV` from the `.env` file of the app, in one read.
/// No other line is looked at.
fn read_env(root: &Path) -> (Option<String>, Option<String>) {
    match std::fs::read_to_string(root.join(".env")) {
        Ok(env) => (env_value(&env, "APP_URL"), env_value(&env, "APP_ENV")),
        Err(_) => (None, None),
    }
}

/// The value of one name in the text of a `.env` file. As in dotenv, a later line
/// of the same name replaces an earlier one.
fn env_value(env: &str, name: &str) -> Option<String> {
    let value = env.lines().rev().find_map(|line| {
        let line = line.trim_start();
        let line = line.strip_prefix("export ").unwrap_or(line);
        line.strip_prefix(name)?.trim_start().strip_prefix('=')
    })?;
    let value = value.trim();
    let value = match value.chars().next() {
        // A quoted value ends at the closing quote.
        Some(quote @ ('"' | '\'')) => value[1..].split(quote).next().unwrap_or_default(),
        // A plain value ends where a comment starts.
        _ => value.split(" #").next().unwrap_or_default().trim_end(),
    };
    (!value.is_empty()).then(|| value.to_owned())
}

/// Reads the app in `root`. Reads only: nothing in the folder is changed and nothing is run.
pub fn read_app(root: &Path) -> Result<AppSnapshot, ReadError> {
    read_app_with(root, &BTreeMap::new())
}

/// Reads the app in `root`, keeping the codes its modules already have.
///
/// `codes` maps a module name to its code. A module that is not in it gets a new
/// code that differs from all of them.
pub fn read_app_with(
    root: &Path,
    codes: &BTreeMap<String, String>,
) -> Result<AppSnapshot, ReadError> {
    read_app_cached(
        root,
        codes,
        &ReadOptions::default(),
        &mut ReadCache::default(),
    )
}

/// What the user set for an app in Mortise, as far as reading it is concerned.
///
/// The coverage limit and report come, first to last, from `coverage_given`, from the
/// `coverage` object of `mortise.json`, and from `coverage_limit` and `coverage_report`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadOptions {
    /// A module covered less than this many per cent is a warning.
    pub coverage_limit: u8,
    /// The clover report, relative to the app folder or absolute. `None` looks in
    /// `phpunit.xml` and the usual places.
    pub coverage_report: Option<PathBuf>,
    /// Coverage given for this read only, such as the flags of `mortise check`. A value
    /// that is set wins over `mortise.json`.
    pub coverage_given: CoverageSettings,
    /// What the package said in the reads the user started. Kept in memory only.
    pub package: PackageData,
}

impl Default for ReadOptions {
    fn default() -> Self {
        Self {
            coverage_limit: DEFAULT_COVERAGE_LIMIT,
            coverage_report: None,
            coverage_given: CoverageSettings::default(),
            package: PackageData::default(),
        }
    }
}

/// Reads the app in `root`. Files that did not change since they were put in
/// `cache` are not read again.
pub fn read_app_cached(
    root: &Path,
    codes: &BTreeMap<String, String>,
    options: &ReadOptions,
    cache: &mut ReadCache,
) -> Result<AppSnapshot, ReadError> {
    let root = std::path::absolute(root).map_err(|source| ReadError::Io {
        path: root.to_owned(),
        source,
    })?;

    if !root.join("artisan").is_file() && !requires_laravel(&root) {
        return Err(ReadError::NotLaravel { root });
    }

    let lock_path = root.join("composer.lock");
    let lock = match std::fs::read(&lock_path) {
        Ok(json) => Lock::parse(&json).map_err(|error| ReadError::InvalidLock {
            path: lock_path,
            message: error.to_string(),
        })?,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            return Err(ReadError::NoLock { root });
        }
        Err(source) => {
            return Err(ReadError::Io {
                path: lock_path,
                source,
            });
        }
    };
    let Some(package) = lock.version(PACKAGE) else {
        return Err(ReadError::NoPackage { root });
    };

    let (layout, config) = Layout::read(&root);
    let kinds = Kinds::new(config.as_ref(), &layout.app_folder);
    let (found, status_entries) = find_modules(&layout, &kinds, cache);

    // A code is kept once given. Only modules without one get a proposal.
    let taken: BTreeSet<String> = codes.values().cloned().collect();
    let new_names = found
        .iter()
        .map(|module| module.name.as_str())
        .filter(|name| !codes.contains_key(*name));
    let mut proposed = propose_codes(new_names, &taken).into_iter();
    let codes: Vec<String> = found
        .iter()
        .map(|module| match codes.get(&module.name) {
            Some(code) => code.clone(),
            None => proposed.next().unwrap_or_default(),
        })
        .collect();

    // A module's folder as git and the coverage report name it.
    let folders: Vec<String> = found
        .iter()
        .map(|module| shown(&root, &module.dir))
        .collect();

    // The rules, and the coverage settings that the app keeps beside them.
    let rules::RulesFile {
        rules,
        source: rules_source,
        coverage: in_file,
        coverage_ignored,
    } = rules::load_file(&root);
    let coverage_limit = options
        .coverage_given
        .limit
        .or(in_file.limit)
        .unwrap_or(options.coverage_limit);

    // A report given for this read must be read; a fault is an error, not a fallback.
    let report = match &options.coverage_given.report {
        Some(given) => {
            let path = root.join(given);
            let fault = |message: String| ReadError::CoverageReport {
                path: path.clone(),
                message,
            };
            let xml = std::fs::read(&path).map_err(|error| fault(error.to_string()))?;
            match clover::read_report(&xml) {
                (files, None) => Some((path, files)),
                (_, Some(message)) => return Err(fault(message)),
            }
        }
        // The report the user named, if it is there; else the one Mortise finds.
        None => in_file
            .report
            .as_ref()
            .or(options.coverage_report.as_ref())
            .map(|path| root.join(path))
            .filter(|path| path.is_file())
            .or_else(|| clover::find_report(&root))
            .and_then(|path| {
                let xml = std::fs::read(&path).ok()?;
                Some((path, clover::read_report(&xml).0))
            }),
    };
    let (report_path, report) = report.unzip();
    let coverage_of = |folder: &str| -> Option<Coverage> {
        // The report has the paths of the machine that ran the tests, so only the
        // part from the modules folder on can be compared.
        let inside = format!("/{folder}/");
        let at_start = format!("{folder}/");
        let mut total: Option<Coverage> = None;
        for file in report.as_ref()? {
            if file.path.contains(&inside) || file.path.starts_with(&at_start) {
                total.get_or_insert_default().add(file.coverage);
            }
        }
        total
    };

    // Which module uses which, checked against the rules of the app.
    let sources: Vec<ModuleSource<'_>> = found
        .iter()
        .zip(&folders)
        .map(|(module, folder)| ModuleSource {
            name: &module.name,
            on: module.state != ModuleState::Off,
            namespace: &module.namespace,
            surface: module.surface.as_ref(),
            files: module
                .scan
                .sources
                .iter()
                // A test is not a boundary of the running app.
                .filter(|source| !source.is_test)
                .map(|source| {
                    (
                        format!("{folder}/{}", source.relative),
                        source.analysis.refs.as_slice(),
                    )
                })
                .collect(),
            declared: module
                .scan
                .sources
                .iter()
                .flat_map(|source| {
                    source
                        .analysis
                        .declares
                        .iter()
                        .map(|declared| declared.class.as_str())
                })
                .collect(),
        })
        .collect();
    let (links, cycles) = links::build(&sources, &rules);
    drop(sources);

    let git = git::read(&root);
    let (churn, uncommitted) = match git {
        Git::Repository { .. } => {
            let dirs: Vec<PathBuf> = found.iter().map(|module| module.dir.clone()).collect();
            (git::churn(&root, &folders), git::uncommitted(&root, &dirs))
        }
        _ => (BTreeMap::new(), vec![None; found.len()]),
    };

    let modules: Vec<Module> = found
        .into_iter()
        .zip(codes)
        .zip(&folders)
        .zip(uncommitted)
        .map(|(((module, code), folder), uncommitted)| Module {
            name: module.name,
            code,
            dir: module.dir,
            state: module.state,
            description: module.description,
            priority: module.priority,
            manifest: module.manifest,
            surface: module.surface,
            namespace: module.namespace,
            files: module.scan.files,
            lines: module.scan.lines,
            kinds: module.scan.kinds,
            commands: module.scan.commands.into_iter().collect(),
            tests: module.scan.tests,
            coverage: coverage_of(folder),
            churn: churn.get(folder).copied(),
            uncommitted,
        })
        .collect();

    let findings = findings::find(&Subject {
        root: &root,
        modules: &modules,
        links: &links,
        cycles: &cycles,
        rules: &rules,
        status_entries: &status_entries,
        statuses_file: &shown(&root, &layout.statuses_file),
        has_coverage_report: report_path.is_some(),
        coverage_limit,
        package: &options.package,
    });

    let package_data = options.package.with_tables(&modules);
    let (app_url, app_env) = read_env(&root);
    Ok(AppSnapshot {
        name: root
            .file_name()
            .map(|name| name.to_string_lossy().into_owned())
            .unwrap_or_else(|| root.display().to_string()),
        laravel: lock.version(LARAVEL),
        package,
        actuator: lock.version(ACTUATOR),
        app_url,
        app_env,
        layout,
        modules,
        coverage_report: report_path.map(|path| CoverageReport {
            written: std::fs::metadata(&path)
                .and_then(|metadata| metadata.modified())
                .ok()
                .and_then(|time| time.duration_since(UNIX_EPOCH).ok())
                .map(|since| since.as_secs() as i64),
            path,
        }),
        git,
        links,
        cycles,
        rules,
        rules_source,
        coverage_ignored,
        findings,
        coverage_limit,
        package_data,
        root,
    })
}

fn requires_laravel(root: &Path) -> bool {
    std::fs::read(root.join("composer.json"))
        .ok()
        .and_then(|json| serde_json::from_slice::<serde_json::Value>(&json).ok())
        .is_some_and(|composer| {
            ["require", "require-dev"]
                .iter()
                .any(|section| composer.get(section).and_then(|s| s.get(LARAVEL)).is_some())
        })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_path_is_shown_with_slashes_and_whole_when_outside_the_app() {
        let (root, outside, whole) = if cfg!(windows) {
            (r"C:\ci\app", r"D:\shared\Modules", "D:/shared/Modules")
        } else {
            ("/ci/app", "/shared/Modules", "/shared/Modules")
        };
        let root = Path::new(root);
        assert_eq!(
            shown(root, &root.join("Modules").join("Billing")),
            "Modules/Billing"
        );
        assert_eq!(shown(root, Path::new(outside)), whole);
        // `base_path('../shared/Modules')` climbs out of the app.
        assert_eq!(
            shown(root, &root.join("..").join("shared").join("Modules")),
            if cfg!(windows) {
                "C:/ci/shared/Modules"
            } else {
                "/ci/shared/Modules"
            }
        );
    }

    #[cfg(windows)]
    #[test]
    fn a_verbatim_or_unc_path_is_shown_as_a_path_that_opens() {
        let root = Path::new(r"\\server\share\repo");
        assert_eq!(
            shown(root, Path::new(r"\\server\share\shared\Modules")),
            "//server/share/shared/Modules"
        );
        assert_eq!(
            shown(root, Path::new(r"\\?\D:\shared\Modules")),
            "D:/shared/Modules"
        );
        assert_eq!(
            shown(root, Path::new(r"\\?\UNC\server\share\shared")),
            "//server/share/shared"
        );
        let volume = r"\\?\Volume{0}\shared";
        assert_eq!(shown(root, Path::new(volume)), volume);
    }

    #[test]
    fn the_url_and_the_environment_of_the_app_are_read_from_their_lines() {
        let env = "APP_NAME=Shop\nAPP_ENV=production\nAPP_URLS=no\nAPP_URL=https://shop-api.test\nDB_PASSWORD=secret\n";
        assert_eq!(
            env_value(env, "APP_URL").as_deref(),
            Some("https://shop-api.test")
        );
        assert_eq!(
            env_value("APP_URL=\"http://localhost:8000\" # dev", "APP_URL").as_deref(),
            Some("http://localhost:8000")
        );
        assert_eq!(
            env_value("APP_URL = http://localhost # dev", "APP_URL").as_deref(),
            Some("http://localhost")
        );
        assert_eq!(
            env_value("export APP_URL='http://a.test'", "APP_URL").as_deref(),
            Some("http://a.test")
        );
        assert_eq!(env_value("APP_URL=", "APP_URL"), None);
        assert_eq!(env_value("# APP_URL=http://old.test", "APP_URL"), None);
        assert_eq!(env_value("APP_ENV=local", "APP_URL"), None);
        assert_eq!(env_value(env, "APP_ENV").as_deref(), Some("production"));
        assert_eq!(
            env_value("APP_URL=http://a.test\nAPP_URL=http://b.test", "APP_URL").as_deref(),
            Some("http://b.test")
        );
    }
}