Skip to content

Commit dbc468f

Browse files
committed
Dedicated error for for_cwd() failures
1 parent 0a2f0dc commit dbc468f

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ v0.3.0 (in development)
1313
separate the hostname from the path with a slash rather than a colon)
1414
- Schemes & hostnames in URLs are now parsed case-insensitively
1515
- Switch from clap to lexopt
16+
- `LocalRepo::for_cwd()` now returns a new dedicated variant of
17+
`LocalRepoError` if the call to `std::env::current_dir()` fails
1618

1719
v0.2.1 (2022-10-19)
1820
-------------------

src/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ impl LocalRepo {
306306
///
307307
/// # Errors
308308
///
309-
/// Returns failures from [`std::env::current_dir()`]
310-
pub fn for_cwd() -> Result<Self, io::Error> {
309+
/// Returns a [`LocalRepoError`] if [`std::env::current_dir()`] failed
310+
pub fn for_cwd() -> Result<Self, LocalRepoError> {
311311
Ok(LocalRepo {
312-
path: env::current_dir()?,
312+
path: env::current_dir().map_err(LocalRepoError::CurdirError)?,
313313
})
314314
}
315315

@@ -331,7 +331,8 @@ impl LocalRepo {
331331
.stdout(Stdio::null())
332332
.stderr(Stdio::null())
333333
.current_dir(&self.path)
334-
.status()?
334+
.status()
335+
.map_err(LocalRepoError::CouldNotExecute)?
335336
.success())
336337
}
337338

@@ -342,7 +343,8 @@ impl LocalRepo {
342343
.args(args)
343344
.current_dir(&self.path)
344345
.stderr(Stdio::inherit())
345-
.output()?;
346+
.output()
347+
.map_err(LocalRepoError::CouldNotExecute)?;
346348
if out.status.success() {
347349
Ok(str::from_utf8(&out.stdout)?.trim().to_string())
348350
} else {
@@ -414,6 +416,10 @@ pub enum LocalRepoError {
414416
/// Returned when the Git command returned nonzero
415417
CommandFailed(ExitStatus),
416418

419+
/// Returned by [`LocalRepo::for_cwd()`] if [`std::env::current_dir()`]
420+
/// errored
421+
CurdirError(io::Error),
422+
417423
/// Returned by [`LocalRepo::current_branch()`] if the repository is in a
418424
/// detached `HEAD` state
419425
DetachedHead,
@@ -444,6 +450,9 @@ impl fmt::Display for LocalRepoError {
444450
LocalRepoError::CommandFailed(r) => {
445451
write!(f, "Git command exited unsuccessfully: {}", r)
446452
}
453+
LocalRepoError::CurdirError(e) => {
454+
write!(f, "Could not determine current directory: {}", e)
455+
}
447456
LocalRepoError::DetachedHead => {
448457
write!(f, "Git repository is in a detached HEAD state")
449458
}
@@ -472,6 +481,7 @@ impl error::Error for LocalRepoError {
472481
match self {
473482
LocalRepoError::CouldNotExecute(e) => Some(e),
474483
LocalRepoError::CommandFailed(_) => None,
484+
LocalRepoError::CurdirError(e) => Some(e),
475485
LocalRepoError::DetachedHead => None,
476486
LocalRepoError::NoSuchRemote(_) => None,
477487
LocalRepoError::NoUpstream(_) => None,
@@ -481,12 +491,6 @@ impl error::Error for LocalRepoError {
481491
}
482492
}
483493

484-
impl From<io::Error> for LocalRepoError {
485-
fn from(e: io::Error) -> LocalRepoError {
486-
LocalRepoError::CouldNotExecute(e)
487-
}
488-
}
489-
490494
impl From<str::Utf8Error> for LocalRepoError {
491495
fn from(e: str::Utf8Error) -> LocalRepoError {
492496
LocalRepoError::InvalidUtf8(e)

tests/errors.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ fn test_display_local_repo_error_could_not_execute() {
3434
);
3535
}
3636

37+
#[test]
38+
fn test_display_local_repo_error_curdir() {
39+
let e = LocalRepoError::CurdirError(Error::from(ErrorKind::NotFound));
40+
assert_eq!(
41+
e.to_string(),
42+
format!(
43+
"Could not determine current directory: {}",
44+
Error::from(ErrorKind::NotFound)
45+
)
46+
);
47+
}
48+
3749
#[test]
3850
fn test_display_local_repo_error_detached_head() {
3951
let e = LocalRepoError::DetachedHead;

0 commit comments

Comments
 (0)