Skip to content

Commit 4d29fc6

Browse files
committed
fix: removed missing Error variant; updated tests
Switched from assert_cmd to escargot because binary isn't being created. See https://docs.rs/assert_cmd/2.0.16/assert_cmd/cargo/#limitations.
1 parent c717d7e commit 4d29fc6

File tree

4 files changed

+73
-111
lines changed

4 files changed

+73
-111
lines changed

Cargo.lock

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

packages/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ path = "src/main.rs"
141141
name = "dx"
142142

143143
[dev-dependencies]
144-
assert_cmd = "2.0"
144+
escargot = "0.5"
145145

146146
[package.metadata.binstall]
147147
pkg-url = "{ repo }/releases/download/v{ version }/dx-{ target }-v{ version }{ archive-suffix }"

packages/cli/src/cli/create.rs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ pub(crate) fn post_create(path: &Path) -> Result<()> {
123123
Ok(v) => Some(v),
124124
// Only 1 error means that CWD isn't a cargo project.
125125
Err(cargo_metadata::Error::CargoMetadata { .. }) => None,
126-
Err(err) => return Err(Error::CargoMetadata(err)),
126+
Err(err) => {
127+
return Err(Error::Other(anyhow::anyhow!(
128+
"Couldn't retrieve cargo metadata: {:?}",
129+
err
130+
)));
131+
}
127132
}
128133
};
129134

@@ -199,21 +204,31 @@ fn remove_triple_newlines(string: &str) -> String {
199204

200205
#[cfg(test)]
201206
pub(crate) mod tests {
202-
use assert_cmd::Command;
207+
use escargot::{CargoBuild, CargoRun};
203208
use std::fs::{create_dir_all, read_to_string};
204209
use std::path::{Path, PathBuf};
210+
use std::process::Command;
211+
use std::sync::LazyLock;
205212
use tempfile::tempdir;
206213
use toml::Value;
207214

215+
static BINARY: LazyLock<CargoRun> = LazyLock::new(|| {
216+
CargoBuild::new()
217+
.bin(env!("CARGO_BIN_NAME"))
218+
.current_release()
219+
.run()
220+
.expect("Couldn't build the binary for tests.")
221+
});
222+
208223
// Note: tests below (at least 6 of them) were written to mainly test
209224
// correctness of project's directory and its name, because previously it
210225
// was broken and tests bring a peace of mind. And also so that I don't have
211226
// to run my local hand-made tests every time.
212227

213228
pub(crate) type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
214229

215-
pub(crate) fn subcommand(name: &str) -> Result<Command> {
216-
let mut command = Command::cargo_bin(env!("CARGO_BIN_NAME"))?;
230+
pub(crate) fn subcommand(name: &str) -> Command {
231+
let mut command = BINARY.command();
217232
command
218233
.arg(name)
219234
.arg("--yes") // Skip any questions by choosing default answers.
@@ -222,7 +237,7 @@ pub(crate) mod tests {
222237
// either `--subtemplate` or `--option`.
223238
// Maybe a simple template in tests/ dir?
224239
.arg("Fullstack");
225-
Ok(command)
240+
command
226241
}
227242

228243
pub(crate) fn get_cargo_toml_path(project_path: &Path) -> PathBuf {
@@ -240,7 +255,7 @@ pub(crate) mod tests {
240255
.to_string())
241256
}
242257

243-
fn subcommand_new() -> Result<Command> {
258+
fn subcommand_new() -> Command {
244259
subcommand("new")
245260
}
246261

@@ -256,11 +271,11 @@ pub(crate) mod tests {
256271
let project_path = &current_dir;
257272
assert!(project_path.exists());
258273

259-
subcommand_new()?
274+
assert!(subcommand_new()
260275
.arg(".")
261276
.current_dir(&current_dir)
262-
.assert()
263-
.success();
277+
.status()
278+
.is_ok());
264279

265280
let cargo_toml_path = get_cargo_toml_path(project_path);
266281
assert!(cargo_toml_path.exists());
@@ -275,11 +290,11 @@ pub(crate) mod tests {
275290

276291
let current_dir = tempdir()?;
277292

278-
subcommand_new()?
293+
assert!(subcommand_new()
279294
.arg(project_dir)
280295
.current_dir(&current_dir)
281-
.assert()
282-
.success();
296+
.status()
297+
.is_ok());
283298

284299
let project_path = current_dir.path().join(project_dir);
285300
let cargo_toml_path = get_cargo_toml_path(&project_path);
@@ -296,11 +311,11 @@ pub(crate) mod tests {
296311

297312
let current_dir = tempdir()?;
298313

299-
subcommand_new()?
314+
assert!(subcommand_new()
300315
.arg(project_dir)
301316
.current_dir(&current_dir)
302-
.assert()
303-
.success();
317+
.status()
318+
.is_ok());
304319

305320
let project_path = current_dir.path().join(project_dir);
306321
let cargo_toml_path = get_cargo_toml_path(&project_path);
@@ -322,13 +337,13 @@ pub(crate) mod tests {
322337
let project_path = &current_dir;
323338
assert!(project_path.exists());
324339

325-
subcommand_new()?
340+
assert!(subcommand_new()
326341
.arg("--name")
327342
.arg(project_name)
328343
.arg(".")
329344
.current_dir(&current_dir)
330-
.assert()
331-
.success();
345+
.status()
346+
.is_ok());
332347

333348
let cargo_toml_path = get_cargo_toml_path(project_path);
334349
assert!(cargo_toml_path.exists());
@@ -343,13 +358,13 @@ pub(crate) mod tests {
343358

344359
let current_dir = tempdir()?;
345360

346-
subcommand_new()?
361+
assert!(subcommand_new()
347362
.arg(project_dir)
348363
.arg("--name")
349364
.arg(project_name)
350365
.current_dir(&current_dir)
351-
.assert()
352-
.success();
366+
.status()
367+
.is_ok());
353368

354369
let project_path = current_dir.path().join(project_dir);
355370
let cargo_toml_path = get_cargo_toml_path(&project_path);
@@ -366,13 +381,13 @@ pub(crate) mod tests {
366381

367382
let current_dir = tempdir()?;
368383

369-
subcommand_new()?
384+
assert!(subcommand_new()
370385
.arg(project_dir)
371386
.arg("--name")
372387
.arg(project_name)
373388
.current_dir(&current_dir)
374-
.assert()
375-
.success();
389+
.status()
390+
.is_ok());
376391

377392
let project_path = current_dir.path().join(project_dir);
378393
let cargo_toml_path = get_cargo_toml_path(&project_path);

0 commit comments

Comments
 (0)