-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(tests): Reduce reliance on CARGO_IS_TEST #659
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Switching to snapbox was done by: #!/usr/bin/env bash
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
#!./.venv/bin/python3
import tomli
import pathlib
cases = []
for case_path in pathlib.Path("tests/snapshots/add").glob("*.toml"):
case_data = tomli.loads(case_path.read_text())
case_data["name"] = case_path.stem
case_data["path"] = case_path
cases.append(case_data)
test = """#![warn(rust_2018_idioms)]
#![allow(clippy::all)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::redundant_clone)]
#[macro_use]
extern crate cargo_test_macro;
pub fn cargo_exe() -> &'static std::path::Path {
snapbox::cmd::cargo_bin!("cargo-add")
}
pub fn cargo_command() -> snapbox::cmd::Command {
snapbox::cmd::Command::new(cargo_exe())
.with_assert(assert())
}
pub fn project_from_template(template_path: impl AsRef<std::path::Path>) -> std::path::PathBuf {
let root = cargo_test_support::paths::root();
let project_root = root.join("case");
snapbox::path::copy_template(template_path.as_ref(), &project_root).unwrap();
project_root
}
pub fn assert() -> snapbox::Assert {
let root = cargo_test_support::paths::root().display().to_string();
let mut subs = snapbox::Substitutions::new();
subs.extend([
("[EXE]", std::borrow::Cow::Borrowed(std::env::consts::EXE_SUFFIX)),
("[ROOT]", std::borrow::Cow::Owned(root.into())),
]).unwrap();
snapbox::Assert::new()
.action_env(snapbox::DEFAULT_ACTION_ENV)
.substitutions(subs)
}
"""
for case in sorted(cases, key=lambda c: c["name"]):
remaining_args = case['args'][1:]
assert "stdin" not in case
stdout_path = case["path"].with_suffix(".stdout")
stdout_path.write_text(case["stdout"])
stderr_path = case["path"].with_suffix(".stderr")
stderr_path.write_text(case["stderr"])
if isinstance(case["status"], dict):
code = case["status"]["code"]
status_func = f"code({code})"
elif case["status"] == "success":
status_func = "success()"
elif case["status"] == "failure":
status_func = "failure()"
else:
raise NotImplementedError(case["status"])
in_path = case["path"].with_suffix(".in")
out_path = case["path"].with_suffix(".out")
toml_cwd = case.get("fs", {}).get("cwd", None)
if toml_cwd:
cwd = "/".join(pathlib.Path(toml_cwd).parts[1:])
init_cwd = f'let cwd = project_root.join("{cwd}");'
cwd_ref = "&cwd"
else:
init_cwd = f"let cwd = &project_root;"
cwd_ref = "cwd"
name = case["name"]
test += f'#[cargo_test]\n'
if not case.get("env"):
test += f'#[cfg(feature = "test-external-apis")]\n'
test += f'fn {name}() {{\n'
test += f' let project_root = project_from_template("{in_path}");\n'
test += f' {init_cwd}\n'
test += f'\n'
test += f' cargo_command()\n'
test += f' .arg("add")\n'
if remaining_args:
args = f"{remaining_args!r}".replace("'", '"')
test += f' .args({args})\n'
if case.get("env"):
test += f' .env("CARGO_IS_TEST", "1")\n'
test += f' .current_dir({cwd_ref})\n'
test += f' .assert()\n'
test += f' .{status_func}\n'
test += f' .stdout_matches_path("{stdout_path}")\n'
test += f' .stderr_matches_path("{stderr_path}");\n'
test += f'\n'
test += f' assert()\n'
test += f' .subset_matches("{out_path}", &project_root);\n'
test += f'}}\n'
test += f'\n'
pathlib.Path("tests/cargo-add.rs").write_text(test) |
epage
force-pushed
the
snap
branch
2 times, most recently
from
March 8, 2022 18:54
b884793
to
b882236
Compare
My hope is this will give us the flexibility we need to still get most of the benefits of trycmd while allowing to use `cargo-test-support`s registry logic so we can remove CARGO_IS_TEST.
This reduces the need for `CARGO_IS_TEST` - Increases fidelity - Allows us to more easily switch to cargo's logic
This comment was marked as outdated.
This comment was marked as outdated.
Updated version of #!./.venv/bin/python3
import shutil
import tomli
import pathlib
TOOL = "upgrade"
TOOL_ROOT = pathlib.Path(f"tests/cargo-{TOOL}")
TOOL_ROOT.mkdir(parents=True, exist_ok=True)
cases = []
for case_path in pathlib.Path(f"tests/cmd/{TOOL}").glob("*.toml"):
case_data = tomli.loads(case_path.read_text())
case_data["name"] = case_path.stem
case_data["path"] = case_path
cases.append(case_data)
main_text = """
#![allow(clippy::all)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::redundant_clone)]
#[macro_use]
extern crate cargo_test_macro;
"""
for case in sorted(cases, key=lambda c: c["name"]):
name = case["name"]
main_text += f"mod {name};\n"
main_text += """
fn init_registry() {
cargo_test_support::registry::init();
add_registry_packages(false);
}
fn init_alt_registry() {
cargo_test_support::registry::alt_init();
add_registry_packages(true);
}
fn add_registry_packages(alt: bool) {
for name in [
"my-package",
"my-package1",
"my-package2",
"my-dev-package1",
"my-dev-package2",
"my-build-package1",
"my-build-package2",
"toml",
"versioned-package",
"cargo-list-test-fixture-dependency",
"unrelateed-crate",
] {
cargo_test_support::registry::Package::new(name, "0.1.1+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "0.2.0+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "0.2.3+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "0.4.1+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "20.0.0+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "99999.0.0+my-package")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new(name, "99999.0.0-alpha.1+my-package")
.alternative(alt)
.publish();
}
cargo_test_support::registry::Package::new("prerelease_only", "0.2.0-alpha.1")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new("test_breaking", "0.2.0")
.alternative(alt)
.publish();
cargo_test_support::registry::Package::new("test_nonbreaking", "0.1.1")
.alternative(alt)
.publish();
// Normalization
cargo_test_support::registry::Package::new("linked-hash-map", "0.5.4")
.alternative(alt)
.feature("clippy", &[])
.feature("heapsize", &[])
.feature("heapsize_impl", &[])
.feature("nightly", &[])
.feature("serde", &[])
.feature("serde_impl", &[])
.feature("serde_test", &[])
.publish();
cargo_test_support::registry::Package::new("inflector", "0.11.4")
.alternative(alt)
.feature("default", &["heavyweight", "lazy_static", "regex"])
.feature("heavyweight", &[])
.feature("lazy_static", &[])
.feature("regex", &[])
.feature("unstable", &[])
.publish();
cargo_test_support::registry::Package::new("your-face", "99999.0.0+my-package")
.alternative(alt)
.feature("nose", &[])
.feature("mouth", &[])
.feature("eyes", &[])
.feature("ears", &[])
.publish();
}
pub fn cargo_exe() -> std::path::PathBuf {
snapbox::cmd::cargo_bin("cargo-upgrade")
}
/// Test the cargo command
pub trait CargoCommand {
fn cargo_ui() -> Self;
}
impl CargoCommand for snapbox::cmd::Command {
fn cargo_ui() -> Self {
use cargo_test_support::TestEnv;
Self::new(cargo_exe())
.with_assert(cargo_test_support::compare::assert_ui())
.test_env()
}
}
"""
(TOOL_ROOT / "main.rs").write_text(main_text)
if pathlib.Path(f"tests/cargo-{TOOL}.rs").exists():
pathlib.Path(f"tests/cargo-{TOOL}.rs").unlink()
for case in sorted(cases, key=lambda c: c["name"]):
remaining_args = case['args'][1:]
assert "stdin" not in case
name = case["name"]
case_root = TOOL_ROOT / name
case_root.mkdir(parents=True, exist_ok=True)
stdout_path = case_root / "stdout.log"
stdout_path.write_text(case["stdout"])
stderr_path = case_root / "stderr.log"
stderr_path.write_text(case["stderr"])
if isinstance(case["status"], dict):
code = case["status"]["code"]
status_func = f"code({code})"
elif case["status"] == "success":
status_func = "success()"
elif case["status"] == "failure":
status_func = "failure()"
elif case["status"] == "failed":
status_func = "failure()"
else:
raise NotImplementedError(case["status"])
old_in_path = case["path"].with_suffix(".in")
in_path = case_root / "in"
shutil.copytree(old_in_path, in_path)
old_out_path = case["path"].with_suffix(".out")
out_path = case_root / "out"
shutil.copytree(old_out_path, out_path)
toml_cwd = case.get("fs", {}).get("cwd", None)
if toml_cwd:
cwd = "/".join(pathlib.Path(toml_cwd).parts[1:])
init_cwd = f'let cwd = project_root.join("{cwd}");'
cwd_ref = "&cwd"
else:
init_cwd = f"let cwd = &project_root;"
cwd_ref = "cwd"
test = """use cargo_test_support::compare::assert_ui;
use cargo_test_support::Project;
use crate::init_registry;
use crate::CargoCommand;
use cargo_test_support::curr_dir;
#[cargo_test]
fn case() {
init_registry();
let project = Project::from_template(curr_dir!().join("in"));
let project_root = project.root();
"""
test += f' {init_cwd}\n'
test += f'\n'
test += f' snapbox::cmd::Command::cargo_ui()\n'
test += f' .arg("upgrade")\n'
if remaining_args:
args = f"{remaining_args!r}".replace("'", '"')
test += f' .args({args})\n'
test += f' .current_dir({cwd_ref})\n'
test += f' .assert()\n'
test += f' .{status_func}\n'
test += f' .stdout_matches_path(curr_dir!().join("stdout.log"))\n'
test += f' .stderr_matches_path(curr_dir!().join("stderr.log"));\n'
test += f'\n'
test += f' assert_ui()\n'
test += f' .subset_matches(curr_dir!().join("out"), &project_root);\n'
test += f'}}\n'
test += f'\n'
(case_root / "mod.rs").write_text(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problems with
CARGO_IS_TEST
:To allow this programamtic fixture creation, I switched from
trycmd
to its lower levelsnapbox
mixed withcargo-test-support
.