github.com/seachicken/gh-poi@v0.9.10/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/fatih/color"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func Test_DeletingBranchesWhenDryRunOptionIsFalse(t *testing.T) {
    15  	onlyCI(t)
    16  
    17  	results := captureOutput(func() { runMain(false, false) })
    18  
    19  	expected := fmt.Sprintf("%s %s", green("✔"), "Deleting branches...")
    20  	assert.Contains(t, results, expected)
    21  }
    22  
    23  func Test_DoNotDeleteBranchesWhenDryRunOptionIsTrue(t *testing.T) {
    24  	onlyCI(t)
    25  
    26  	results := captureOutput(func() { runMain(true, false) })
    27  
    28  	expected := fmt.Sprintf("%s %s", hiBlack("-"), "Deleting branches...")
    29  	assert.Contains(t, results, expected)
    30  }
    31  
    32  func Test_ProtectAndUnprotect(t *testing.T) {
    33  	onlyCI(t)
    34  
    35  	runProtect([]string{"main"}, false)
    36  	protectResults := captureOutput(func() { runMain(true, false) })
    37  	expected := fmt.Sprintf("main %s", hiBlack("[protected]"))
    38  	assert.Contains(t, protectResults, expected)
    39  
    40  	runUnprotect([]string{"main"}, false)
    41  	unprotectResults := captureOutput(func() { runMain(true, false) })
    42  	assert.NotContains(t, unprotectResults, expected)
    43  }
    44  
    45  func onlyCI(t *testing.T) {
    46  	if os.Getenv("CI") == "" {
    47  		t.Skip("skipping test in local")
    48  	}
    49  
    50  	os.Chdir("ci-test")
    51  }
    52  
    53  func captureOutput(f func()) string {
    54  	org := os.Stdout
    55  	defer func() {
    56  		os.Stdout = org
    57  	}()
    58  
    59  	r, w, _ := os.Pipe()
    60  	os.Stdout = w
    61  	color.Output = w
    62  
    63  	f()
    64  
    65  	w.Close()
    66  	var buf bytes.Buffer
    67  	io.Copy(&buf, r)
    68  
    69  	return buf.String()
    70  }