github.com/grantbow/fit@v0.7.1-0.20220916164603-1f7c88ac81e6/fitapp/Status_test.go (about)

     1  package fitapp
     2  
     3  import (
     4  	"fmt"
     5  	bugs "github.com/grantbow/fit/issues"
     6  	"io/ioutil"
     7  	"os"
     8  	"regexp"
     9  	"testing"
    10  )
    11  
    12  //var dops = bugs.Directory(os.PathSeparator)
    13  //var sops = string(os.PathSeparator)
    14  
    15  // Priority and Status find printed differently
    16  
    17  func runstatus(args argumentList, expected string, t *testing.T) {
    18  	config := bugs.Config{}
    19  	stdout, stderr := captureOutput(func() {
    20  		Status(args, config)
    21  	}, t)
    22  	if stderr != "" {
    23  		t.Error("Unexpected error: " + stderr)
    24  	}
    25  	re := regexp.MustCompile(expected)
    26  	matched := re.MatchString(stdout)
    27  	if !matched {
    28  		t.Error("Unexpected output on STDOUT for fitapp/Status_test")
    29  		fmt.Printf("Expected: %s\nGot: %s\n", expected, stdout)
    30  	}
    31  }
    32  
    33  func TestStatus(t *testing.T) {
    34  	config := bugs.Config{}
    35  	config.FitDirName = "fit"
    36  	var gdir string
    37  	gdir, err := ioutil.TempDir("", "statusgit")
    38  	pwd, _ := os.Getwd()
    39  	if err == nil {
    40  		os.Chdir(gdir)
    41  		// Hack to get around the fact that /tmp is a symlink on
    42  		// OS X, and it causes the directory checks to fail..
    43  		gdir, _ = os.Getwd()
    44  		defer os.RemoveAll(gdir)
    45  	} else {
    46  		t.Error("Failed creating temporary directory for detect")
    47  		return
    48  	}
    49  	// Fake a git repo
    50  	os.Mkdir(".git", 0755)
    51  	// Make an issues Directory
    52  	os.Mkdir(config.FitDirName, 0755)
    53  
    54  	err = os.Setenv("FIT", gdir)
    55  	if err != nil {
    56  		t.Error("Could not set environment variable: " + err.Error())
    57  		return
    58  	}
    59  	// bug
    60  	_, _ = captureOutput(func() {
    61  		Create(argumentList{"-n", "no_status_bug"}, config)
    62  	}, t)
    63  	// before
    64  	runfind(argumentList{"status", "foo"}, "", t)
    65  	// add
    66  	runstatus(argumentList{"1", "foo"}, "", t) // no cmd as argument
    67  	// after
    68  	runfind(argumentList{"status", "foo"}, "Issue 1: no_status_bug \\(Status: foo\\)\n", t)
    69  	file, err := ioutil.ReadFile(fmt.Sprintf("%s%s%s%sno_status_bug%sStatus", gdir, sops, config.FitDirName, sops, sops))
    70  	if err != nil {
    71  		t.Error("Could not load Status file" + err.Error())
    72  	}
    73  	if len(file) == 0 {
    74  		t.Error("Expected a Status file")
    75  	}
    76  	os.Chdir(pwd)
    77  }