Fix tests, move rpassword usage to utils.rs to implement something different for wasm.

This commit is contained in:
Oleksandr Kozachuk
2023-01-01 19:04:56 +01:00
parent eced302282
commit 7563645518
5 changed files with 25 additions and 23 deletions
+2 -2
View File
@@ -1,5 +1,4 @@
use regex::Regex;
use rpassword::prompt_password;
use sha1::{Digest, Sha1};
use std::cmp::min;
use std::collections::{HashMap, HashSet};
@@ -13,6 +12,7 @@ use crate::password::{Name, Password, PasswordRef};
use crate::repl::LKEval;
use crate::structs::{LKOut, Radix, CORRECT_FILE, DUMP_FILE};
use crate::utils::{call_cmd_with_input, get_cmd_args_from_command, get_copy_command_from_env, rnd};
use crate::utils::editor::password;
impl<'a> LKEval<'a> {
pub fn get_password(&self, name: &String) -> Option<PasswordRef> {
@@ -251,7 +251,7 @@ impl<'a> LKEval<'a> {
match command_parser::script(&script) {
Ok(cmd_list) => {
for cmd in cmd_list {
let print = LKEval::new(cmd, self.state.clone(), prompt_password).eval();
let print = LKEval::new(cmd, self.state.clone(), password).eval();
print.out.copy(&out);
if print.quit { return true; }
}
+8 -9
View File
@@ -1,10 +1,9 @@
use rpassword::prompt_password;
use std::{cell::RefCell, rc::Rc};
use crate::lk::LK;
use crate::parser::command_parser;
use crate::structs::{Command, LKErr, LKOut, HISTORY_FILE};
use crate::utils::editor::Editor;
use crate::utils::editor::{ Editor, password };
#[derive(Debug)]
pub struct LKRead {
@@ -36,7 +35,7 @@ impl LKRead {
prompt,
state,
cmd: "".to_string(),
read_password: prompt_password,
read_password: password,
}
}
@@ -153,8 +152,8 @@ mod tests {
use super::*;
use crate::password::Password;
use crate::structs::Mode;
use chrono::naive::NaiveDate;
use std::collections::HashMap;
use crate::utils::date::Date;
impl<'a> LKEval<'a> {
pub fn news(cmd: Command<'a>, state: Rc<RefCell<LK>>) -> Self {
@@ -181,7 +180,7 @@ mod tests {
length: None,
mode: Mode::Regular,
seq: 99,
date: NaiveDate::from_ymd_opt(2022, 12, 30).unwrap(),
date: Date::new(2022, 12, 30),
comment: Some("comment".to_string()),
parent: None,
}));
@@ -208,7 +207,7 @@ mod tests {
length: None,
mode: Mode::Regular,
seq: 99,
date: NaiveDate::from_ymd_opt(2022, 12, 31).unwrap(),
date: Date::new(2022, 12, 31),
comment: Some("bli blup".to_string()),
parent: None,
}));
@@ -252,7 +251,7 @@ mod tests {
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 30).unwrap(),
Date::new(2022, 12, 30),
None,
)));
let t2 = Rc::new(RefCell::new(Password::new(
@@ -261,7 +260,7 @@ mod tests {
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 30).unwrap(),
Date::new(2022, 12, 30),
None,
)));
let t3 = Rc::new(RefCell::new(Password::new(
@@ -270,7 +269,7 @@ mod tests {
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 12, 30).unwrap(),
Date::new(2022, 12, 30),
None,
)));
assert_eq!(
+9 -10
View File
@@ -1,5 +1,4 @@
use crate::password::{Comment, Name, PasswordRef};
use rpassword::prompt_password;
use std::fmt;
use std::path::Path;
use std::{cell::RefCell, rc::Rc};
@@ -8,7 +7,7 @@ use crate::lk::LK;
use crate::parser::command_parser;
use crate::repl::{LKEval, LKRead};
use crate::utils::home;
use crate::utils::editor::Editor;
use crate::utils::editor::{ Editor, password };
lazy_static! {
pub static ref HISTORY_FILE: Box<Path> = {
@@ -270,11 +269,11 @@ pub fn init() -> Option<LKRead> {
Ok(script) => match command_parser::script(&script) {
Ok(cmd_list) => {
for cmd in cmd_list {
if !LKEval::new(cmd, lk.clone(), prompt_password).eval().print() { return None; }
if !LKEval::new(cmd, lk.clone(), password).eval().print() { return None; }
}
}
Err(err) => {
LKEval::new(Command::Error(LKErr::ParseError(err)), lk.clone(), prompt_password).eval().print();
LKEval::new(Command::Error(LKErr::ParseError(err)), lk.clone(), password).eval().print();
}
},
Err(err) if err.kind() == std::io::ErrorKind::NotFound => (),
@@ -284,7 +283,7 @@ pub fn init() -> Option<LKRead> {
format!("Failed to read init file {:?}: {}", INIT_FILE.to_str(), err).as_str(),
)),
lk.clone(),
prompt_password,
password,
)
.eval()
.print();
@@ -297,7 +296,7 @@ pub fn init() -> Option<LKRead> {
mod tests {
use super::*;
use crate::password::Password;
use chrono::naive::NaiveDate;
use crate::utils::date::Date;
use std::io::{BufWriter, Write};
use std::os::unix::fs::PermissionsExt;
@@ -358,7 +357,7 @@ mod tests {
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 10, 10).unwrap(),
Date::new(2022, 10, 10),
None,
)));
let t2 = Rc::new(RefCell::new(Password::new(
@@ -367,7 +366,7 @@ mod tests {
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 10, 10).unwrap(),
Date::new(2022, 10, 10),
Some("test".to_string()),
)));
t2.borrow_mut().parent = Some(t1.clone());
@@ -377,7 +376,7 @@ mod tests {
None,
Mode::Regular,
99,
NaiveDate::from_ymd_opt(2022, 10, 10).unwrap(),
Date::new(2022, 10, 10),
Some("aoeu".to_string()),
)));
t3.borrow_mut().parent = Some(t2.clone());
@@ -385,7 +384,7 @@ mod tests {
assert_eq!(*lkread.state.borrow().db.get("t2").unwrap().borrow(), *t2.borrow());
assert_eq!(*lkread.state.borrow().db.get("t3").unwrap().borrow(), *t3.borrow());
LKEval::new(command_parser::cmd("dump").unwrap(), lkread.state.clone(), prompt_password)
LKEval::new(command_parser::cmd("dump").unwrap(), lkread.state.clone(), password)
.eval()
.print();
assert_eq!(
+5 -1
View File
@@ -10,7 +10,7 @@ pub mod date {
use chrono::naive::NaiveDate;
use chrono::Local;
#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct Date {
date: NaiveDate
}
@@ -107,6 +107,10 @@ pub mod editor {
}
}
}
pub fn password(prompt: impl ToString) -> std::io::Result<String> {
rpassword::prompt_password(prompt)
}
}
pub fn call_cmd_with_input(cmd: &str, args: &Vec<String>, input: &str) -> io::Result<String> {