github.com/erikjuhani/git-gong@v0.0.0-20220213141213-6b9fa82d4e7c/cmd/undo_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/erikjuhani/git-gong/gong"
     9  )
    10  
    11  func TestUndoCmd(t *testing.T) {
    12  	tests := []struct {
    13  		name string
    14  	}{
    15  		{
    16  			name: `Command gong undo. Should undo the last command the user has done.
    17  e.g. User commit file to wrong branch.`,
    18  		},
    19  	}
    20  
    21  	repo, clean, err := gong.TestRepo()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	defer clean()
    26  
    27  	expected, err := repo.Seed("a")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	// Mistake commit
    33  	_, err = repo.Seed("b", "b.file")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	workdir := repo.Path
    39  
    40  	if err := os.Chdir(workdir); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			args := []string{undoCmd.Name()}
    47  			rootCmd.SetArgs(args)
    48  
    49  			if err := rootCmd.Execute(); err != nil {
    50  				t.Fatal(err)
    51  			}
    52  
    53  			actual, err := repo.Head.Commit()
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			if expected.ID.String() != actual.ID.String() {
    59  				t.Fatal(fmt.Errorf("expected head state: %s did not match the actual state: %s", expected.ID.String(), actual.ID.String()))
    60  			}
    61  		})
    62  	}
    63  }