Skip to content

Commit

Permalink
Enable cargo override to be run correctly as a cargo subcommand
Browse files Browse the repository at this point in the history
Fixes #23

This makes it possible to run `cargo override --path ../dep` where previously `cargo-override --path ../dep` had to be used.
  • Loading branch information
eopb committed Aug 8, 2024
1 parent 4f32fef commit 41d4b71
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
24 changes: 17 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ pub static DEFAULT_REGISTRY: &str = "crates-io";
pub static CARGO_TOML: &str = "Cargo.toml";

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short, long)]
pub path: String,
#[command(bin_name = "cargo")]
pub struct Cli {
#[command(subcommand)]
pub command: CargoInvocation,
}

pub fn run(working_dir: &Path, args: Args) -> anyhow::Result<()> {
let patch_manifest_path = patch_manifest(working_dir, &args.path)?;
#[derive(Parser, Debug)]
pub enum CargoInvocation {
#[command(name = "override")]
Override { path: String },
}

pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {
let Cli {
command: CargoInvocation::Override { path },
} = args;

let patch_manifest_path = patch_manifest(working_dir, &path)?;

let project_manifest_path = project_manifest(working_dir)?;

Expand All @@ -37,7 +47,7 @@ pub fn run(working_dir: &Path, args: Args) -> anyhow::Result<()> {
let project_patch_overrides_table =
create_subtable(project_patch_table, DEFAULT_REGISTRY, false)?;

let Ok(new_patch) = format!("{{ path = \"{}\" }}", args.path).parse::<toml_edit::Item>() else {
let Ok(new_patch) = format!("{{ path = \"{}\" }}", path).parse::<toml_edit::Item>() else {
todo!("We haven't escaped the path so we can't be sure this will parse")
};

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::env::current_dir;

use cargo_override::{run, Args};
use cargo_override::{run, Cli};

use clap::Parser;

fn main() {
let args = Args::parse();
let args = Cli::parse();

if let Err(e) = run(&current_dir().unwrap(), args) {
eprintln!("error: {e}");
Expand Down
38 changes: 12 additions & 26 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
path::{Path, PathBuf},
};

use cargo_override::{run, Args, CARGO_TOML};
use cargo_override::{run, CargoInvocation, Cli, CARGO_TOML};

use fake::{Fake, Faker};
use googletest::{
Expand Down Expand Up @@ -42,12 +42,7 @@ fn patch_exists() {
&Manifest::new(Header::basic(patch_crate_name)).render(),
);

let result = run(
working_dir,
Args {
path: patch_folder.to_string(),
},
);
let result = run(working_dir, override_path(patch_folder));
expect_that!(result, ok(eq(())));

let manifest = fs::read_to_string(working_dir_manifest_path).unwrap();
Expand Down Expand Up @@ -95,12 +90,7 @@ fn patch_exists_put_project_does_not_have_dep() {
&Manifest::new(Header::basic("patch-package")).render(),
);

let result = run(
working_dir,
Args {
path: patch_folder.to_string(),
},
);
let result = run(working_dir, override_path(patch_folder));
expect_that!(result, err(anything()));
}

Expand Down Expand Up @@ -128,7 +118,7 @@ fn missing_manifest() {

File::create_new(patch_manifest).expect("failed to create patch manifest file");

let result = run(working_dir, Args { path: patch_folder });
let result = run(working_dir, override_path(patch_folder));

expect_that!(
result,
Expand All @@ -145,12 +135,7 @@ fn patch_path_doesnt_exist() {

let patch_folder: String = Faker.fake();

let result = run(
working_dir,
Args {
path: patch_folder.clone(),
},
);
let result = run(working_dir, override_path(patch_folder.clone()));

expect_that!(
result,
Expand All @@ -171,12 +156,7 @@ fn patch_manifest_doesnt_exist() {

fs::create_dir(patch_folder_path).expect("failed to create patch folder");

let result = run(
working_dir,
Args {
path: patch_folder.clone(),
},
);
let result = run(working_dir, override_path(patch_folder.clone()));

expect_that!(
result,
Expand All @@ -186,3 +166,9 @@ fn patch_manifest_doesnt_exist() {
))))
)
}

fn override_path(path: impl Into<String>) -> Cli {
Cli {
command: CargoInvocation::Override { path: path.into() },
}
}

0 comments on commit 41d4b71

Please sign in to comment.