Unpack scenario root requirements in test cases (#757)

As mentioned in #746, instead of just installing the scenario root we
will unpack the root dependencies into the install command to allow
better coverage of direct user requests with scenarios.

I added display of the package tree provided by each scenario.

Use a mustache template for iterative replacements.
This commit is contained in:
Zanie Blue 2024-01-03 17:31:29 -06:00 committed by GitHub
parent 02b157085e
commit 1f2112191f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 196 additions and 117 deletions

View file

@ -1,65 +1,35 @@
"""
Generates snapshot test cases from packse scenarios.
#!/usr/bin/env python3
#
# Generates snapshot test cases from packse scenarios.
#
# Usage:
# $ scripts/scenarios/generate.py > crates/puffin-cli/tests/pip_install_scenarios.rs
Usage:
$ python scripts/scenarios/generate.py > crates/puffin-cli/tests/pip_install_scenarios.rs
"""
try:
import packse
except ImportError:
print("packse must be installed")
exit(1)
try:
import chevron_blue
except ImportError:
print("chevron-blue must be installed")
exit(1)
import sys
import subprocess
import json
from pathlib import Path
__dir__ = Path(__file__).parent
TEMPLATE = """
/// {{ name }}
///
/// {{ description }}
#[test]
fn {{ test_name }}() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let cache_dir = assert_fs::TempDir::new()?;
let venv = create_venv_py312(&temp_dir, &cache_dir);
TEMPLATE = __dir__ / "template.mustache"
DATA = __dir__ / "data.json"
insta::with_settings!({
filters => INSTA_FILTERS.to_vec()
}, {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.arg("pip-install")
.arg("{{ prefix }}")
.arg("--extra-index-url")
.arg("https://test.pypi.org/simple")
.arg("--cache-dir")
.arg(cache_dir.path())
.env("VIRTUAL_ENV", venv.as_os_str())
.current_dir(&temp_dir), @r###"<snapshot>
"###);
});
Ok(())
}
"""
PRELUDE = """
#![cfg(all(feature = "python", feature = "pypi"))]
/// Generated by `{{ generated_by }}`
use std::process::Command;
use anyhow::Result;
use insta_cmd::_macro_support::insta;
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
use common::{create_venv_py312, BIN_NAME, INSTA_FILTERS};
mod common;
"""
scenarios = json.loads(
data = json.loads(
subprocess.check_output(
[
"packse",
@ -67,20 +37,20 @@ scenarios = json.loads(
str(packse.__development_base_path__ / "scenarios"),
],
)
)["scenarios"]
)
output = PRELUDE.lstrip().replace("{{ generated_by }}", " ".join(sys.argv))
# Add a generated
data["generated_by"] = f"Generated by `{' '.join(sys.argv)}`"
for scenario in scenarios:
# Skip the example scenario file
# Add normalized names for tests
for scenario in data["scenarios"]:
scenario["normalized_name"] = scenario["name"].replace("-", "_")
# Drop the example scenario
for index, scenario in enumerate(data["scenarios"]):
if scenario["name"] == "example":
continue
data["scenarios"].pop(index)
output += (
TEMPLATE.replace("{{ name }}", scenario["name"])
.replace("{{ test_name }}", scenario["name"].replace("-", "_"))
.replace("{{ description }}", scenario["description"])
.replace("{{ prefix }}", scenario["prefix"])
)
output = chevron_blue.render(template=TEMPLATE.read_text(), data=data, no_escape=True)
print(output)

View file

@ -0,0 +1,50 @@
#![cfg(all(feature = "python", feature = "pypi"))]
/// {{generated_by}}
use std::process::Command;
use anyhow::Result;
use insta_cmd::_macro_support::insta;
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
use common::{create_venv_py312, BIN_NAME, INSTA_FILTERS};
mod common;
{{#scenarios}}
/// {{name}}
///
/// {{description}}
///
/// {{prefix}}
{{#tree}}
/// {{.}}
{{/tree}}
#[test]
fn {{normalized_name}}() -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let cache_dir = assert_fs::TempDir::new()?;
let venv = create_venv_py312(&temp_dir, &cache_dir);
insta::with_settings!({
filters => INSTA_FILTERS.to_vec()
}, {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.arg("pip-install")
{{#root.requires}}
.arg("{{prefix}}-{{.}}")
{{/root.requires}}
.arg("--extra-index-url")
.arg("https://test.pypi.org/simple")
.arg("--cache-dir")
.arg(cache_dir.path())
.env("VIRTUAL_ENV", venv.as_os_str())
.current_dir(&temp_dir), @r###"<snapshot>
"###);
});
Ok(())
}
{{/scenarios}}