szip: run vfmt, refactor test

pull/9539/head^2
Delyan Angelov 2021-04-02 08:26:14 +03:00
parent 55c8e6d1cc
commit 5ac9e39d44
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 33 additions and 37 deletions

View File

@ -203,7 +203,6 @@ pub fn (mut zentry Zip) extract_entry(path string) ? {
} }
} }
// extract zip file to directory // extract zip file to directory
pub fn extract_zip_to_dir(file string, dir string) ?bool { pub fn extract_zip_to_dir(file string, dir string) ?bool {
if C.access(dir.str, 0) == -1 { if C.access(dir.str, 0) == -1 {
@ -215,19 +214,17 @@ pub fn extract_zip_to_dir(file string, dir string) ?bool {
// zip files (full path) to zip file // zip files (full path) to zip file
pub fn zip_files(path_to_file []string, path_to_export_zip string) ? { pub fn zip_files(path_to_file []string, path_to_export_zip string) ? {
// open or create new zip // open or create new zip
mut zip := szip.open(path_to_export_zip, .no_compression, .write) or { panic(err) } mut zip := open(path_to_export_zip, .no_compression, .write) or { panic(err) }
// add all files from the directory to the archive // add all files from the directory to the archive
for file in path_to_file { for file in path_to_file {
// add file to zip // add file to zip
zip.open_entry(os.base(file)) or { panic(err) } zip.open_entry(os.base(file)) or { panic(err) }
file_as_byte := os.read_bytes(file) or { panic(err) } file_as_byte := os.read_bytes(file) or { panic(err) }
zip.write_entry(file_as_byte) or { panic(err) } zip.write_entry(file_as_byte) or { panic(err) }
zip.close_entry() zip.close_entry()
} }
// close zip // close zip
@ -237,7 +234,6 @@ pub fn zip_files(path_to_file []string, path_to_export_zip string) ? {
} }
/* /*
TODO add TODO add
// zip all files in directory to zip file // zip all files in directory to zip file
pub fn zip_folder(path_to_dir string, path_to_export_zip string) { pub fn zip_folder(path_to_dir string, path_to_export_zip string) {

View File

@ -1,37 +1,37 @@
import szip import szip
import os import os
fn test_szip() { const (
test_out_zip = 'v_test_zip.zip'
test_path = 'zip files'
fpath1 = os.join_path(test_path, 'file_1.txt')
fpath2 = os.join_path(test_path, 'file_2.txt')
)
// create temp files for zip/unzip test fn test_szip_create_temp_files() ? {
test_path := 'zip files' os.chdir(os.temp_dir())
test_out_zip := 'v_test_zip.zip' os.rmdir_all(test_path) or {}
os.mkdir(test_path) ?
os.mkdir(test_path) or { panic(err) } os.write_file(fpath1, 'file one') ?
os.write_file(test_path + os.path_separator + 'file_1.txt', 'file one') or { panic(err) } os.write_file(fpath2, 'file two') ?
os.write_file(test_path + os.path_separator + 'file_2.txt', 'file file two') or { panic(err) } assert os.exists(fpath1)
assert os.exists(fpath2)
// get list files from directory }
mut files := os.ls(test_path) or { panic(err) }
for mut file in files { fn test_zipping_files() ? {
file = os.getwd() + os.path_separator + test_path + os.path_separator + *file files := (os.ls(test_path) ?).map(os.join_path(test_path, it))
} szip.zip_files(files, test_out_zip) ?
assert os.exists(test_out_zip)
// zip files }
szip.zip_files(files, test_out_zip) or { panic(err) }
assert os.exists(test_out_zip) fn test_extract_zipped_files() ? {
os.rm(fpath1) ?
// remove files before next test os.rm(fpath2) ?
os.rm(test_path + os.path_separator + 'file_1.txt') or { panic(err) } szip.extract_zip_to_dir(test_out_zip, test_path) ?
os.rm(test_path + os.path_separator + 'file_2.txt') or { panic(err) } assert os.exists(fpath1)
assert os.exists(fpath2)
// extract test assert (os.read_file(fpath1) ?) == 'file one'
szip.extract_zip_to_dir(test_out_zip, test_path) or { panic(err) } assert (os.read_file(fpath2) ?) == 'file two'
assert os.exists(test_path + os.path_separator + 'file_1.txt') os.rmdir_all(test_path) ?
assert os.exists(test_path + os.path_separator + 'file_2.txt') os.rm(test_out_zip) or {}
// clear temp files
// remove temp files and dir
os.rmdir_all (test_path) or { panic(err) }
os.rm(test_out_zip) or { }
} }