File: simple.rs

package info (click to toggle)
python-media-type-version 0.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 500 kB
  • sloc: python: 584; sh: 66; makefile: 10
file content (157 lines) | stat: -rw-r--r-- 5,384 bytes parent folder | download
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
#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]
// SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
// SPDX-License-Identifier: BSD-2-Clause
//! Run a couple of tests for the `mtv-extract` command-line utility.

#![expect(clippy::tests_outside_test_module, reason = "integration test")]

use core::str::FromStr as _;
use std::env::{self, VarError};
use std::fs;
use std::process::{Command, Stdio};
use std::sync::LazyLock;

use camino::Utf8PathBuf;
use eyre::{Result, WrapErr as _, bail, eyre};
use facet_testhelpers::test;
use feature_check::defs::{Config as FcConfig, Obtained as FcObtained};
use feature_check::obtain as fc_obtain;
use feature_check::version::Version as FcVersion;
use log::info;

fn get_exe_path() -> Result<Utf8PathBuf> {
    static PATH: LazyLock<Result<Utf8PathBuf>> = LazyLock::new(|| {
        let current = Utf8PathBuf::from_path_buf(
            env::current_exe().context("Could not get the current executable file's path")?,
        )
        .map_err(|path| {
            eyre!(
                "Could not represent {path} as valid UTF-8",
                path = path.display()
            )
        })?;
        let exe_dir = {
            let basedir = current
                .parent()
                .ok_or_else(|| eyre!("Could not get the parent directory of {current}"))?;
            if basedir
                .file_name()
                .ok_or_else(|| eyre!("Could not get the base name of {basedir}"))?
                == "deps"
            {
                basedir
                    .parent()
                    .ok_or_else(|| eyre!("Could not get the parent directory of {basedir}"))?
            } else {
                basedir
            }
        };
        let exe_path = exe_dir.join("mtv-extract");
        if !exe_path.is_file() {
            bail!("Not a regular file: {exe_path}");
        }
        Ok(exe_path)
    });

    match *PATH {
        Ok(ref res) => Ok(res.clone()),
        Err(ref err) => bail!("Could not determine the path to the test program: {err}"),
    }
}

#[test]
fn stdin_ok() -> Result<()> {
    let tempd_obj = tempfile::tempdir().context("tempd")?;
    let tempd = Utf8PathBuf::from_path_buf(tempd_obj.path().to_path_buf())
        .map_err(|err| eyre!("tempd UTF-8: {err}", err = err.display()))?;
    let testfile = tempd.join("data.txt");
    fs::write(
        &testfile,
        "vnd.ringlet.test/config.v0.3+plain\nvnd.ringlet.test/config.v1.6+plain\n",
    )
    .context("testfile write")?;

    let exe_path = get_exe_path().context("get exe path")?;
    let output = Command::new(&exe_path)
        .args([
            "lines",
            "-p",
            "vnd.ringlet.test/config",
            "-s",
            "+plain",
            "--",
            testfile.as_ref(),
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit())
        .output()
        .context("run")?;
    if !output.status.success() {
        bail!("{output:?}");
    }
    let versions = String::from_utf8(output.stdout).context("output UTF-8")?;
    assert_eq!(versions, "0\t3\n1\t6\n");
    Ok(())
}

fn find_uvoxen() -> Result<Option<String>> {
    let uvoxen_prog = match env::var("UVOXEN") {
        Ok(value) => value,
        Err(VarError::NotPresent) => "uvoxen".to_owned(),
        Err(err) => {
            return Err(err).context("Could not examine the `UVOXEN` environment variable");
        }
    };

    info!("Trying to obtain the features supported by `{uvoxen_prog}`");
    let fc_cfg = FcConfig::default().with_program(uvoxen_prog.clone());
    let FcObtained::Features(features) = fc_obtain::obtain_features(&fc_cfg)
        .with_context(|| format!("Could not query `{uvoxen_prog}` for supported features"))?
    else {
        info!("Could not parse the output of `{uvoxen_prog} --features`, skipping the test");
        return Ok(None);
    };
    let Some(fc_ver) = features.get("uvoxen") else {
        info!("No 'uvoxen' feature in the output of `{uvoxen_prog} --features`, skipping the test");
        return Ok(None);
    };

    let ref_ver_min = FcVersion::from_str("0.2").context("Could not parse 0.2 as a version")?;
    let ref_ver_max = FcVersion::from_str("0.3").context("Could not parse 0.3 as a version")?;
    if *fc_ver < ref_ver_min {
        bail!("Unsupported version for `{uvoxen_prog}`: {fc_ver} < {ref_ver_min}");
    }
    if *fc_ver >= ref_ver_max {
        bail!("Unsupported version for `{uvoxen_prog}`: {fc_ver} >= {ref_ver_max}");
    }
    info!("Supported version {fc_ver} for `{uvoxen_prog}`");
    Ok(Some(uvoxen_prog))
}

#[test]
fn python_test_prog_via_uvoxen() -> Result<()> {
    let Some(uvoxen) = find_uvoxen()? else {
        return Ok(());
    };

    // Well, here goes nothing...
    let top_dir = format!("{proj_dir}/../..", proj_dir = env!("CARGO_MANIFEST_DIR"));
    info!("Running `{uvoxen} uv run ...` in {top_dir}");
    if !Command::new(&uvoxen)
        .args(["uv", "run", "-e", "unit-tests-cli"])
        .current_dir(&top_dir)
        .env(
            "TEST_MTV_EXTRACT_PROG",
            &get_exe_path().context("get exe path")?,
        )
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
        .with_context(|| format!("Could not run `{uvoxen} uv run`"))?
        .success()
    {
        bail!("`{uvoxen} uv run` failed");
    }
    Ok(())
}