Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
semver = "0.11"
path_abs = { version = "0.5", default-features = false }
clircle = "0.1.3"

[dependencies.git2]
version = "0.13"
Expand Down
14 changes: 14 additions & 0 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::output::OutputType;
#[cfg(feature = "paging")]
use crate::paging::PagingMode;
use crate::printer::{InteractivePrinter, Printer, SimplePrinter};
use std::convert::TryFrom;

pub struct Controller<'a> {
config: &'a Config<'a>,
Expand Down Expand Up @@ -66,6 +67,14 @@ impl<'b> Controller<'b> {
}

let attached_to_pager = output_type.is_pager();
let rw_cycle_detected = !attached_to_pager && {
let identifiers: Vec<_> = inputs
.iter()
.flat_map(clircle::Identifier::try_from)
.collect();
clircle::stdout_among_inputs(&identifiers)
};

let writer = output_type.handle()?;
let mut no_errors: bool = true;

Expand All @@ -78,6 +87,11 @@ impl<'b> Controller<'b> {
}
};

if rw_cycle_detected {
print_error(&"The output file is also an input!".into(), writer);
return Ok(false);
}

for (index, input) in inputs.into_iter().enumerate() {
match input.open(io::stdin().lock()) {
Err(error) => {
Expand Down
14 changes: 14 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::convert::TryFrom;
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::io::{self, BufRead, BufReader, Read};
use std::path::Path;

use content_inspector::{self, ContentType};

Expand Down Expand Up @@ -192,6 +194,18 @@ impl<'a> Input<'a> {
}
}

impl TryFrom<&'_ Input<'_>> for clircle::Identifier {
type Error = ();

fn try_from(input: &Input) -> std::result::Result<clircle::Identifier, ()> {
match input.kind {
InputKind::OrdinaryFile(ref path) => Self::try_from(Path::new(path)).map_err(|_| ()),
InputKind::StdIn => Self::try_from(clircle::Stdio::Stdin).map_err(|_| ()),
InputKind::CustomReader(_) => Err(()),
}
}
}

pub(crate) struct InputReader<'a> {
inner: Box<dyn BufRead + 'a>,
pub(crate) first_line: Vec<u8>,
Expand Down
Empty file added tests/examples/cycle.txt
Empty file.
37 changes: 34 additions & 3 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use assert_cmd::Command;
use assert_cmd::assert::OutputAssertExt;
use assert_cmd::cargo::CommandCargoExt;
use predicates::{prelude::predicate, str::PredicateStrExt};
use std::fs::File;
use std::path::Path;
use std::process::{Command, Stdio};
use std::str::from_utf8;

const EXAMPLES_DIR: &str = "tests/examples";

fn bat_with_config() -> Command {
fn bat_as_std() -> Command {
let mut cmd = Command::cargo_bin("bat").unwrap();
cmd.current_dir("tests/examples");
cmd.env_remove("PAGER");
Expand All @@ -17,7 +20,11 @@ fn bat_with_config() -> Command {
cmd
}

fn bat() -> Command {
fn bat_with_config() -> assert_cmd::Command {
assert_cmd::Command::from_std(bat_as_std())
}

fn bat() -> assert_cmd::Command {
let mut cmd = bat_with_config();
cmd.arg("--no-config");
cmd
Expand Down Expand Up @@ -189,6 +196,30 @@ fn line_range_multiple() {
.stdout("line 1\nline 2\nline 4\n");
}

#[test]
fn basic_io_cycle() {
let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
bat_as_std()
.arg("test.txt")
.arg("cycle.txt")
.stdout(file_out)
.assert()
.failure();
}

#[test]
fn stdin_to_stdout_cycle() {
let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
let file_in = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
bat_as_std()
.stdin(file_in)
.arg("test.txt")
.arg("-")
.stdout(file_out)
.assert()
.failure();
}

#[test]
fn tabs_numbers() {
bat()
Expand Down