Skip to content

Commit

Permalink
Some more thought about whitespace and empty input (#427)
Browse files Browse the repository at this point in the history
Also partially done with experimentation using `git rev-parse`.
  • Loading branch information
Byron committed May 29, 2022
1 parent 67162ba commit 6c5801a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion git-revision/src/spec.rs
Expand Up @@ -32,7 +32,9 @@ pub mod parse {
/// - **Navigation** - where to go once from the initial revision.
/// - **range** - to learn if the specification is for a single or multiple references.
pub trait Delegate {
fn resolve_ref(&mut self, input: &BStr) -> Option<()>;
/// Resolve `name` as reference which might not be a valid reference name. The name may be partial like `main` or full like
/// `refs/heads/main` solely depending on the users input.
fn resolve_ref(&mut self, name: &BStr) -> Option<()>;
fn find_by_prefix(&mut self, input: &BStr) -> Option<()>;

fn nth_ancestor(&mut self, n: usize) -> Option<()>;
Expand Down
23 changes: 23 additions & 0 deletions git-revision/tests/spec/mod.rs
Expand Up @@ -6,6 +6,7 @@ mod parse {
struct Recorder {
resolve_ref_input: Option<BString>,
kind: Option<spec::Kind>,
calls: usize,
}
impl spec::parse::Delegate for Recorder {
fn resolve_ref(&mut self, input: &BStr) -> Option<()> {
Expand All @@ -15,6 +16,7 @@ mod parse {
input
);
self.resolve_ref_input = input.to_owned().into();
self.calls += 1;
Some(())
}

Expand All @@ -31,6 +33,7 @@ mod parse {
}

fn kind(&mut self, kind: spec::Kind) {
self.calls += 1;
self.kind = Some(kind);
}
}
Expand All @@ -41,6 +44,26 @@ mod parse {
rec
}

#[test]
#[ignore]
fn empty_specs_are_valid() {
// they should of course be invalid for the delegate. CLIs may pre-process the input as well if they wish
// but git itself doesn't do that.
for spec in ["", " ", "\n\t"] {
let rec = parse(spec);
assert_eq!(rec.calls, 0);
}
}

#[test]
#[ignore]
fn all_characters_are_taken_verbatim_which_includes_whitespace() {
let spec = " HEAD \n";
let rec = parse(spec);
assert!(rec.kind.is_none());
assert_eq!(rec.resolve_ref_input.unwrap(), spec);
}

#[test]
#[ignore]
fn leading_caret_is_range_kind() {
Expand Down

0 comments on commit 6c5801a

Please sign in to comment.