yew-e2e 0.2.3

Browser tests for Yew applications, and the runner that drives them.
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
//! Getting hold of a WebDriver server the suite can drive.

use std::fs;
use std::io::{Cursor, Read, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail};

use crate::driver::Engine;

/// Where a fetched driver is kept, relative to the build directory, so that it
/// is covered by the same run lock as everything else a run writes there.
const CACHE: &str = "e2e";

/// Chrome for Testing's index of what it publishes, keyed by
/// `MAJOR.MINOR.BUILD` and answering with the latest patch of each and every
/// download that goes with it.
const INDEX: &str = "https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build-with-downloads.json";

/// What Chrome for Testing calls the platform this is being built for.
const PLATFORM: &str = if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
    "win64"
} else if cfg!(all(target_os = "windows", target_arch = "x86")) {
    "win32"
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
    "mac-arm64"
} else if cfg!(all(target_os = "macos", target_arch = "x86_64")) {
    "mac-x64"
} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
    "linux64"
} else {
    ""
};

/// Where a fetched driver for `engine` that matches the installed browser is,
/// whether or not it is there yet.
///
/// **Named by the browser's build**, so a browser that updates itself to a new
/// build misses the cache and gets a fresh driver, rather than every session
/// failing on a driver that "only supports Chrome version N" until someone
/// deletes it by hand.
pub(crate) fn cached(engine: Engine) -> Result<PathBuf> {
    if engine != Engine::Chrome {
        bail!("{} is never fetched", engine.driver());
    }

    let build = build_of(&chrome_version()?)?;
    Ok(cache_path(&crate::dist::target_dir()?, engine, &build))
}

/// Where the driver for `engine` matching `build` is kept under `target`.
fn cache_path(target: &Path, engine: Engine, build: &str) -> PathBuf {
    let mut name = format!("{}-{build}", engine.driver());

    if cfg!(windows) {
        name.push_str(".exe");
    }

    target.join(CACHE).join(name)
}

/// Fetch a driver for `engine` into [`cached`], and answer where it went.
pub(crate) fn fetch(engine: Engine) -> Result<PathBuf> {
    if engine != Engine::Chrome {
        bail!(
            "{} is not fetched: it drives every version of its browser, so a \
             machine that has the browser needs it installed once and never \
             matched. Install it and put it on PATH.",
            engine.driver()
        );
    }

    if PLATFORM.is_empty() {
        bail!(
            "Chrome for Testing publishes no chromedriver for {} on {}. Install \
             one by hand and put it on PATH.",
            std::env::consts::OS,
            std::env::consts::ARCH
        );
    }

    let installed = chrome_version()?;
    let build = build_of(&installed)?;

    tracing::info!("Chrome {installed} is installed; looking for a chromedriver for {build}");

    let url = download_url(&build, &installed)?;

    tracing::info!("Fetching {url}");

    let archive = reqwest::blocking::get(&url)
        .and_then(reqwest::blocking::Response::error_for_status)
        .with_context(|| anyhow!("Fetching {url}"))?
        .bytes()
        .with_context(|| anyhow!("Reading {url}"))?;

    let at = cache_path(&crate::dist::target_dir()?, engine, &build);

    extract(&archive, engine.driver(), &at)?;

    tracing::info!("chromedriver {installed} is at {}", at.display());

    Ok(at)
}

/// Pull the one file out of the archive and put it where it belongs.
///
/// **Written beside its destination and renamed into place**, executable
/// before it gets there. A run killed part way through (the outer `timeout`
/// is expected to) leaves at most a stray temporary file, never a truncated
/// or non-executable driver at `at` that every later run would take as
/// fetched.
fn extract(archive: &[u8], name: &str, at: &Path) -> Result<()> {
    let mut zip = zip::ZipArchive::new(Cursor::new(archive)).context("Reading the archive")?;

    for index in 0..zip.len() {
        let mut entry = zip.by_index(index)?;

        let Some(entry_name) = entry.enclosed_name() else {
            continue;
        };

        let Some(stem) = entry_name.file_stem().and_then(|s| s.to_str()) else {
            continue;
        };

        if stem != name {
            continue;
        }

        let mut bytes = Vec::with_capacity(entry.size() as usize);
        entry.read_to_end(&mut bytes)?;

        let dir = at.parent().context("the cache has a parent")?;
        fs::create_dir_all(dir).with_context(|| anyhow!("Creating {}", dir.display()))?;

        let mut temp = tempfile::Builder::new()
            .prefix(&format!(".{name}-"))
            .tempfile_in(dir)
            .with_context(|| anyhow!("Creating a temporary file in {}", dir.display()))?;

        temp.write_all(&bytes)
            .and_then(|()| temp.as_file().sync_all())
            .with_context(|| anyhow!("Writing {}", temp.path().display()))?;

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o755))?;
        }

        temp.persist(at)
            .map_err(|e| e.error)
            .with_context(|| anyhow!("Moving the driver into place at {}", at.display()))?;

        return Ok(());
    }

    bail!("The archive holds no {name}")
}

/// Whether this machine has a Chrome at all.
pub(crate) fn chrome_present() -> bool {
    chrome_version().is_ok()
}

/// The version of the Chrome this machine has.
fn chrome_version() -> Result<String> {
    #[cfg(windows)]
    {
        windows_chrome_version()
    }

    #[cfg(not(windows))]
    {
        unix_chrome_version()
    }
}

/// **Read off the install directory rather than asked of the browser.** On
/// Windows `chrome.exe --version` prints nothing anywhere a pipe can catch it —
/// it is a GUI subsystem binary, so it detaches and the shell gets an empty
/// stdout and a success. What Chrome does have is a directory named for the
/// version it is currently on, beside the executable, which the updater
/// maintains.
#[cfg(windows)]
fn windows_chrome_version() -> Result<String> {
    let mut tried = Vec::new();

    for root in chrome_roots() {
        tried.push(root.display().to_string());

        let Ok(entries) = fs::read_dir(&root) else {
            continue;
        };

        let mut best: Option<String> = None;

        for entry in entries.flatten() {
            let name = entry.file_name();

            let Some(name) = name.to_str() else {
                continue;
            };

            if !is_version(name) || !entry.path().is_dir() {
                continue;
            }

            if best.as_deref().is_none_or(|had| older(had, name)) {
                best = Some(name.to_owned());
            }
        }

        if let Some(version) = best {
            return Ok(version);
        }
    }

    bail!(
        "No Chrome found. Looked in: {}. The suite needs a browser to drive; \
         install Chrome, or install geckodriver and run with --browser firefox.",
        tried.join(", ")
    )
}

/// Everywhere Chrome installs itself on Windows, per-machine and per-user.
#[cfg(windows)]
fn chrome_roots() -> Vec<PathBuf> {
    const SUFFIX: &str = r"Google\Chrome\Application";

    ["ProgramFiles", "ProgramFiles(x86)", "LOCALAPPDATA"]
        .into_iter()
        .filter_map(std::env::var_os)
        .map(|base| PathBuf::from(base).join(SUFFIX))
        .collect()
}

/// Everywhere else, where the browser answers for itself.
#[cfg(not(windows))]
fn unix_chrome_version() -> Result<String> {
    const NAMES: [&str; 4] = [
        "google-chrome",
        "google-chrome-stable",
        "chromium",
        "chromium-browser",
    ];

    for name in NAMES {
        let Ok(output) = std::process::Command::new(name).arg("--version").output() else {
            continue;
        };

        if !output.status.success() {
            continue;
        }

        let line = String::from_utf8_lossy(&output.stdout);

        if let Some(version) = line.split_whitespace().find(|word| is_version(word)) {
            return Ok(version.to_owned());
        }
    }

    bail!(
        "No Chrome found: tried {}. The suite needs a browser to drive; install \
         Chrome, or install geckodriver and run with --browser firefox.",
        NAMES.join(", ")
    )
}

/// Whether a word is a Chrome version: four numbers separated by dots.
fn is_version(word: &str) -> bool {
    let mut parts = 0;

    for part in word.split('.') {
        if part.is_empty() || !part.bytes().all(|b| b.is_ascii_digit()) {
            return false;
        }

        parts += 1;
    }

    parts == 4
}

/// Whether `a` is an earlier version than `b`, compared number by number.
#[cfg_attr(
    all(not(windows), not(test)),
    expect(dead_code, reason = "used where Chrome will not say its own version")
)]
fn older(a: &str, b: &str) -> bool {
    let numbers = |v: &str| {
        v.split('.')
            .map(|p| p.parse::<u64>().unwrap_or(0))
            .collect::<Vec<_>>()
    };

    numbers(a) < numbers(b)
}

/// The `MAJOR.MINOR.BUILD` a full version belongs to, which is how Chrome for
/// Testing keys what it publishes.
fn build_of(version: &str) -> Result<String> {
    let mut parts = version.split('.');

    let (Some(major), Some(minor), Some(build)) = (parts.next(), parts.next(), parts.next()) else {
        bail!("{version:?} is not a Chrome version");
    };

    Ok(format!("{major}.{minor}.{build}"))
}

/// Ask Chrome for Testing where the chromedriver for this build is.
fn download_url(build: &str, installed: &str) -> Result<String> {
    let index: serde_json::Value = reqwest::blocking::get(INDEX)
        .and_then(reqwest::blocking::Response::error_for_status)
        .with_context(|| anyhow!("Fetching {INDEX}"))?
        .json()
        .with_context(|| anyhow!("Reading {INDEX}"))?;

    let entry = index
        .get("builds")
        .and_then(|builds| builds.get(build))
        .with_context(|| {
            anyhow!(
                "Chrome for Testing publishes nothing for build {build} (Chrome \
                 {installed} is installed). A Chrome newer than the index is the \
                 usual reason; a driver installed by hand and put on PATH is \
                 used in preference to anything fetched."
            )
        })?;

    let downloads = entry
        .get("downloads")
        .and_then(|downloads| downloads.get("chromedriver"))
        .and_then(serde_json::Value::as_array)
        .with_context(|| anyhow!("Build {build} publishes a Chrome but no chromedriver"))?;

    for download in downloads {
        if download.get("platform").and_then(serde_json::Value::as_str) == Some(PLATFORM) {
            let url = download
                .get("url")
                .and_then(serde_json::Value::as_str)
                .context("a download has a url")?;

            return Ok(url.to_owned());
        }
    }

    bail!("Build {build} publishes no chromedriver for {PLATFORM}")
}

#[cfg(test)]
mod tests {
    use std::io::{Cursor, Write};
    use std::path::Path;

    use zip::write::SimpleFileOptions;

    use super::{build_of, cache_path, extract, is_version, older};
    use crate::driver::Engine;

    /// A zip laid out the way Chrome for Testing lays out its own.
    fn archive(entries: &[(&str, &[u8])]) -> Vec<u8> {
        let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));

        for (name, bytes) in entries {
            zip.start_file(*name, SimpleFileOptions::default()).unwrap();
            zip.write_all(bytes).unwrap();
        }

        zip.finish().unwrap().into_inner()
    }

    /// Everything in `dir`, by name.
    fn listing(dir: &Path) -> Vec<String> {
        let mut names = std::fs::read_dir(dir)
            .unwrap()
            .map(|entry| entry.unwrap().file_name().into_string().unwrap())
            .collect::<Vec<_>>();
        names.sort();
        names
    }

    /// **The cache is keyed by build**, so a Chrome that updated itself looks
    /// somewhere new and fetches, and it lives under the build directory it is
    /// given, which is the one the run lock covers.
    #[test]
    fn a_fetched_driver_is_kept_per_build_under_the_build_directory() {
        let target = Path::new("/elsewhere/build");

        let at = cache_path(target, Engine::Chrome, "151.0.7922");
        let suffix = if cfg!(windows) { ".exe" } else { "" };

        assert_eq!(
            at,
            target
                .join("e2e")
                .join(format!("chromedriver-151.0.7922{suffix}"))
        );

        assert_ne!(at, cache_path(target, Engine::Chrome, "152.0.7977"));
    }

    /// The driver comes out of its subdirectory in the archive, executable,
    /// and nothing else is left beside it.
    #[test]
    fn extracting_puts_only_the_driver_in_place() {
        let dir = tempfile::tempdir().unwrap();
        let at = dir.path().join("e2e").join("chromedriver-151.0.7922");

        let zip = archive(&[
            ("chromedriver-linux64/LICENSE.chromedriver", b"license"),
            ("chromedriver-linux64/chromedriver", b"the driver"),
        ]);

        extract(&zip, "chromedriver", &at).unwrap();

        assert_eq!(std::fs::read(&at).unwrap(), b"the driver");
        assert_eq!(listing(at.parent().unwrap()), ["chromedriver-151.0.7922"]);

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&at).unwrap().permissions().mode();
            assert_eq!(mode & 0o777, 0o755);
        }

        // A second fetch replaces the first rather than failing on it.
        let zip = archive(&[("chromedriver-linux64/chromedriver", b"a newer driver")]);
        extract(&zip, "chromedriver", &at).unwrap();

        assert_eq!(std::fs::read(&at).unwrap(), b"a newer driver");
        assert_eq!(listing(at.parent().unwrap()), ["chromedriver-151.0.7922"]);
    }

    /// An archive without the driver leaves nothing behind that a later run
    /// could take for one.
    #[test]
    fn an_archive_without_the_driver_writes_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let at = dir.path().join("chromedriver-151.0.7922");

        let zip = archive(&[("chromedriver-linux64/LICENSE.chromedriver", b"license")]);

        assert!(extract(&zip, "chromedriver", &at).is_err());
        assert!(listing(dir.path()).is_empty());
    }

    /// **The shape test decides what gets turned into a download URL**, since it
    /// is what picks Chrome's version out of a directory listing that also holds
    /// files and `SetupMetrics`. Loose, it would name one of those.
    #[test]
    fn only_a_four_part_number_is_a_version() {
        assert!(is_version("151.0.7922.174"));
        assert!(is_version("99.0.0.0"));

        for not in [
            "151.0.7922",
            "151.0.7922.174.1",
            "SetupMetrics",
            "151.0.7922.x",
            "chrome.exe",
            "",
            "...",
            "151..7922.174",
        ] {
            assert!(!is_version(not), "{not:?} was taken for a version");
        }
    }

    /// **Numbers, not text.** Chrome's patch number runs past 99 within a build,
    /// so a lexicographic pick would leave a machine on `…99` while it is running
    /// `…138` — a major-version match by luck rather than by construction.
    #[test]
    fn the_newest_install_is_picked_by_number() {
        assert!(older("151.0.7922.99", "151.0.7922.138"));
        assert!(!older("151.0.7922.138", "151.0.7922.99"));

        assert!(older("151.0.7922.174", "152.0.7977.54"));
        assert!(!older("151.0.7922.174", "151.0.7922.174"));
    }

    /// The key Chrome for Testing publishes under is the build, which is the
    /// version with its patch taken off.
    #[test]
    fn a_version_names_the_build_it_belongs_to() {
        assert_eq!(build_of("151.0.7922.174").unwrap(), "151.0.7922");
        assert_eq!(build_of("113.0.5672.63").unwrap(), "113.0.5672");

        assert!(build_of("151.0").is_err());
    }
}