mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
Cleanup routines added for the created files during runs
This commit is contained in:
parent
8e7e878eab
commit
aef794d020
@ -12,17 +12,40 @@ fn main() {
|
||||
// TODO : What would be the expected text ?
|
||||
assert_eq!(, contents);
|
||||
}
|
||||
Err(_) => {
|
||||
panic!("Error reading file.");
|
||||
Err(err) => {
|
||||
eprintln!("File read error. {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
file_cleanup();
|
||||
}
|
||||
|
||||
fn create_required_files() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
|
||||
if !file_path.exists() {
|
||||
fs::write(file_path, "This is the file content.").unwrap();
|
||||
println!("File created.");
|
||||
let file_write_result = fs::write(file_path, "This is the file content.");
|
||||
if file_write_result.is_ok() {
|
||||
println!("Successfully wrote to file.");
|
||||
} else {
|
||||
panic!("Error writing to file.");
|
||||
}
|
||||
} else {
|
||||
println!("File already exist.");
|
||||
}
|
||||
}
|
||||
|
||||
fn file_cleanup() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
|
||||
if file_path.exists() {
|
||||
let remove_status = fs::remove_file(file_path);
|
||||
if remove_status.is_ok() {
|
||||
println!("Successfully removed file.");
|
||||
} else {
|
||||
panic!("Error deleting file.");
|
||||
}
|
||||
} else {
|
||||
println!("No cleanup necassary since file not exist.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,19 +2,21 @@ use std::fs;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
use std::path::Path;
|
||||
|
||||
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
|
||||
const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt";
|
||||
const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt";
|
||||
|
||||
fn main() {
|
||||
create_required_files();
|
||||
let input_file = fs::File::open(TEST_FILE_NAME);
|
||||
let input_file = fs::File::open(TEST_INPUT_FILE_NAME);
|
||||
|
||||
if input_file.is_err() {
|
||||
panic!("Input file open error");
|
||||
}
|
||||
|
||||
let buffered_input_file = BufReader::new(input_file.unwrap());
|
||||
// TODO : How to create a new BufReader using input file
|
||||
let buffered_input_file =;
|
||||
|
||||
let output_file = fs::File::create("MultiLineOutputFile.txt");
|
||||
let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME);
|
||||
|
||||
if output_file.is_err() {
|
||||
eprintln!(
|
||||
@ -23,8 +25,7 @@ fn main() {
|
||||
);
|
||||
panic!("Output file open error");
|
||||
}
|
||||
// TODO : How to create a new BufReader using input file
|
||||
let buffered_input_file =;
|
||||
let mut buffered_file_writer = BufWriter::new(output_file.ok().unwrap());
|
||||
|
||||
let mut line_number = 1;
|
||||
|
||||
@ -33,26 +34,59 @@ fn main() {
|
||||
let write_result =
|
||||
buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
|
||||
if write_result.is_err() {
|
||||
eprintln!("Write result error: {}", write_result.unwrap_err());
|
||||
eprintln!("Line write error: {}", write_result.unwrap_err());
|
||||
break;
|
||||
}
|
||||
line_number += 1;
|
||||
} else {
|
||||
panic!("Write line error");
|
||||
panic!("Line read error");
|
||||
}
|
||||
}
|
||||
|
||||
println!("{} : lines processed", line_number - 1);
|
||||
file_cleanup();
|
||||
}
|
||||
|
||||
fn create_required_files() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
let file_path = Path::new(TEST_INPUT_FILE_NAME);
|
||||
|
||||
if !file_path.exists() {
|
||||
let text = "This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.";
|
||||
fs::write(file_path, text).unwrap();
|
||||
println!("File created.");
|
||||
let file_write_result = fs::write(file_path, text);
|
||||
|
||||
if file_write_result.is_ok() {
|
||||
println!("Multi line file created successfully!");
|
||||
} else {
|
||||
eprintln!(
|
||||
"Error creating file : {} , error : {:?}",
|
||||
file_path.display(),
|
||||
file_write_result.err()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn file_cleanup() {
|
||||
let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME];
|
||||
|
||||
for file_name in file_names {
|
||||
let file_path = Path::new(file_name);
|
||||
|
||||
if file_path.exists() {
|
||||
let remove_status = fs::remove_file(file_path);
|
||||
if remove_status.is_ok() {
|
||||
println!("Successfully deleted file {}", file_name);
|
||||
} else {
|
||||
eprintln!(
|
||||
"Error deleting file {}, err : {:?}",
|
||||
file_name,
|
||||
remove_status.err()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
println!("No cleanup necassary since {} not exist.", file_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,14 +13,19 @@ fn main() {
|
||||
|
||||
if let Ok(meta_data) = meta_data_result {
|
||||
println!("Metadata about the file : {:?}", path_buffer);
|
||||
println!("File creation time {:?}", meta_data.created().unwrap());
|
||||
println!("File creation time {:?}", meta_data.created());
|
||||
println!("File size {}", meta_data.len());
|
||||
assert_eq!(meta_data.len(), 117);
|
||||
println!("File permissions {:?}", meta_data.permissions());
|
||||
assert!(!meta_data.permissions().readonly());
|
||||
} else {
|
||||
panic!("Could not get metadata");
|
||||
eprintln!(
|
||||
"Could not get metadata. Error: {:?}",
|
||||
meta_data_result.err()
|
||||
);
|
||||
}
|
||||
|
||||
file_cleanup();
|
||||
}
|
||||
|
||||
fn create_required_files() {
|
||||
@ -29,15 +34,53 @@ fn create_required_files() {
|
||||
let dir_path = file_path.parent().unwrap();
|
||||
|
||||
if !dir_path.exists() {
|
||||
fs::create_dir(dir_path).unwrap();
|
||||
println!("Created directory {:?}", dir_path);
|
||||
let dir_create_result = fs::create_dir(dir_path);
|
||||
if dir_create_result.is_ok() {
|
||||
println!("{:?} created", dir_path);
|
||||
}
|
||||
}
|
||||
|
||||
if !file_path.exists() {
|
||||
let text = "This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.";
|
||||
fs::write(file_path, text).unwrap();
|
||||
println!("File created.");
|
||||
let file_write_result = fs::write(&file_path, text);
|
||||
|
||||
if file_write_result.is_ok() {
|
||||
println!("Multi line file created successfully!");
|
||||
} else {
|
||||
eprintln!(
|
||||
"Error creating file : {} , error : {:?}",
|
||||
file_path.display(),
|
||||
file_write_result.err()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn file_cleanup() {
|
||||
let mut path_buffer = PathBuf::new();
|
||||
|
||||
path_buffer.push("SampleFilesFolder");
|
||||
path_buffer.push("MultiLineTextFile.txt");
|
||||
|
||||
if path_buffer.exists() {
|
||||
let remove_status = fs::remove_file(&path_buffer);
|
||||
if remove_status.is_ok() {
|
||||
println!("Test file deleted.");
|
||||
} else {
|
||||
panic!("Error deleting file.");
|
||||
}
|
||||
}
|
||||
|
||||
path_buffer.pop();
|
||||
|
||||
if path_buffer.exists() {
|
||||
let remove_status = fs::remove_dir(&path_buffer);
|
||||
if remove_status.is_ok() {
|
||||
println!("Test directory deleted.");
|
||||
} else {
|
||||
panic!("Error deleting directory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,17 +11,40 @@ fn main() {
|
||||
Ok(contents) => {
|
||||
assert_eq!("This is the file content.", contents);
|
||||
}
|
||||
Err(_) => {
|
||||
panic!("Error reading file.");
|
||||
Err(err) => {
|
||||
eprintln!("File read error. {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
file_cleanup();
|
||||
}
|
||||
|
||||
fn create_required_files() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
|
||||
if !file_path.exists() {
|
||||
fs::write(file_path, "This is the file content.").unwrap();
|
||||
println!("File created.");
|
||||
let file_write_result = fs::write(file_path, "This is the file content.");
|
||||
if file_write_result.is_ok() {
|
||||
println!("Successfully wrote to file.");
|
||||
} else {
|
||||
panic!("Error writing to file.");
|
||||
}
|
||||
} else {
|
||||
println!("File already exist.");
|
||||
}
|
||||
}
|
||||
|
||||
fn file_cleanup() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
|
||||
if file_path.exists() {
|
||||
let remove_status = fs::remove_file(file_path);
|
||||
if remove_status.is_ok() {
|
||||
println!("Successfully removed file.");
|
||||
} else {
|
||||
panic!("Error deleting file.");
|
||||
}
|
||||
} else {
|
||||
println!("No cleanup necassary since file not exist.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,11 +2,12 @@ use std::fs;
|
||||
use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
use std::path::Path;
|
||||
|
||||
const TEST_FILE_NAME: &str = "MultiLineTextFile.txt";
|
||||
const TEST_INPUT_FILE_NAME: &str = "MultiLineTextFile.txt";
|
||||
const TEST_OUTPUT_FILE_NAME: &str = "MultiLineOutputFile.txt";
|
||||
|
||||
fn main() {
|
||||
create_required_files();
|
||||
let input_file = fs::File::open(TEST_FILE_NAME);
|
||||
let input_file = fs::File::open(TEST_INPUT_FILE_NAME);
|
||||
|
||||
if input_file.is_err() {
|
||||
panic!("Input file open error");
|
||||
@ -14,7 +15,7 @@ fn main() {
|
||||
|
||||
let buffered_input_file = BufReader::new(input_file.unwrap());
|
||||
|
||||
let output_file = fs::File::create("MultiLineOutputFile.txt");
|
||||
let output_file = fs::File::create(TEST_OUTPUT_FILE_NAME);
|
||||
|
||||
if output_file.is_err() {
|
||||
eprintln!(
|
||||
@ -32,26 +33,59 @@ fn main() {
|
||||
let write_result =
|
||||
buffered_file_writer.write(format!("Line {} : {}\n", line_number, line).as_bytes());
|
||||
if write_result.is_err() {
|
||||
eprintln!("Write result error: {}", write_result.unwrap_err());
|
||||
eprintln!("Line write error: {}", write_result.unwrap_err());
|
||||
break;
|
||||
}
|
||||
line_number += 1;
|
||||
} else {
|
||||
panic!("Write line error");
|
||||
panic!("Line read error");
|
||||
}
|
||||
}
|
||||
|
||||
println!("{} : lines processed", line_number - 1);
|
||||
file_cleanup();
|
||||
}
|
||||
|
||||
fn create_required_files() {
|
||||
let file_path = Path::new(TEST_FILE_NAME);
|
||||
let file_path = Path::new(TEST_INPUT_FILE_NAME);
|
||||
|
||||
if !file_path.exists() {
|
||||
let text = "This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.";
|
||||
fs::write(file_path, text).unwrap();
|
||||
println!("File created.");
|
||||
let file_write_result = fs::write(file_path, text);
|
||||
|
||||
if file_write_result.is_ok() {
|
||||
println!("Multi line file created successfully!");
|
||||
} else {
|
||||
eprintln!(
|
||||
"Error creating file : {} , error : {:?}",
|
||||
file_path.display(),
|
||||
file_write_result.err()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn file_cleanup() {
|
||||
let file_names = vec![TEST_INPUT_FILE_NAME, TEST_OUTPUT_FILE_NAME];
|
||||
|
||||
for file_name in file_names {
|
||||
let file_path = Path::new(file_name);
|
||||
|
||||
if file_path.exists() {
|
||||
let remove_status = fs::remove_file(file_path);
|
||||
if remove_status.is_ok() {
|
||||
println!("Successfully deleted file {}", file_name);
|
||||
} else {
|
||||
eprintln!(
|
||||
"Error deleting file {}, err : {:?}",
|
||||
file_name,
|
||||
remove_status.err()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
println!("No cleanup necassary since {} not exist.", file_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,14 +12,19 @@ fn main() {
|
||||
|
||||
if let Ok(meta_data) = meta_data_result {
|
||||
println!("Metadata about the file : {:?}", path_buffer);
|
||||
println!("File creation time {:?}", meta_data.created().unwrap());
|
||||
println!("File creation time {:?}", meta_data.created());
|
||||
println!("File size {}", meta_data.len());
|
||||
assert_eq!(meta_data.len(), 117);
|
||||
println!("File permissions {:?}", meta_data.permissions());
|
||||
assert!(!meta_data.permissions().readonly());
|
||||
} else {
|
||||
panic!("Could not get metadata");
|
||||
eprintln!(
|
||||
"Could not get metadata. Error: {:?}",
|
||||
meta_data_result.err()
|
||||
);
|
||||
}
|
||||
|
||||
file_cleanup();
|
||||
}
|
||||
|
||||
fn create_required_files() {
|
||||
@ -28,15 +33,53 @@ fn create_required_files() {
|
||||
let dir_path = file_path.parent().unwrap();
|
||||
|
||||
if !dir_path.exists() {
|
||||
fs::create_dir(dir_path).unwrap();
|
||||
println!("Created directory {:?}", dir_path);
|
||||
let dir_create_result = fs::create_dir(dir_path);
|
||||
if dir_create_result.is_ok() {
|
||||
println!("{:?} created", dir_path);
|
||||
}
|
||||
}
|
||||
|
||||
if !file_path.exists() {
|
||||
let text = "This is the first line of the text.
|
||||
This is the second line.
|
||||
And this is the third and the last line.";
|
||||
fs::write(file_path, text).unwrap();
|
||||
println!("File created.");
|
||||
let file_write_result = fs::write(&file_path, text);
|
||||
|
||||
if file_write_result.is_ok() {
|
||||
println!("Multi line file created successfully!");
|
||||
} else {
|
||||
eprintln!(
|
||||
"Error creating file : {} , error : {:?}",
|
||||
file_path.display(),
|
||||
file_write_result.err()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn file_cleanup() {
|
||||
let mut path_buffer = PathBuf::new();
|
||||
|
||||
path_buffer.push("SampleFilesFolder");
|
||||
path_buffer.push("MultiLineTextFile.txt");
|
||||
|
||||
if path_buffer.exists() {
|
||||
let remove_status = fs::remove_file(&path_buffer);
|
||||
if remove_status.is_ok() {
|
||||
println!("Test file deleted.");
|
||||
} else {
|
||||
panic!("Error deleting file.");
|
||||
}
|
||||
}
|
||||
|
||||
path_buffer.pop();
|
||||
|
||||
if path_buffer.exists() {
|
||||
let remove_status = fs::remove_dir(&path_buffer);
|
||||
if remove_status.is_ok() {
|
||||
println!("Test directory deleted.");
|
||||
} else {
|
||||
panic!("Error deleting directory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user