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

     1  package cmd
     2  
     3  import (
     4  	"os/exec"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/zaquestion/lab/internal/git"
     9  )
    10  
    11  func Test_ciTrace(t *testing.T) {
    12  	t.Parallel()
    13  	repo := copyTestRepo(t)
    14  	cmd := git.New("fetch", "origin")
    15  	cmd.Dir = repo
    16  	cmd.Stdout = nil
    17  	cmd.Stderr = nil
    18  	if b, err := cmd.CombinedOutput(); err != nil {
    19  		t.Log(string(b))
    20  		t.Fatal(err)
    21  	}
    22  
    23  	cmd = git.New("checkout", "origin/ci_test_pipeline")
    24  	cmd.Dir = repo
    25  	cmd.Stdout = nil
    26  	cmd.Stderr = nil
    27  	if b, err := cmd.CombinedOutput(); err != nil {
    28  		t.Log(string(b))
    29  		t.Fatal(err)
    30  	}
    31  
    32  	cmd = git.New("checkout", "-b", "ci_test_pipeline")
    33  	cmd.Dir = repo
    34  	cmd.Stdout = nil
    35  	cmd.Stderr = nil
    36  	if b, err := cmd.CombinedOutput(); err != nil {
    37  		t.Log(string(b))
    38  		t.Fatal(err)
    39  	}
    40  
    41  	tests := []struct {
    42  		desc           string
    43  		args           []string
    44  		assertContains func(t *testing.T, out string)
    45  	}{
    46  		{
    47  			desc: "noargs",
    48  			args: []string{},
    49  			assertContains: func(t *testing.T, out string) {
    50  				assert.Contains(t, out, "Showing logs for deploy10")
    51  				assert.Contains(t, out, "Checking out 09b519cb as ci_test_pipeline...")
    52  				assert.Contains(t, out, "For example you might run an update here or install a build dependency")
    53  				assert.Contains(t, out, "$ echo \"Or perhaps you might print out some debugging details\"")
    54  				assert.Contains(t, out, "Job succeeded")
    55  			},
    56  		},
    57  		{
    58  			desc: "arg job name",
    59  			args: []string{"origin", ":deploy1"},
    60  			assertContains: func(t *testing.T, out string) {
    61  				assert.Contains(t, out, "Showing logs for deploy1")
    62  				assert.Contains(t, out, "Checking out 09b519cb as ci_test_pipeline...")
    63  				assert.Contains(t, out, "For example you might run an update here or install a build dependency")
    64  				assert.Contains(t, out, "$ echo \"Or perhaps you might print out some debugging details\"")
    65  				assert.Contains(t, out, "Job succeeded")
    66  			},
    67  		},
    68  		{
    69  			desc: "explicit branch:job",
    70  			args: []string{"origin", "ci_test_pipeline:deploy1"},
    71  			assertContains: func(t *testing.T, out string) {
    72  				assert.Contains(t, out, "Showing logs for deploy1")
    73  				assert.Contains(t, out, "Checking out 09b519cb as ci_test_pipeline...")
    74  				assert.Contains(t, out, "For example you might do some cleanup here")
    75  				assert.Contains(t, out, "Job succeeded")
    76  			},
    77  		},
    78  		{
    79  			desc: "explicit branch",
    80  			args: []string{"origin", "ci_test_pipeline"},
    81  			assertContains: func(t *testing.T, out string) {
    82  				assert.Contains(t, out, "Showing logs for deploy10")
    83  				assert.Contains(t, out, "Checking out 09b519cb as ci_test_pipeline...")
    84  				assert.Contains(t, out, "For example you might do some cleanup here")
    85  				assert.Contains(t, out, "Job succeeded")
    86  			},
    87  		},
    88  	}
    89  
    90  	for _, test := range tests {
    91  		test := test
    92  		t.Run(test.desc, func(t *testing.T) {
    93  			t.Parallel()
    94  			cmd = exec.Command(labBinaryPath, append([]string{"ci", "trace"}, test.args...)...)
    95  			cmd.Dir = repo
    96  
    97  			b, err := cmd.CombinedOutput()
    98  			if err != nil {
    99  				t.Log(string(b))
   100  				t.Fatal(err)
   101  			}
   102  			out := string(b)
   103  			test.assertContains(t, out)
   104  		})
   105  	}
   106  
   107  }