github.com/zaquestion/lab@v0.25.1/cmd/todo_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // a simple helper to read the todo list
    12  func readTodoList(t *testing.T) string {
    13  	cmd := exec.Command(labBinaryPath, "todo", "list", "lab-testing")
    14  	l, err := cmd.CombinedOutput()
    15  	if err != nil {
    16  		t.Log(string(l))
    17  		t.Fatal(err)
    18  	}
    19  	return string(l)
    20  }
    21  
    22  func Test_todoMergeRequestTest(t *testing.T) {
    23  	repo := copyTestRepo(t)
    24  
    25  	todoListOrig := readTodoList(t)
    26  
    27  	// create a Merge Request
    28  	git := exec.Command("git", "checkout", "-b", "local/mrtest", "origin/mrtest")
    29  	git.Dir = repo
    30  	b, err := git.CombinedOutput()
    31  	if err != nil {
    32  		t.Log(string(b))
    33  		t.Fatal(err)
    34  	}
    35  
    36  	cmd := exec.Command(labBinaryPath, "mr", "create", "lab-testing", "master", "-m", "mr TODO title test", "-m", "mr description")
    37  	cmd.Dir = repo
    38  
    39  	b, err = cmd.CombinedOutput()
    40  	if err != nil {
    41  		t.Fatalf("Error creating mr: %s (%s)", string(b), err)
    42  	}
    43  	out := string(b)
    44  	require.Contains(t, out, "https://gitlab.com/lab-testing/test/-/merge_requests")
    45  
    46  	i := strings.Index(out, "/diffs\n")
    47  	mrID := strings.TrimPrefix(out[:i], "https://gitlab.com/lab-testing/test/-/merge_requests/")
    48  
    49  	// Add the Merge request to the Todo list
    50  	cmd = exec.Command(labBinaryPath, "todo", "mr", "lab-testing", mrID)
    51  	cmd.Dir = repo
    52  	a, err := cmd.CombinedOutput()
    53  	if err != nil {
    54  		t.Log(string(a))
    55  		t.Fatal(err)
    56  	}
    57  	todoID := strings.Split(string(a), " ")[0]
    58  
    59  	todoListAfterAdd := readTodoList(t)
    60  
    61  	// Remove the Merge Request from the Todo list
    62  	cmd = exec.Command(labBinaryPath, "todo", "done", todoID)
    63  	cmd.Dir = repo
    64  	a, err = cmd.CombinedOutput()
    65  	if err != nil {
    66  		t.Log(string(a))
    67  		t.Fatal(err)
    68  	}
    69  	doneMsg := string(a)
    70  
    71  	todoListAfterDone := readTodoList(t)
    72  
    73  	cmd = exec.Command(labBinaryPath, "mr", "close", "lab-testing", mrID)
    74  	cmd.Dir = repo
    75  
    76  	a, err = cmd.CombinedOutput()
    77  	if err != nil {
    78  		t.Log(string(a))
    79  		t.Fatal(err)
    80  	}
    81  
    82  	// At the beginning of the test, the Todo item must not be on the Todo list
    83  	require.NotContains(t, todoListOrig, todoID)
    84  	// The Todo item must be on the Todo list after it is added
    85  	require.Contains(t, todoListAfterAdd, todoID)
    86  	// The Todo item must not be on the Todo list after the Todo is marked done
    87  	require.NotContains(t, todoListAfterDone, todoID)
    88  	// The 'done' message must contain the Todo item
    89  	require.Contains(t, doneMsg, todoID)
    90  	// The 'done' message must indicate that the Todo item was marked 'Done'
    91  	require.Contains(t, doneMsg, "marked as Done")
    92  }
    93  
    94  func Test_todoIssueTest(t *testing.T) {
    95  	repo := copyTestRepo(t)
    96  
    97  	todoListOrig := readTodoList(t)
    98  
    99  	cmd := exec.Command(labBinaryPath, "issue", "create", "lab-testing",
   100  		"-m", "issue title")
   101  	cmd.Dir = repo
   102  
   103  	a, err := cmd.CombinedOutput()
   104  	if err != nil {
   105  		t.Fatalf("Error creating issue: %s (%s)", string(a), err)
   106  	}
   107  	out := string(a)
   108  	require.Contains(t, out, "https://gitlab.com/lab-testing/test/-/issues")
   109  
   110  	i := strings.Index(out, "\n")
   111  	issueID := strings.TrimPrefix(out[:i], "https://gitlab.com/lab-testing/test/-/issues/")
   112  
   113  	// add it to todolist
   114  	cmd = exec.Command(labBinaryPath, "todo", "issue", "lab-testing", issueID)
   115  	cmd.Dir = repo
   116  	a, err = cmd.CombinedOutput()
   117  	if err != nil {
   118  		t.Log(string(a))
   119  		t.Fatal(err)
   120  	}
   121  	todoID := strings.Split(string(a), " ")[0]
   122  
   123  	todoListAfterAdd := readTodoList(t)
   124  
   125  	// remove it from todolist
   126  	cmd = exec.Command(labBinaryPath, "todo", "done", todoID)
   127  	cmd.Dir = repo
   128  	a, err = cmd.CombinedOutput()
   129  	if err != nil {
   130  		t.Log(string(a))
   131  		t.Fatal(err)
   132  	}
   133  	doneMsg := string(a)
   134  
   135  	todoListAfterDone := readTodoList(t)
   136  
   137  	// At the beginning of the test, the Todo item must not be on the Todo list
   138  	require.NotContains(t, todoListOrig, todoID)
   139  	// The Todo item must be on the Todo list after it is added
   140  	require.Contains(t, todoListAfterAdd, todoID)
   141  	// The Todo item must not be on the Todo list after the Todo is marked done
   142  	require.NotContains(t, todoListAfterDone, todoID)
   143  	// The 'done' message must contain the Todo item
   144  	require.Contains(t, doneMsg, todoID)
   145  	// The 'done' message must indicate that the Todo item was marked 'Done'
   146  	require.Contains(t, doneMsg, "marked as Done")
   147  }