Skip to content

Commit 80a4329

Browse files
committed
feat: add add_with_path and update_with_path for test data dir control and remove global test dir env var
1 parent 97cdf08 commit 80a4329

File tree

11 files changed

+142
-64
lines changed

11 files changed

+142
-64
lines changed

crates/but-path/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::path::PathBuf;
22

33
pub fn app_data_dir() -> anyhow::Result<PathBuf> {
4-
if let Ok(test_dir) = std::env::var("TEST_APP_DATA_DIR") {
5-
return Ok(PathBuf::from(test_dir));
6-
}
74
dirs::data_dir()
85
.ok_or(anyhow::anyhow!("Could not get app data dir"))
96
.map(|dir| dir.join(identifier()))

crates/gitbutler-branch-actions/tests/virtual_branches/amend.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ fn forcepush_allowed() -> anyhow::Result<()> {
1717
..
1818
} = &Test::default();
1919

20-
gitbutler_testsupport::set_test_data_dir(data_dir.as_ref().unwrap());
21-
gitbutler_project::update(&projects::UpdateRequest {
22-
id: *project_id,
23-
..Default::default()
24-
})
20+
gitbutler_project::update_with_path(
21+
data_dir.as_ref().unwrap(),
22+
&projects::UpdateRequest {
23+
id: *project_id,
24+
..Default::default()
25+
},
26+
)
2527
.unwrap();
2628

2729
gitbutler_branch_actions::set_base_branch(
@@ -32,10 +34,13 @@ fn forcepush_allowed() -> anyhow::Result<()> {
3234
)
3335
.unwrap();
3436

35-
gitbutler_project::update(&projects::UpdateRequest {
36-
id: *project_id,
37-
..Default::default()
38-
})
37+
gitbutler_project::update_with_path(
38+
data_dir.as_ref().unwrap(),
39+
&projects::UpdateRequest {
40+
id: *project_id,
41+
..Default::default()
42+
},
43+
)
3944
.unwrap();
4045

4146
let stack_entry = gitbutler_branch_actions::create_virtual_branch(

crates/gitbutler-branch-actions/tests/virtual_branches/init.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ fn twice() {
99
let test_project = TestProject::default();
1010

1111
{
12-
gitbutler_testsupport::set_test_data_dir(data_dir.path());
1312
let project =
14-
gitbutler_project::add(test_project.path(), None, None).expect("failed to add project");
13+
gitbutler_project::add_with_path(data_dir.path(), test_project.path(), None, None)
14+
.expect("failed to add project");
1515
let ctx = CommandContext::open(&project, AppSettings::default()).unwrap();
1616

1717
gitbutler_branch_actions::set_base_branch(
@@ -23,11 +23,13 @@ fn twice() {
2323
.unwrap();
2424
let stacks = stack_details(&ctx);
2525
assert_eq!(stacks.len(), 0);
26-
gitbutler_project::delete(project.id).unwrap();
26+
gitbutler_project::delete_with_path(data_dir.path(), project.id).unwrap();
2727
}
2828

2929
{
30-
let project = gitbutler_project::add(test_project.path(), None, None).unwrap();
30+
let project =
31+
gitbutler_project::add_with_path(data_dir.path(), test_project.path(), None, None)
32+
.unwrap();
3133
let ctx = CommandContext::open(&project, AppSettings::default()).unwrap();
3234
gitbutler_branch_actions::set_base_branch(
3335
&ctx,

crates/gitbutler-branch-actions/tests/virtual_branches/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl Default for Test {
3131
let data_dir = paths::data_dir();
3232

3333
let test_project = TestProject::default();
34-
gitbutler_testsupport::set_test_data_dir(data_dir.as_ref());
3534
let project =
36-
gitbutler_project::add(test_project.path(), None, None).expect("failed to add project");
35+
gitbutler_project::add_with_path(data_dir.as_ref(), test_project.path(), None, None)
36+
.expect("failed to add project");
3737
let ctx = CommandContext::open(&project, AppSettings::default()).unwrap();
3838

3939
Self {

crates/gitbutler-branch-actions/tests/virtual_branches/update_commit_message.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn middle() {
136136
#[test]
137137
fn forcepush_allowed() {
138138
let Test {
139+
data_dir,
139140
repo,
140141
project_id,
141142

@@ -151,10 +152,13 @@ fn forcepush_allowed() {
151152
)
152153
.unwrap();
153154

154-
gitbutler_project::update(&projects::UpdateRequest {
155-
id: *project_id,
156-
..Default::default()
157-
})
155+
gitbutler_project::update_with_path(
156+
data_dir.as_ref().unwrap(),
157+
&projects::UpdateRequest {
158+
id: *project_id,
159+
..Default::default()
160+
},
161+
)
158162
.unwrap();
159163

160164
let stack_entry = gitbutler_branch_actions::create_virtual_branch(

crates/gitbutler-cli/src/command/project.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ pub fn list() -> Result<()> {
2020
Ok(())
2121
}
2222

23-
pub fn add(path: PathBuf, refname: Option<RemoteRefname>) -> Result<()> {
23+
pub fn add(data_dir: PathBuf, path: PathBuf, refname: Option<RemoteRefname>) -> Result<()> {
2424
let path = gix::discover(path)?
2525
.workdir()
2626
.context("Only non-bare repositories can be added")?
2727
.to_owned()
2828
.canonicalize()?;
29-
let project = gitbutler_project::add(path, None, None)?;
29+
let project = gitbutler_project::add_with_path(data_dir, path, None, None)?;
3030
let ctx = CommandContext::open(&project, AppSettings::default())?;
3131
if let Some(refname) = refname {
3232
gitbutler_branch_actions::set_base_branch(

crates/gitbutler-cli/src/main.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use anyhow::Result;
1+
use std::path::PathBuf;
2+
3+
use anyhow::{bail, Context, Result};
24

35
mod args;
46
use args::Args;
@@ -58,8 +60,8 @@ fn main() -> Result<()> {
5860
}
5961
}
6062
args::Subcommands::Project(project::Platform {
61-
app_data_dir: _,
62-
app_suffix: _,
63+
app_data_dir,
64+
app_suffix,
6365
cmd,
6466
}) => match cmd {
6567
Some(project::SubCommands::SwitchToWorkspace { remote_ref_name }) => {
@@ -69,11 +71,42 @@ fn main() -> Result<()> {
6971
Some(project::SubCommands::Add {
7072
switch_to_workspace,
7173
path,
72-
}) => command::project::add(path, switch_to_workspace),
74+
}) => command::project::add(
75+
data_dir(app_suffix, app_data_dir)?,
76+
path,
77+
switch_to_workspace,
78+
),
7379
None => command::project::list(),
7480
},
7581
}
7682
}
83+
pub fn data_dir(
84+
app_suffix: Option<String>,
85+
app_data_dir: Option<PathBuf>,
86+
) -> anyhow::Result<PathBuf> {
87+
let path = if let Some(dir) = app_data_dir {
88+
std::fs::create_dir_all(&dir).context("Failed to assure the designated data-dir exists")?;
89+
dir
90+
} else {
91+
dirs_next::data_dir()
92+
.map(|dir| {
93+
dir.join(format!(
94+
"com.gitbutler.app{}",
95+
app_suffix
96+
.map(|mut suffix| {
97+
suffix.insert(0, '.');
98+
suffix
99+
})
100+
.unwrap_or_default()
101+
))
102+
})
103+
.context("no data-directory available on this platform")?
104+
};
105+
if !path.is_dir() {
106+
bail!("Path '{}' must be a valid directory", path.display());
107+
}
108+
Ok(path)
109+
}
77110

78111
mod trace {
79112
use tracing::metadata::LevelFilter;

crates/gitbutler-project/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ pub fn get(id: ProjectId) -> anyhow::Result<Project> {
3030
controller.get(id)
3131
}
3232

33+
/// Testing purpose only.
34+
pub fn get_with_path<P: AsRef<Path>>(data_dir: P, id: ProjectId) -> anyhow::Result<Project> {
35+
let controller = Controller::from_path(data_dir.as_ref());
36+
controller.get(id)
37+
}
38+
3339
pub fn get_validated(id: ProjectId) -> anyhow::Result<Project> {
3440
let controller = Controller::from_path(but_path::app_data_dir()?);
3541
controller.get_validated(id)
@@ -45,6 +51,15 @@ pub fn update(project: &UpdateRequest) -> anyhow::Result<Project> {
4551
controller.update(project)
4652
}
4753

54+
/// Testing purpose only.
55+
pub fn update_with_path<P: AsRef<Path>>(
56+
data_dir: P,
57+
project: &UpdateRequest,
58+
) -> anyhow::Result<Project> {
59+
let controller = Controller::from_path(data_dir.as_ref());
60+
controller.update(project)
61+
}
62+
4863
pub fn add<P: AsRef<Path>>(
4964
path: P,
5065
name: Option<String>,
@@ -54,6 +69,17 @@ pub fn add<P: AsRef<Path>>(
5469
controller.add(path, name, email)
5570
}
5671

72+
/// Testing purpose only.
73+
pub fn add_with_path<P: AsRef<Path>>(
74+
data_dir: P,
75+
path: P,
76+
name: Option<String>,
77+
email: Option<String>,
78+
) -> anyhow::Result<Project> {
79+
let controller = Controller::from_path(data_dir.as_ref());
80+
controller.add(path, name, email)
81+
}
82+
5783
pub fn list() -> anyhow::Result<Vec<Project>> {
5884
let controller = Controller::from_path(but_path::app_data_dir()?);
5985
controller.list()
@@ -64,6 +90,12 @@ pub fn delete(id: ProjectId) -> anyhow::Result<()> {
6490
controller.delete(id)
6591
}
6692

93+
/// Testing purpose only.
94+
pub fn delete_with_path<P: AsRef<Path>>(data_dir: P, id: ProjectId) -> anyhow::Result<()> {
95+
let controller = Controller::from_path(data_dir.as_ref());
96+
controller.delete(id)
97+
}
98+
6799
pub fn assure_app_can_startup_or_fix_it(
68100
projects: anyhow::Result<Vec<Project>>,
69101
) -> anyhow::Result<Vec<Project>> {

crates/gitbutler-project/tests/projects/main.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ mod add {
1313
let tmp = paths::data_dir();
1414
let repository = gitbutler_testsupport::TestProject::default();
1515
let path = repository.path();
16-
gitbutler_testsupport::set_test_data_dir(tmp.path());
17-
let project = gitbutler_project::add(path, None, None).unwrap();
16+
let project = gitbutler_project::add_with_path(tmp.path(), path, None, None).unwrap();
1817
assert_eq!(project.path, path);
1918
assert_eq!(
2019
project.title,
@@ -30,8 +29,8 @@ mod add {
3029
fn non_bare_without_worktree() {
3130
let tmp = paths::data_dir();
3231
let root = repo_path_at("non-bare-without-worktree");
33-
gitbutler_testsupport::set_test_data_dir(tmp.path());
34-
let err = gitbutler_project::add(root.as_path(), None, None).unwrap_err();
32+
let err = gitbutler_project::add_with_path(tmp.path(), root.as_path(), None, None)
33+
.unwrap_err();
3534
assert_eq!(
3635
err.to_string(),
3736
"Cannot add non-bare repositories without a workdir"
@@ -42,8 +41,8 @@ mod add {
4241
fn submodule() {
4342
let tmp = paths::data_dir();
4443
let root = repo_path_at("with-submodule").join("submodule");
45-
gitbutler_testsupport::set_test_data_dir(tmp.path());
46-
let err = gitbutler_project::add(root.as_path(), None, None).unwrap_err();
44+
let err = gitbutler_project::add_with_path(tmp.path(), root.as_path(), None, None)
45+
.unwrap_err();
4746
assert_eq!(
4847
err.to_string(),
4948
"A git-repository without a `.git` directory cannot currently be added"
@@ -54,11 +53,15 @@ mod add {
5453
fn missing() {
5554
let data_dir = paths::data_dir();
5655
let tmp = tempfile::tempdir().unwrap();
57-
gitbutler_testsupport::set_test_data_dir(data_dir.path());
5856
assert_eq!(
59-
gitbutler_project::add(tmp.path().join("missing"), None, None)
60-
.unwrap_err()
61-
.to_string(),
57+
gitbutler_project::add_with_path(
58+
data_dir.path(),
59+
&tmp.path().join("missing"),
60+
None,
61+
None
62+
)
63+
.unwrap_err()
64+
.to_string(),
6265
"path not found"
6366
);
6467
}
@@ -69,9 +72,8 @@ mod add {
6972
let tmp = tempfile::tempdir().unwrap();
7073
let path = tmp.path();
7174
std::fs::write(path.join("file.txt"), "hello world").unwrap();
72-
gitbutler_testsupport::set_test_data_dir(data_dir.path());
7375
assert_eq!(
74-
gitbutler_project::add(path, None, None)
76+
gitbutler_project::add_with_path(data_dir.path(), path, None, None)
7577
.unwrap_err()
7678
.to_string(),
7779
"must be a Git repository"
@@ -82,8 +84,8 @@ mod add {
8284
fn empty() {
8385
let data_dir = paths::data_dir();
8486
let tmp = tempfile::tempdir().unwrap();
85-
gitbutler_testsupport::set_test_data_dir(data_dir.path());
86-
let err = gitbutler_project::add(tmp.path(), None, None).unwrap_err();
87+
let err = gitbutler_project::add_with_path(data_dir.path(), tmp.path(), None, None)
88+
.unwrap_err();
8789
assert_eq!(err.to_string(), "must be a Git repository");
8890
}
8991

@@ -92,10 +94,9 @@ mod add {
9294
let data_dir = paths::data_dir();
9395
let repository = gitbutler_testsupport::TestProject::default();
9496
let path = repository.path();
95-
gitbutler_testsupport::set_test_data_dir(data_dir.path());
96-
gitbutler_project::add(path, None, None).unwrap();
97+
gitbutler_project::add_with_path(data_dir.path(), path, None, None).unwrap();
9798
assert_eq!(
98-
gitbutler_project::add(path, None, None)
99+
gitbutler_project::add_with_path(data_dir.path(), path, None, None)
99100
.unwrap_err()
100101
.to_string(),
101102
"project already exists"
@@ -111,8 +112,9 @@ mod add {
111112
let repo = git2::Repository::init_bare(&repo_dir).unwrap();
112113
create_initial_commit(&repo);
113114

114-
gitbutler_testsupport::set_test_data_dir(data_dir.path());
115-
let err = gitbutler_project::add(repo_dir.as_path(), None, None).unwrap_err();
115+
let err =
116+
gitbutler_project::add_with_path(data_dir.path(), repo_dir.as_path(), None, None)
117+
.unwrap_err();
116118
assert_eq!(err.to_string(), "bare repositories are unsupported");
117119
}
118120

@@ -127,8 +129,9 @@ mod add {
127129
create_initial_commit(&repo);
128130

129131
let worktree = repo.worktree("feature", &worktree_dir, None).unwrap();
130-
gitbutler_testsupport::set_test_data_dir(data_dir.path());
131-
let err = gitbutler_project::add(worktree.path(), None, None).unwrap_err();
132+
let err =
133+
gitbutler_project::add_with_path(data_dir.path(), worktree.path(), None, None)
134+
.unwrap_err();
132135
assert_eq!(err.to_string(), "can only work in main worktrees");
133136
}
134137

@@ -164,13 +167,12 @@ mod delete {
164167
#[test]
165168
fn success() {
166169
let data_dir = paths::data_dir();
167-
gitbutler_testsupport::set_test_data_dir(&data_dir);
168170
let repository = gitbutler_testsupport::TestProject::default();
169171
let path = repository.path();
170-
let project = gitbutler_project::add(path, None, None).unwrap();
171-
assert!(gitbutler_project::delete(project.id).is_ok());
172-
assert!(gitbutler_project::delete(project.id).is_ok()); // idempotent
173-
assert!(gitbutler_project::get(project.id).is_err());
172+
let project = gitbutler_project::add_with_path(data_dir.path(), path, None, None).unwrap();
173+
assert!(gitbutler_project::delete_with_path(data_dir.path(), project.id).is_ok());
174+
assert!(gitbutler_project::delete_with_path(data_dir.path(), project.id).is_ok()); // idempotent
175+
assert!(gitbutler_project::get_with_path(data_dir.path(), project.id).is_err());
174176
assert!(!project.gb_dir().exists());
175177
}
176178
}

0 commit comments

Comments
 (0)