Skip to content

Commit

Permalink
fix(tests): creating and removing file at test time only
Browse files Browse the repository at this point in the history
  • Loading branch information
nephi-dev committed Jul 12, 2024
1 parent 8de1d1a commit 19b83f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ pub fn read_string(xml_string: String, root_tag: String) -> Node {
mod tests {
use crate::f_str;
use crate::read::read_file;
use std::fs::{remove_file, File};
use std::io::prelude::*;
#[test]
fn test_read_file() {
let mut file = File::create("tests/test.xml").unwrap();
file.write_all(b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root test=\"test\">\n <child test=\"test\">\n <child>test</child>\n </child>\n</root>")
.unwrap();
let node = read_file(f_str!("tests/test.xml"), f_str!("root"));
remove_file("tests/test.xml").unwrap();
assert_eq!(node.name, f_str!("root"));
assert_eq!(node.attrs.len(), 1);
assert_eq!(node.attrs.get("test").unwrap(), "test");
Expand Down
5 changes: 3 additions & 2 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mod tests {
use crate::entities::Node;
use crate::write::{write_file, write_node_to_string, write_string};
use std::collections::HashMap;
use std::fs::{read_to_string, remove_file};
fn root_node() -> Node {
let mut attrs = HashMap::new();
attrs.insert("test".to_string(), "test".to_string());
Expand Down Expand Up @@ -117,8 +118,8 @@ mod tests {
Some(4),
Some(true),
);
let file_str = std::fs::read_to_string("tests/test_write.xml").unwrap();
let file_str = read_to_string("tests/test_write.xml").unwrap();
remove_file("tests/test_write.xml").unwrap();
assert_eq!(file_str, expected);
std::fs::remove_file("tests/test_write.xml").unwrap();
}
}

0 comments on commit 19b83f6

Please sign in to comment.