Skip to content

Commit 9f9a754

Browse files
authored
Merge pull request #2076 from senekor/remo/snryotxotoxv
Improve initialization in workspace
2 parents f7b0cfe + 8b43d79 commit 9f9a754

File tree

4 files changed

+81
-26
lines changed

4 files changed

+81
-26
lines changed

Cargo.lock

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ serde_json = "1.0.122"
5757
serde.workspace = true
5858
toml_edit.workspace = true
5959

60+
[dev-dependencies]
61+
tempfile = "3.12.0"
62+
6063
[profile.release]
6164
panic = "abort"
6265

src/init.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
env::set_current_dir,
55
fs::{self, create_dir},
66
io::{self, Write},
7-
path::Path,
7+
path::{Path, PathBuf},
88
process::{Command, Stdio},
99
};
1010

@@ -22,14 +22,27 @@ pub fn init() -> Result<()> {
2222
let mut stdout = io::stdout().lock();
2323
let mut init_git = true;
2424

25-
if Path::new("Cargo.toml").exists() {
25+
let manifest_path = Command::new("cargo")
26+
.args(["locate-project", "--message-format=plain"])
27+
.output()?;
28+
if manifest_path.status.success() {
29+
let manifest_path: PathBuf = String::from_utf8_lossy(&manifest_path.stdout).trim().into();
30+
2631
if Path::new("exercises").exists() && Path::new("solutions").exists() {
2732
bail!(IN_INITIALIZED_DIR_ERR);
2833
}
29-
30-
stdout.write_all(CARGO_TOML_EXISTS_PROMPT_MSG)?;
31-
press_enter_prompt(&mut stdout)?;
32-
init_git = false;
34+
if fs::read_to_string(manifest_path)?.contains("[workspace]") {
35+
// make sure "rustlings" is added to `workspace.members` by making
36+
// cargo initialize a new project
37+
let output = Command::new("cargo").args(["new", "rustlings"]).output()?;
38+
if !output.status.success() {
39+
bail!("Failed to initilize new workspace member");
40+
}
41+
fs::remove_dir_all("rustlings")?;
42+
init_git = false;
43+
} else {
44+
bail!(IN_NON_WORKSPACE_CARGO_PROJECT_ERR);
45+
}
3346
}
3447

3548
stdout.write_all(b"This command will create the directory `rustlings/` which will contain the exercises.\nPress ENTER to continue ")?;
@@ -128,19 +141,9 @@ You probably already initialized Rustlings.
128141
Run `cd rustlings`
129142
Then run `rustlings` again";
130143

131-
const CARGO_TOML_EXISTS_PROMPT_MSG: &[u8] = br#"You are about to initialize Rustlings in a directory that already contains a `Cargo.toml` file!
132-
133-
=> It is recommended to abort with CTRL+C and initialize Rustlings in another directory <=
134-
135-
If you know what you are doing and want to initialize Rustlings in a Cargo workspace,
136-
then you need to add its directory to `members` in the `workspace` section of the `Cargo.toml` file:
137-
138-
```toml
139-
[workspace]
140-
members = ["rustlings"]
141-
```
142-
143-
Press ENTER if you are sure that you want to continue after reading the warning above "#;
144+
const IN_NON_WORKSPACE_CARGO_PROJECT_ERR: &str = "\
145+
The current directory is already part of a cargo project.
146+
Please initialize rustlings in a different directory.";
144147

145148
const POST_INIT_MSG: &str = "Run `cd rustlings` to go into the generated directory.
146149
Then run `rustlings` to get started.";

tests/integration_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,28 @@ fn hint() {
155155

156156
#[test]
157157
fn init() {
158-
let _ = fs::remove_dir_all("tests/rustlings");
158+
let test_dir = tempfile::TempDir::new().unwrap();
159+
let initialized_dir = test_dir.path().join("rustlings");
160+
let test_dir = test_dir.path().to_str().unwrap();
159161

160-
Cmd::default().current_dir("tests").fail();
162+
Cmd::default().current_dir(test_dir).fail();
161163

162164
Cmd::default()
163-
.current_dir("tests")
165+
.current_dir(test_dir)
164166
.args(&["init"])
165167
.success();
166168

167169
// Running `init` after a successful initialization.
168170
Cmd::default()
169-
.current_dir("tests")
171+
.current_dir(test_dir)
170172
.args(&["init"])
171173
.output(PartialStderr("`cd rustlings`"))
172174
.fail();
173175

174176
// Running `init` in the initialized directory.
175177
Cmd::default()
176-
.current_dir("tests/rustlings")
178+
.current_dir(initialized_dir.to_str().unwrap())
177179
.args(&["init"])
178180
.output(PartialStderr("already initialized"))
179181
.fail();
180-
181-
fs::remove_dir_all("tests/rustlings").unwrap();
182182
}

0 commit comments

Comments
 (0)