Skip to content

Commit

Permalink
Merge branch 'dev-test-command'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Sep 10, 2024
2 parents 957f4ee + fd7c15b commit a467b95
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/elements/command/error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
../../../test/test_compound.bash
19 changes: 11 additions & 8 deletions src/elements/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{error_message, ShellCore, Feeder};
use std::path::Path;
use crate::utils::file_check;
use super::{Command, Redirect};
use crate::elements::command;
use crate::elements::word::Word;
Expand Down Expand Up @@ -186,13 +186,16 @@ impl TestCommand {
}

fn unary_calc(op: &str, s: &String, stack: &mut Vec<Elem>) -> Result<(), String> {
match op {
"-a" => {
let ans = Path::new(s).is_file();
stack.push( Elem::Ans(ans) );
},
_ => stack.push( Elem::Ans(false) ),
}
let result = match op {
"-a" | "-e" => file_check::exists(s),
"-b" => file_check::type_check(s, "-b"),
"-c" => file_check::type_check(s, "-c"),
"-d" => file_check::is_dir(s),
"-f" => file_check::is_regular_file(s),
_ => return Err("unsupported option".to_string()),
};

stack.push( Elem::Ans(result) );
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda <ryuichiueda@gmail.com>
//SPDX-License-Identifier: BSD-3-Clause

pub mod file_check;
pub mod glob;
pub mod directory;

Expand Down
31 changes: 31 additions & 0 deletions src/utils/file_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda ryuichiueda@gmail.com
//SPDX-License-Identifier: BSD-3-Clause

use std::fs;
use std::os::unix::fs::FileTypeExt;
use std::path::Path;

pub fn exists(name: &str) -> bool {
fs::metadata(name).is_ok()
}

pub fn is_regular_file(name: &str) -> bool {
Path::new(name).is_file()
}

pub fn is_dir(name: &str) -> bool {
Path::new(name).is_dir()
}

pub fn type_check(name: &str, tp: &str) -> bool {
let meta = match fs::metadata(name) {
Ok(m) => m,
_ => return false,
};

match tp {
"-b" => meta.file_type().is_block_device(),
"-c" => meta.file_type().is_char_device(),
_ => false,
}
}
3 changes: 3 additions & 0 deletions test/error
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
../../../../test/test_compound.bash
../../../../test/test_compound.bash
../../../../test/test_compound.bash
5 changes: 5 additions & 0 deletions test/ok
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
./test_compound.bash
./test_others.bash
./test_job.bash
../../../../test/test_compound.bash
../../../../test/test_compound.bash
../../../../test/test_compound.bash
../../../../test/test_compound.bash
../../../../test/test_compound.bash
27 changes: 27 additions & 0 deletions test/test_compound.bash
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,18 @@ res=$($com <<< '(( 0 + 1 + 2+3 ))')
res=$($com -c '[[ -a /etc/passwd ]]')
[ "$?" = "0" ] || err $LINENO

res=$($com -c '[[ -e /etc/passwd ]]')
[ "$?" = "0" ] || err $LINENO

res=$($com -c '[[ -a /etc/passwdaaa ]]')
[ "$?" = "1" ] || err $LINENO

res=$($com -c '[[ -e /etc/passwdaaa ]]')
[ "$?" = "1" ] || err $LINENO

res=$($com -c '[[ ! -e /dev/tty0 ]] || [[ ! -a /dev/tty0 ]]')
[ "$?" = "1" ] || err $LINENO

res=$($com -c '[[ -a ]]')
[ "$?" = "2" ] || err $LINENO

Expand All @@ -557,6 +566,24 @@ res=$($com -c '[[ ! -a /etc/passwdaaa ]]')
res=$($com -c '[[ -a ( /etc/passwdaaa ) ]]')
[ "$?" = "2" ] || err $LINENO

res=$($com -c '[[ ! -a /dev/nvme0n1 ]] || [[ -b /dev/nvme0n1 ]]')
[ "$?" = "0" ] || err $LINENO

res=$($com -c '[[ ! -a /dev/tty0 ]] || [[ ! -b /dev/tty0 ]]')
[ "$?" = "0" ] || err $LINENO

res=$($com -c '[[ ! -a /dev/nvme0n1 ]] || [[ ! -c /dev/nvme0n1 ]]')
[ "$?" = "0" ] || err $LINENO

res=$($com -c '[[ ! -a /dev/tty0 ]] || [[ -c /dev/tty0 ]]')
[ "$?" = "0" ] || err $LINENO

res=$($com -c '[[ -d /etc/ ]]')
[ "$?" = "0" ] || err $LINENO

res=$($com -c '[[ -d /etc/passwd ]]')
[ "$?" = "1" ] || err $LINENO

res=$($com -c '[[ -a ]]')
[ "$?" = "2" ] || err $LINENO

Expand Down

0 comments on commit a467b95

Please sign in to comment.