Skip to content

Commit

Permalink
test that hooks are not run when using --instance explicity
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen committed Feb 11, 2025
1 parent 7974ee2 commit 4c088e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/branch/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::portable::project;
use std::fs;
use std::sync::Mutex;

#[derive(Debug)]
pub struct Context {
/// Instance name provided either with --instance or inferred from the project.
instance_name: Option<InstanceName>,
Expand All @@ -26,7 +27,7 @@ pub struct Context {
current_branch: Option<String>,

/// Project manifest cache
manifest_cache: Mutex<Option<Option<project::Context>>>,
project_ctx_cache: Mutex<Option<project::Context>>,
}

impl Context {
Expand All @@ -35,7 +36,7 @@ impl Context {
instance_name: None,
current_branch: None,
project: None,
manifest_cache: Mutex::new(None),
project_ctx_cache: Mutex::new(None),
};

// use instance name provided with --instance
Expand Down Expand Up @@ -112,15 +113,18 @@ impl Context {
}

pub async fn get_project(&self) -> anyhow::Result<Option<project::Context>> {
if let Some(mani) = &*self.manifest_cache.lock().unwrap() {
return Ok(mani.clone());
if let Some(ctx) = &*self.project_ctx_cache.lock().unwrap() {
return Ok(Some(ctx.clone()));
}

let manifest = project::load_ctx(None).await?;
let Some(location) = &self.project else {
return Ok(None);
};
let ctx = project::load_ctx_at_async(location.clone()).await?;

let mut cache_lock = self.manifest_cache.lock().unwrap();
*cache_lock = Some(manifest.clone());
Ok(manifest)
let mut cache_lock = self.project_ctx_cache.lock().unwrap();
*cache_lock = Some(ctx.clone());
Ok(Some(ctx))
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/portable/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,13 @@ pub fn database_name(stash_dir: &Path) -> anyhow::Result<Option<String>> {
Ok(Some(inst.trim().into()))
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Context {
pub location: Location,
pub manifest: manifest::Manifest,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Location {
pub root: PathBuf,
pub manifest: PathBuf,
Expand Down Expand Up @@ -353,6 +353,10 @@ pub async fn load_ctx(override_dir: Option<&Path>) -> anyhow::Result<Option<Cont

#[tokio::main(flavor = "current_thread")]
pub async fn load_ctx_at(location: Location) -> anyhow::Result<Context> {
load_ctx_at_async(location).await
}

pub async fn load_ctx_at_async(location: Location) -> anyhow::Result<Context> {
let manifest = manifest::read(&location.manifest)?;
Ok(Context { location, manifest })
}
Expand Down
14 changes: 14 additions & 0 deletions tests/portable_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ fn hooks() {

let branch_log = fs::read_to_string(branch_log_file).unwrap();
assert_eq!(branch_log, "another\ndefault-branch-name\n");

// branch switch, but with explict --instance arg
// This should prevent hooks from being executed, since
// this action is not executed "on a project", but "on an instance".
Command::new("edgedb")
.current_dir("tests/proj/project3")
.arg("--instance=inst2")
.arg("branch")
.arg("switch")
.arg("another")
.assert()
.context("branch-switch-3", "")
.success()
.stderr(ContainsHooks { expected: &[] });
}

#[derive(Debug)]
Expand Down

0 comments on commit 4c088e8

Please sign in to comment.