Add assertions of expected scenario results (#791)

Uses new metadata added in https://github.com/zanieb/packse/pull/61 to
assert that resolution succeeded or failed _and_ that the installed
package versions match the expected result.
This commit is contained in:
Zanie Blue 2024-01-05 10:32:37 -06:00 committed by GitHub
parent 673bece595
commit 08edbc9f60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 374 additions and 6 deletions

View file

@ -6,16 +6,48 @@
/// GENERATED WITH `{{generated_with}}`
/// SCENARIOS FROM `{{generated_from}}`
use std::path::Path;
use std::process::Command;
use anyhow::Result;
use assert_cmd::assert::Assert;
use assert_cmd::prelude::*;
use insta_cmd::_macro_support::insta;
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
use common::{create_venv, BIN_NAME, INSTA_FILTERS};
mod common;
fn assert_command(venv: &Path, command: &str, temp_dir: &Path) -> Assert {
Command::new(venv.join("bin").join("python"))
.arg("-c")
.arg(command)
.current_dir(temp_dir)
.assert()
}
fn assert_installed(
venv: &Path,
package: &'static str,
version: &'static str,
temp_dir: &Path,
) {
assert_command(
venv,
format!(
"import {package} as package; print(package.__version__, end='')"
)
.as_str(),
temp_dir,
)
.success()
.stdout(version);
}
fn assert_not_installed(venv: &Path, package: &'static str, temp_dir: &Path) {
assert_command(venv, format!("import {package}").as_str(), temp_dir).failure();
}
{{#scenarios}}
/// {{name}}
@ -46,6 +78,9 @@ fn {{normalized_name}}() -> Result<()> {
{{#root.requires}}
.arg("{{prefix}}-{{.}}")
{{/root.requires}}
{{#environment.prereleases}}
.arg("--prerelease=allow")
{{/environment.prereleases}}
.arg("--extra-index-url")
.arg("https://test.pypi.org/simple")
.arg("--cache-dir")
@ -56,6 +91,25 @@ fn {{normalized_name}}() -> Result<()> {
"###);
});
{{#expected.explanation_lines}}
// {{.}}
{{/expected.explanation_lines}}
{{#expected.satisfiable}}
{{#expected.packages_list}}
assert_installed(
&venv,
"{{prefix_module}}_{{package_module}}",
"{{version}}",
&temp_dir
);
{{/expected.packages_list}}
{{/expected.satisfiable}}
{{^expected.satisfiable}}
{{#root.requires_packages}}
assert_not_installed(&venv, "{{prefix_module}}_{{package_module}}", &temp_dir);
{{/root.requires_packages}}
{{/expected.satisfiable}}
Ok(())
}
{{/scenarios}}

View file

@ -21,6 +21,7 @@ import shutil
import subprocess
import sys
import textwrap
import packaging.requirements
from pathlib import Path
@ -106,7 +107,7 @@ else:
)
if commit != PACKSE_COMMIT:
print("WARNING: Expected commit {PACKSE_COMMIT!r} but found {commit!r}.")
print(f"WARNING: Expected commit {PACKSE_COMMIT!r} but found {commit!r}.")
print("Loading scenario metadata...", file=sys.stderr)
data = json.loads(
@ -137,6 +138,46 @@ for index, scenario in enumerate(data["scenarios"]):
for scenario in data["scenarios"]:
scenario["description_lines"] = textwrap.wrap(scenario["description"], width=80)
# Wrap the expected explanation onto multiple lines
for scenario in data["scenarios"]:
expected = scenario["expected"]
expected["explanation_lines"] = (
textwrap.wrap(expected["explanation"], width=80)
if expected["explanation"]
else []
)
# Convert the expected packages into a list for rendering
for scenario in data["scenarios"]:
expected = scenario["expected"]
expected["packages_list"] = []
for key, value in expected["packages"].items():
expected["packages_list"].append(
{
"package": key,
"version": value,
# Include a converted version of the package name to its Python module
"package_module": key.replace("-", "_"),
}
)
# Convert the required packages into a list without versions
for scenario in data["scenarios"]:
requires_packages = scenario["root"]["requires_packages"] = []
for requirement in scenario["root"]["requires"]:
package = packaging.requirements.Requirement(requirement).name
requires_packages.append(
{"package": package, "package_module": package.replace("-", "_")}
)
# Include the Python module name of the prefix
for scenario in data["scenarios"]:
scenario["prefix_module"] = scenario["prefix"].replace("-", "_")
# Render the template
print("Rendering template...", file=sys.stderr)
output = chevron_blue.render(template=TEMPLATE.read_text(), data=data, no_escape=True)