use super::{Directory, File, fixture}; use dever::compiler::{build, project::Project}; #[test] fn stream_aliases_continue_after_partial_reads_and_close_independently_of_file() { let directory = Directory::new(); let path = directory.0.join("input"); std::fs::write(&path, b"abcdef").unwrap(); let file = File::open(path.to_str().unwrap()).unwrap(); assert!(file.chunks(0, |value| value).is_err()); let stream = file.chunks(2, |value| value).unwrap(); let alias = stream.clone(); assert_eq!(stream.next().unwrap().unwrap(), b"ab"); assert_eq!(alias.next().unwrap().unwrap(), b"cd"); alias.close(); stream.close(); assert!(stream.next().is_none()); assert_eq!(file.read(2).unwrap().unwrap(), b"ef"); let end = file.chunks(2, |value| value).unwrap(); assert!(end.next().is_none()); assert!(end.clone().next().is_none()); file.close().unwrap(); assert!(file.chunks(2, |value| value).is_err()); } #[cfg(target_os = "linux")] #[test] fn terminal_stream_error_releases_source_before_returning_the_event() { let directory = Directory::new(); let file = File::open(directory.0.to_str().unwrap()).unwrap(); let stream = file.chunks(2, |value| value).unwrap(); drop(file); let count = || { std::fs::read_dir("/proc/self/fd") .unwrap() .filter_map(Result::ok) .filter(|entry| std::fs::read_link(entry.path()).ok().as_ref() == Some(&directory.0)) .count() }; assert_eq!(count(), 1); assert!(stream.next().unwrap().is_err()); assert_eq!(count(), 0); assert!(stream.next().is_none()); assert!(stream.clone().next().is_none()); } #[test] fn native_stream_reduction_preserves_cursor_terminal_error_and_cleanup() { let directory = Directory::new(); std::fs::write(directory.0.join("input"), b"abcdef").unwrap(); let project = Project::load(&fixture().with_file_name("stream.dever")).unwrap(); let program = project.check().unwrap(); let binary = build::build( &program, &project.sources, "verify", &directory.0.join("verify"), ) .unwrap(); let output = std::process::Command::new(binary) .current_dir(&directory.0) .output() .unwrap(); assert!( output.status.success(), "{}", String::from_utf8_lossy(&output.stderr) ); assert_eq!(output.stdout, b"true\n"); }