Skip to content

Commit

Permalink
Adding tests for list command
Browse files Browse the repository at this point in the history
Signed-off-by: Dusan Malusev <dusan@dusanmalusev.dev>
  • Loading branch information
CodeLieutenant committed Sep 12, 2022
1 parent 2436e05 commit ca84cda
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions hoster/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "hoster"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
authors = ["Dusan Malusev <dusan@dusanmalusev.dev>"]
description = "Small parser and lexer library for Hosts file format"
categories = ["parsing", "filesystem", "development-tools"]
documentation = "https://docs.rs/hoster/0.1.1"
documentation = "https://docs.rs/hoster/0.2.0"
homepage = "https://github.com/BrosSquad/hosts"
keywords = [
"hosts",
Expand Down
2 changes: 1 addition & 1 deletion hosts-edit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hosts-edit"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
authors = ["Dusan Malusev <dusan@dusanmalusev.dev>"]
categories = ["parsing", "filesystem", "development-tools"]
Expand Down
3 changes: 3 additions & 0 deletions hosts-edit/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ where
let _n = hosts.write(&data)?;
}
Commands::List { with_comments } => {
let stdout = std::io::stdout();

list_command(
&mut file_options.append(false).read(true).open(path.into())?,
&mut stdout.lock(),
with_comments,
)?;
}
Expand Down
68 changes: 61 additions & 7 deletions hosts-edit/src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use std::error::Error;
use std::io::Read;
use std::io::{Read, Write};

use hoster::cst::CstNode;
use hoster::parser::Parser;
use hoster::tokenizer::Tokenizer;
use hoster::visitor::CstVisitor;

pub(crate) fn execute<R>(reader: &mut R, _with_comments: bool) -> Result<(), Box<dyn Error>>
pub(crate) fn execute<R, W>(
reader: &mut R,
output: &mut W,
_with_comments: bool,
) -> Result<(), Box<dyn Error>>
where
R: Read,
W: Write,
{
let tokens = Tokenizer::new_with_reader(reader).parse()?.get_tokens();

let mut parser = Parser::builder()
.visitor(Visitor {
print_new_line: false,
output,
})
.build();

Expand All @@ -24,25 +30,30 @@ where
Ok(())
}

pub(crate) struct Visitor {
pub(crate) struct Visitor<W> {
print_new_line: bool,
output: W,
}

impl CstVisitor for Visitor {
impl<W> CstVisitor for Visitor<W>
where
W: Write,
{
fn visit(&mut self, _idx: usize, node: &CstNode) -> Option<()> {
match node {
CstNode::Host(host) => {
self.print_new_line = true;
print!("\t{}", host);
write!(self.output, "\t{}", host).unwrap();
}
CstNode::IP(ip) => {
print!("{}", ip);
write!(self.output, "{}", ip).unwrap();

self.print_new_line = false;
}
CstNode::NewLine => {
if self.print_new_line {
self.print_new_line = false;
println!();
writeln!(self.output).unwrap();
}
}
_ => {}
Expand All @@ -51,3 +62,46 @@ impl CstVisitor for Visitor {
Some(())
}
}

#[cfg(test)]
mod tests {
use std::io::Cursor;

use super::*;

#[test]
fn test_list_without_comments() {
let mut output = Cursor::new(Vec::new());

let mut reader = Cursor::new(
"\
# localhost name resolution is handled within DNS itself.
# Added by Docker Desktop
192.168.0.17\thost.docker.internal
192.168.0.17 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
\t127.0.0.1\tkubernetes.docker.internal
# End of section
"
.to_string(),
);

let result = execute(&mut reader, &mut output, false);

assert!(result.is_ok());

let output = String::from_utf8(output.into_inner()).unwrap();

assert_eq!(
"\
192.168.0.17\thost.docker.internal
192.168.0.17\tgateway.docker.internal
127.0.0.1\tkubernetes.docker.internal
"
.to_string(),
output
)
}
}

0 comments on commit ca84cda

Please sign in to comment.