-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File.Moveto method implemented, tested, and doc'd #25
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2565d63
File.Moveto method implemented, tested, and doc'd
hulto a1889f5
Merge branch 'main' into hulto-eldritch-feature-file-mv
hulto 811b42d
Merge branch 'main' into hulto-eldritch-feature-file-mv
hulto 4a04c2a
Merge branch 'main' into hulto-eldritch-feature-file-mv
hulto f05b16f
Merge branch 'main' into hulto-eldritch-feature-file-mv
hulto 68317dd
Merge branch 'main' into hulto-eldritch-feature-file-mv
hulto 123cf46
Merge branch 'main' into hulto-eldritch-feature-file-mv
hulto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use anyhow::Result; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
pub fn moveto(old: String, new: String) -> Result<()> { | ||
// If path is a dir delete it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels terrifying, but I respect the unified behavior |
||
// This will help unify behavior across systems. | ||
// https://doc.rust-lang.org/std/fs/fn.rename.html#platform-specific-behavior | ||
if Path::new(&new.clone()).is_dir() { | ||
fs::remove_dir_all(new.clone())?; | ||
} else if Path::new(&new.clone()).is_file() { | ||
fs::remove_file(new.clone())?; | ||
} | ||
|
||
fs::rename(old, new)?; | ||
Ok(()) | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use tempfile::{NamedTempFile,tempdir}; | ||
use std::path::Path; | ||
use std::fs::File; | ||
|
||
#[test] | ||
fn test_moveto_dir() -> anyhow::Result<()>{ | ||
let tmp_dir = tempdir()?; | ||
let path_old = String::from(tmp_dir.path().to_str().unwrap()).clone(); | ||
let tmp_dir_new = tempdir()?; | ||
let path_new = String::from(tmp_dir_new.path().to_str().unwrap()).clone(); | ||
tmp_dir_new.close()?; | ||
|
||
|
||
moveto(path_old.clone(), path_new.clone())?; | ||
|
||
assert_eq!(Path::new(&path_old).is_dir(), false); | ||
assert_eq!(Path::new(&path_new).is_dir(), true); | ||
Ok(()) | ||
} | ||
#[test] | ||
fn test_moveto_file() -> anyhow::Result<()>{ | ||
let tmp_file = NamedTempFile::new()?; | ||
let path_old = String::from(tmp_file.path().to_str().unwrap()).clone(); | ||
let tmp_file_new = NamedTempFile::new()?; | ||
let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); | ||
tmp_file_new.close()?; | ||
// Run our code | ||
|
||
moveto(path_old.clone(), path_new.clone())?; | ||
|
||
assert_eq!(Path::new(&path_old).is_file(), false); | ||
assert_eq!(Path::new(&path_new).is_file(), true); | ||
Ok(()) | ||
} | ||
#[test] | ||
fn test_moveto_file_overwrites() -> anyhow::Result<()>{ | ||
let tmp_file = NamedTempFile::new()?; | ||
let path_old = String::from(tmp_file.path().to_str().unwrap()).clone(); | ||
let tmp_file_new = NamedTempFile::new()?; | ||
let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); | ||
// Run our code | ||
|
||
moveto(path_old.clone(), path_new.clone())?; | ||
|
||
assert_eq!(Path::new(&path_old).is_file(), false); | ||
assert_eq!(Path::new(&path_new).is_file(), true); | ||
Ok(()) | ||
} | ||
#[test] | ||
fn test_moveto_dir_overwrites() -> anyhow::Result<()>{ | ||
// Create initial directory and file. | ||
let tmp_dir = tempdir()?; | ||
let tmp_dir_path = tmp_dir.path(); | ||
|
||
let path_old = String::from(tmp_dir_path.to_str().unwrap()).clone(); | ||
let path_old_file = String::from(tmp_dir_path.join("myfile2").to_str().unwrap()).clone(); | ||
let _ = File::create(path_old_file.clone())?; | ||
let _ = fs::write(path_old_file.clone(),"Hello"); | ||
|
||
// Create destination directory and the file path we expect after moveto "win" | ||
let tmp_dir_new = tempdir()?; | ||
let path_new = String::from(tmp_dir_new.path().to_str().unwrap()).clone(); | ||
let path_new_file = String::from(tmp_dir_new.path().join("myfile").to_str().unwrap()).clone(); | ||
let path_new_file_win = String::from(tmp_dir_new.path().join("myfile2").to_str().unwrap()).clone(); | ||
|
||
let _ = File::create(path_new_file.clone())?; | ||
|
||
|
||
// Run our code | ||
moveto(path_old.clone(), path_new.clone())?; | ||
|
||
// Assert | ||
assert_eq!(Path::new(&path_old).is_dir(), false); | ||
assert_eq!(Path::new(&path_new_file_win).is_file(), true); | ||
assert_eq!(Path::new(&path_new_file).is_file(), false); | ||
assert_eq!(Path::new(&path_new).is_dir(), true); | ||
Ok(()) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧠