Skip to content

fix: project json runnable kind bin substitution #20396

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions crates/ide/src/runnables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ pub enum TestId {
Path(String),
}

impl fmt::Display for TestId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl TestId {
pub fn as_str(&self) -> &str {
match self {
TestId::Name(name) => name.fmt(f),
TestId::Path(path) => path.fmt(f),
Self::Name(n) => n,
Self::Path(p) => p,
}
}
}

impl fmt::Display for TestId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum RunnableKind {
TestMod { path: String },
Expand Down
54 changes: 26 additions & 28 deletions crates/rust-analyzer/src/target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,32 @@ pub(crate) struct ProjectJsonTargetSpec {
impl ProjectJsonTargetSpec {
pub(crate) fn runnable_args(&self, kind: &RunnableKind) -> Option<Runnable> {
match kind {
RunnableKind::Bin => {
for runnable in &self.shell_runnables {
if matches!(runnable.kind, project_model::project_json::RunnableKind::Run) {
return Some(runnable.clone());
}
}

None
}
RunnableKind::Test { test_id, .. } => {
for runnable in &self.shell_runnables {
if matches!(runnable.kind, project_model::project_json::RunnableKind::TestOne) {
let mut runnable = runnable.clone();

let replaced_args: Vec<_> = runnable
.args
.iter()
.map(|arg| arg.replace("{test_id}", &test_id.to_string()))
.map(|arg| arg.replace("{label}", &self.label))
.collect();
runnable.args = replaced_args;

return Some(runnable);
}
}

None
}
RunnableKind::Bin => self
.shell_runnables
.iter()
.find(|r| matches!(r.kind, project_model::project_json::RunnableKind::Run))
.cloned()
.map(|mut runnable| {
runnable.args.iter_mut().for_each(|arg| {
*arg = arg.replace("{label}", &self.label);
});

runnable
}),
RunnableKind::Test { test_id, .. } => self
.shell_runnables
.iter()
.find(|r| matches!(r.kind, project_model::project_json::RunnableKind::TestOne))
.cloned()
.map(|mut runnable| {
runnable.args.iter_mut().for_each(|arg| {
*arg = arg
.replace("{label}", &self.label)
.replace("{test_id}", test_id.as_str());
});

runnable
}),
RunnableKind::TestMod { .. } => None,
RunnableKind::Bench { .. } => None,
RunnableKind::DocTest { .. } => None,
Expand Down
Loading