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
39 changes: 1 addition & 38 deletions Cargo.lock

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

9 changes: 1 addition & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@ clap = "4.0.18"
git2 = "0.15"
glob = "0.3.0"

[dev-dependencies.uuid]
version = "1.3.0"
features = [
"v4",
]

[dev-dependencies]
cucumber = "0.19"
futures = "0.3"
stdio-override = "0.1.3"
tempfile = "3.5.0"
shlex = "1.1.0"
# fake = "2.5.0"
# rand = "0.8.5"

[[test]]
name = "example"
name = "features"
harness = false
9 changes: 0 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ fn matches_to_options(matches: ArgMatches) -> Options {
}
}

#[cfg(test)]
pub fn get_options_from(cmd: &str) -> Result<Options> {
let args = cmd.split(' ');

let matches = command().try_get_matches_from(args)?;

Ok(matches_to_options(matches))
}

pub fn get_options() -> Result<Options> {
let matches = command().try_get_matches()?;

Expand Down
7 changes: 0 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ fn execute(options: Options) -> Result<()> {
Ok(())
}

#[cfg(test)]
pub fn from(cmd: &str) -> Result<()> {
let options = cli::get_options_from(cmd)?;

execute(options)
}

fn main() -> Result<()> {
let options = cli::get_options()?;

Expand Down
117 changes: 0 additions & 117 deletions tests/example.rs

This file was deleted.

134 changes: 134 additions & 0 deletions tests/features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use cucumber::gherkin::Step;
use cucumber::given;
use cucumber::then;
use cucumber::when;
use cucumber::World;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::process::Output;
use tempfile::TempDir;

fn main() {
futures::executor::block_on(GitWorld::cucumber().run_and_exit("tests/features"));
}

#[derive(Debug, World)]
#[world(init = Self::new)]
pub struct GitWorld {
home: TempDir,
directory: TempDir,
output: Option<Output>,
}

#[given("an empty directory")]
fn empty_directory(_world: &mut GitWorld) {
// GitWorld::new creates the directory before the first step.
}

#[when(expr = "the user executes the command: {string}")]
fn run_command(world: &mut GitWorld, command: String) {
let (program, args) = split_command(&command);

let output = world.run(&program, &args);

assert!(
output.status.success(),
"The command failed: {command}\n{}",
text(&output.stderr)
);
}

#[when(expr = "the user executes the tool like: {string}")]
fn run_tool(world: &mut GitWorld, command: String) {
let (program, args) = split_command(&command);
assert_eq!(program, "git-changed", "This step runs only git-changed");

let output = world.run(env!("CARGO_BIN_EXE_git-changed"), &args);

world.output = Some(output);
}

#[then("it fails with the following error")]
fn assert_failure(world: &mut GitWorld, step: &Step) {
let output = world.tool_output();

assert!(!output.status.success(), "The tool did not fail");
assert_eq!(text(&output.stderr), expected_text(step));
}

#[then("it succeeds with the following output")]
fn assert_success(world: &mut GitWorld, step: &Step) {
let output = world.tool_output();

assert!(
output.status.success(),
"The tool failed: {}",
text(&output.stderr)
);
assert_eq!(text(&output.stdout), expected_text(step));
}

impl GitWorld {
fn new() -> Self {
let home = TempDir::new().expect("Cannot create the home directory");
write_global_config(home.path());

Self {
home,
directory: TempDir::new().expect("Cannot create the work directory"),
output: None,
}
}

fn run(&self, program: &str, args: &[String]) -> Output {
command_without_user_config(program, self.home.path(), self.directory.path())
.args(args)
.output()
.unwrap_or_else(|error| panic!("Cannot run {program}: {error}"))
}

fn tool_output(&self) -> &Output {
self.output.as_ref().expect("The tool did not run")
}
}

fn command_without_user_config(program: &str, home: &Path, directory: &Path) -> Command {
let mut command = Command::new(program);

// git and libgit2 read the global config from HOME and XDG_CONFIG_HOME.
command
.current_dir(directory)
.env("HOME", home)
.env("XDG_CONFIG_HOME", home)
.env("GIT_CONFIG_NOSYSTEM", "1");

command
}

fn write_global_config(home: &Path) {
// libgit2 ignores GIT_CONFIG_NOSYSTEM. This value overrides the one in /etc/gitconfig.
let config = "[init]\n\tdefaultBranch = main\n";

fs::write(home.join(".gitconfig"), config).expect("Cannot write the global git config");
}

fn split_command(command: &str) -> (String, Vec<String>) {
let mut parts =
shlex::split(command).unwrap_or_else(|| panic!("Cannot parse the command: {command}"));
assert!(!parts.is_empty(), "The command is empty");

let program = parts.remove(0);

(program, parts)
}

fn expected_text(step: &Step) -> String {
let doc_string = step.docstring.as_ref().expect("The step has no doc string");

doc_string.trim().to_string()
}

fn text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).trim().to_string()
}
2 changes: 1 addition & 1 deletion tests/features/help.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Feature: Help
When the user executes the tool like: "git-changed -h"
Then it fails with the following error
"""
Lists files modified since branching off
Error: Lists files modified since branching off

Usage: git-changed [OPTIONS] [glob]

Expand Down
Loading
Loading