Skip to content

Commit

Permalink
Merge pull request #70 from abhillman/aryeh/support-current-rust-tool…
Browse files Browse the repository at this point in the history
…chain

Support Current Rust Toolchain
  • Loading branch information
mettke authored Sep 19, 2024
2 parents 5075f0e + 7cccb65 commit f99685f
Show file tree
Hide file tree
Showing 10 changed files with 1,373 additions and 848 deletions.
2,121 changes: 1,306 additions & 815 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ categories = ["command-line-utilities"]

[dependencies]
anyhow = "1"
cargo = "0.72"
cargo = "0.82"
fs_extra = "1"
patch = "0.7"
semver = "1"
toml = "0.7"
toml = "0.8"

[dev-dependencies]
cargo-test-macro = { git = "https://github.com/rust-lang/cargo.git", version = "0.1.0", tag = "0.72.1" }
cargo-test-support = { git = "https://github.com/rust-lang/cargo.git", version = "0.1.0", tag = "0.72.1" }
cargo-test-macro = "0.3"
cargo-test-support = "0.3"
31 changes: 20 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ use cargo::{
PackageId, Resolve, Workspace,
},
ops::{get_resolved_packages, load_pkg_lockfile, resolve_with_previous},
util::{config::Config, important_paths::find_root_manifest_for_wd},
util::important_paths::find_root_manifest_for_wd,
GlobalContext,
};

use cargo::sources::SourceConfigMap;
use cargo::util::cache_lock::CacheLockMode::DownloadExclusive;
use fs_extra::dir::{copy, CopyOptions};
use patch::{Line, Patch};
use semver::VersionReq;
Expand Down Expand Up @@ -148,23 +152,28 @@ fn clear_patch_folder() -> Result<()> {
}
}

fn setup_config() -> Result<Config> {
let config = Config::default()?;
config.shell().set_verbosity(Verbosity::Quiet);
Ok(config)
fn setup_gctx() -> Result<GlobalContext> {
let gctx = GlobalContext::default()?;
gctx.shell().set_verbosity(Verbosity::Quiet);
Ok(gctx)
}

fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
let path = fs::canonicalize(path)?;
find_root_manifest_for_wd(&path)
}

fn fetch_workspace<'a>(config: &'a Config, path: &Path) -> Result<Workspace<'a>> {
Workspace::new(path, config)
fn fetch_workspace<'gctx>(
gctx: &'gctx GlobalContext,
path: &Path,
) -> Result<Workspace<'gctx>> {
Workspace::new(path, gctx)
}

fn resolve_ws<'a>(ws: &Workspace<'a>) -> Result<(PackageSet<'a>, Resolve)> {
let mut registry = PackageRegistry::new(ws.config())?;
let scm = SourceConfigMap::new(ws.gctx())?;
let mut registry = PackageRegistry::new_with_source_config(ws.gctx(), scm)?;

registry.lock_patches();
let resolve = {
let prev = load_pkg_lockfile(ws)?;
Expand Down Expand Up @@ -489,10 +498,10 @@ fn read_to_string(path: &Path) -> Result<String> {

pub fn patch() -> Result<()> {
clear_patch_folder()?;
let config = setup_config()?;
let _lock = config.acquire_package_cache_lock()?;
let gctx = setup_gctx()?;
let _lock = gctx.acquire_package_cache_lock(DownloadExclusive)?;
let workspace_path = find_cargo_toml(&PathBuf::from("."))?;
let workspace = fetch_workspace(&config, &workspace_path)?;
let workspace = fetch_workspace(&gctx, &workspace_path)?;
let (pkg_set, resolve) = resolve_ws(&workspace)?;

let custom_metadata = workspace.custom_metadata().into_iter().chain(
Expand Down
23 changes: 15 additions & 8 deletions tests/patch_crates_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod common;
use cargo_test_macro::cargo_test;
use cargo_test_support::{main_file, project};

#[allow(deprecated)]
#[cargo_test]
fn patch_crates_io_invalid_dependency() {
let manifest = r#"
Expand Down Expand Up @@ -34,6 +35,7 @@ fn patch_crates_io_invalid_dependency() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_crates_io_missing_patch() {
let manifest = r#"
Expand Down Expand Up @@ -61,6 +63,7 @@ fn patch_crates_io_missing_patch() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_crates_io_invalid_patch() {
let manifest = r#"
Expand Down Expand Up @@ -89,6 +92,7 @@ fn patch_crates_io_invalid_patch() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_crates_io_simple() {
let manifest = r#"
Expand Down Expand Up @@ -135,11 +139,12 @@ fn patch_crates_io_simple() {
.join("patch")
.join("serde-1.0.110")
.join("LICENSE-MIT");
let licenes =
let licenses =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(licenses.contains("PATCHED"));
}

#[allow(deprecated)]
#[cargo_test]
fn patch_crates_io_detailed() {
let manifest = r#"
Expand Down Expand Up @@ -186,11 +191,12 @@ fn patch_crates_io_detailed() {
.join("patch")
.join("serde-1.0.110")
.join("LICENSE-MIT");
let licenes =
let licenses =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(licenses.contains("PATCHED"));
}

#[allow(deprecated)]
#[cargo_test]
fn patch_git_workspace_root() {
let manifest = r#"
Expand Down Expand Up @@ -247,11 +253,12 @@ fn patch_git_workspace_root() {
.join("patch")
.join("serde-1.0.110")
.join("LICENSE-MIT");
let licenes =
let licenses =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(licenses.contains("PATCHED"));
}

#[allow(deprecated)]
#[cargo_test]
fn patch_git_workspace_metadata() {
let manifest = r#"
Expand Down Expand Up @@ -302,7 +309,7 @@ fn patch_git_workspace_metadata() {
.join("patch")
.join("serde-1.0.110")
.join("LICENSE-MIT");
let licenes =
let licenses =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(licenses.contains("PATCHED"));
}
3 changes: 3 additions & 0 deletions tests/patch_create_and_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ patches = ["test.patch"]
(p.process(common::cargo_patch_exe()), p)
}

#[allow(deprecated)]
#[cargo_test]
fn patch_create_file() {
let (mut e, p) = gen_execs(
Expand All @@ -54,6 +55,7 @@ fn patch_create_file() {
assert_eq!(content.as_str(), TEST_CONTENT);
}

#[allow(deprecated)]
#[cargo_test]
fn patch_delete_file() {
let (mut e, p) = gen_execs(
Expand Down Expand Up @@ -85,6 +87,7 @@ fn patch_delete_file() {
assert!(!file.exists())
}

#[allow(deprecated)]
#[cargo_test]
fn patch_invalid_both_empty() {
let (mut e, _) = gen_execs(
Expand Down
4 changes: 4 additions & 0 deletions tests/patch_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod common;
use cargo_test_macro::cargo_test;
use cargo_test_support::{main_file, project};

#[allow(deprecated)]
#[cargo_test]
fn patch_empty_no_config() {
let p = project().build();
Expand All @@ -13,6 +14,7 @@ fn patch_empty_no_config() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_empty_no_src() {
let manifest = r#"
Expand All @@ -29,6 +31,7 @@ fn patch_empty_no_src() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_empty_simple() {
let manifest = r#"
Expand All @@ -47,6 +50,7 @@ fn patch_empty_simple() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_empty_missing_dependency() {
let manifest = r#"
Expand Down
2 changes: 2 additions & 0 deletions tests/patch_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const MANIFEST: &str = r#"
patches = ["test.patch"]
"#;

#[allow(deprecated)]
#[cargo_test]
fn patch_context_mismatch() {
let patch = r#"--- LICENSE-MIT 2020-05-20 18:44:09.709027472 +0200
Expand Down Expand Up @@ -44,6 +45,7 @@ fn patch_context_mismatch() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_deleted_mismatch() {
let patch = r#"--- LICENSE-MIT 2020-05-20 18:44:09.709027472 +0200
Expand Down
18 changes: 12 additions & 6 deletions tests/patch_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod common;
use cargo_test_macro::cargo_test;
use cargo_test_support::{main_file, project};

#[allow(deprecated)]
#[cargo_test]
fn patch_git_invalid_dependency() {
let manifest = r#"
Expand Down Expand Up @@ -33,6 +34,7 @@ fn patch_git_invalid_dependency() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_git_missing_patch() {
let manifest = r#"
Expand Down Expand Up @@ -60,6 +62,7 @@ fn patch_git_missing_patch() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_git_invalid_patch() {
let manifest = r#"
Expand Down Expand Up @@ -88,6 +91,7 @@ fn patch_git_invalid_patch() {
.run();
}

#[allow(deprecated)]
#[cargo_test]
fn patch_git_detailed() {
let manifest = r#"
Expand Down Expand Up @@ -133,11 +137,12 @@ fn patch_git_detailed() {
.join("patch")
.join("serde")
.join("LICENSE-MIT");
let licenes =
let licenses =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(licenses.contains("PATCHED"));
}

#[allow(deprecated)]
#[cargo_test]
fn patch_git_workspace_root() {
let manifest = r#"
Expand Down Expand Up @@ -194,11 +199,12 @@ fn patch_git_workspace_root() {
.join("patch")
.join("serde")
.join("LICENSE-MIT");
let licenes =
let licenses =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(licenses.contains("PATCHED"));
}

#[allow(deprecated)]
#[cargo_test]
fn patch_git_workspace_metadata() {
let manifest = r#"
Expand Down Expand Up @@ -249,7 +255,7 @@ fn patch_git_workspace_metadata() {
.join("patch")
.join("serde")
.join("LICENSE-MIT");
let licenes =
let licenses =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(licenses.contains("PATCHED"));
}
1 change: 1 addition & 0 deletions tests/patch_github_pr_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ patches = [{ path = "test.patch", source = "GithubPrDiff" }]
(p.process(common::cargo_patch_exe()), p)
}

#[allow(deprecated)]
#[cargo_test]
fn patch_file() {
let (mut e, p) = gen_execs(
Expand Down
10 changes: 6 additions & 4 deletions tests/patch_using_build_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ mod common;

use cargo_test_macro::cargo_test;
use cargo_test_support::{main_file, project};
use std::fmt;

#[cargo_test]
fn patch_using_build_rs() {
let memchr_version = "2.7.4";
let manifest = format!(
r#"
[package]
Expand All @@ -13,7 +15,7 @@ fn patch_using_build_rs() {
authors = ["wycats@example.com"]
[dependencies]
memchr = "=2.5.0"
memchr = "={memchr_version}"
[build-dependencies]
cargo-patch = {{ path = "{cargo_patch_path}" }}
Expand Down Expand Up @@ -50,9 +52,9 @@ fn patch_using_build_rs() {
let license_mit = p
.build_dir()
.join("patch")
.join("memchr-2.5.0")
.join(fmt::format(format_args!("memchr-{}", memchr_version)))
.join("LICENSE-MIT");
let licenes =
let license =
std::fs::read_to_string(license_mit).expect("Unable to read license file");
assert!(licenes.contains("PATCHED"));
assert!(license.contains("PATCHED"));
}

0 comments on commit f99685f

Please sign in to comment.