#tdd #test #unit-testing #git #tcr

app git-gamble

blend TCR + TDD to make sure to develop the right thing, babystep by babystep

34 releases (8 stable)

new 2.3.0 Oct 23, 2021
2.0.0 Sep 25, 2021
1.3.0 Mar 12, 2021
1.1.0 May 25, 2020
0.4.0 Mar 31, 2020

#17 in Testing

Download history 16/week @ 2021-06-28 5/week @ 2021-07-05 4/week @ 2021-07-12 38/week @ 2021-07-19 35/week @ 2021-07-26 62/week @ 2021-08-02 1/week @ 2021-08-09 34/week @ 2021-08-16 1/week @ 2021-08-23 1/week @ 2021-09-13 11/week @ 2021-09-20 65/week @ 2021-09-27 16/week @ 2021-10-04 67/week @ 2021-10-11

99 downloads per month

ISC license

36KB
308 lines

git-gamble's logo

Git-Gamble

Crate available on Crates.io AppImage available on GitLab Debian available on GitLab

dependency status pipeline status coverage report AppVeyor for Homebrew status

License ISC Contributor Covenant

Blend TCR (test && commit || revert) + TDD (Test Driven Development) to make sure to develop the right thing, babystep by babystep

Original idea by Xavier Detant

Theory

TCR

TCR (test && commit || revert) is cool! It encourages doing baby steps, reducing the waste when we are wrong

But it doesn't allow us to see the tests failing

So:

  • Maybe we test nothing (assert forgotten)

    def test_should_be_Buzz_given_5():
        input = 5
        actual = fizz_buzz(input)
        # Oops! Assert has been forgotten
    
  • Maybe we are testing something that is not the thing we should be testing

    it("should be Fizz given 3", () => {
      const input = 3;
      const actual = fizzBuzz(input);
      expect(input).toBe("Fizz");
      // Oops! Asserts on the input value instead of the actual value
    });
    

TDD

TDD (Test Driven Development) is cool! It makes sure we develop the right thing, step by step

TCRDD

TCRDD = TCR + TDD

TCRDD blends the constraints of the two methods to benefit from their advantages

Therefore, TCRDD makes sure we develop the right thing, step by step, and we are encouraged to do so by baby steps, reducing the waste when we are wrong

@startuml
skinparam ArrowColor black

start
repeat
	partition "red" #Coral {
		repeat
			:Write a test]
			:Gamble that the tests fail/
			if (Actually run tests) then (Fail)
				-[#Red]->
				:Commit;
				break
			else (Pass)
				-[#Green]->
				:Revert;
			endif
		repeat while (Write another test)
	}
	partition "green" #Lime {
		repeat
			:Write the minimum code]
			:Gamble that the tests pass/
			if (Actually run tests) then (Pass)
				-[#Green]->
				:Commit;
				break
			else (Fail)
				-[#Red]->
				:Revert;
			endif
		repeat while (Try something else)
	}
	partition "refactor" #668cff {
		repeat
			repeat
				:Write code
				without changing the behavior]
				:Gamble that the tests pass/
				if (Actually run tests) then (Pass)
					-[#Green]->
					:Commit;
					break
				else (Fail)
					-[#Tomato]->
					:Revert;
				endif
			repeat while (Change something else)
		repeat while (Another things to refactor ?)
	}
repeat while (Another feature to add ?)
stop
@enduml

If the above diagram is missing, you can see it on GitLab

git-gamble is a tool that helps to use the TCRDD method

How to install

Requirements

git-gamble doesn't repackage git, it uses the one installed on your system

You have to install git manually if you install git-gamble using :

  • AppImage
  • Debian
  • Cargo

Other installation methods include git as a dependency

Make sure it is available in your $PATH, you can check it with this command

git --help

AppImage

  1. Download the latest version

    curl --location "https://gitlab.com/api/v4/projects/pinage404%2Fgit-gamble/packages/generic/git-gamble-AppImage/2.3.0/git-gamble-v2.3.0-x86_64.AppImage" --output git-gamble-v2.3.0-x86_64.AppImage
    
  2. Make it executable

    chmod +x git-gamble-v2.3.0-x86_64.AppImage
    
  3. Put it your $PATH

    # for example
    mkdir -p ~/.local/bin
    ln git-gamble-v2.3.0-x86_64.AppImage ~/.local/bin/git-gamble
    export PATH+=":~/.local/bin"
    

Debian

  1. Go to the package registry page

  2. Go to the latest version of git-gamble-debian

  3. Download the latest version git-gamble_2.3.0_amd64.deb

  4. Install package

    As root

    dpkg --install git-gamble*.deb
    

This is not really convenient but a better way will come when GitLab Debian Package Manager MVC will be available

Mac OS X / Homebrew

Install Homebrew

brew tap pinage404/git-gamble https://gitlab.com/pinage404/git-gamble.git
brew install --HEAD git-gamble

Windows / Chocolatey

Install Chocolatey

  1. Go to the package registry page

  2. Go to the latest version of git-gamble.portable

  3. Download the latest version git-gamble.portable.2.3.0.nupkg

  4. Install package

    Follow GitLab's documentation and Chocolatey's documentation

    choco install ./git-gamble.portable.2.3.0.nupkg
    

This is not really convenient, contributions are welcome

Nix / NixOS

Installation can be done at different levels, see below

You will have to run command several times to change the sha256 and cargoSha256 by the given one by Nix

Project level

In shell.nix

{ pkgs ? import <nixpkgs> {} }:

let
  git-gamble-derivation = pkgs.fetchurl {
    url = "https://gitlab.com/pinage404/git-gamble/-/raw/main/packaging/nix/git-gamble/default.nix?";
    sha256 = "0000000000000000000000000000000000000000000000000000";
  };
  git-gamble = pkgs.callPackage git-gamble-derivation {
    version = "2.3.0";
    sha256 = "1111111111111111111111111111111111111111111111111111";
    cargoSha256 = "0000000000000000000000000000000000000000000000000000";
  };
in
pkgs.mkShell {
  buildInputs = [
    git-gamble

    # others dependencies here
    pkgs.nodejs
  ];
}

Then run this command

nix-shell
DirEnv

To automate the setup of the environment it's recommanded to install DirEnv

Add in .envrc

use nix

Then run this command

direnv allow

Home-Manager / user level

Install Home Manager

In ~/.config/nixpkgs/home.nix

{ pkgs, ... }:

let
  git-gamble-derivation = pkgs.fetchurl {
    url = "https://gitlab.com/pinage404/git-gamble/-/raw/main/packaging/nix/git-gamble/default.nix?";
    sha256 = "0000000000000000000000000000000000000000000000000000";
  };
  git-gamble = pkgs.callPackage git-gamble-derivation {
    version = "2.3.0";
    sha256 = "1111111111111111111111111111111111111111111111111111";
    cargoSha256 = "0000000000000000000000000000000000000000000000000000";
  };
in
{
  home.packages = [
    git-gamble

    # others dependencies here
    pkgs.gitAndTools.git-absorb
  ];
}

Then run this command

home-manager switch

NixOS / system level

In /etc/nixos/configuration.nix

{ pkgs, ... }:

let
  git-gamble-derivation = pkgs.fetchurl {
    url = "https://gitlab.com/pinage404/git-gamble/-/raw/main/packaging/nix/git-gamble/default.nix?";
    sha256 = "0000000000000000000000000000000000000000000000000000";
  };
  git-gamble = pkgs.callPackage git-gamble-derivation {
    version = "2.3.0";
    sha256 = "1111111111111111111111111111111111111111111111111111";
    cargoSha256 = "0000000000000000000000000000000000000000000000000000";
  };
in
{
  environment.systemPackages = [
    git-gamble

    # others dependencies here
    pkgs.bat
  ];

  # This value determines the NixOS release with which your system is to be compatible, in order to avoid breaking some software such as database servers
  # You should change this only after NixOS release notes say you should
  system.stateVersion = "20.09";
}

Then run this command

nixos-rebuild switch

Cargo

Install Cargo

cargo install git-gamble

Add ~/.cargo/bin to your $PATH

Fish:

set --export --append PATH ~/.cargo/bin

Bash / ZSH:

export PATH=$PATH:~/.cargo/bin

Download the binary

Only for Linux and Windows x86_64

  1. Download the binary on the release page
  2. Put it in your $PATH

Check the installation

Check if all have been well settled

git gamble

If it has been well settled, it should output this :

error: The following required arguments were not provided:
    <test-command>...
    <--pass|--fail>

USAGE:
    git-gamble --repository-path <repository-path> <--pass|--fail>

For more information try --help

If it has been badly settled, it should output this :

git : 'gamble' is not a git command. See 'git --help'.

How to use

To see all available flags and options

git-gamble --help # dash between `git` and `gamble` is only needed for --help
  1. Write a failing test in your codebase, then :

    git gamble --fail -- $YOUR_TEST_COMMAND
    
  2. Write the minimum code to make tests pass, then :

    git gamble --pass -- $YOUR_TEST_COMMAND
    
  3. Refactor your code, then :

    git gamble --pass -- $YOUR_TEST_COMMAND
    

It's a bit tedious to always repeat the test command

So you can set an environment variable with the test command to avoid repeating it all the time

export GAMBLE_TEST_COMMAND="sh -c 'cargo fix --allow-dirty ; cargo clippy --all-targets ; cargo check --all-targets ; cargo fmt ; cargo test'"
git gamble --pass

Test command must exit with a 0 status when there are 0 failing tests, anything else is considered as a failure

Usage

git-gamble --help

git-gamble 2.3.0
Blend TCR (`test && commit || revert`) + TDD (Test Driven Development) to make sure to develop
the right thing, babystep by babystep

USAGE:
    git-gamble [FLAGS] [OPTIONS] <--pass|--fail> [--] <test-command>...
    git-gamble [FLAGS] [OPTIONS] <SUBCOMMAND>

FLAGS:
    -r, --fail         Gamble that tests should fail [aliases: red]
    -g, --pass         Gamble that tests should pass [aliases: green, refactor]
    -n, --dry-run      Do not make any changes
    -h, --help         Prints help information
        --no-verify    Do not run git hooks
    -V, --version      Prints version information

OPTIONS:
    -m, --message <message>                    Commit's message [default: ]
    -C, --repository-path <repository-path>    Repository path [default: .]

ARGS:
    <test-command>...    The command to execute to know the result [env: GAMBLE_TEST_COMMAND=]

SUBCOMMANDS:
    generate-shell-completions    
    help                          Prints this message or the help of the given subcommand(s)

Any contributions (feedback, bug report, merge request ...) are welcome
https://gitlab.com/pinage404/git-gamble

Shells completions

To manually generate shell completions you can use this command

git gamble generate-shell-completions --help

git-gamble-generate-shell-completions 2.3.0

USAGE:
    git-gamble generate-shell-completions <shell>

FLAGS:
    -h, --help       
            Prints help information

    -V, --version    
            Prints version information


ARGS:
    <shell>    
            Put generated file here :
            * Fish https://fishshell.com/docs/current/completions.html#where-to-put-completions
            * Others shells ; Don't know, MR are welcome
             [possible values: zsh, bash, fish, powershell, elvish]

When to use it ?

Backlog

  • pass options to git commit
    • --fixup
    • --squash
    • --edit OR --edit-message option to open $EDITOR on commit like git commit --edit
      • how to test it ?
  • when revert git clean
  • git workspace support
    • git update-ref should contain an unique identifier to the workspace
      • branche name ?
      • folder path ?
  • gamble hooks
    • branch based developement
      • git commit --fixup
      • git rebase --autosquash
    • trunk based developement
      • git pull
      • git push
  • like git, flags & options & arguments should be retrieved from CLI or environment variable or config's file
    • re-use git config to store in file ?
    • repository level config using direnv and environment variable ?
  • stash instead of revert ?
  • shell completion
    • in the package ?
      • Cargo
      • AppImage
      • Chocolatey
    • in sub command ?
  • for git gamble not only git-gamble

Distribution / publishing backlog

Adding package to the X packages repository

Where the X packages repository is e.g. Nixpgks, Debian, Homebrew, Chocolatey ...

Feel free to do it, we don't plan to do it at the moment, it's too long to learn and understand how each of them works

If you do it, please file an issue or open an MR to update the documentation

CheckList

  • package in local
  • package in CI
  • upload
  • make public available
  • complete how to install
  • manual test
  • update backlog

Technical improvement opportunities

  • licence check
    • cargo license
    • cargo deny
  • smaller binary
    • cargo bloat
    • cargo deps

Reinvent the wheel

Why reinvent the wheel?

This script already works well

Because i would like to learn Rust ¯\_()_/¯

Contributing

Contributor Covenant

Any contributions (feedback, bug report, merge request ...) are welcome

Respect the code of conduct

Follow Keep a Changelog

Development

Setup

To contribute with merge request

Simple

Install Direnv

Install Nix

Let direnv automagically set up the environment by executing the following command in the project directory

direnv allow
Manual

Debug

There are some logs in the programs, pretty_env_logger is used to display them

There are 5 levels of logging (from the lightest to the most verbose) :

  • error
  • warn
  • info
  • debug
  • trace

The git-gamble logs are "hidden" behind the with_log feature

The option --features with_log (or --all-features) must be added to each cargo command for which you want to see logs (e.g.) :

cargo build --all-features
cargo run --all-features
cargo test --all-features

Then, to display logs, add this environment variable :

export RUST_LOG="git_gamble=debug"

To display really everything :

export RUST_LOG="trace"

There are other possibilities for logging in the env_logger documentation

Deployment

Just run cargo release (in a wide terminal, in order to have --help not truncated in the usage section)

cargo release

Dependencies

~3–4MB
~87K SLoC