mkdir command #13

This commit is contained in:
sagie gur ari 2020-01-01 18:33:50 +00:00
parent 2e32c564b2
commit c0bb87446a
9 changed files with 134 additions and 10 deletions

View file

@ -0,0 +1,19 @@
```sh
var = readfile file
```
The readfile command will read the requested file and return the value to the output variable.
#### Parameters
A single parameter holding the file path.
#### Return Value
The file content.
#### Examples
```sh
text = readfile ./Cargo.toml
```

View file

@ -0,0 +1,41 @@
use crate::utils::{io, pckg};
use duckscript::types::command::{Command, CommandResult};
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "CreateDirectory")
}
fn aliases(&self) -> Vec<String> {
vec!["mkdir".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("Directory name not provided.".to_string())
} else {
match io::create_directory(&arguments[0]) {
Ok(_) => CommandResult::Continue(Some("true".to_string())),
Err(error) => CommandResult::Error(error),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,48 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_directory_path_provided() {
test::run_script_and_fail(vec![create("")], "mkdir");
}
#[test]
fn run_directory_provided() {
test::run_script_and_validate(
vec![create("")],
"out = mkdir ./target/_duckscript/mkdir/run_no_directory_provided/1/2",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_directory_already_exists() {
test::run_script_and_validate(
vec![create("")],
r#"
mkdir ./target/_duckscript/mkdir/run_no_directory_already_exists
out = mkdir ./target/_duckscript/mkdir/run_no_directory_already_exists
"#,
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_directory_exists_as_file() {
let result = io::write_text_file(
"./target/_duckscript/mkdir/run_directory_exists_as_file/test.txt",
"test file",
);
assert!(result.is_ok());
test::run_script_and_fail(
vec![create("")],
"out = mkdir ./target/_duckscript/mkdir/run_directory_exists_as_file/test.txt",
);
}

View file

@ -1,3 +1,4 @@
mod mkdir;
mod print;
mod read;
mod write;
@ -11,6 +12,7 @@ static PACKAGE: &str = "fs";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(mkdir::create(&package))?;
commands.set(print::create(&package))?;
commands.set(read::create(&package))?;
commands.set(write::create(&package))?;

View file

@ -1,5 +1,4 @@
use crate::utils::io;
use crate::utils::pckg;
use crate::utils::{io, pckg};
use duckscript::types::command::{Command, CommandResult};
#[cfg(test)]

View file

@ -1,5 +1,4 @@
use crate::utils::io;
use crate::utils::pckg;
use crate::utils::{io, pckg};
use duckscript::types::command::{Command, CommandResult};
#[cfg(test)]

View file

@ -14,16 +14,19 @@ fn run_no_file_provided() {
#[test]
fn run_no_text_provided() {
test::run_script_and_fail(vec![create("")], "writefile ./target/tests/writefile.txt");
test::run_script_and_fail(
vec![create("")],
"writefile ./target/_duckscript/write/writefile.txt",
);
}
#[test]
fn run_valid() {
test::run_script_and_validate(
vec![create("")],
r#"out = writefile ./target/tests/writefile.txt "line 1\nline 2""#,
r#"out = writefile ./target/_duckscript/write/writefile.txt "line 1\nline 2""#,
CommandValidation::Match("out".to_string(), "true".to_string()),
);
let text = io::read_text_file("./target/tests/writefile.txt").unwrap();
let text = io::read_text_file("./target/_duckscript/write/writefile.txt").unwrap();
assert_eq!(text, "line 1\nline 2")
}

View file

@ -7,6 +7,19 @@ use std::path::Path;
#[path = "./io_test.rs"]
mod io_test;
pub(crate) fn create_directory(directory: &str) -> Result<(), String> {
let directory_path = Path::new(directory);
create_directory_for_path(&directory_path)
}
fn create_directory_for_path(directory_path: &Path) -> Result<(), String> {
match create_dir_all(&directory_path) {
Ok(_) => Ok(()),
Err(error) => Err(error.to_string()),
}
}
pub(crate) fn read_text_file(file: &str) -> Result<String, ScriptError> {
let file_path = Path::new(file);
@ -29,7 +42,7 @@ pub(crate) fn write_text_file(file: &str, text: &str) -> Result<(), ScriptError>
// create parent directory
let directory = file_path.parent();
match directory {
Some(directory_path) => match create_dir_all(&directory_path) {
Some(directory_path) => match create_directory_for_path(&directory_path) {
_ => (),
},
None => (),

View file

@ -16,11 +16,11 @@ fn read_text_file_not_found() {
#[test]
fn write_text_file_valid() {
let result = write_text_file("./target/temp/test/test.txt", "test file");
let result = write_text_file("./target/_duckscript/temp/test/test.txt", "test file");
assert!(result.is_ok());
let text = read_text_file("./target/temp/test/test.txt").unwrap();
let text = read_text_file("./target/_duckscript/temp/test/test.txt").unwrap();
assert_eq!(text, "test file");
}