github.com/grantbow/fit@v0.7.1-0.20220916164603-1f7c88ac81e6/fitapp/Tag_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  //func getAllTags() []string {
    16  //func Tag(Args argumentList) {
    17  
    18  func runtagsassigned(args argumentList, expected string, t *testing.T) {
    19  	config := bugs.Config{}
    20  	stdout, stderr := captureOutput(func() {
    21  		TagsAssigned(args, config)
    22  	}, t)
    23  	if stderr != "" {
    24  		t.Error("Unexpected error: " + stderr)
    25  	}
    26  	re := regexp.MustCompile(expected)
    27  	matched := re.MatchString(stdout)
    28  	if !matched {
    29  		t.Error("Unexpected output on STDOUT for fitapp/Tag_test")
    30  		fmt.Printf("Expected: %s\nGot: %s\n", expected, stdout)
    31  	}
    32  }
    33  
    34  func runtagsnone(args argumentList, expected string, t *testing.T) {
    35  	config := bugs.Config{}
    36  	stdout, stderr := captureOutput(func() {
    37  		TagsNone(config)
    38  	}, t)
    39  	if stderr != "" {
    40  		t.Error("Unexpected error: " + stderr)
    41  	}
    42  	re := regexp.MustCompile(expected)
    43  	matched := re.MatchString(stdout)
    44  	if !matched {
    45  		t.Error("Unexpected output on STDOUT for fitapp/Tag_test")
    46  		fmt.Printf("Expected: %s\nGot: %s\n", expected, stdout)
    47  	}
    48  }
    49  
    50  func runtag(args argumentList, expected string, t *testing.T) {
    51  	config := bugs.Config{}
    52  	stdout, stderr := captureOutput(func() {
    53  		Tag(args, config)
    54  	}, t)
    55  	if stderr != "" {
    56  		t.Error("Unexpected error: " + stderr)
    57  	}
    58  	re := regexp.MustCompile(expected)
    59  	matched := re.MatchString(stdout)
    60  	if !matched {
    61  		t.Error("Unexpected output on STDOUT for fitapp/Tag_test")
    62  		fmt.Printf("Expected: %s\nGot: %s\n", expected, stdout)
    63  	}
    64  }
    65  
    66  func TestTag(t *testing.T) {
    67  	config := bugs.Config{}
    68  	config.FitDirName = "fit"
    69  	var gdir string
    70  	gdir, err := ioutil.TempDir("", "taggit")
    71  	pwd, _ := os.Getwd()
    72  	if err == nil {
    73  		os.Chdir(gdir)
    74  		// Hack to get around the fact that /tmp is a symlink on
    75  		// OS X, and it causes the directory checks to fail..
    76  		gdir, _ = os.Getwd()
    77  		defer os.RemoveAll(gdir)
    78  	} else {
    79  		t.Error("Failed creating temporary directory for detect")
    80  		return
    81  	}
    82  	// Fake a git repo
    83  	os.Mkdir(".git", 0755)
    84  	// Make an issues Directory
    85  	os.Mkdir(config.FitDirName, 0755)
    86  
    87  	err = os.Setenv("FIT", gdir)
    88  	if err != nil {
    89  		t.Error("Could not set environment variable: " + err.Error())
    90  		return
    91  	}
    92  	// bug
    93  	_, _ = captureOutput(func() {
    94  		Create(argumentList{"-n", "no_tag_bug"}, config)
    95  	}, t)
    96  	// before
    97  	runfind(argumentList{"tags", "foo"}, "", t) // find uses tags but tag uses tag
    98  	runtagsnone(argumentList{""}, "No tags assigned:", t)
    99  	// add with too few args
   100  	runtag(argumentList{}, "", t) // no cmd as argument
   101  	// add
   102  	runtag(argumentList{"1", "foo"}, "", t) // no cmd as argument
   103  	// after
   104  	runfind(argumentList{"tags", "foo"}, "Issue 1: no_tag_bug \\(foo\\)\n", t)                                              // boolean flags not tags
   105  	_, err = ioutil.ReadFile(fmt.Sprintf("%s%s%s%sno_tag_bug%stags%sfoo", gdir, sops, config.FitDirName, sops, sops, sops)) // file is empty
   106  	if err != nil {
   107  		t.Error("Could not load tags/foo file" + err.Error())
   108  	}
   109  	// tags can have more than one
   110  	runtag(argumentList{"1", "bar"}, "", t)                                                                                 // no cmd as argument
   111  	runfind(argumentList{"tags", "foo"}, "Issue 1: no_tag_bug \\(bar, foo\\)\n", t)                                         // boolean flags not tags
   112  	_, err = ioutil.ReadFile(fmt.Sprintf("%s%s%s%sno_tag_bug%stags%sbar", gdir, sops, config.FitDirName, sops, sops, sops)) // file is empty
   113  	if err != nil {
   114  		t.Error("Could not load tags/bar file" + err.Error())
   115  	}
   116  	// non existent bug
   117  	runtag(argumentList{"3", "baz"}, "", t) // no cmd as argument
   118  	// --rm a tag
   119  	runtag(argumentList{"--rm", "1", "bar"}, "", t) // no cmd as argument
   120  	os.Chdir(pwd)
   121  }
   122  
   123  func TestTagsAssigned(t *testing.T) {
   124  	runtagsassigned(argumentList{""}, "<none assigned yet>", t)
   125  }
   126  func TestTagsNone(t *testing.T) {
   127  	runtagsnone(argumentList{""}, "No tags assigned:", t)
   128  }