Add list support to zip command, refactor the tests

This commit is contained in:
Nikita Medvedev 2023-01-04 21:41:01 +07:00
parent fbac8cf292
commit 7c1589b8a8
3 changed files with 98 additions and 31 deletions

View file

@ -1,10 +1,13 @@
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;
use zip::write::FileOptions;
use zip::{CompressionMethod, ZipWriter};
use duckscript::types::command::{Command, CommandResult};
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use crate::utils::{pckg, state};
#[derive(Clone)]
pub(crate) struct CommandImpl {
@ -35,7 +38,20 @@ impl Command for CommandImpl {
Box::new((*self).clone())
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
fn requires_context(&self) -> bool {
true
}
fn run_with_context(
&self,
arguments: Vec<String>,
state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
_instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize) -> CommandResult
{
if arguments.len() < 2 {
return CommandResult::Error(
"Paths to the ZIP file and/or files to pack are not provided.".to_string());
@ -46,7 +62,7 @@ impl Command for CommandImpl {
let mut base = None;
let mut compression = CompressionMethod::Deflated;
let mut zipfile = None;
let mut files = vec![];
let mut file_args = Vec::new();
for argument in &arguments {
match looking_for {
@ -79,11 +95,11 @@ impl Command for CommandImpl {
looking_for = LookingFor::Options;
},
LookingFor::Files => files.push(argument.as_str()),
LookingFor::Files => file_args.push(argument.as_str()),
}
}
if files.is_empty() {
if file_args.is_empty() {
return CommandResult::Error("Input files not provided.".to_string());
}
@ -128,8 +144,26 @@ impl Command for CommandImpl {
.compression_method(compression)
.unix_permissions(0o755);
let mut files = Vec::new();
for arg in file_args {
match state::get_handle(state, arg.to_string()) {
Some(value) =>
match value {
StateValue::List(entries) =>
for entry in entries {
match state::get_as_string(entry) {
Ok(file) => files.push(file),
Err(err) => return CommandResult::Error(err),
}
}
_ => files.push(arg.to_string()),
}
None => files.push(arg.to_string()),
};
}
for file_to_add_str in files {
let file_to_add_path = Path::new(file_to_add_str);
let file_to_add_path = Path::new(&file_to_add_str);
let mut file_to_add = match File::open(file_to_add_path) {
Ok(file) => file,
Err(err) => return CommandResult::Error(

View file

@ -1,58 +1,87 @@
base = set ./target/_duckscript_test/zip/pack
fn randfile
length = random_range 10 50
text = random_text ${length}
writefile ${1} ${text}
end
fn test_create_simple
zipped = zip ./target/_duckscript_test/zip/test_simple.zip ./Cargo.toml ./Cargo.lock
randfile ${base}/foo.txt
randfile ${base}/bar/baz.txt
zipped = zip ${base}/test_simple.zip ${base}/foo.txt ${base}/bar/baz.txt
assert ${zipped}
exists = is_file ./target/_duckscript_test/zip/test_simple.zip
exists = is_file ${base}/test_simple.zip
assert ${exists}
end
fn test_create_base
touch ./target/_duckscript_test/zip/foo.txt
fn test_create_from_array
files = array foo.txt bar.txt baz/qux.txt
files_to_zip = array
for file in ${files}
randfile ${base}/${file}
array_push ${files_to_zip} ${base}/${file}
end
zipped = zip --base ./target/_duckscript_test ./target/_duckscript_test/zip/test_base.zip ./target/_duckscript_test/zip/foo.txt
zipped = zip ${base}/test_array.zip ${files_to_zip}
assert ${zipped}
exists = is_file ./target/_duckscript_test/zip/test_base.zip
exists = is_file ${base}/test_array.zip
assert ${exists}
release ${files}
release ${files_to_zip}
end
fn test_create_base
randfile ${base}/foo.txt
zipped = zip --base ${base} ${base}/test_base.zip ${base}/foo.txt
assert ${zipped}
exists = is_file ${base}/test_base.zip
assert ${exists}
end
fn test_append
touch ./target/_duckscript_test/zip/foo.txt
touch ./target/_duckscript_test/zip/bar.txt
touch ./target/_duckscript_test/zip/baz/qux.txt
randfile ${base}/foo.txt
randfile ${base}/bar.txt
randfile ${base}/baz/qux.txt
zipped = zip --base ./target/_duckscript_test ./target/_duckscript_test/zip/test_append.zip ./target/_duckscript_test/zip/foo.txt
zipped = zip --base ${base} ${base}/test_append.zip ${base}/foo.txt
assert ${zipped}
zipped = zip --append --base ./target/_duckscript_test ./target/_duckscript_test/zip/test_append.zip ./target/_duckscript_test/zip/bar.txt
zipped = zip --append --base ${base} ${base}/test_append.zip ${base}/bar.txt
assert ${zipped}
zipped = zip --append --base ./target/_duckscript_test ./target/_duckscript_test/zip/test_append.zip ./target/_duckscript_test/zip/baz/qux.txt
zipped = zip --append --base ${base} ${base}/test_append.zip ${base}/baz/qux.txt
assert ${zipped}
exists = is_file ./target/_duckscript_test/zip/test_append.zip
exists = is_file ${base}/test_append.zip
assert ${exists}
end
fn test_compression_mode
touch ./target/_duckscript_test/zip/foo.txt
touch ./target/_duckscript_test/zip/bar.txt
randfile ${base}/foo.txt
randfile ${base}/bar.txt
zipped = zip --compression deflate ./target/_duckscript_test/zip/test_deflate.zip ./target/_duckscript_test/zip/foo.txt ./target/_duckscript_test/zip/bar.txt
zipped = zip --compression deflate ${base}/test_deflate.zip ${base}/foo.txt ${base}/bar.txt
assert ${zipped}
exists = is_file ./target/_duckscript_test/zip/test_deflate.zip
exists = is_file ${base}/test_deflate.zip
assert ${exists}
zipped = zip --compression bzip2 ./target/_duckscript_test/zip/test_bzip2.zip ./target/_duckscript_test/zip/foo.txt ./target/_duckscript_test/zip/bar.txt
zipped = zip --compression bzip2 ${base}/test_bzip2.zip ${base}/foo.txt ${base}/bar.txt
assert ${zipped}
exists = is_file ./target/_duckscript_test/zip/test_bzip2.zip
exists = is_file ${base}/test_bzip2.zip
assert ${exists}
zipped = zip --compression none ./target/_duckscript_test/zip/test_none.zip ./target/_duckscript_test/zip/foo.txt ./target/_duckscript_test/zip/bar.txt
zipped = zip --compression none ${base}/test_none.zip ${base}/foo.txt ${base}/bar.txt
assert ${zipped}
exists = is_file ./target/_duckscript_test/zip/test_none.zip
exists = is_file ${base}/test_none.zip
assert ${exists}
end

View file

@ -1,11 +1,15 @@
base = set ./target/_duckscript_test/zip/unpack
fn randfile
length = random_range 10 50
text = random_text ${length}
writefile ${1} ${text}
end
fn test_unpack_simple
files = array foo.txt bar/baz.txt bar/baf.txt qux.txt
for file in ${files}
length = random_range 10 50
text = random_text ${length}
writefile ${base}/src/${file} ${text}
randfile ${base}/src/${file}
end
zipped = zip --base ${base}/src ${base}/test_unpack.zip ${base}/src/foo.txt ${base}/src/bar/baz.txt ${base}/src/bar/baf.txt ${base}/src/qux.txt