Skip to content

Commit ca84cda

Browse files
Adding tests for list command
Signed-off-by: Dusan Malusev <[email protected]>
1 parent 2436e05 commit ca84cda

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hoster/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "hoster"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2021"
55
authors = ["Dusan Malusev <[email protected]>"]
66
description = "Small parser and lexer library for Hosts file format"
77
categories = ["parsing", "filesystem", "development-tools"]
8-
documentation = "https://docs.rs/hoster/0.1.1"
8+
documentation = "https://docs.rs/hoster/0.2.0"
99
homepage = "https://github.com/BrosSquad/hosts"
1010
keywords = [
1111
"hosts",

hosts-edit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hosts-edit"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2021"
55
authors = ["Dusan Malusev <[email protected]>"]
66
categories = ["parsing", "filesystem", "development-tools"]

hosts-edit/src/app.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ where
6363
let _n = hosts.write(&data)?;
6464
}
6565
Commands::List { with_comments } => {
66+
let stdout = std::io::stdout();
67+
6668
list_command(
6769
&mut file_options.append(false).read(true).open(path.into())?,
70+
&mut stdout.lock(),
6871
with_comments,
6972
)?;
7073
}

hosts-edit/src/commands/list.rs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
use std::error::Error;
2-
use std::io::Read;
2+
use std::io::{Read, Write};
33

44
use hoster::cst::CstNode;
55
use hoster::parser::Parser;
66
use hoster::tokenizer::Tokenizer;
77
use hoster::visitor::CstVisitor;
88

9-
pub(crate) fn execute<R>(reader: &mut R, _with_comments: bool) -> Result<(), Box<dyn Error>>
9+
pub(crate) fn execute<R, W>(
10+
reader: &mut R,
11+
output: &mut W,
12+
_with_comments: bool,
13+
) -> Result<(), Box<dyn Error>>
1014
where
1115
R: Read,
16+
W: Write,
1217
{
1318
let tokens = Tokenizer::new_with_reader(reader).parse()?.get_tokens();
1419

1520
let mut parser = Parser::builder()
1621
.visitor(Visitor {
1722
print_new_line: false,
23+
output,
1824
})
1925
.build();
2026

@@ -24,25 +30,30 @@ where
2430
Ok(())
2531
}
2632

27-
pub(crate) struct Visitor {
33+
pub(crate) struct Visitor<W> {
2834
print_new_line: bool,
35+
output: W,
2936
}
3037

31-
impl CstVisitor for Visitor {
38+
impl<W> CstVisitor for Visitor<W>
39+
where
40+
W: Write,
41+
{
3242
fn visit(&mut self, _idx: usize, node: &CstNode) -> Option<()> {
3343
match node {
3444
CstNode::Host(host) => {
3545
self.print_new_line = true;
36-
print!("\t{}", host);
46+
write!(self.output, "\t{}", host).unwrap();
3747
}
3848
CstNode::IP(ip) => {
39-
print!("{}", ip);
49+
write!(self.output, "{}", ip).unwrap();
50+
4051
self.print_new_line = false;
4152
}
4253
CstNode::NewLine => {
4354
if self.print_new_line {
4455
self.print_new_line = false;
45-
println!();
56+
writeln!(self.output).unwrap();
4657
}
4758
}
4859
_ => {}
@@ -51,3 +62,46 @@ impl CstVisitor for Visitor {
5162
Some(())
5263
}
5364
}
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use std::io::Cursor;
69+
70+
use super::*;
71+
72+
#[test]
73+
fn test_list_without_comments() {
74+
let mut output = Cursor::new(Vec::new());
75+
76+
let mut reader = Cursor::new(
77+
"\
78+
# localhost name resolution is handled within DNS itself.
79+
# Added by Docker Desktop
80+
192.168.0.17\thost.docker.internal
81+
192.168.0.17 gateway.docker.internal
82+
# To allow the same kube context to work on the host and the container:
83+
\t127.0.0.1\tkubernetes.docker.internal
84+
85+
86+
# End of section
87+
"
88+
.to_string(),
89+
);
90+
91+
let result = execute(&mut reader, &mut output, false);
92+
93+
assert!(result.is_ok());
94+
95+
let output = String::from_utf8(output.into_inner()).unwrap();
96+
97+
assert_eq!(
98+
"\
99+
192.168.0.17\thost.docker.internal
100+
192.168.0.17\tgateway.docker.internal
101+
127.0.0.1\tkubernetes.docker.internal
102+
"
103+
.to_string(),
104+
output
105+
)
106+
}
107+
}

0 commit comments

Comments
 (0)